Powered by AppSignal & Oban Pro

Lesson 05 Livebook: Command Vessel

livebooks/05_command_vessel.livemd

Lesson 05 Livebook: Command Vessel

Orpheus can no longer fly the corridor itself. It has to live on the brief Khepri sends back.

Mix.install([
  {:helios_fleet, path: "../05_command_vessel"}
])

Application.ensure_all_started(:helios_fleet)

Helpers

defmodule Lesson05Helpers do
  alias HeliosFleet.Runtime

  def unique_agent_id(prefix) do
    "#{prefix}-#{System.unique_integer([:positive])}"
  end

  def safe_stop_family(parent_id) do
    child_ids = [
      "#{parent_id}/khepri/port_relay",
      "#{parent_id}/khepri/starboard_relay",
      "#{parent_id}/khepri"
    ]

    Enum.each(child_ids, fn child_id ->
      case Runtime.whereis(child_id) do
        nil -> :ok
        _pid -> Runtime.stop_agent(child_id)
      end
    end)

    case Runtime.whereis(parent_id) do
      nil -> :ok
      _pid -> Runtime.stop_agent(parent_id)
    end
  end
end

Put Khepri Under Command

alias HeliosFleet
alias Jido.AgentServer
alias Jido.Signal

vessel_id = Lesson05Helpers.unique_agent_id("orpheus")
{:ok, pid} = HeliosFleet.start_command_vessel(vessel_id)

{:ok, _vessel} =
  AgentServer.call(
    pid,
    Signal.new!(
      "fleet.command.probe_deployment_requested",
      %{
        probe_tag: "khepri",
        probe_id: "HF-17 Khepri",
        target: "ice fragment 6R"
      },
      source: "/orpheus/bridge"
    )
  )

{:ok, command_state} = AgentServer.state(pid)
khepri_pid = command_state.children.khepri.pid

{:ok, _probe} =
  AgentServer.call(
    khepri_pid,
    Signal.new!(
      "fleet.probe.relay_chain_requested",
      %{
        corridor: "icefall-west",
        relays: ["port_relay", "starboard_relay"]
      },
      source: "/orpheus/bridge"
    )
  )

{:ok, probe_state} = AgentServer.state(khepri_pid)
port_relay_pid = probe_state.children.port_relay.pid

{:ok, _relay} =
  AgentServer.call(
    port_relay_pid,
    Signal.new!(
      "relay.hold_corridor_assigned",
      %{
        corridor: "icefall-west",
        vector: "port_arc"
      },
      source: "/khepri"
    )
  )

Process.sleep(50)

{:ok, _probe} =
  AgentServer.call(
    khepri_pid,
    Signal.new!(
      "sensors.contact_classified",
      %{
        target: "ice fragment 6R",
        classification: "volatile_ice",
        confidence: "medium"
      },
      source: "/sensor/mast"
    )
  )

{:ok, _probe} =
  AgentServer.call(
    khepri_pid,
    Signal.new!("fleet.probe.command_update_requested", %{}, source: "/orpheus/bridge")
  )

Process.sleep(50)

{:ok, updated_command_state} = AgentServer.state(pid)
{:ok, updated_probe_state} = AgentServer.state(khepri_pid)

Lesson05Helpers.safe_stop_family(vessel_id)

%{
  fleet_updates: updated_command_state.agent.state.fleet_updates,
  local_probe_detail: %{
    relay_updates: updated_probe_state.agent.state.relay_updates,
    last_contact: updated_probe_state.agent.state.sensors.last_contact
  }
}