Skip to content

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

fix(web_chat): await progress bridge before emitting chat_done

Steven Enamakel6b8c83613b2a
diff --git a/crates/openhuman-core/src/flows/ops/streaming.rs b/crates/openhuman-core/src/flows/ops/streaming.rs
index a99fe6d407..b3325dde8e 100644
--- a/crates/openhuman-core/src/flows/ops/streaming.rs
+++ b/crates/openhuman-core/src/flows/ops/streaming.rs
@@ -69,7 +69,7 @@ pub(super) fn attach_flow_progress_bridge(
source = %source,
"[flows] progress bridge: attaching (streaming copilot/scout turn)"
);
- crate::web_chat::spawn_progress_bridge(
+ let _progress_bridge = crate::web_chat::spawn_progress_bridge(
progress_rx,
"system".to_string(),
target.thread_id.clone(),
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index 299e72a129..88168a851e 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -246,7 +246,7 @@ pub(crate) fn spawn_progress_bridge(
turn_state_store: TurnStateStore,
metadata: ChatRequestMetadata,
config: crate::config::Config,
-) {
+) -> tokio::task::JoinHandle<()> {
use crate::agent::progress::AgentProgress;
use std::collections::HashMap;
use tinyagents_session::run_ledger::{
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs b/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
index d41e5447d3..00b4f3729f 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
@@ -101,7 +101,7 @@ async fn tool_call_completed_forwards_real_output_on_tool_result() {
let store = TurnStateStore::new(tmp.path().join("turn_states"));
let (tx, rx) = tokio::sync::mpsc::channel(16);
let mut bus = super::super::event_bus::subscribe_web_channel_events();
- spawn_progress_bridge(
+ let _progress_bridge = spawn_progress_bridge(
rx,
"client-out".into(),
"thread-out".into(),
@@ -209,7 +209,7 @@ fn spawn_test_bridge(
// Keep the tempdir alive for the bridge task's lifetime by leaking it —
// a test-only allocation; the OS reclaims it on process exit.
std::mem::forget(dir);
- spawn_progress_bridge(
+ let _progress_bridge = spawn_progress_bridge(
rx,
"client-hb-4270".to_string(),
thread_id.to_string(),
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index a8ce3507c9..c0729531cd 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -133,7 +133,7 @@ pub(crate) async fn run_chat_task(
// can attribute the run (`agent.id` attr / `agent.turn:<id>` trace name).
let mut bridge_metadata = metadata.clone();
bridge_metadata.agent_id = Some(current_fp.target_agent_id.clone());
- spawn_progress_bridge(
+ let progress_bridge = spawn_progress_bridge(
progress_rx,
client_id.to_string(),
thread_id.to_string(),
@@ -258,7 +258,20 @@ pub(crate) async fn run_chat_task(
}
}
+ // `run_single` can finish while the bridge still has its final
+ // ToolCallCompleted event buffered. Close the sender and drain the bridge
+ // before the caller emits `chat_done`; otherwise an SSE client can observe
+ // the terminal event first and miss the tool result it is meant to settle.
agent.set_on_progress(None);
+ if let Err(error) = progress_bridge.await {
+ log::warn!(
+ "[web-channel] progress bridge ended unexpectedly client={} thread={} request_id={} error={}",
+ client_id,
+ thread_id,
+ request_id,
+ error
+ );
+ }
// Only the primary (non-fork) turn writes its agent back to the shared
// cache; a fork is fully isolated and lets its agent drop here.