Skip to content

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

feat(web_chat): signal parent turn completion via oneshot channel

Steven Enamakelbd17c47adffa
diff --git a/crates/openhuman-core/src/flows/ops/streaming.rs b/crates/openhuman-core/src/flows/ops/streaming.rs
index b3325dde8e..aab38a4145 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)"
);
- let _progress_bridge = crate::web_chat::spawn_progress_bridge(
+ crate::web_chat::spawn_progress_bridge(
progress_rx,
"system".to_string(),
target.thread_id.clone(),
@@ -80,6 +80,7 @@ pub(super) fn attach_flow_progress_bridge(
..Default::default()
},
config.clone(),
+ None,
);
}
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index 21c5f19321..678bebb488 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -246,7 +246,8 @@ pub(crate) fn spawn_progress_bridge(
turn_state_store: TurnStateStore,
metadata: ChatRequestMetadata,
config: crate::config::Config,
-) -> tokio::task::JoinHandle<()> {
+ mut parent_turn_processed: Option<tokio::sync::oneshot::Sender<()>>,
+) {
use crate::agent::progress::AgentProgress;
use std::collections::HashMap;
use tinyagents_session::run_ledger::{
@@ -1306,6 +1307,9 @@ pub(crate) fn spawn_progress_bridge(
metadata.source,
metadata.session_id,
);
+ if let Some(sender) = parent_turn_processed.take() {
+ let _ = sender.send(());
+ }
}
AgentProgress::TurnCostUpdated {
model,
@@ -1427,7 +1431,7 @@ pub(crate) fn spawn_progress_bridge(
round,
events_seen,
);
- })
+ });
}
#[cfg(test)]
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 00b4f3729f..ceaf602ebb 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();
- let _progress_bridge = spawn_progress_bridge(
+ spawn_progress_bridge(
rx,
"client-out".into(),
"thread-out".into(),
@@ -109,6 +109,7 @@ async fn tool_call_completed_forwards_real_output_on_tool_result() {
store,
ChatRequestMetadata::default(),
config,
+ None,
);
tx.send(crate::agent::progress::AgentProgress::ToolCallCompleted {
@@ -209,7 +210,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);
- let _progress_bridge = spawn_progress_bridge(
+ spawn_progress_bridge(
rx,
"client-hb-4270".to_string(),
thread_id.to_string(),
@@ -217,6 +218,7 @@ fn spawn_test_bridge(
store,
ChatRequestMetadata::default(),
Config::default(),
+ None,
);
tx
}
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index c0729531cd..e519a05ca8 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -133,7 +133,8 @@ 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());
- let progress_bridge = spawn_progress_bridge(
+ let (parent_turn_processed_tx, parent_turn_processed_rx) = tokio::sync::oneshot::channel();
+ spawn_progress_bridge(
progress_rx,
client_id.to_string(),
thread_id.to_string(),
@@ -141,6 +142,7 @@ pub(crate) async fn run_chat_task(
turn_state_store,
bridge_metadata,
config.clone(),
+ Some(parent_turn_processed_tx),
);
// `run_single`'s future is very large; box it so the two ambient-scope
@@ -259,18 +261,29 @@ 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.
+ // ToolCallCompleted event buffered. Wait until it has processed the
+ // parent's TurnCompleted marker before the caller emits `chat_done`;
+ // detached subagents retain their own progress senders, so waiting for the
+ // entire bridge to close would incorrectly hold the parent chat open.
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
- );
+ if result.is_ok() {
+ match tokio::time::timeout(std::time::Duration::from_secs(5), parent_turn_processed_rx)
+ .await
+ {
+ Ok(Ok(())) => {}
+ Ok(Err(_)) => log::warn!(
+ "[web-channel] progress bridge closed before parent completion client={} thread={} request_id={}",
+ client_id,
+ thread_id,
+ request_id
+ ),
+ Err(_) => log::warn!(
+ "[web-channel] timed out waiting for parent progress client={} thread={} request_id={}",
+ client_id,
+ thread_id,
+ request_id
+ ),
+ }
}
// Only the primary (non-fork) turn writes its agent back to the shared