GenServer that manages an agent session.
The session maintains:
- Conversation history (messages)
- Current model configuration
- Available tools
- Streaming state
The Agent Loop
When a prompt is received:
- Add user message to history
- Call LLM with system prompt, messages, and tools
- If LLM returns tool calls:
- Execute each tool
- Add tool results to history
- Go to step 2
- If LLM returns text only, return response
Summary
Functions
Aborts the current operation.
Returns a specification to start this module under a supervisor.
Clears the conversation history.
Runs the configured compactor against the current message history.
Queues a follow-up message.
Returns the conversation history.
Returns the unique identifier for this session.
Describes the run the session's previous process died in, or nil.
Continues the run the session's previous process died in.
Continues the run the session's previous process died in, streaming it.
Runs a prompt synchronously, returning the final response.
Delivers a signal to the running turn's parked tool calls.
Injects a steering message.
Streams a prompt, returning an enumerable of events.
Stops the changes watch/1 subscribed the caller to.
Returns the session's current Condukt.Session.View: its history, the turn
in flight with its partial answer and running tools, queued input, and agent
configuration, free of secrets.
Returns what a parked turn waits for, or nil while it runs or is idle.
Returns the session's current Condukt.Session.View and subscribes the
caller to the changes after it.
Functions
Aborts the current operation.
Replies to a running caller with {:error, :aborted}, or closes its stream
after that error. The turn's process is shut down before this returns, and
the processes linked below it go down with it: running tool calls and what
they own, such as a foreground sub-agent's session. Tools that ask to
:finish (see Condukt.Tool) run to the end instead. Side effects already
made are not rolled back, and late events and results from the turn cannot
affect the session or a replacement turn.
Returns a specification to start this module under a supervisor.
See Supervisor.
Clears the conversation history.
Runs the configured compactor against the current message history.
No-op when no compactor is configured. The compacted snapshot is
immediately persisted if a session store is configured. With background
compaction enabled, waits without blocking the session process, and returns
{:error, :already_streaming} if a turn is active. Returns
{:error, :interrupted} while the session has an interrupted run, which
resume/2 continues and a new run gives up.
Queues a follow-up message.
It starts a turn once the running turn finishes, or at once when the session
is idle. Turns started this way stream their events to the notifier and
handle_event/2. Aborting withdraws queued messages.
Returns the conversation history.
Returns the unique identifier for this session.
Sessions are identified by a UUIDv7 generated when the session starts, or by
the value passed via the :id option to start_link/2 / start/2. The id
is included in telemetry metadata so subscribers can group all events
emitted by a single agentic run.
Describes the run the session's previous process died in, or nil.
Returns %{run_id: id, request_id: id_or_nil, restarts: count}.
Continues the run the session's previous process died in.
Only a session whose store keeps a journal (see Condukt.SessionStore) can
have an interrupted run; interrupted/1 says whether this one does. The run
picks up after its last stored step: an interrupted model request is made
again, a tool call that was interrupted runs again only when its tool is
replay-safe (Condukt.Tool), and otherwise the model gets an error result
saying the call was interrupted. A run restarted more than the session's
:restart_limit times in a row without progress ends with
{:error, {:restart_limit, restarts}} instead.
Returns the run's result like run/3, or {:error, :nothing_to_resume}.
resume_stream/2 streams the run instead.
Continues the run the session's previous process died in, streaming it.
The streaming counterpart of resume/2, returning the same events as
stream/3. Tool results the resume decides, interrupted ones included, are
emitted before the run goes on; a final answer that was stored but never
delivered is emitted as one {:text, answer}. Emits {:error, reason} and
ends when there is nothing to resume, another run is streaming, the run
reached its restart limit, or its restart cannot be stored.
Runs a prompt synchronously, returning the final response.
Returns {:error, :already_streaming} while another caller's turn runs. A
turn started from queued input, such as a follow-up or a background
sub-agent's report, has no caller, so the prompt waits for it instead.
Returns {:error, {:session_exit, reason}} when the session process cannot
complete the call because it exits, is missing, or exceeds the call timeout.
With :request_id, a retry of the same id returns the first outcome instead
of running again, and resumes the run when the previous process died in it.
See the Sessions and Persistence guide.
Delivers a signal to the running turn's parked tool calls.
agent is a session pid, name, or the id of a session started with
Condukt.Sessions. An id reaches whatever sessions the configured
Condukt.Sessions.Registry sees, which with a cluster-wide adapter includes
other nodes. kind is a Condukt.Signal module, whose cast/1
checks value in the calling process.
Returns :ok when the value is kept for the call parked on {kind, key},
which wakes, or for the first call that parks on it later in the turn.
Otherwise returns {:error, reason}:
:duplicate- the key already holds a value, as for a webhook retry.{:invalid, reason}-cast/1rejected the value; nothing is kept.:closed- no turn is running, or the key's wait is over: its deadline came first, another of its keys woke it, or its turn ended. Keys stay closed for the rest of the session.:inbox_full- the turn already holds 100 signals for keys no call has parked on.:not_found- no session answers underagent.
Treat :duplicate and :closed as final answers to the sender. Nothing
checks who sends a signal, so authenticate senders in the host.
Injects a steering message.
A running native turn takes it before its next model request, after the current tool round. An idle session starts a turn with it.
Streams a prompt, returning an enumerable of events.
With :request_id, a retry continues or replays the request as run/3
does. When the session process dies before the stream ends, the stream
emits {:error, {:session_exit, reason}} and ends.
Stops the changes watch/1 subscribed the caller to.
Returns the session's current Condukt.Session.View: its history, the turn
in flight with its partial answer and running tools, queued input, and agent
configuration, free of secrets.
Pair it with numbered changes from a notifier on another node; see
Condukt.Notifiers.PubSub. In the same node, or with the session's pid,
watch/1 returns the view and subscribes in one step.
Returns what a parked turn waits for, or nil while it runs or is idle.
A turn is waiting once each of its pending tool calls is parked and its
worker blocks until a signal or a deadline wakes one. The result is
%{tool_calls: [%{tool_call_id: id, tool: name, signals: keys, until: deadline}], wake_at: deadline},
where wake_at is the earliest deadline or nil. The session publishes the
same map as a {:waiting, map} event whenever it changes.
Returns the session's current Condukt.Session.View and subscribes the
caller to the changes after it.
The caller receives {:condukt_change, session_id, epoch, seq, change} for
every change, starting with the one numbered after the view's seq, and
applies them with Condukt.Session.View.advance/4. Nothing between the view
and the first change is lost or repeated. Watching again only returns the
current view. The subscription ends with unwatch/1 or the caller's exit.