ExAgent.Conversations (ExAgent v3.0.0)

View Source

Supervise temporary, lazily restored conversation Servers. Keys are {namespace, agent_id} using the same identity rules as Server. The :server factory receives the key as its first argument.

Stability — Stable.

:registry names the owners: :local (default) on this node, :global across the connected cluster, or a {module, opts} adapter of ExAgent.Conversations.Registry. A cluster registry requires durable, atomic continuations; see that behaviour for the netsplit guarantees. Owners start on the node that calls ensure/3.

Each manager on a node shares one cold activation per key and accepts up to :max_activations distinct cold keys at a time (16 by default, in 1..1024). :max_activation_waiters caps callers awaiting each key (128 by default, in 1..1024, including the first caller). :factory_timeout bounds preparation of the Server options (5,000 ms by default, in 1..4294967295). Server initialization has its own lifecycle. Owners that are already initialized bypass this budget.

Summary

Functions

Apply one Server operation. The callback may be invoked a second time after non-delivery or a pre-execution writer fence. Non-delivery includes an owner that explicitly discarded the unread request during termination. The exit reason alone cannot establish non-delivery. Keep other effects outside it. To submit a keyed turn, mint its Server.new_admission_key/1 before building the callback and capture it in the closure. The callback and any caller retry then identify the same logical turn. One :timeout (5,000 ms) covers lookup, activation and any wait or retry for a replacement owner. An expired budget skips the retry. The callback runs in the caller and uses the Server operation's own timeout.

Returns a specification to start this module under a supervisor.

Discard an unused branch in an authorized scoped Store and free its slot.

Drain this node's conversation manager before stopping its supervisor.

Return the current owner, or share a cold activation of the key. Set create: false to restore only. It can use an owner initialized by another caller, but it cannot start a conversation without stored state. When a restore-only activation finds no state, only callers that allow creation continue to the creation attempt.

Create new_id in the source conversation's scoped Store and namespace.

Start a manager with a local registered atom :name and a :server factory. The factory is a unary function or an MFA that receives the key before its extra arguments. Invalid options raise ArgumentError in the caller.

Stop the current owner. It stops its active worker, cancels queued requests, emits :server_stopped, and exits with :reason (default {:shutdown, :stopped}). An active caller receives {:error, :result_unconfirmed} and a durable continuation restores as its Store left it.

Registered address of the conversation in the configured registry.

Look up an initialized owner, on this node or, with a cluster registry, another.

Types

key()

@type key() :: {String.t() | nil, String.t()}

Functions

call(name, key, fun, opts \\ [])

Apply one Server operation. The callback may be invoked a second time after non-delivery or a pre-execution writer fence. Non-delivery includes an owner that explicitly discarded the unread request during termination. The exit reason alone cannot establish non-delivery. Keep other effects outside it. To submit a keyed turn, mint its Server.new_admission_key/1 before building the callback and capture it in the closure. The callback and any caller retry then identify the same logical turn. One :timeout (5,000 ms) covers lookup, activation and any wait or retry for a replacement owner. An expired budget skips the retry. The callback runs in the caller and uses the Server operation's own timeout.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

discard_fork(store, id)

(candidate API)
@spec discard_fork(ExAgent.Store.scoped(), String.t()) :: :ok | {:error, term()}

Discard an unused branch in an authorized scoped Store and free its slot.

Quiesce the branch on every node before discarding it. Only the initial fork snapshot with no owner or admissions is eligible. The Store checks that its inventory references are still present elsewhere in the family. The discarded ID is sealed. Other family members retain their data and share the original retention boundary.

A live or changed branch returns {:error, :fork_in_use}. A branch with unique inventory references returns {:error, :fork_retained_references}. Erase the family with ExAgent.Retention.erase/3 to remove used branches.

drain(name, opts \\ [])

@spec drain(atom(), keyword()) :: :ok | {:error, :unavailable | :result_unconfirmed}

Drain this node's conversation manager before stopping its supervisor.

