Skip to main content

ToolEventSink

Trait ToolEventSink 

Source
pub trait ToolEventSink: Send + Sync {
    // Required methods
    fn tool_started<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>(
        &'life0 self,
        agent: &'life1 str,
        agent_id: &'life2 str,
        call_id: &'life3 str,
        tool: &'life4 str,
        args_preview: &'life5 str,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait,
             'life5: 'async_trait;
    fn tool_finished<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>(
        &'life0 self,
        agent: &'life1 str,
        agent_id: &'life2 str,
        call_id: &'life3 str,
        tool: &'life4 str,
        success: bool,
        result_preview: &'life5 str,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait,
             'life5: 'async_trait;
    fn tool_error<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>(
        &'life0 self,
        agent: &'life1 str,
        agent_id: &'life2 str,
        call_id: &'life3 str,
        tool: &'life4 str,
        error: &'life5 str,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait,
             'life5: 'async_trait;

    // Provided methods
    fn context_budget<'life0, 'life1, 'async_trait>(
        &'life0 self,
        snapshot: &'life1 ContextBudgetSnapshot,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn tool_telemetry<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>(
        &'life0 self,
        _agent: &'life1 str,
        _agent_id: &'life2 str,
        _call_id: &'life3 str,
        _tool: &'life4 str,
        _telemetry: &'life5 ToolTelemetry,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait,
             'life5: 'async_trait { ... }
    fn agent_message<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
        &'life0 self,
        _agent: &'life1 str,
        _agent_id: &'life2 str,
        _turn_id: &'life3 str,
        _delta: &'life4 str,
        _done: bool,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait { ... }
}
Expand description

Observes tool dispatch lifecycle events from an AgentLoop run.

Why: The only way to make an agent loop’s tool activity observable to an external subscriber (a session.attached client) without forking the loop or the tool registry. What: Four hooks; call_id is the ToolCall::id the model assigned, correlating every hook for the same invocation. All are &self (not &mut self) so a sink can be shared as Arc<dyn ToolEventSink> across a PM loop and every delegated sub-agent loop.

(UI Phase 1) Every hook takes agent — the name of the agent whose loop is dispatching. It is a PARAMETER rather than sink state precisely BECAUSE one sink instance is shared by the PM’s loop and every delegated sub-agent’s loop (see task::executor::run_and_record, which clones one Arc into both): a sink that stored its own agent name could only ever report one of them. The calling AgentLoop is the only layer that knows which agent it is, so it passes its identity per call — see AgentLoop::with_agent. (DOC-39 AC-13.1/13.2) Every hook ALSO takes agent_id — the dispatching loop’s STABLE per-spawn id (see AgentLoop::with_agent_id). agent alone cannot distinguish two concurrently-delegated sub-agents that share one name (e.g. two python-engineer delegations): both attribute as "python-engineer" on this stream, and prior to agent_id a client’s only recourse was fragile AgentSpawned/AgentDone stream-ordering inference. agent_id is minted once per delegation (runner::in_process::InProcessAgentRunner::run_pipeline) and threaded through unchanged for that whole sub-agent run. Test: crate::task::sink::tests::*.

Required Methods§

Source

fn tool_started<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>( &'life0 self, agent: &'life1 str, agent_id: &'life2 str, call_id: &'life3 str, tool: &'life4 str, args_preview: &'life5 str, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait, 'life5: 'async_trait,

A tool invocation is about to run.

Source

fn tool_finished<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>( &'life0 self, agent: &'life1 str, agent_id: &'life2 str, call_id: &'life3 str, tool: &'life4 str, success: bool, result_preview: &'life5 str, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait, 'life5: 'async_trait,

A tool invocation completed (successfully or with a recoverable error — success distinguishes the two). Use Self::tool_error instead for an exceptional (non-recoverable) failure.

Source

fn tool_error<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>( &'life0 self, agent: &'life1 str, agent_id: &'life2 str, call_id: &'life3 str, tool: &'life4 str, error: &'life5 str, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait, 'life5: 'async_trait,

A tool invocation raised an exceptional (non-recoverable) error.

Provided Methods§

Source

fn context_budget<'life0, 'life1, 'async_trait>( &'life0 self, snapshot: &'life1 ContextBudgetSnapshot, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

The working-context budget was measured at a turn boundary (epic #2343).

Why: cadence::maybe_cadence_compress recomputes this every turn and, before this hook existed, DISCARDED it — its own doc said it existed “for observability/tests” while observability consumed nothing. This is the seam that lets the measurement reach a subscriber, reusing the sink the loop already carries rather than threading an event bus (and a session id the engine layer must not know about) into it. What: called at most once per turn, only from a loop that has cadence ENABLED — cadence is PM-only by construction (AgentLoopConfig.cadence defaults None), so a delegated engineer loop never calls this. Defaults to a no-op so every pre-existing sink implementation compiles and behaves exactly as before.

Source

fn tool_telemetry<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>( &'life0 self, _agent: &'life1 str, _agent_id: &'life2 str, _call_id: &'life3 str, _tool: &'life4 str, _telemetry: &'life5 ToolTelemetry, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait, 'life5: 'async_trait,

A tool reported structured telemetry about what it actually did (UI Phase 1) — a real search lane, or which recalled memories entered context. Fires IN ADDITION to Self::tool_finished, never instead of it, so existing consumers of the generic tool events are unaffected.

Why: a default no-op body keeps every pre-existing sink impl (and every test double) compiling unchanged — only sinks that actually serve the UI need to override it. What: telemetry is the tool’s own account, carried on its ToolResult; see crate::tools::telemetry. Test: crate::task::sink::tests::forwards_search_and_recall_telemetry.

Source

fn agent_message<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>( &'life0 self, _agent: &'life1 str, _agent_id: &'life2 str, _turn_id: &'life3 str, _delta: &'life4 str, _done: bool, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait,

An incremental piece of one assistant turn’s text became available (tcode streaming epic #3696 — Gap A Slice 1, completed by Gap B in #4425).

Why: before this hook, a text-bearing assistant turn was recorded to the transcript (transcript.push_response) but never surfaced to a session.attached client — only tool activity was observable via the hooks above. This is the seam that makes the assistant’s own words streamable, reusing the same optional-sink pattern rather than threading the event bus into the engine layer. What: a default no-op body keeps every pre-existing sink impl (and every test double) compiling unchanged — only sinks that actually serve the UI need to override it. turn_id MUST be unique within the session across ALL agents (see crate::events::Event::AgentMessageDelta’s doc for why a per-agent-local counter is unsafe); callers mint a UUID v4 per turn and reuse it for EVERY delta of that turn.

§Delta protocol (#4425)

AgentLoop calls this repeatedly per text-bearing turn: once per content chunk with done: false, then exactly once with done: true and an EMPTY delta. A subscriber therefore appends every delta it receives and treats the done: true call purely as “this bubble is complete” — it carries no text of its own, so appending it unconditionally is correct. Chunk boundaries are the provider’s, not semantic: against a transport without native streaming (the Bedrock path until #4426, and every scripted test double) the whole turn arrives as ONE content delta followed by the terminal one, which is indistinguishable to a correct consumer.

A tool-only turn (the model emitted no text) calls this ZERO times — there is no bubble to render — so a consumer must not assume every turn produces a terminal delta. Test: crate::task::sink::tests::agent_message_reaches_stream_as_delta; the per-turn call sequence is pinned by crate::agent_loop::tests::sink_events::sink_receives_agent_message_delta_for_text_turn_only and …::native_streaming_transport_emits_incremental_deltas.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§