Synapse.AgentRegistry (Synapse v0.1.1)

View Source

Registry for managing agent instances and ensuring idempotent spawning.

Provides a centralized way to track active agents and prevent duplicate spawning of the same agent instance.

Summary

Functions

Returns a specification to start this module under a supervisor.

Gets or spawns an agent, ensuring only one instance exists.

Lists all registered agents.

Looks up an agent by ID.

Registers an already-started agent.

Starts the agent registry.

Types

agent_id()

@type agent_id() :: String.t()

agent_module()

@type agent_module() :: module()

agent_pid()

@type agent_pid() :: pid()

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_or_spawn(registry \\ __MODULE__, agent_id, agent_module, opts \\ [])

@spec get_or_spawn(GenServer.server(), agent_id(), agent_module(), keyword()) ::
  {:ok, agent_pid(), boolean()} | {:error, term()}

Gets or spawns an agent, ensuring only one instance exists.

Returns {:ok, pid, started?} where started? is true when a new agent process was launched and false when an existing agent was reused. Returns {:error, reason} if spawning failed.

Examples

{:ok, pid} = AgentRegistry.get_or_spawn(registry, "security_specialist", SecurityAgent)
{:ok, ^pid} = AgentRegistry.get_or_spawn(registry, "security_specialist", SecurityAgent)

list_agents(registry \\ __MODULE__)

@spec list_agents(GenServer.server()) :: [{agent_id(), agent_pid()}]

Lists all registered agents.

Returns a list of {agent_id, pid} tuples.

lookup(registry \\ __MODULE__, agent_id)

@spec lookup(GenServer.server(), agent_id()) ::
  {:ok, agent_pid()} | {:error, :not_found}

Looks up an agent by ID.

Returns {:ok, pid} if found, {:error, :not_found} otherwise.

register(registry \\ __MODULE__, agent_id, pid)

@spec register(GenServer.server(), agent_id(), agent_pid()) ::
  :ok | {:error, :already_registered}

Registers an already-started agent.

Returns :ok if successful, {:error, :already_registered} if agent_id is taken.

start_link(opts \\ [])

Starts the agent registry.

unregister(registry \\ __MODULE__, agent_id)

@spec unregister(GenServer.server(), agent_id()) :: :ok

Unregisters an agent.