Skip to content

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

fix(web_chat): handle missing progress bridge in task runner

Steven Enamakelfe9619abb675
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index c3a4c22a84..d32d9f8442 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -1303,10 +1303,6 @@ pub(crate) fn spawn_progress_bridge(
}
AgentProgress::TurnCompleted { iterations } => {
parent_completed = true;
- // Everything the parent queued before this event has been
- // forwarded; release a caller waiting to publish the
- // terminal event (after this arm's own events below).
- let release_drain = scopeguard_release(&drained_tx);
timing.done(iterations, MIN_INTERIM_NARRATION_CHARS, &request_id);
// Turn is done — stop liveness beats (issue #4270). The FE
// clears its silence timer on `chat_done`/`chat_error`; this
@@ -1349,6 +1345,13 @@ pub(crate) fn spawn_progress_bridge(
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,
@@ -1470,7 +1473,11 @@ pub(crate) fn spawn_progress_bridge(
round,
events_seen,
);
+ let _ = drained_tx.send(true);
});
+ ProgressBridgeHandle {
+ drained: drained_rx,
+ }
}
#[cfg(test)]
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index a8ce3507c9..e7bcd1e6b6 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 bridge = spawn_progress_bridge(
progress_rx,
client_id.to_string(),
thread_id.to_string(),
@@ -260,6 +260,24 @@ pub(crate) async fn run_chat_task(
agent.set_on_progress(None);
+ // The caller publishes the terminal `chat_done`/`chat_error` as soon as
+ // this returns. Let the bridge forward everything the turn queued first,
+ // so the terminal event cannot overtake the turn's own last tool results
+ // and narration on the socket. Bounded (see `BRIDGE_DRAIN_TIMEOUT`).
+ if !bridge
+ .wait_drained(super::progress_bridge::BRIDGE_DRAIN_TIMEOUT)
+ .await
+ {
+ log::warn!(
+ "[web-channel] progress bridge did not drain within {:?}; delivering anyway \
+ client={} thread={} request_id={}",
+ super::progress_bridge::BRIDGE_DRAIN_TIMEOUT,
+ client_id,
+ thread_id,
+ request_id
+ );
+ }
+
// 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.
if !fork {