Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(web_chat): extract progress bridge spawn into its own module

Steven Enamakel43cd21546166
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index 975600fac1..051d3e6a2d 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -107,889 +107,9 @@ pub(super) use ledger::{ledger_append_event, ledger_upsert_agent_run, ledger_ups
include!("progress_bridge_attribution.rs");
-/// Spawn a background task that reads [`AgentProgress`] events from the
-/// agent turn loop and translates them into [`WebChannelEvent`]s tagged
-/// with the correct client/thread/request IDs. The task runs until the
-/// sender is dropped (i.e. when the agent turn finishes).
-pub(crate) fn spawn_progress_bridge(
- mut rx: tokio::sync::mpsc::Receiver<crate::progress::AgentProgress>,
- client_id: String,
- thread_id: String,
- request_id: String,
- turn_state_store: std::sync::Arc<dyn TurnStates>,
- metadata: ChatRequestMetadata,
- config: crate::config::Config,
- dispatch_ctx: crate::core::runtime::DispatchCtx,
-) -> ProgressBridgeHandle {
- use crate::progress::AgentProgress;
- use std::collections::HashMap;
- use tinyagents_session::run_ledger::{
- AgentRunKind, AgentRunStatus, AgentRunUpsert, RunEventAppend, RunTelemetryUpsert,
- };
-
- let (drained_tx, drained_rx) = tokio::sync::watch::channel(false);
- let timing_snapshot: std::sync::Arc<
- std::sync::Mutex<Option<super::turn_timing::TurnTimingSnapshot>>,
- > = std::sync::Arc::new(std::sync::Mutex::new(None));
- let timing_snapshot_for_task = timing_snapshot.clone();
- dispatch_ctx.clone().spawn(async move {
- log::debug!(
- "[web_channel][bridge] spawned client_id={} thread_id={} request_id={} speak_reply={:?} source={:?} session_id={:?} hold_text_stream={}",
- client_id,
- thread_id,
- request_id,
- metadata.speak_reply,
- metadata.source,
- metadata.session_id,
- metadata.hold_text_stream,
- );
- let mut round: u32 = 0;
- // Accumulates the parent agent's streamed narration for the current
- // round. When a tool call closes the round, this leading narration is
- // flushed as an interim chat bubble (`chat_interim`) so it persists in
- // the thread instead of vanishing on settle — the final answer arrives
- // separately via `deliver_response` and is never part of this buffer
- // (it belongs to the terminal round, which ends with no tool call).
- let mut pending_narration = String::new();
- let mut timing = super::turn_timing::TurnTiming::start();
- let mut turn_cost_throttle = super::turn_timing::TurnCostThrottle::new();
- let mut events_seen: u64 = 0;
- // Per-request monotonic ordering key stamped on every emitted
- // web-channel event (see `publish_seq_stamped`). Unique per emission so
- // the frontend can dedup by `(request_id, seq)`.
- let mut emit_seq: u64 = 0;
- let mut parent_completed = false;
- let mut parent_tool_count: u64 = 0;
- let mut child_tool_counts: HashMap<String, u64> = HashMap::new();
- // task_id -> the parent tool-call id that spawned it, remembered from
- // `SubagentSpawned` so later lifecycle events for the same task
- // (`subagent_completed`/`_failed`/`_awaiting_user`) can still carry
- // it even though those `AgentProgress` variants don't repeat it.
- let mut subagent_parent_call_ids: HashMap<String, Option<String>> = HashMap::new();
- let mut turn_state =
- TurnStateMirror::new(turn_state_store, thread_id.clone(), request_id.clone());
-
- // #3886: structured tracing export (`progress_bridge_trace.rs`).
- let mut journal_trace_ctx =
- trace::trace_context(&config, &metadata, &client_id, &thread_id, &request_id);
-
- // #4270: emit a periodic liveness beat for the whole in-flight turn so
- // the frontend silence timer never false-fires during a long prefill or
- // a buffered-reasoning phase that streams no progress events. The beat
- // is gated on `turn_active` (set once `TurnStarted` is observed) so we
- // never emit before the turn's `inference_start` has armed the timer.
- let mut heartbeat =
- tokio::time::interval(std::time::Duration::from_secs(INFERENCE_HEARTBEAT_SECS));
- // Wall-clock cadence: a slow turn must not produce a burst of catch-up
- // beats once it finally yields control.
- heartbeat.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
- // `interval`'s first tick resolves immediately — consume it so the first
- // real beat lands one full interval after the turn begins.
- heartbeat.tick().await;
- let mut turn_active = false;
- let bridge_ctx =
- BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);
-
- loop {
- let event = tokio::select! {
- // Drain real progress events preferentially over the timer so a
- // busy turn never starves event handling to emit a beat.
- biased;
- maybe = rx.recv() => match maybe {
- Some(ev) => ev,
- None => break,
- },
- _ = heartbeat.tick() => {
- if turn_active {
- log::trace!(
- "[web_channel][bridge] inference_heartbeat thread_id={} request_id={}",
- thread_id,
- request_id,
- );
- publish_seq_stamped(&dispatch_ctx, &mut emit_seq, WebChannelEvent {
- event: "inference_heartbeat".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- ..Default::default()
- });
- }
- continue;
- }
- };
- events_seen += 1;
- turn_state.observe(&event);
- match &event {
- AgentProgress::TextDelta { delta, iteration } => {
- log::trace!(
- "[web_channel][bridge] text_delta round={} chars={} request_id={}",
- iteration,
- delta.len(),
- request_id,
- );
- }
- AgentProgress::ThinkingDelta { delta, iteration } => {
- log::trace!(
- "[web_channel][bridge] thinking_delta round={} chars={} request_id={}",
- iteration,
- delta.len(),
- request_id,
- );
- }
- AgentProgress::ToolCallArgsDelta {
- call_id,
- tool_name,
- delta,
- iteration,
- } => {
- log::trace!(
- "[web_channel][bridge] tool_args_delta round={} tool={} call_id={} chars={} request_id={}",
- iteration,
- tool_name,
- call_id,
- delta.len(),
- request_id,
- );
- }
- AgentProgress::ToolCallStarted {
- call_id,
- tool_name,
- iteration,
- ..
- } => {
- log::debug!(
- "[web_channel][bridge] tool_call round={} tool={} call_id={} request_id={}",
- iteration,
- tool_name,
- call_id,
- request_id,
- );
- }
- AgentProgress::ToolCallCompleted {
- call_id,
- tool_name,
- success,
- iteration,
- ..
- } => {
- log::debug!(
- "[web_channel][bridge] tool_result round={} tool={} call_id={} success={} request_id={}",
- iteration,
- tool_name,
- call_id,
- success,
- request_id,
- );
- }
- AgentProgress::SubagentFailed {
- agent_id, error, ..
- } => {
- log::warn!(
- "[web_channel][bridge] subagent_failed agent_id={} err={} client_id={} thread_id={} request_id={}",
- agent_id,
- error,
- client_id,
- thread_id,
- request_id,
- );
- }
- other => {
- log::debug!(
- "[web_channel][bridge] lifecycle event={:?} request_id={}",
- std::mem::discriminant(other),
- request_id,
- );
- }
- }
- match event {
- AgentProgress::TurnStarted => {
- // Turn is live — start emitting liveness beats (issue #4270).
- turn_active = true;
- ledger_upsert_agent_run(
- &config,
- AgentRunUpsert {
- id: request_id.clone(),
- kind: AgentRunKind::BackgroundAgent,
- parent_run_id: None,
- parent_thread_id: Some(thread_id.clone()),
- agent_id: Some("orchestrator".to_string()),
- status: AgentRunStatus::Running,
- prompt_ref: Some(format!("thread:{thread_id}:request:{request_id}")),
- worker_thread_id: None,
- checkpoint_path: None,
- checkpoint: None,
- summary: None,
- error: None,
- metadata: json!({
- "clientId": client_id,
- "source": "web_channel",
- "schemaVersion": 1
- }),
- started_at: None,
- completed_at: None,
- },
- );
- ledger_append_event(
- &config,
- RunEventAppend {
- run_id: request_id.clone(),
- event_type: "turn_started".to_string(),
- payload: json!({ "threadId": thread_id, "clientId": client_id }),
- },
- );
- publish_seq_stamped(
- &dispatch_ctx,
- &mut emit_seq,
- WebChannelEvent {
- event: "inference_start".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- ..Default::default()
- },
- );
- }
- AgentProgress::IterationStarted {
- iteration,
- max_iterations,
- } => {
- round = iteration;
- publish_seq_stamped(
- &dispatch_ctx,
- &mut emit_seq,
- WebChannelEvent {
- event: "iteration_start".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- message: Some(format!("Iteration {iteration}/{max_iterations}")),
- round: Some(iteration),
- ..Default::default()
- },
- );
- }
- AgentProgress::ContextCompacting => {
- publish_seq_stamped(
- &dispatch_ctx,
- &mut emit_seq,
- WebChannelEvent {
- event: "context_compacting".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- round: Some(round),
- ..Default::default()
- },
- );
- }
- AgentProgress::ToolCallStarted {
- call_id,
- tool_name,
- arguments,
- iteration,
- display_label,
- display_detail,
- } => {
- timing.tool_call(&tool_name, iteration, &request_id);
- // The parent's leading narration for this round is complete
- // once it calls a tool — flush it as an interim bubble so it
- // persists interleaved with the tool activity.
- flush_interim_narration(
- &dispatch_ctx,
- &mut pending_narration,
- iteration,
- &client_id,
- &thread_id,
- &request_id,
- &mut emit_seq,
- );
- parent_tool_count += 1;
- ledger_append_event(
- &config,
- RunEventAppend {
- run_id: request_id.clone(),
- event_type: "tool_call_started".to_string(),
- payload: json!({
- "callId": call_id,
- "toolName": tool_name,
- "iteration": iteration
- }),
- },
- );
- ledger_upsert_telemetry(
- &config,
- RunTelemetryUpsert {
- run_id: request_id.clone(),
- tool_count: Some(parent_tool_count),
- ..Default::default()
- },
- );
- publish_seq_stamped(
- &dispatch_ctx,
- &mut emit_seq,
- WebChannelEvent {
- event: "tool_call".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- tool_name: Some(tool_name),
- skill_id: Some("web_channel".to_string()),
- args: cap_wire_args(Some(arguments)),
- round: Some(iteration),
- tool_call_id: Some(call_id),
- tool_display_label: display_label,
- tool_display_detail: display_detail,
- ..Default::default()
- },
- );
- }
- AgentProgress::ToolCallCompleted {
- call_id,
- tool_name,
- success,
- output_chars,
- output,
- arguments,
- elapsed_ms,
- iteration,
- failure,
- display_label,
- display_detail,
- structured,
- } => {
- // Serialize the classified failure (if any) for the UI + ledger.
- let failure_json = failure.as_ref().and_then(|f| serde_json::to_value(f).ok());
- ledger_append_event(
- &config,
- RunEventAppend {
- run_id: request_id.clone(),
- event_type: "tool_call_completed".to_string(),
- payload: json!({
- "callId": call_id,
- "toolName": tool_name,
- "success": success,
- "outputChars": output_chars,
- "elapsedMs": elapsed_ms,
- "iteration": iteration,
- "failure": failure_json,
- }),
- },
- );
- log::debug!(
- "[web_channel][bridge] tool_result round={} tool={} call_id={} \
- success={} elapsed_ms={} has_structured={} request_id={}",
- iteration,
- tool_name,
- call_id,
- success,
- elapsed_ms,
- structured.is_some(),
- request_id
- );
- publish_seq_stamped(
- &dispatch_ctx,
- &mut emit_seq,
- WebChannelEvent {
- event: "tool_result".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- tool_name: Some(tool_name),
- skill_id: Some("web_channel".to_string()),
- // Forward the real tool result (size-capped) so the UI
- // can render tool output — mirrors the subagent
- // `subagent_tool_result` path. Frontends that only
- // need size/timing read the ledger telemetry instead.
- output: Some(cap_wire_output(output)),
- // The call arguments the harness captured at
- // completion (`ToolCallStarted.arguments` is
- // always `Null` on this path). Omitted when the
- // harness ran with payload capture off.
- args: cap_wire_args(arguments),
- success: Some(success),
- round: Some(iteration),
- tool_call_id: Some(call_id),
- failure: failure_json,
- elapsed_ms: Some(elapsed_ms),
- structured,
- // Recomputed from the real arguments (unlike the
- // started event's args-free computation), so a
- // completed row can pick up a detail that only
- // became knowable once the arguments existed.
- tool_display_label: display_label,
- tool_display_detail: display_detail,
- ..Default::default()
- },
- );
- }
- AgentProgress::SubagentSpawned {
- agent_id,
- task_id,
- mode,
- dedicated_thread,
- prompt_chars,
- worker_thread_id,
- display_name,
- parent_call_id,
- ..
- } => {
- subagent_events::on_subagent_spawned(
- &bridge_ctx,
- &mut emit_seq,
- &mut subagent_parent_call_ids,
- round,
- agent_id,
- task_id,
- mode,
- dedicated_thread,
- prompt_chars,
- worker_thread_id,
- display_name,
- parent_call_id,
- );
- }
- AgentProgress::SubagentCompleted {
- agent_id,
- task_id,
- elapsed_ms,
- iterations,
- output_chars,
- output,
- usage,
- worktree_path,
- changed_files,
- dirty_status,
- ..
- } => {
- subagent_events::on_subagent_completed(
- &bridge_ctx,
- &mut emit_seq,
- &mut subagent_parent_call_ids,
- &child_tool_counts,
- round,
- agent_id,
- task_id,
- elapsed_ms,
- iterations,
- output_chars,
- output,
- usage,
- worktree_path,
- changed_files,
- dirty_status,
- );
- }
- AgentProgress::SubagentFailed {
- agent_id,
- task_id,
- error,
- } => {
- subagent_events::on_subagent_failed(
- &bridge_ctx,
- &mut emit_seq,
- &mut subagent_parent_call_ids,
- &child_tool_counts,
- round,
- agent_id,
- task_id,
- error,
- );
- }
- AgentProgress::SubagentAwaitingUser {
- agent_id,
- task_id,
- question,
- worker_thread_id,
- checkpoint_path,
- } => {
- subagent_events::on_subagent_awaiting_user(
- &bridge_ctx,
- &mut emit_seq,
- &subagent_parent_call_ids,
- round,
- agent_id,
- task_id,
- question,
- worker_thread_id,
- checkpoint_path,
- );
- }
- AgentProgress::SubagentIterationStarted {
- agent_id,
- task_id,
- iteration,
- max_iterations,
- extended_policy,
- } => {
- subagent_events::on_subagent_iteration_started(
- &bridge_ctx,
- &mut emit_seq,
- round,
- agent_id,
- task_id,
- iteration,
- max_iterations,
- extended_policy,
- );
- }
- AgentProgress::SubagentToolCallStarted {
- agent_id,
- task_id,
- call_id,
- tool_name,
- arguments,
- iteration,
- display_label,
- display_detail,
- } => {
- subagent_events::on_subagent_tool_call_started(
- &bridge_ctx,
- &mut emit_seq,
- &mut child_tool_counts,
- round,
- agent_id,
- task_id,
- call_id,
- tool_name,
- arguments,
- iteration,
- display_label,
- display_detail,
- );
- }
- AgentProgress::SubagentToolCallCompleted {
- agent_id,
- task_id,
- call_id,
- tool_name,
- success,
- output_chars,
- output,
- arguments,
- elapsed_ms,
- iteration,
- failure,
- display_label,
- display_detail,
- structured,
- } => {
- subagent_events::on_subagent_tool_call_completed(
- &bridge_ctx,
- &mut emit_seq,
- round,
- agent_id,
- task_id,
- call_id,
- tool_name,
- success,
- output_chars,
- output,
- arguments,
- elapsed_ms,
- iteration,
- failure,
- display_label,
- display_detail,
- structured,
- );
- }
- AgentProgress::SubagentTextDelta {
- agent_id,
- task_id,
- delta,
- iteration,
- } => {
- subagent_events::on_subagent_text_delta(
- &bridge_ctx,
- &mut emit_seq,
- round,
- agent_id,
- task_id,
- delta,
- iteration,
- );
- }
- AgentProgress::SubagentThinkingDelta {
- agent_id,
- task_id,
- delta,
- iteration,
- } => {
- subagent_events::on_subagent_thinking_delta(
- &bridge_ctx,
- &mut emit_seq,
- round,
- agent_id,
- task_id,
- delta,
- iteration,
- );
- }
- AgentProgress::TextDelta { delta, iteration } => {
- timing.text_delta(&delta, iteration, &request_id);
- // Buffer the round's narration so it can be flushed as an
- // interim bubble if a tool call closes this round.
- pending_narration.push_str(&delta);
- if !metadata.hold_text_stream {
- text_events::publish_text_delta(
- &dispatch_ctx,
- &mut emit_seq,
- &client_id,
- &thread_id,
- &request_id,
- iteration,
- delta,
- );
- }
- }
- AgentProgress::ThinkingDelta { delta, iteration } => {
- publish_seq_stamped(
- &dispatch_ctx,
- &mut emit_seq,
- WebChannelEvent {
- event: "thinking_delta".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- round: Some(iteration),
- delta: Some(delta),
- delta_kind: Some("thinking".to_string()),
- ..Default::default()
- },
- );
- }
- AgentProgress::ToolCallArgsDelta {
- call_id,
- tool_name,
- delta,
- iteration,
- } => {
- publish_seq_stamped(
- &dispatch_ctx,
- &mut emit_seq,
- WebChannelEvent {
- event: "tool_args_delta".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- tool_name: if tool_name.is_empty() {
- None
- } else {
- Some(tool_name)
- },
- skill_id: Some("web_channel".to_string()),
- round: Some(iteration),
- delta: Some(delta),
- delta_kind: Some("tool_args".to_string()),
- tool_call_id: Some(call_id),
- ..Default::default()
- },
- );
- }
- AgentProgress::TurnCompleted { iterations, .. } => {
- parent_completed = true;
- timing.done(iterations, MIN_INTERIM_NARRATION_CHARS, &request_id);
- if let Ok(mut guard) = timing_snapshot_for_task.lock() {
- *guard = Some(timing.snapshot());
- }
- // Turn is done — stop liveness beats (issue #4270). The FE
- // clears its silence timer on `chat_done`/`chat_error`; this
- // also prevents a stray beat racing the channel close.
- turn_active = false;
- let completed_at = chrono::Utc::now();
- ledger_upsert_agent_run(
- &config,
- AgentRunUpsert {
- id: request_id.clone(),
- kind: AgentRunKind::BackgroundAgent,
- parent_run_id: None,
- parent_thread_id: Some(thread_id.clone()),
- agent_id: Some("orchestrator".to_string()),
- status: AgentRunStatus::Completed,
- prompt_ref: Some(format!("thread:{thread_id}:request:{request_id}")),
- worker_thread_id: None,
- checkpoint_path: None,
- checkpoint: None,
- summary: Some(format!("Completed in {iterations} iteration(s)")),
- error: None,
- metadata: json!({}),
- started_at: None,
- completed_at: Some(completed_at),
- },
- );
- ledger_append_event(
- &config,
- RunEventAppend {
- run_id: request_id.clone(),
- event_type: "turn_completed".to_string(),
- payload: json!({ "iterations": iterations }),
- },
- );
- log::debug!(
- "[web_channel] turn completed after {iterations} iteration(s) \
- client_id={client_id} thread_id={thread_id} request_id={request_id} \
- speak_reply={:?} source={:?} session_id={:?}",
- metadata.speak_reply,
- metadata.source,
- metadata.session_id,
- );
- // Every event the parent queued before `TurnCompleted` has
- // now been forwarded, in order: release a caller waiting
- // to publish the terminal `chat_done`.
- let _ = drained_tx.send(true);
- log::debug!(
- "[web_channel][bridge] drained parent events_seen={events_seen} request_id={request_id}"
- );
- }
- AgentProgress::TurnCostUpdated {
- model,
- iteration,
- input_tokens,
- output_tokens,
- cached_input_tokens,
- total_usd,
- } => {
- ledger_upsert_telemetry(
- &config,
- RunTelemetryUpsert {
- run_id: request_id.clone(),
- input_tokens: Some(input_tokens),
- output_tokens: Some(output_tokens),
- cached_input_tokens: Some(cached_input_tokens),
- cost_usd: Some(total_usd),
- model: Some(model.clone()),
- ..Default::default()
- },
- );
- log::debug!(
- "[web_channel] turn cost update model={model} iter={iteration} \
- in={input_tokens} out={output_tokens} cached_in={cached_input_tokens} \
- total_usd={total_usd:.4} client_id={client_id} thread_id={thread_id}"
- );
-
- // Live cost readout: throttled so a fast multi-round turn
- // doesn't flood the socket with one `turn_cost` per model
- // call. `TurnCostUpdated` is the parent's cumulative
- // rollup only — it carries no per-sub-agent breakdown, so
- // `subagents` stays empty here; the final `chat_done.usage`
- // (built from `LastTurnUsage` at delivery) is still where
- // sub-agent attribution shows up.
- if turn_cost_throttle.should_emit() {
- publish_seq_stamped(
- &dispatch_ctx,
- &mut emit_seq,
- WebChannelEvent {
- event: "turn_cost".to_string(),
- client_id: client_id.clone(),
- thread_id: thread_id.clone(),
- request_id: request_id.clone(),
- round: Some(iteration),
- usage: Some(crate::web_chat::TurnUsagePayload {
- input_tokens,
- output_tokens,
- cached_input_tokens,
- // A live rollup of the known spend so far;
- // the turn's final figure (or `null`)
- // arrives on `chat_done`.
- cost_usd: Some(total_usd),
- cost_source: tinyagents_harness::cost::CostSource::Charged,
- context_window: 0,
- context_tokens: 0,
- subagents: Vec::new(),
- }),
- ..Default::default()
- },
- );
- }
- }
- AgentProgress::ToolCallProgress { call_id, message } => {
- let ids = (client_id.as_str(), thread_id.as_str(), request_id.as_str());
- tool_progress::publish(&dispatch_ctx, &mut emit_seq, ids, call_id, message);
- }
- AgentProgress::TurnContent { .. } => {
- // Prompt/reply content is attached to the trace span by the
- // span collector above; the ledger/telemetry bridge ignores it.
- }
- AgentProgress::ModelCallCompleted {
- model,
- iteration,
- input_tokens,
- output_tokens,
- cost_usd,
- ..
- } => {
- // Per-call usage is consumed by the span collector above
- // (per-call Langfuse generation); the socket/ledger surfaces
- // stay on the cumulative TurnCostUpdated rollup.
- log::debug!(
- "[web_channel][bridge] model_call_completed model={model} iter={iteration} \
- in={input_tokens} out={output_tokens} cost_usd={cost_usd:?} request_id={request_id}"
- );
- }
- }
- }
- turn_state.finish();
- if !parent_completed {
- ledger_upsert_agent_run(
- &config,
- AgentRunUpsert {
- id: request_id.clone(),
- kind: AgentRunKind::BackgroundAgent,
- parent_run_id: None,
- parent_thread_id: Some(thread_id.clone()),
- agent_id: Some("orchestrator".to_string()),
- status: AgentRunStatus::Interrupted,
- prompt_ref: Some(format!("thread:{thread_id}:request:{request_id}")),
- worker_thread_id: None,
- checkpoint_path: None,
- checkpoint: None,
- summary: None,
- error: Some("progress bridge exited before turn completion".to_string()),
- metadata: json!({}),
- started_at: None,
- completed_at: Some(chrono::Utc::now()),
- },
- );
- ledger_append_event(
- &config,
- RunEventAppend {
- run_id: request_id.clone(),
- event_type: "turn_interrupted".to_string(),
- payload: json!({ "eventsSeen": events_seen }),
- },
- );
- }
- // #3886: seal any spans still open after the stream closed and hand the
- // run's trace to the configured tracing sink. Best-effort and gated;
- // never affects the turn outcome.
- // The response presenter waits only briefly for this signal before
- // publishing an error. Trace export is best-effort I/O and must not
- // hold up terminal delivery after the progress stream closed.
- let _ = drained_tx.send(true);
- if let Some(trace_ctx) = journal_trace_ctx.take() {
- crate::core::observability::progress_tracing::export_run_trace_from_journal(
- &dispatch_ctx,
- &config,
- &request_id,
- trace_ctx,
- )
- .await;
- }
-
- log::debug!(
- "[web_channel][bridge] exit client_id={} thread_id={} request_id={} round={} events_seen={}",
- client_id,
- thread_id,
- request_id,
- round,
- events_seen,
- );
- });
- ProgressBridgeHandle {
- drained: drained_rx,
- timing: timing_snapshot,
- }
-}
+#[path = "progress_bridge_spawn.rs"]
+mod spawn;
+pub(crate) use spawn::spawn_progress_bridge;
#[cfg(test)]
#[path = "progress_bridge_tests.rs"]
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge_spawn.rs b/crates/openhuman-core/src/web_chat/progress_bridge_spawn.rs
new file mode 100644
index 0000000000..0cb3f77acd
--- /dev/null
+++ b/crates/openhuman-core/src/web_chat/progress_bridge_spawn.rs
@@ -0,0 +1,886 @@
+use super::*;
+
+/// Spawn a background task that reads [`AgentProgress`] events from the
+/// agent turn loop and translates them into [`WebChannelEvent`]s tagged
+/// with the correct client/thread/request IDs. The task runs until the
+/// sender is dropped (i.e. when the agent turn finishes).
+pub(crate) fn spawn_progress_bridge(
+ mut rx: tokio::sync::mpsc::Receiver<crate::progress::AgentProgress>,
+ client_id: String,
+ thread_id: String,
+ request_id: String,
+ turn_state_store: std::sync::Arc<dyn TurnStates>,
+ metadata: ChatRequestMetadata,
+ config: crate::config::Config,
+ dispatch_ctx: crate::core::runtime::DispatchCtx,
+) -> ProgressBridgeHandle {
+ use crate::progress::AgentProgress;
+ use std::collections::HashMap;
+ use tinyagents_session::run_ledger::{
+ AgentRunKind, AgentRunStatus, AgentRunUpsert, RunEventAppend, RunTelemetryUpsert,
+ };
+
+ let (drained_tx, drained_rx) = tokio::sync::watch::channel(false);
+ let timing_snapshot: std::sync::Arc<
+ std::sync::Mutex<Option<super::turn_timing::TurnTimingSnapshot>>,
+ > = std::sync::Arc::new(std::sync::Mutex::new(None));
+ let timing_snapshot_for_task = timing_snapshot.clone();
+ dispatch_ctx.clone().spawn(async move {
+ log::debug!(
+ "[web_channel][bridge] spawned client_id={} thread_id={} request_id={} speak_reply={:?} source={:?} session_id={:?} hold_text_stream={}",
+ client_id,
+ thread_id,
+ request_id,
+ metadata.speak_reply,
+ metadata.source,
+ metadata.session_id,
+ metadata.hold_text_stream,
+ );
+ let mut round: u32 = 0;
+ // Accumulates the parent agent's streamed narration for the current
+ // round. When a tool call closes the round, this leading narration is
+ // flushed as an interim chat bubble (`chat_interim`) so it persists in
+ // the thread instead of vanishing on settle — the final answer arrives
+ // separately via `deliver_response` and is never part of this buffer
+ // (it belongs to the terminal round, which ends with no tool call).
+ let mut pending_narration = String::new();
+ let mut timing = super::turn_timing::TurnTiming::start();
+ let mut turn_cost_throttle = super::turn_timing::TurnCostThrottle::new();
+ let mut events_seen: u64 = 0;
+ // Per-request monotonic ordering key stamped on every emitted
+ // web-channel event (see `publish_seq_stamped`). Unique per emission so
+ // the frontend can dedup by `(request_id, seq)`.
+ let mut emit_seq: u64 = 0;
+ let mut parent_completed = false;
+ let mut parent_tool_count: u64 = 0;
+ let mut child_tool_counts: HashMap<String, u64> = HashMap::new();
+ // task_id -> the parent tool-call id that spawned it, remembered from
+ // `SubagentSpawned` so later lifecycle events for the same task
+ // (`subagent_completed`/`_failed`/`_awaiting_user`) can still carry
+ // it even though those `AgentProgress` variants don't repeat it.
+ let mut subagent_parent_call_ids: HashMap<String, Option<String>> = HashMap::new();
+ let mut turn_state =
+ TurnStateMirror::new(turn_state_store, thread_id.clone(), request_id.clone());
+
+ // #3886: structured tracing export (`progress_bridge_trace.rs`).
+ let mut journal_trace_ctx =
+ trace::trace_context(&config, &metadata, &client_id, &thread_id, &request_id);
+
+ // #4270: emit a periodic liveness beat for the whole in-flight turn so
+ // the frontend silence timer never false-fires during a long prefill or
+ // a buffered-reasoning phase that streams no progress events. The beat
+ // is gated on `turn_active` (set once `TurnStarted` is observed) so we
+ // never emit before the turn's `inference_start` has armed the timer.
+ let mut heartbeat =
+ tokio::time::interval(std::time::Duration::from_secs(INFERENCE_HEARTBEAT_SECS));
+ // Wall-clock cadence: a slow turn must not produce a burst of catch-up
+ // beats once it finally yields control.
+ heartbeat.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
+ // `interval`'s first tick resolves immediately — consume it so the first
+ // real beat lands one full interval after the turn begins.
+ heartbeat.tick().await;
+ let mut turn_active = false;
+ let bridge_ctx =
+ BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);
+
+ loop {
+ let event = tokio::select! {
+ // Drain real progress events preferentially over the timer so a
+ // busy turn never starves event handling to emit a beat.
+ biased;
+ maybe = rx.recv() => match maybe {
+ Some(ev) => ev,
+ None => break,
+ },
+ _ = heartbeat.tick() => {
+ if turn_active {
+ log::trace!(
+ "[web_channel][bridge] inference_heartbeat thread_id={} request_id={}",
+ thread_id,
+ request_id,
+ );
+ publish_seq_stamped(&dispatch_ctx, &mut emit_seq, WebChannelEvent {
+ event: "inference_heartbeat".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ ..Default::default()
+ });
+ }
+ continue;
+ }
+ };
+ events_seen += 1;
+ turn_state.observe(&event);
+ match &event {
+ AgentProgress::TextDelta { delta, iteration } => {
+ log::trace!(
+ "[web_channel][bridge] text_delta round={} chars={} request_id={}",
+ iteration,
+ delta.len(),
+ request_id,
+ );
+ }
+ AgentProgress::ThinkingDelta { delta, iteration } => {
+ log::trace!(
+ "[web_channel][bridge] thinking_delta round={} chars={} request_id={}",
+ iteration,
+ delta.len(),
+ request_id,
+ );
+ }
+ AgentProgress::ToolCallArgsDelta {
+ call_id,
+ tool_name,
+ delta,
+ iteration,
+ } => {
+ log::trace!(
+ "[web_channel][bridge] tool_args_delta round={} tool={} call_id={} chars={} request_id={}",
+ iteration,
+ tool_name,
+ call_id,
+ delta.len(),
+ request_id,
+ );
+ }
+ AgentProgress::ToolCallStarted {
+ call_id,
+ tool_name,
+ iteration,
+ ..
+ } => {
+ log::debug!(
+ "[web_channel][bridge] tool_call round={} tool={} call_id={} request_id={}",
+ iteration,
+ tool_name,
+ call_id,
+ request_id,
+ );
+ }
+ AgentProgress::ToolCallCompleted {
+ call_id,
+ tool_name,
+ success,
+ iteration,
+ ..
+ } => {
+ log::debug!(
+ "[web_channel][bridge] tool_result round={} tool={} call_id={} success={} request_id={}",
+ iteration,
+ tool_name,
+ call_id,
+ success,
+ request_id,
+ );
+ }
+ AgentProgress::SubagentFailed {
+ agent_id, error, ..
+ } => {
+ log::warn!(
+ "[web_channel][bridge] subagent_failed agent_id={} err={} client_id={} thread_id={} request_id={}",
+ agent_id,
+ error,
+ client_id,
+ thread_id,
+ request_id,
+ );
+ }
+ other => {
+ log::debug!(
+ "[web_channel][bridge] lifecycle event={:?} request_id={}",
+ std::mem::discriminant(other),
+ request_id,
+ );
+ }
+ }
+ match event {
+ AgentProgress::TurnStarted => {
+ // Turn is live — start emitting liveness beats (issue #4270).
+ turn_active = true;
+ ledger_upsert_agent_run(
+ &config,
+ AgentRunUpsert {
+ id: request_id.clone(),
+ kind: AgentRunKind::BackgroundAgent,
+ parent_run_id: None,
+ parent_thread_id: Some(thread_id.clone()),
+ agent_id: Some("orchestrator".to_string()),
+ status: AgentRunStatus::Running,
+ prompt_ref: Some(format!("thread:{thread_id}:request:{request_id}")),
+ worker_thread_id: None,
+ checkpoint_path: None,
+ checkpoint: None,
+ summary: None,
+ error: None,
+ metadata: json!({
+ "clientId": client_id,
+ "source": "web_channel",
+ "schemaVersion": 1
+ }),
+ started_at: None,
+ completed_at: None,
+ },
+ );
+ ledger_append_event(
+ &config,
+ RunEventAppend {
+ run_id: request_id.clone(),
+ event_type: "turn_started".to_string(),
+ payload: json!({ "threadId": thread_id, "clientId": client_id }),
+ },
+ );
+ publish_seq_stamped(
+ &dispatch_ctx,
+ &mut emit_seq,
+ WebChannelEvent {
+ event: "inference_start".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ ..Default::default()
+ },
+ );
+ }
+ AgentProgress::IterationStarted {
+ iteration,
+ max_iterations,
+ } => {
+ round = iteration;
+ publish_seq_stamped(
+ &dispatch_ctx,
+ &mut emit_seq,
+ WebChannelEvent {
+ event: "iteration_start".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ message: Some(format!("Iteration {iteration}/{max_iterations}")),
+ round: Some(iteration),
+ ..Default::default()
+ },
+ );
+ }
+ AgentProgress::ContextCompacting => {
+ publish_seq_stamped(
+ &dispatch_ctx,
+ &mut emit_seq,
+ WebChannelEvent {
+ event: "context_compacting".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ round: Some(round),
+ ..Default::default()
+ },
+ );
+ }
+ AgentProgress::ToolCallStarted {
+ call_id,
+ tool_name,
+ arguments,
+ iteration,
+ display_label,
+ display_detail,
+ } => {
+ timing.tool_call(&tool_name, iteration, &request_id);
+ // The parent's leading narration for this round is complete
+ // once it calls a tool — flush it as an interim bubble so it
+ // persists interleaved with the tool activity.
+ flush_interim_narration(
+ &dispatch_ctx,
+ &mut pending_narration,
+ iteration,
+ &client_id,
+ &thread_id,
+ &request_id,
+ &mut emit_seq,
+ );
+ parent_tool_count += 1;
+ ledger_append_event(
+ &config,
+ RunEventAppend {
+ run_id: request_id.clone(),
+ event_type: "tool_call_started".to_string(),
+ payload: json!({
+ "callId": call_id,
+ "toolName": tool_name,
+ "iteration": iteration
+ }),
+ },
+ );
+ ledger_upsert_telemetry(
+ &config,
+ RunTelemetryUpsert {
+ run_id: request_id.clone(),
+ tool_count: Some(parent_tool_count),
+ ..Default::default()
+ },
+ );
+ publish_seq_stamped(
+ &dispatch_ctx,
+ &mut emit_seq,
+ WebChannelEvent {
+ event: "tool_call".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ tool_name: Some(tool_name),
+ skill_id: Some("web_channel".to_string()),
+ args: cap_wire_args(Some(arguments)),
+ round: Some(iteration),
+ tool_call_id: Some(call_id),
+ tool_display_label: display_label,
+ tool_display_detail: display_detail,
+ ..Default::default()
+ },
+ );
+ }
+ AgentProgress::ToolCallCompleted {
+ call_id,
+ tool_name,
+ success,
+ output_chars,
+ output,
+ arguments,
+ elapsed_ms,
+ iteration,
+ failure,
+ display_label,
+ display_detail,
+ structured,
+ } => {
+ // Serialize the classified failure (if any) for the UI + ledger.
+ let failure_json = failure.as_ref().and_then(|f| serde_json::to_value(f).ok());
+ ledger_append_event(
+ &config,
+ RunEventAppend {
+ run_id: request_id.clone(),
+ event_type: "tool_call_completed".to_string(),
+ payload: json!({
+ "callId": call_id,
+ "toolName": tool_name,
+ "success": success,
+ "outputChars": output_chars,
+ "elapsedMs": elapsed_ms,
+ "iteration": iteration,
+ "failure": failure_json,
+ }),
+ },
+ );
+ log::debug!(
+ "[web_channel][bridge] tool_result round={} tool={} call_id={} \
+ success={} elapsed_ms={} has_structured={} request_id={}",
+ iteration,
+ tool_name,
+ call_id,
+ success,
+ elapsed_ms,
+ structured.is_some(),
+ request_id
+ );
+ publish_seq_stamped(
+ &dispatch_ctx,
+ &mut emit_seq,
+ WebChannelEvent {
+ event: "tool_result".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ tool_name: Some(tool_name),
+ skill_id: Some("web_channel".to_string()),
+ // Forward the real tool result (size-capped) so the UI
+ // can render tool output — mirrors the subagent
+ // `subagent_tool_result` path. Frontends that only
+ // need size/timing read the ledger telemetry instead.
+ output: Some(cap_wire_output(output)),
+ // The call arguments the harness captured at
+ // completion (`ToolCallStarted.arguments` is
+ // always `Null` on this path). Omitted when the
+ // harness ran with payload capture off.
+ args: cap_wire_args(arguments),
+ success: Some(success),
+ round: Some(iteration),
+ tool_call_id: Some(call_id),
+ failure: failure_json,
+ elapsed_ms: Some(elapsed_ms),
+ structured,
+ // Recomputed from the real arguments (unlike the
+ // started event's args-free computation), so a
+ // completed row can pick up a detail that only
+ // became knowable once the arguments existed.
+ tool_display_label: display_label,
+ tool_display_detail: display_detail,
+ ..Default::default()
+ },
+ );
+ }
+ AgentProgress::SubagentSpawned {
+ agent_id,
+ task_id,
+ mode,
+ dedicated_thread,
+ prompt_chars,
+ worker_thread_id,
+ display_name,
+ parent_call_id,
+ ..
+ } => {
+ subagent_events::on_subagent_spawned(
+ &bridge_ctx,
+ &mut emit_seq,
+ &mut subagent_parent_call_ids,
+ round,
+ agent_id,
+ task_id,
+ mode,
+ dedicated_thread,
+ prompt_chars,
+ worker_thread_id,
+ display_name,
+ parent_call_id,
+ );
+ }
+ AgentProgress::SubagentCompleted {
+ agent_id,
+ task_id,
+ elapsed_ms,
+ iterations,
+ output_chars,
+ output,
+ usage,
+ worktree_path,
+ changed_files,
+ dirty_status,
+ ..
+ } => {
+ subagent_events::on_subagent_completed(
+ &bridge_ctx,
+ &mut emit_seq,
+ &mut subagent_parent_call_ids,
+ &child_tool_counts,
+ round,
+ agent_id,
+ task_id,
+ elapsed_ms,
+ iterations,
+ output_chars,
+ output,
+ usage,
+ worktree_path,
+ changed_files,
+ dirty_status,
+ );
+ }
+ AgentProgress::SubagentFailed {
+ agent_id,
+ task_id,
+ error,
+ } => {
+ subagent_events::on_subagent_failed(
+ &bridge_ctx,
+ &mut emit_seq,
+ &mut subagent_parent_call_ids,
+ &child_tool_counts,
+ round,
+ agent_id,
+ task_id,
+ error,
+ );
+ }
+ AgentProgress::SubagentAwaitingUser {
+ agent_id,
+ task_id,
+ question,
+ worker_thread_id,
+ checkpoint_path,
+ } => {
+ subagent_events::on_subagent_awaiting_user(
+ &bridge_ctx,
+ &mut emit_seq,
+ &subagent_parent_call_ids,
+ round,
+ agent_id,
+ task_id,
+ question,
+ worker_thread_id,
+ checkpoint_path,
+ );
+ }
+ AgentProgress::SubagentIterationStarted {
+ agent_id,
+ task_id,
+ iteration,
+ max_iterations,
+ extended_policy,
+ } => {
+ subagent_events::on_subagent_iteration_started(
+ &bridge_ctx,
+ &mut emit_seq,
+ round,
+ agent_id,
+ task_id,
+ iteration,
+ max_iterations,
+ extended_policy,
+ );
+ }
+ AgentProgress::SubagentToolCallStarted {
+ agent_id,
+ task_id,
+ call_id,
+ tool_name,
+ arguments,
+ iteration,
+ display_label,
+ display_detail,
+ } => {
+ subagent_events::on_subagent_tool_call_started(
+ &bridge_ctx,
+ &mut emit_seq,
+ &mut child_tool_counts,
+ round,
+ agent_id,
+ task_id,
+ call_id,
+ tool_name,
+ arguments,
+ iteration,
+ display_label,
+ display_detail,
+ );
+ }
+ AgentProgress::SubagentToolCallCompleted {
+ agent_id,
+ task_id,
+ call_id,
+ tool_name,
+ success,
+ output_chars,
+ output,
+ arguments,
+ elapsed_ms,
+ iteration,
+ failure,
+ display_label,
+ display_detail,
+ structured,
+ } => {
+ subagent_events::on_subagent_tool_call_completed(
+ &bridge_ctx,
+ &mut emit_seq,
+ round,
+ agent_id,
+ task_id,
+ call_id,
+ tool_name,
+ success,
+ output_chars,
+ output,
+ arguments,
+ elapsed_ms,
+ iteration,
+ failure,
+ display_label,
+ display_detail,
+ structured,
+ );
+ }
+ AgentProgress::SubagentTextDelta {
+ agent_id,
+ task_id,
+ delta,
+ iteration,
+ } => {
+ subagent_events::on_subagent_text_delta(
+ &bridge_ctx,
+ &mut emit_seq,
+ round,
+ agent_id,
+ task_id,
+ delta,
+ iteration,
+ );
+ }
+ AgentProgress::SubagentThinkingDelta {
+ agent_id,
+ task_id,
+ delta,
+ iteration,
+ } => {
+ subagent_events::on_subagent_thinking_delta(
+ &bridge_ctx,
+ &mut emit_seq,
+ round,
+ agent_id,
+ task_id,
+ delta,
+ iteration,
+ );
+ }
+ AgentProgress::TextDelta { delta, iteration } => {
+ timing.text_delta(&delta, iteration, &request_id);
+ // Buffer the round's narration so it can be flushed as an
+ // interim bubble if a tool call closes this round.
+ pending_narration.push_str(&delta);
+ if !metadata.hold_text_stream {
+ text_events::publish_text_delta(
+ &dispatch_ctx,
+ &mut emit_seq,
+ &client_id,
+ &thread_id,
+ &request_id,
+ iteration,
+ delta,
+ );
+ }
+ }
+ AgentProgress::ThinkingDelta { delta, iteration } => {
+ publish_seq_stamped(
+ &dispatch_ctx,
+ &mut emit_seq,
+ WebChannelEvent {
+ event: "thinking_delta".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ round: Some(iteration),
+ delta: Some(delta),
+ delta_kind: Some("thinking".to_string()),
+ ..Default::default()
+ },
+ );
+ }
+ AgentProgress::ToolCallArgsDelta {
+ call_id,
+ tool_name,
+ delta,
+ iteration,
+ } => {
+ publish_seq_stamped(
+ &dispatch_ctx,
+ &mut emit_seq,
+ WebChannelEvent {
+ event: "tool_args_delta".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ tool_name: if tool_name.is_empty() {
+ None
+ } else {
+ Some(tool_name)
+ },
+ skill_id: Some("web_channel".to_string()),
+ round: Some(iteration),
+ delta: Some(delta),
+ delta_kind: Some("tool_args".to_string()),
+ tool_call_id: Some(call_id),
+ ..Default::default()
+ },
+ );
+ }
+ AgentProgress::TurnCompleted { iterations, .. } => {
+ parent_completed = true;
+ timing.done(iterations, MIN_INTERIM_NARRATION_CHARS, &request_id);
+ if let Ok(mut guard) = timing_snapshot_for_task.lock() {
+ *guard = Some(timing.snapshot());
+ }
+ // Turn is done — stop liveness beats (issue #4270). The FE
+ // clears its silence timer on `chat_done`/`chat_error`; this
+ // also prevents a stray beat racing the channel close.
+ turn_active = false;
+ let completed_at = chrono::Utc::now();
+ ledger_upsert_agent_run(
+ &config,
+ AgentRunUpsert {
+ id: request_id.clone(),
+ kind: AgentRunKind::BackgroundAgent,
+ parent_run_id: None,
+ parent_thread_id: Some(thread_id.clone()),
+ agent_id: Some("orchestrator".to_string()),
+ status: AgentRunStatus::Completed,
+ prompt_ref: Some(format!("thread:{thread_id}:request:{request_id}")),
+ worker_thread_id: None,
+ checkpoint_path: None,
+ checkpoint: None,
+ summary: Some(format!("Completed in {iterations} iteration(s)")),
+ error: None,
+ metadata: json!({}),
+ started_at: None,
+ completed_at: Some(completed_at),
+ },
+ );
+ ledger_append_event(
+ &config,
+ RunEventAppend {
+ run_id: request_id.clone(),
+ event_type: "turn_completed".to_string(),
+ payload: json!({ "iterations": iterations }),
+ },
+ );
+ log::debug!(
+ "[web_channel] turn completed after {iterations} iteration(s) \
+ client_id={client_id} thread_id={thread_id} request_id={request_id} \
+ speak_reply={:?} source={:?} session_id={:?}",
+ metadata.speak_reply,
+ metadata.source,
+ metadata.session_id,
+ );
+ // Every event the parent queued before `TurnCompleted` has
+ // now been forwarded, in order: release a caller waiting
+ // to publish the terminal `chat_done`.
+ let _ = drained_tx.send(true);
+ log::debug!(
+ "[web_channel][bridge] drained parent events_seen={events_seen} request_id={request_id}"
+ );
+ }
+ AgentProgress::TurnCostUpdated {
+ model,
+ iteration,
+ input_tokens,
+ output_tokens,
+ cached_input_tokens,
+ total_usd,
+ } => {
+ ledger_upsert_telemetry(
+ &config,
+ RunTelemetryUpsert {
+ run_id: request_id.clone(),
+ input_tokens: Some(input_tokens),
+ output_tokens: Some(output_tokens),
+ cached_input_tokens: Some(cached_input_tokens),
+ cost_usd: Some(total_usd),
+ model: Some(model.clone()),
+ ..Default::default()
+ },
+ );
+ log::debug!(
+ "[web_channel] turn cost update model={model} iter={iteration} \
+ in={input_tokens} out={output_tokens} cached_in={cached_input_tokens} \
+ total_usd={total_usd:.4} client_id={client_id} thread_id={thread_id}"
+ );
+
+ // Live cost readout: throttled so a fast multi-round turn
+ // doesn't flood the socket with one `turn_cost` per model
+ // call. `TurnCostUpdated` is the parent's cumulative
+ // rollup only — it carries no per-sub-agent breakdown, so
+ // `subagents` stays empty here; the final `chat_done.usage`
+ // (built from `LastTurnUsage` at delivery) is still where
+ // sub-agent attribution shows up.
+ if turn_cost_throttle.should_emit() {
+ publish_seq_stamped(
+ &dispatch_ctx,
+ &mut emit_seq,
+ WebChannelEvent {
+ event: "turn_cost".to_string(),
+ client_id: client_id.clone(),
+ thread_id: thread_id.clone(),
+ request_id: request_id.clone(),
+ round: Some(iteration),
+ usage: Some(crate::web_chat::TurnUsagePayload {
+ input_tokens,
+ output_tokens,
+ cached_input_tokens,
+ // A live rollup of the known spend so far;
+ // the turn's final figure (or `null`)
+ // arrives on `chat_done`.
+ cost_usd: Some(total_usd),
+ cost_source: tinyagents_harness::cost::CostSource::Charged,
+ context_window: 0,
+ context_tokens: 0,
+ subagents: Vec::new(),
+ }),
+ ..Default::default()
+ },
+ );
+ }
+ }
+ AgentProgress::ToolCallProgress { call_id, message } => {
+ let ids = (client_id.as_str(), thread_id.as_str(), request_id.as_str());
+ tool_progress::publish(&dispatch_ctx, &mut emit_seq, ids, call_id, message);
+ }
+ AgentProgress::TurnContent { .. } => {
+ // Prompt/reply content is attached to the trace span by the
+ // span collector above; the ledger/telemetry bridge ignores it.
+ }
+ AgentProgress::ModelCallCompleted {
+ model,
+ iteration,
+ input_tokens,
+ output_tokens,
+ cost_usd,
+ ..
+ } => {
+ // Per-call usage is consumed by the span collector above
+ // (per-call Langfuse generation); the socket/ledger surfaces
+ // stay on the cumulative TurnCostUpdated rollup.
+ log::debug!(
+ "[web_channel][bridge] model_call_completed model={model} iter={iteration} \
+ in={input_tokens} out={output_tokens} cost_usd={cost_usd:?} request_id={request_id}"
+ );
+ }
+ }
+ }
+ turn_state.finish();
+ if !parent_completed {
+ ledger_upsert_agent_run(
+ &config,
+ AgentRunUpsert {
+ id: request_id.clone(),
+ kind: AgentRunKind::BackgroundAgent,
+ parent_run_id: None,
+ parent_thread_id: Some(thread_id.clone()),
+ agent_id: Some("orchestrator".to_string()),
+ status: AgentRunStatus::Interrupted,
+ prompt_ref: Some(format!("thread:{thread_id}:request:{request_id}")),
+ worker_thread_id: None,
+ checkpoint_path: None,
+ checkpoint: None,
+ summary: None,
+ error: Some("progress bridge exited before turn completion".to_string()),
+ metadata: json!({}),
+ started_at: None,
+ completed_at: Some(chrono::Utc::now()),
+ },
+ );
+ ledger_append_event(
+ &config,
+ RunEventAppend {
+ run_id: request_id.clone(),
+ event_type: "turn_interrupted".to_string(),
+ payload: json!({ "eventsSeen": events_seen }),
+ },
+ );
+ }
+ // #3886: seal any spans still open after the stream closed and hand the
+ // run's trace to the configured tracing sink. Best-effort and gated;
+ // never affects the turn outcome.
+ // The response presenter waits only briefly for this signal before
+ // publishing an error. Trace export is best-effort I/O and must not
+ // hold up terminal delivery after the progress stream closed.
+ let _ = drained_tx.send(true);
+ if let Some(trace_ctx) = journal_trace_ctx.take() {
+ crate::core::observability::progress_tracing::export_run_trace_from_journal(
+ &dispatch_ctx,
+ &config,
+ &request_id,
+ trace_ctx,
+ )
+ .await;
+ }
+
+ log::debug!(
+ "[web_channel][bridge] exit client_id={} thread_id={} request_id={} round={} events_seen={}",
+ client_id,
+ thread_id,
+ request_id,
+ round,
+ events_seen,
+ );
+ });
+ ProgressBridgeHandle {
+ drained: drained_rx,
+ timing: timing_snapshot,
+ }
+}
+