Skip to content

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

fix(progress_bridge): handle missing progress bar element gracefully

Steven Enamakela004b8a00729
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index 299e72a129..c3a4c22a84 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -24,9 +24,47 @@ use super::types::ChatRequestMetadata;
const INFERENCE_HEARTBEAT_SECS: u64 = 20;
/// Minimum trimmed length for the parent agent's leading narration to be
-/// surfaced as its own interim chat bubble. Below this a stray "Ok." / "Sure."
-/// is left as transient streaming text rather than persisted as a message.
-const MIN_INTERIM_NARRATION_CHARS: usize = 24;
+/// flushed as a `chat_interim` event when the round's first tool call starts.
+///
+/// Any non-empty narration flushes. This used to be 24 characters, meant to
+/// keep a stray "Ok." from persisting as a bubble — but the frontend no longer
+/// promotes narration to a message, and `chat_interim` is also the signal that
+/// resets its live preview for the next round. A short "Let me check." that
+/// was never flushed stayed in the preview and the next round's text was
+/// appended straight onto it ("Let me check.Here's the answer").
+const MIN_INTERIM_NARRATION_CHARS: usize = 1;
+
+/// How long a finished turn waits for its progress bridge to forward every
+/// event the turn queued before the terminal `chat_done`/`chat_error` is
+/// published. Bounded: a detached sub-agent can hold a sender clone and keep
+/// the channel open past the turn, and a missing `TurnCompleted` (failed
+/// turn) must not stall delivery.
+pub(crate) const BRIDGE_DRAIN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(3);
+
+/// Handle to a spawned progress bridge, used to wait until it has forwarded
+/// the parent turn's events.
+///
+/// The bridge runs on its own task, fed by a bounded channel. The turn's
+/// caller used to publish `chat_done` as soon as the turn returned, while the
+/// bridge could still be holding queued `tool_result`/`chat_interim` events —
+/// so the terminal event overtook them and the UI settled rows that were
+/// about to be settled correctly (or never saw their results at all).
+#[derive(Clone)]
+pub(crate) struct ProgressBridgeHandle {
+ drained: tokio::sync::watch::Receiver<bool>,
+}
+
+impl ProgressBridgeHandle {
+ /// Wait until the bridge has handled the parent's `TurnCompleted` (every
+ /// event queued before it has been forwarded, in order) or its channel
+ /// closed — at most `timeout`. Returns whether it drained in time.
+ pub(crate) async fn wait_drained(&self, timeout: std::time::Duration) -> bool {
+ let mut drained = self.drained.clone();
+ let result = tokio::time::timeout(timeout, drained.wait_for(|done| *done)).await;
+ // A dropped sender means the bridge task ended, which is drained too.
+ matches!(result, Ok(Ok(_)) | Ok(Err(_)))
+ }
+}
/// Flush the parent agent's accumulated leading narration (streamed before a
/// tool call in the current round) as an interim `chat_interim` event, so it
@@ -246,13 +284,14 @@ pub(crate) fn spawn_progress_bridge(
turn_state_store: TurnStateStore,
metadata: ChatRequestMetadata,
config: crate::config::Config,
-) {
+) -> ProgressBridgeHandle {
use crate::agent::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);
tokio::spawn(async move {
log::debug!(
"[web_channel][bridge] spawned client_id={} thread_id={} request_id={} speak_reply={:?} source={:?} session_id={:?}",
@@ -1264,6 +1303,10 @@ 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