Skip to main content

spawn_agent

Function spawn_agent 

Source
pub async fn spawn_agent<H>(
    harness: &H,
    spec: AgentRunSpec,
    event_sender: ActivityEventSender,
    control_receiver: Option<ControlReceiver>,
) -> Result<DispatchOutcome, HarnessError>
where H: AgentHarness,
Expand description

Drives one activity attempt through the neutral AgentHarness seam and returns its terminal DispatchOutcome.

This is the harness-blind trait driver (NOI-4). It:

  1. starts the harness for spec (spawn/connect + capability handshake),
  2. concurrently pumps the session’s AgentSession::events to event_sender and feeds control_receiver commands to AgentSession::intervene until the event stream ends, then
  3. awaits AgentSession::wait_result and maps it into DispatchOutcome::Completed { output }.

A control_receiver of None runs the session with no intervention channel — the observability-only shape. When present, a command whose primitive the session does not advertise is rejected by the session with HarnessError::CapabilityNotSupported; the driver logs that rejection and keeps running (a gated command is a normal, non-fatal outcome, not a run failure).

The event stream ending signals end-of-run: the driver then takes the terminal result. This is why events must be a distinct channel from the result — the driver relies on the stream closing (not on inspecting any event) to know the run is done, and the result arrives only from AgentSession::wait_result.

§Errors

Returns HarnessError when the harness cannot be started or when the terminal result cannot be received. A harness-reported application failure (HarnessError::Harness) is returned to the caller, which maps it to a DispatchOutcome::Failed via harness_error_to_outcome; transport and protocol faults are surfaced as-is for the caller to classify.