Skip to main content

leviath_runtime/pipeline/
tools.rs

1//! Tool dispatch: batching, policy triage, and handing calls to the tool lane.
2
3use super::*;
4
5/// The agent's tool batch has been handed to the tool lane; it is waiting for
6/// the results (which the tool-collect system will apply).
7#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]
8pub struct AwaitingTools;
9
10/// Marker: this agent's advertised tools should be re-resolved before its next
11/// turn - mid-run dynamic tool discovery. Consumed by
12/// [`refresh_advertised_tools`], which asks the [`ToolService`] for the stage's
13/// fresh tool defs and writes them into the live [`StageInference`].
14#[derive(Component, Debug, Clone, Copy)]
15pub struct ToolsNeedRefresh;
16
17/// Marker: this agent opted into `dynamic_tools`. Only such agents
18/// are polled by [`poll_dynamic_tool_refresh`] for a pending tool re-scan, so the
19/// default (static) agent pays nothing.
20#[derive(Component, Debug, Clone, Copy)]
21pub struct DynamicTools;
22
23/// Reports one tool call's result the moment it resolves, from inside the
24/// executor - `(tool_call_id, result)`. Dispatch builds one per batch to journal
25/// each completion as a `ToolCallDone` record, so a crash mid-batch loses only
26/// the calls that genuinely never finished (issue #96). Implementors that don't
27/// journal get a no-op.
28pub type ToolProgress = Arc<dyn Fn(&str, &str) + Send + Sync>;
29
30/// A [`ToolProgress`] that reports nowhere - for worlds without a persistence
31/// lane and for `ToolService` impls under test.
32pub fn noop_progress() -> ToolProgress {
33    Arc::new(|_, _| {})
34}
35
36/// Provides a per-agent tool-execution closure. The concrete implementation
37/// (in the CLI) holds each agent's tool registry, workdir, and permission
38/// policy; the pipeline stays agnostic to *how* tools run. `exec_for` returns a
39/// boxed closure the tool worker runs off the tick.
40pub trait ToolService: Send + Sync {
41    /// Build the closure that runs `calls` for `entity`, resolving `(id, result)`
42    /// pairs. The executor calls `progress` with each call's result as it
43    /// resolves (per-call, not at batch end).
44    fn exec_for(
45        &self,
46        entity: Entity,
47        calls: Vec<leviath_providers::ToolCall>,
48        progress: ToolProgress,
49    ) -> BoxedToolExec;
50
51    /// Notify the service that `entity` entered the stage at `stage_index` named
52    /// `stage_name`, so it can re-sync that agent's per-stage tool permissions.
53    /// Default no-op for services without per-stage policy.
54    fn sync_stage(&self, _entity: Entity, _stage_index: usize, _stage_name: &str) {}
55
56    /// Re-resolve `entity`'s advertised tool defs for the stage at `stage_index` -
57    /// e.g. after new tools were discovered on disk. `None` means "no change"
58    /// (the default, for services without dynamic tools); `Some(tools)` replaces
59    /// the stage's advertised set.
60    fn refresh_tools(
61        &self,
62        _entity: Entity,
63        _stage_index: usize,
64    ) -> Option<Vec<leviath_providers::Tool>> {
65        None
66    }
67
68    /// Whether `entity` (a `dynamic_tools` agent) has pending tool changes that
69    /// warrant a re-scan + re-advertise. Polled by [`poll_dynamic_tool_refresh`];
70    /// implementors return (and clear) a per-agent dirty flag. Default `false`.
71    fn wants_refresh(&self, _entity: Entity) -> bool {
72        false
73    }
74}
75
76/// The tool service, as a world resource.
77#[derive(Resource, Clone)]
78pub struct ToolServiceRes(pub Arc<dyn ToolService>);
79
80/// The job sender feeding the tool lane, as a world resource, paired with the
81/// lane's occupancy counters so dispatch can record what it queued.
82#[derive(Resource, Clone)]
83pub struct ToolStage {
84    /// Where batches are handed to the lane.
85    pub jobs: UnboundedSender<ToolJob>,
86    /// Shared with the lane's workers; see [`crate::tool_bridge::ToolLaneStats`].
87    pub stats: Arc<crate::tool_bridge::ToolLaneStats>,
88}
89
90impl ToolStage {
91    /// A stage wired to a real lane's counters.
92    pub fn new(
93        jobs: UnboundedSender<ToolJob>,
94        stats: Arc<crate::tool_bridge::ToolLaneStats>,
95    ) -> Self {
96        Self { jobs, stats }
97    }
98
99    /// A stage with counters of its own, for callers that drive `dispatch_tools`
100    /// without a lane behind it (tests read the channel directly).
101    pub fn detached(jobs: UnboundedSender<ToolJob>) -> Self {
102        Self::new(jobs, Arc::new(crate::tool_bridge::ToolLaneStats::new(1)))
103    }
104}
105
106/// Context-tool results computed inline by [`dispatch_tools`] (the `context_*`
107/// tools mutate the ECS window, so they can't run on the async lane), held until
108/// [`collect_tools`] merges them with the lane results. Absent when a batch had
109/// no context tools.
110#[derive(Component, Debug, Clone, Default)]
111pub struct ContextToolResults(pub Vec<(String, String)>);
112
113/// Merge context + lane tool results into one `(id, result)` list in the
114/// original tool-call order (Anthropic requires a `tool_result` per `tool_use`,
115/// in order).
116/// Collapse a possibly-multiline string to a single trimmed line capped at
117/// `max` characters (with an ellipsis when truncated), for one-line log entries.
118pub(crate) fn one_line(s: &str, max: usize) -> String {
119    let flat = s.split_whitespace().collect::<Vec<_>>().join(" ");
120    if flat.chars().count() > max {
121        format!("{}…", flat.chars().take(max).collect::<String>())
122    } else {
123        flat
124    }
125}
126
127pub(crate) fn merge_in_call_order(
128    tool_calls: &[crate::components::ToolCall],
129    parts: &[(String, String)],
130) -> Vec<(String, String)> {
131    tool_calls
132        .iter()
133        .map(|tc| {
134            let result = parts
135                .iter()
136                .find(|(id, _)| id == &tc.tool_id)
137                .map(|(_, r)| r.clone())
138                .unwrap_or_default();
139            (tc.tool_id.clone(), result)
140        })
141        .collect()
142}
143
144/// Whether a tool result describes a call whose side effect never happened.
145///
146/// `[error]` (it ran and failed), `[denied]` (policy refused it),
147/// `[unavailable]` (the stage never offered it) and `[blocked]` (the taint
148/// gate stopped it) all mean the same thing to anything reasoning about what
149/// the agent *did*: file tracking must not record a write that was not
150/// written, and the modification counters behind a transition gate must not
151/// count it as work. Separate prefix lists are exactly how a new prefix gets
152/// missed - `[unavailable]` was, when dispatch began refusing unoffered tools
153/// and both call sites still listed only two, and `[blocked]` was again, so a
154/// taint-blocked write counted as a modification until issue #155's pass.
155pub(crate) fn call_had_no_effect(result: &str) -> bool {
156    result.starts_with("[error]")
157        || result.starts_with("[denied]")
158        || result.starts_with("[unavailable]")
159        || result.starts_with("[blocked]")
160}
161
162/// The effective tool names this stage advertised, canonicalised.
163///
164/// The same narrowing the request builder applies: `tools`, then `tool_filter`
165/// when it is set and non-empty. Deriving both from one function is what keeps
166/// "what the model was offered" and "what the model may call" the same set.
167pub(crate) fn offered_tool_names(stage: &StageInference) -> Vec<&str> {
168    stage
169        .tools
170        .iter()
171        .filter(|t| match stage.tool_filter.as_deref() {
172            Some(filter) if !filter.is_empty() => filter.iter().any(|f| f == &t.name),
173            _ => true,
174        })
175        .map(|t| leviath_tools::canonical_tool_name(&t.name))
176        .collect()
177}
178
179/// `Some(message)` when `name` is not among the stage's advertised tools.
180///
181/// The message is written for the model, not the user: it says plainly that the
182/// tool does not exist *here* and lists what does, so the next turn is a usable
183/// call rather than a retry of the same one. A stage advertising nothing says so
184/// instead of printing an empty list.
185pub(crate) fn unoffered_tool_refusal(stage: &StageInference, name: &str) -> Option<String> {
186    let canonical = leviath_tools::canonical_tool_name(name);
187    let offered = offered_tool_names(stage);
188    if offered.contains(&canonical) {
189        return None;
190    }
191    Some(match offered.is_empty() {
192        true => format!(
193            "[unavailable] '{name}' is not available in this stage, which has no \
194             tools at all. Answer directly instead of calling a tool."
195        ),
196        false => format!(
197            "[unavailable] '{name}' is not available in this stage. You may call: {}.",
198            offered.join(", ")
199        ),
200    })
201}
202
203/// How long a dispatched batch may wait for its journal record's ack before
204/// running anyway. The wait is what keeps the `ToolBatch` record on disk ahead
205/// of the batch's side effects; the bound is the liveness valve - a dead or
206/// backed-up persistence worker degrades to an unjournaled dispatch instead of
207/// wedging every tool batch behind it.
208pub(crate) const BATCH_JOURNAL_ACK_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1);
209
210/// Wrap a tool-execution closure so it first waits (bounded) for the batch's
211/// journal-record ack. Both outcomes - acked, or timeout/dropped sender -
212/// proceed to run the batch.
213pub(crate) fn barrier_then(
214    exec: BoxedToolExec,
215    ack: tokio::sync::oneshot::Receiver<()>,
216    timeout: std::time::Duration,
217) -> BoxedToolExec {
218    Box::new(move || {
219        Box::pin(async move {
220            let _ = tokio::time::timeout(timeout, ack).await;
221            exec().await
222        })
223    })
224}
225
226/// `Some(refusal)` when `name`'s arguments do not satisfy the schema the
227/// stage advertised for it.
228///
229/// The def is found by canonicalising both the called name and each advertised
230/// name, the same resolution `offered_tool_names` applies, so a tool offered
231/// as `bash` validates a call to `shell` and vice versa. A name with no def
232/// here validates as fine - after the unoffered-tool check that cannot happen,
233/// and the schema's absence is not the model's mistake to be refused over.
234///
235/// A schema that does not compile (a typo'd Rhai `@param` type, an MCP
236/// fragment this crate cannot interpret) is logged and skipped rather than
237/// refused: validation must never turn a working tool into an unusable one.
238///
239/// Schemas are compiled per call, deliberately. They are small, calls arrive
240/// at model latency, and a compiled-validator cache would need invalidating on
241/// every dynamic-tools re-advertisement and scoping per agent - real
242/// complexity for unmeasurable savings.
243pub(crate) fn invalid_args_refusal(
244    stage: &StageInference,
245    name: &str,
246    args: &serde_json::Value,
247) -> Option<String> {
248    let canonical = leviath_tools::canonical_tool_name(name);
249    let tool = stage
250        .tools
251        .iter()
252        .find(|t| leviath_tools::canonical_tool_name(&t.name) == canonical)?;
253    match leviath_tools::validate_tool_args(name, &tool.parameters, args) {
254        leviath_tools::ArgValidation::Valid => None,
255        leviath_tools::ArgValidation::SchemaUnusable(e) => {
256            tracing::warn!(
257                tool = %name,
258                error = %e,
259                "tool schema did not compile; skipping argument validation"
260            );
261            None
262        }
263        leviath_tools::ArgValidation::Invalid(msg) => Some(msg),
264    }
265}
266
267/// What `dispatch_tools` selects.
268///
269/// `&'static` is bevy's `WorldQuery` convention, not a claim about
270/// lifetimes: the borrow is bound when the query is fetched.
271type DispatchToolsQuery = (
272    Entity,
273    &'static AgentState,
274    &'static StageInference,
275    &'static crate::components::InferenceResult,
276    &'static mut ContextWindow,
277    Option<&'static crate::components::ToolResultRoutingComponent>,
278    Option<&'static ToolSensitivities>,
279    Option<&'static mut crate::taint::TaintGate>,
280    Option<&'static crate::gate_prompt::GateResolved>,
281    Option<&'static crate::components::GateAutoApprove>,
282    Option<&'static InFlightWork>,
283    Option<&'static StageCursor>,
284    Option<&'static RunMetadata>,
285    Option<&'static crate::components::OutputValidators>,
286);
287
288/// The resources the daemon installs, which a bare world does not have.
289///
290/// Every field is optional because `lev run` drives these same systems with no
291/// daemon behind them: no gate lane to prompt through, no persistence lane, no
292/// event sink. Bundled as one `SystemParam` so a system's signature stays about
293/// what it *queries* rather than listing the six things that might be wired.
294#[derive(bevy_ecs::system::SystemParam)]
295pub struct DaemonServices<'w> {
296    /// The taint-gate policy, when one is configured.
297    pub policy: Option<Res<'w, PolicyGate>>,
298    /// Rhai rules the gate consults before blocking.
299    pub script_rules: Option<Res<'w, GateScriptRules>>,
300    /// The hub a blocked call can prompt through.
301    pub hub: Option<Res<'w, InteractionHub>>,
302    /// The lane a gate prompt's answer comes back on.
303    pub gate_stage: Option<Res<'w, crate::gate_prompt::GatePromptStage>>,
304    /// The lane run state is written on.
305    pub persist: Option<Res<'w, PersistenceStage>>,
306    /// Where world events are broadcast.
307    pub sink: Option<Res<'w, crate::host::WorldEventSink>>,
308}
309
310/// Tool-dispatch system: for each `ReadyForTools` agent, apply its `context_*`
311/// tool calls inline (they mutate the ECS window) and hand the rest to the
312/// sequential tool lane, moving it to `AwaitingTools`. If a batch is *all*
313/// context tools there is nothing for the lane, so the results are applied
314/// immediately and the agent loops straight back to `ReadyToInfer`. The lane
315/// serializes execution, so there is no permit gate - every ready agent is
316/// enqueued in turn.
317///
318/// A persisted agent's batch is journaled at dispatch: a `ToolBatch` record
319/// (inline results pre-filled, lane calls pending) goes to the persistence lane
320/// with an ack the exec waits on, and a per-call [`ToolProgress`] journals each
321/// completion as a `ToolCallDone`. On a crash mid-batch, recovery replays the
322/// recorded results instead of re-running their side effects (issue #96).
323pub fn dispatch_tools(
324    mut agents: Query<DispatchToolsQuery, With<ReadyForTools>>,
325    service: Res<ToolServiceRes>,
326    stage: Res<ToolStage>,
327    daemon: DaemonServices,
328    mut commands: Commands,
329) {
330    let DaemonServices {
331        policy,
332        script_rules,
333        hub,
334        gate_stage,
335        persist,
336        sink,
337    } = daemon;
338    crate::tick_scope::clear();
339    let default_policy = leviath_core::PolicyConfig::default();
340    let policy_ref = policy.as_ref().map(|p| &p.0).unwrap_or(&default_policy);
341    let script_checker = script_rules.as_ref().map(|r| r.0.as_ref());
342    // Interactive gate prompting is available only when both the hub and the
343    // gate-prompt lane are wired (the daemon); otherwise blocks are returned as
344    // `[blocked]` immediately, preserving the headless/non-interactive behavior.
345    let interactive = hub.as_ref().zip(gate_stage.as_ref());
346    for (
347        entity,
348        state,
349        stage_inf,
350        result,
351        mut window,
352        routing,
353        sensitivities,
354        mut gate,
355        resolved,
356        auto_gate,
357        in_flight,
358        cursor,
359        metadata,
360        validators,
361    ) in agents.iter_mut()
362    {
363        crate::tick_scope::enter(entity);
364        // `--yolo`: waive taint-gate enforcement so a headless run never blocks
365        // on a gate prompt no one can answer (taint tracking still records).
366        let auto_approve_gates = auto_gate.is_some();
367        if state.status != AgentStatus::Active {
368            continue; // paused / waiting / cancelled - don't start new work
369        }
370
371        // Apply context_* tools inline (they need world access); collect the rest
372        // for the async lane. A taint-gated agent's outbound call that would leak
373        // over-cleared data (and isn't allowlisted) is blocked - either returned
374        // as `[blocked]`, or (interactive) held for a user gate prompt.
375        let mut context_results = Vec::new();
376        let mut lane_calls = Vec::new();
377        // A final output submitted in this batch, committed to the entity after
378        // the loop (the loop holds borrows `commands` would conflict with).
379        let mut submitted: Option<leviath_core::output::FinalOutput> = None;
380        // (tool_id, name, taint, clearance) for blocked calls awaiting a prompt.
381        let mut pending_prompts: Vec<(
382            String,
383            String,
384            leviath_core::TaintLevel,
385            leviath_core::TaintLevel,
386        )> = Vec::new();
387        for c in &result.tool_calls {
388            // Layer 1, enforced rather than merely advertised.
389            //
390            // A stage's `available_tools` was applied only when building the
391            // schema list sent to the model. Nothing checked it again here, so a
392            // model that *named* a tool it had never been offered got that call
393            // dispatched anyway - reaching the permission gate, and for a
394            // default-`Ask` tool surfacing to the user as an approval prompt for
395            // something the stage was never granted.
396            //
397            // That is not hypothetical. A `plan` stage granting only
398            // `read_file`/`list_dir`/`ask_user_*`/`edit_document` emitted
399            // `write_file` with a complete source file in it, and the user was
400            // asked to approve writing code from the planning stage. Declining
401            // it was the only thing that stopped it.
402            //
403            // Checked against `StageInference`, which *is* the set advertised
404            // for this stage - resolved at spawn, swapped on every transition,
405            // and rewritten by the dynamic-tools refresh - so enforcement cannot
406            // drift from advertising the way a second copy of the rule would.
407            if let Some(refusal) = unoffered_tool_refusal(stage_inf, &c.name) {
408                context_results.push((c.tool_id.clone(), refusal));
409                continue;
410            }
411            // Layer 2: the call must satisfy the schema the model was shown.
412            // A mismatched call is refused back to the model with the
413            // validator's message, so the next turn can self-correct, rather
414            // than executed on garbage or surfaced to the user as a permission
415            // prompt for arguments that were never valid. Deterministic, so a
416            // gate-prompt re-run of the same batch refuses identically.
417            if let Some(refusal) = invalid_args_refusal(stage_inf, &c.name, &c.arguments) {
418                context_results.push((c.tool_id.clone(), refusal));
419                continue;
420            }
421            if crate::context_tools::is_context_tool(&c.name) {
422                let text =
423                    crate::context_tools::handle_context_tool(&c.name, &c.arguments, &mut window);
424                context_results.push((c.tool_id.clone(), text));
425                continue;
426            }
427            // Applied inline for the same reason the context tools are: it
428            // writes the live window and an ECS component, neither of which the
429            // async lane can reach. Recorded here and committed after the loop,
430            // because `commands` cannot be borrowed inside it.
431            if crate::output_tool::is_output_tool(&c.name) {
432                let (text, output) = crate::output_tool::handle_output_tool(
433                    &c.arguments,
434                    stage_inf.output.as_ref(),
435                    validators,
436                    &state.current_stage,
437                    chrono::Utc::now().timestamp(),
438                    metadata.map(|m| std::path::Path::new(&m.workdir)),
439                    &mut window,
440                );
441                // A refused submission leaves any earlier one alone: a bad
442                // correction must not erase a good answer.
443                if let Some(output) = output {
444                    submitted = Some(output);
445                }
446                context_results.push((c.tool_id.clone(), text));
447                continue;
448            }
449            // A call the user already resolved in a prior prompt round.
450            if let Some(resolved) = resolved {
451                if let Some(msg) = resolved.denied.get(&c.tool_id) {
452                    context_results.push((c.tool_id.clone(), msg.clone()));
453                    continue;
454                }
455                if resolved.approved.contains(&c.tool_id) {
456                    lane_calls.push(leviath_providers::ToolCall {
457                        id: c.tool_id.clone(),
458                        name: c.name.clone(),
459                        arguments: c.arguments.clone(),
460                        thought_signature: c.thought_signature.clone(),
461                    });
462                    continue;
463                }
464            }
465            if let Some(gate) = gate.as_deref_mut() {
466                let decision = gate.check_with_policy(
467                    &state.agent_id,
468                    &c.name,
469                    &window,
470                    None,
471                    policy_ref,
472                    script_checker,
473                );
474                if !decision.is_allowed() {
475                    if auto_approve_gates {
476                        // `--yolo`: waive enforcement but record the override in
477                        // the audit trail (rather than skipping the gate), so the
478                        // over-cleared call is still accounted for. Fall through
479                        // to dispatch the call.
480                        let (taint, clearance) = decision
481                            .blocked_levels()
482                            .expect("a non-Allowed GateDecision is always Blocked");
483                        gate.record_allow(
484                            &state.agent_id,
485                            &c.name,
486                            taint,
487                            clearance,
488                            leviath_core::taint::GateDecisionSource::YoloAutoApprove,
489                        );
490                    } else {
491                        match (interactive, decision.blocked_levels()) {
492                            (Some(_), Some((taint, clearance))) => {
493                                pending_prompts.push((
494                                    c.tool_id.clone(),
495                                    c.name.clone(),
496                                    taint,
497                                    clearance,
498                                ));
499                            }
500                            _ => {
501                                context_results
502                                    .push((c.tool_id.clone(), taint_block_message(&decision)));
503                            }
504                        }
505                        continue;
506                    }
507                }
508            }
509            lane_calls.push(leviath_providers::ToolCall {
510                id: c.tool_id.clone(),
511                name: c.name.clone(),
512                arguments: c.arguments.clone(),
513                thought_signature: c.thought_signature.clone(),
514            });
515        }
516
517        // Commit a submitted output before any of the paths below can take an
518        // early exit, so an answer is recorded whether the rest of the batch
519        // dispatches, holds for a gate prompt, or turns out to be empty.
520        // Re-applying it on a gate-prompt re-run is harmless: the same
521        // submission produces the same component.
522        if let Some(output) = submitted {
523            commands
524                .entity(entity)
525                .insert(crate::persistence::FinalOutput(output));
526        }
527
528        // Hold the batch and ask the user about each blocked call.
529        if let (false, Some((hub, gate_stage))) = (pending_prompts.is_empty(), interactive) {
530            let n = pending_prompts.len();
531            for (tool_id, name, taint, clearance) in pending_prompts {
532                gate_stage
533                    .runtime
534                    .spawn(crate::gate_prompt::run_gate_prompt(
535                        crate::gate_prompt::GatedCall {
536                            entity,
537                            agent_id: state.agent_id.clone(),
538                            tool_id,
539                            tool_name: name,
540                            taint,
541                            clearance,
542                        },
543                        crate::interaction_hub::PromptLane {
544                            hub: (*hub).clone(),
545                            outcomes: gate_stage.outcomes.clone(),
546                            wake: gate_stage.wake.clone(),
547                        },
548                    ));
549            }
550            commands
551                .entity(entity)
552                .remove::<ReadyForTools>()
553                .insert(crate::gate_prompt::AwaitingGatePrompt(n))
554                .insert(crate::gate_prompt::GateResolved::default());
555            continue; // re-run after the prompts resolve
556        }
557
558        // Dispatching the batch consumes any resolution state from a prior round.
559        commands
560            .entity(entity)
561            .remove::<crate::gate_prompt::GateResolved>();
562
563        if lane_calls.is_empty() {
564            // Nothing async to run - apply the context results now and loop back.
565            let merged = merge_in_call_order(&result.tool_calls, &context_results);
566            apply_tool_results(
567                &mut window,
568                &result.response,
569                &result.tool_calls,
570                &merged,
571                routing.map(|c| &c.routing),
572                sensitivities.map(|s| &s.0),
573            );
574            commands
575                .entity(entity)
576                .remove::<ReadyForTools>()
577                .insert(ReadyToInfer);
578            continue;
579        }
580
581        // Journal the batch before it can run: a `ToolBatch` record with the
582        // dispatcher's inline results pre-filled and every lane call pending,
583        // plus a per-call progress hook that records each completion. Worlds
584        // without a persistence lane or run metadata (tests, unpersisted
585        // agents) dispatch unjournaled with a no-op progress.
586        let (progress, ack) = match (persist.as_ref(), metadata) {
587            (Some(persist), Some(md)) => {
588                let record = leviath_core::run_archive::RunRecord::ToolBatch {
589                    calls: result
590                        .tool_calls
591                        .iter()
592                        .map(|c| leviath_core::run_archive::ToolCallRecord {
593                            id: c.tool_id.clone(),
594                            name: c.name.clone(),
595                            arguments: c.arguments.to_string(),
596                            result: context_results
597                                .iter()
598                                .find(|(id, _)| id == &c.tool_id)
599                                .map(|(_, r)| r.clone()),
600                            thought_signature: c.thought_signature.clone(),
601                        })
602                        .collect(),
603                    at: chrono::Utc::now().timestamp(),
604                    stage_index: cursor.map_or(0, |c| c.index),
605                    iteration: state.iteration,
606                    response: result.response.clone(),
607                };
608                let (ack_tx, ack_rx) = tokio::sync::oneshot::channel();
609                let _ = persist.0.send(PersistMsg::Append {
610                    run_id: md.run_id.clone(),
611                    record: Box::new(record),
612                    ack: Some(ack_tx),
613                });
614                let sender = persist.0.clone();
615                let run_id = md.run_id.clone();
616                let iteration = state.iteration;
617                let progress: ToolProgress = Arc::new(move |call_id: &str, result: &str| {
618                    let _ = sender.send(PersistMsg::Append {
619                        run_id: run_id.clone(),
620                        record: Box::new(leviath_core::run_archive::RunRecord::ToolCallDone {
621                            iteration,
622                            call_id: call_id.to_string(),
623                            result: result.to_string(),
624                            at: chrono::Utc::now().timestamp(),
625                        }),
626                        ack: None,
627                    });
628                });
629                (progress, Some(ack_rx))
630            }
631            _ => (noop_progress(), None),
632        };
633        // Announce each lane-bound call before it starts executing. Inline
634        // results (context tools, refusals, blocks) never reach the lane and
635        // are deliberately not announced.
636        if let (Some(sink), Some(md)) = (sink.as_ref(), metadata) {
637            for call in &lane_calls {
638                let _ = sink.0.send(crate::host::WorldEvent::ToolCallStarted {
639                    run_id: md.run_id.clone(),
640                    agent_id: state.agent_id.clone(),
641                    call_id: call.id.clone(),
642                    tool: call.name.clone(),
643                });
644            }
645        }
646        let exec = service.0.exec_for(entity, lane_calls, progress);
647        let exec = match ack {
648            Some(ack) => barrier_then(exec, ack, BATCH_JOURNAL_ACK_TIMEOUT),
649            None => exec,
650        };
651        let cancel = crate::cancel::CancelToken::new();
652        // The lane is alive for the world's lifetime; a failed send would
653        // only happen during shutdown, where dropping the job is fine.
654        stage.stats.enqueued();
655        let _ = stage.jobs.send(ToolJob {
656            entity,
657            exec,
658            cancel: cancel.clone(),
659        });
660        track_in_flight(&mut commands, entity, in_flight, cancel);
661        commands
662            .entity(entity)
663            .remove::<ReadyForTools>()
664            .insert(AwaitingTools)
665            .insert(ContextToolResults(context_results));
666    }
667}