Normandy.Coordination.AgentSupervisor
(normandy v2.2.0)
View Source
Dynamic supervisor for managing agent processes.
AgentSupervisor provides fault-tolerant supervision of agent processes, enabling automatic restarts on failures and dynamic agent pool management.
Features
- Dynamic agent spawning
- Automatic restart on failure
- Configurable restart intensity (
:max_restarts,:max_seconds) - Agent process discovery
Example
# Start supervisor
{:ok, sup_pid} = AgentSupervisor.start_link(
name: :agent_supervisor,
strategy: :one_for_one
)
# Start an agent under supervision
{:ok, agent_pid} = AgentSupervisor.start_agent(
sup_pid,
agent: my_agent,
agent_id: "research_agent"
)
# List all supervised agents
agents = AgentSupervisor.list_agents(sup_pid)
Summary
Functions
Returns a specification to start this module under a supervisor.
Returns count of supervised agents.
Looks up a running agent by its agent_id: {:ok, pid} or :error.
Lists all supervised agent processes.
Starts an agent process under supervision.
Starts the agent supervisor.
Terminates an agent process.
Terminates all supervised agents.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec count_agents(Supervisor.supervisor()) :: non_neg_integer()
Returns count of supervised agents.
Example
count = AgentSupervisor.count_agents(supervisor)
#=> 5
@spec fetch_agent(Supervisor.supervisor(), String.t()) :: {:ok, pid()} | :error
Looks up a running agent by its agent_id: {:ok, pid} or :error.
Example
{:ok, pid} = AgentSupervisor.fetch_agent(supervisor, "agent_1")
@spec list_agents(Supervisor.supervisor()) :: [{pid(), String.t()}]
Lists all supervised agent processes.
Returns list of {pid, agent_id} tuples.
Example
agents = AgentSupervisor.list_agents(supervisor)
#=> [{#PID<0.123.0>, "agent_1"}, {#PID<0.124.0>, "agent_2"}]
@spec start_agent( Supervisor.supervisor(), keyword() ) :: DynamicSupervisor.on_start_child()
Starts an agent process under supervision.
Options
AgentProcess.start_link/1's options, plus :restart:
:agent(a%Normandy.Agents.BaseAgentConfig{}) - Required. The agent config the process runs.:name(term/0) - AGenServer.name/0for the process.:agent_id(String.t/0) - The agent's id (default: a new UUID). In:servermode it is also the session id.:context_pid- ANormandy.Coordination.StatefulContextprocess, kept in the agent's state.:turn_engine-:inlinerunsBaseAgent.run/3in a worker process, one turn at a time;:serverroutes turns throughTurn.Session/Turn.Server(approval parking, passivation, persistence). The default value is:inline.:store({module, handle}) -:servermode: theSessionStoreas{module, handle}. Without it the process starts and owns an in-memory store.:registry({module, handle}) -:servermode: theSessionRegistryas{module, handle}. Without it the process uses a scope of its own in a registry Normandy starts.:supervisor(a pid or a supervisor name) -:servermode: the session supervisor. Without it the process starts and owns aTurn.Supervisor.:supervisor_mod-:servermode: the module whosestart_server/2starts sessions (defaultNormandy.Agents.Turn.Supervisor).:template_provider({module, handle}) -:servermode: theAgentTemplateas{module, handle}.:resume_policy-:servermode: the session's resume policy (default:lazy).:template_id(String.t/0) -:servermode: the template id (default: the config's name).:subscriber(function of arity 2) -:servermode: passed toTurn.Server.start_link/1.:handlers(aNormandy.Agents.Turn.Driver.Handlersornil) -:servermode: passed toTurn.Server.start_link/1.:approval_timeout_ms(timeout/0) -:servermode: passed toTurn.Server.start_link/1.:on_approval_timeout-:servermode: passed toTurn.Server.start_link/1.:idle_timeout_ms(timeout/0) -:servermode: passed toTurn.Server.start_link/1.:max_pending_runs(a positive integer or:infinity) - Runs and casts admitted and not finished (queued or in flight). One more is refused with a:queue_fullNormandy.TurnError. The default value is100.:restart- The child's restart value. Not passed to the agent process. The default value is:transient.
Example
{:ok, pid} = AgentSupervisor.start_agent(
supervisor,
agent: my_agent,
agent_id: "agent_1",
name: :research_agent
)
@spec start_link(keyword()) :: Supervisor.on_start()
Starts the agent supervisor.
Options
:name(term/0) - AGenServer.name/0for the supervisor.:strategy-DynamicSupervisorsupports only:one_for_one. The default value is:one_for_one.:max_restarts(non_neg_integer/0) - Restarts allowed within:max_seconds. The default value is3.:max_seconds(pos_integer/0) - The window for:max_restarts, in seconds. The default value is5.
Example
{:ok, pid} = AgentSupervisor.start_link(name: :my_supervisor)
@spec terminate_agent(Supervisor.supervisor(), pid()) :: :ok | {:error, :not_found}
Terminates an agent process.
Example
:ok = AgentSupervisor.terminate_agent(supervisor, agent_pid)
@spec terminate_all(Supervisor.supervisor()) :: :ok
Terminates all supervised agents.
Example
:ok = AgentSupervisor.terminate_all(supervisor)