The manager refuses new operations through ensure/3 and call/4 with {:error, :unavailable}. Local owners stop taking new turns, finish the current one, and cancel queued turns with the usual receipts. A successful turn releases its C7 claim at its regular checkpoint. The node scope applies with both local and cluster registries.

:ok confirms the local owners exited with their persistence and queued cancellations confirmed. A missing manager returns {:error, :unavailable}. If the relative :timeout (default 5,000 ms) expires after dispatch, or an owner cannot confirm cleanup, the caller receives {:error, :result_unconfirmed}. A dispatched drain continues after the caller stops waiting. Repeat the call to await the same drain. Start a new manager to reopen admission.

ensure(name, key, opts \\ [])

@spec ensure(atom(), key(), keyword()) :: {:ok, pid()} | {:error, term()}

Return the current owner, or share a cold activation of the key. Set create: false to restore only. It can use an owner initialized by another caller, but it cannot start a conversation without stored state. When a restore-only activation finds no state, only callers that allow creation continue to the creation attempt.

:timeout (5,000 ms) bounds owner lookup and the wait for activation with one deadline. Expiry returns {:error, :result_unconfirmed}. An activation already requested may still finish. Look up its owner with whereis/2. A start with an unconfirmed acknowledgement keeps its reservation while the supervisor may still initialize the owner.

A missing or draining manager returns {:error, :unavailable}. A new key at capacity returns {:error, {:start_failed, :activation_capacity}}. A full wait queue for the key returns {:error, {:start_failed, :activation_queue_full}} immediately. If option preparation exceeds the manager's :factory_timeout, callers receive {:error, {:start_failed, :factory_timeout}}.

fork(source, new_id, opts)

(candidate API)
@spec fork(
  GenServer.server() | {ExAgent.Store.t(), String.t()},
  String.t(),
  keyword()
) ::
  {:ok, map()} | {:error, term()}

Create new_id in the source conversation's scoped Store and namespace.

The source is a Server address or {scoped_store, source_id} for cold access. Pass the original root run ID as :at. The branch includes the settled turn with that ID, its prior accepted rows and its historical tool results. It starts with zero accumulated usage, forked_from snapshot metadata and a fresh C7 journal on its first new run. The source and its branches share retention.

Forking requires the built-in supervised ETS or PostgreSQL Store. The destination must be a fresh ID. An existing destination returns {:error, :destination_exists}; an unresolved effect in the selected prefix returns {:error, :open_effects}. A snapshot without C7 may copy historical unknown returns from earlier failed turns into a later settled turn's prefix. The family shares 64 branch slots. Once full, this call returns {:error, :fork_limit}. Use discard_fork/2 to release unused branches.

A live source accepts :timeout for acknowledgement. Check the destination before repeating an unconfirmed fork. The host authorizes both identities and can activate the new ID with ensure/3 using create: false.

start_link(opts)

@spec start_link(keyword()) :: Supervisor.on_start()

Start a manager with a local registered atom :name and a :server factory. The factory is a unary function or an MFA that receives the key before its extra arguments. Invalid options raise ArgumentError in the caller.

stop(name, key, opts \\ [])

@spec stop(atom(), key(), keyword()) ::
  :ok | {:error, :unavailable | :result_unconfirmed}

Stop the current owner. It stops its active worker, cancels queued requests, emits :server_stopped, and exits with :reason (default {:shutdown, :stopped}). An active caller receives {:error, :result_unconfirmed} and a durable continuation restores as its Store left it.

:ok confirms that the owner exited and resolved every queued cancellation with a live key. An unresolved queued cancellation emits :server_request_cancelled with unconfirmed persistence. The owner still exits. If the stop or any of these writes lacks confirmation, the caller receives {:error, :result_unconfirmed}. After reopening, check the original key with ExAgent.Server.receipt/3.

The relative :timeout (5,000 ms by default) includes owner lookup. An absent owner or a lookup that expires before dispatch returns {:error, :unavailable}.

via(name, key)

Registered address of the conversation in the configured registry.

whereis(name, key)

Look up an initialized owner, on this node or, with a cluster registry, another.