Skip to content

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

feat(web_chat): forward late sub-agent events after turn completion

Steven Enamakelf074ccb2ec3d
diff --git a/crates/openhuman-core/src/web_chat/turn_host.rs b/crates/openhuman-core/src/web_chat/turn_host.rs
index f69037f73d..0ff2498f16 100644
--- a/crates/openhuman-core/src/web_chat/turn_host.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host.rs
@@ -252,8 +252,21 @@ impl WebHost {
output: None,
});
let (drained_tx, mut drained_rx) = watch::channel(false);
- let tap = tap_progress(&ctx, progress_rx, progress, activity, drained_tx);
- let _tap = AbortOnDrop(tap);
+ // The tap outlives the turn on purpose: a detached sub-agent may still
+ // report through its sender clone after the parent finished, and the
+ // tap ends when the last sender drops.
+ let _tap = tap_progress(
+ &ctx,
+ progress_rx,
+ progress,
+ activity,
+ drained_tx,
+ Some(LateSubagentSink {
+ client_id: payload.client_id.clone(),
+ thread_id: thread_id.to_owned(),
+ request_id: id.to_owned(),
+ }),
+ );
let _beat = AbortOnDrop(spawn_heartbeat(&ctx, projection.clone()));
let origin = AgentTurnOrigin::WebChat {
diff --git a/crates/openhuman-core/src/web_chat/turn_host_progress.rs b/crates/openhuman-core/src/web_chat/turn_host_progress.rs
index 1c2abe10e2..bdbed297e1 100644
--- a/crates/openhuman-core/src/web_chat/turn_host_progress.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host_progress.rs
@@ -57,29 +57,56 @@ pub(super) fn settle_turn_snapshot(
/// Forward the agent's progress to the runner, folding each event into the
/// idle record and the work tracker on the way. Signals `drained` once the
/// parent's `TurnCompleted` was forwarded or the stream ended.
+///
+/// A detached sub-agent can outlive its parent turn and still report (a
+/// cancelled child settles its card with `subagent_failed`). Once the runner has
+/// finished with the turn it no longer reads the stream, so with a `late` sink
+/// the tap keeps running and publishes only the sub-agent events straight to
+/// the web event stream, under the turn's ids; without one it stops, as before.
pub(super) fn tap_progress(
ctx: &DispatchCtx,
mut rx: mpsc::Receiver<AgentProgress>,
progress: mpsc::Sender<TurnSignal>,
activity: TurnActivity,
drained: watch::Sender<bool>,
+ late: Option<LateSubagentSink>,
) -> tokio::task::JoinHandle<()> {
+ let publish_ctx = ctx.clone();
ctx.spawn(async move {
let mut work = WorkTracker::default();
+ let mut runner_gone = false;
while let Some(event) = rx.recv().await {
+ if runner_gone {
+ if let Some(late) = late.as_ref() {
+ late.publish(&publish_ctx, event);
+ }
+ continue;
+ }
activity.observe(&event);
let completed = matches!(event, AgentProgress::TurnCompleted { .. });
if let Some(signal) = work.observe(&event) {
if progress.send(signal).await.is_err() {
- break;
+ runner_gone = true;
+ if late.is_none() {
+ break;
+ }
}
}
- if progress
- .send(TurnSignal::Progress(Box::new(event)))
- .await
- .is_err()
+ if !runner_gone
+ && progress
+ .send(TurnSignal::Progress(Box::new(event.clone())))
+ .await
+ .is_err()
{
- break;
+ runner_gone = true;
+ if late.is_none() {
+ break;
+ }
+ }
+ if runner_gone {
+ if let Some(late) = late.as_ref() {
+ late.publish(&publish_ctx, event);
+ }
}
if completed {
let _ = drained.send(true);
@@ -89,6 +116,52 @@ pub(super) fn tap_progress(
})
}
+/// Where a finished turn's late sub-agent events go: the web event stream,
+/// addressed like the turn.
+#[derive(Clone)]
+pub(super) struct LateSubagentSink {
+ pub(super) client_id: String,
+ pub(super) thread_id: String,
+ pub(super) request_id: String,
+}
+
+impl LateSubagentSink {
+ fn publish(&self, ctx: &DispatchCtx, event: AgentProgress) {
+ let is_subagent = matches!(
+ event,
+ AgentProgress::SubagentSpawned { .. }
+ | AgentProgress::SubagentCompleted { .. }
+ | AgentProgress::SubagentFailed { .. }
+ | AgentProgress::SubagentAwaitingUser { .. }
+ | AgentProgress::SubagentIterationStarted { .. }
+ | AgentProgress::SubagentToolCallStarted { .. }
+ | AgentProgress::SubagentToolCallCompleted { .. }
+ );
+ if !is_subagent {
+ return;
+ }
+ let Some(output) = super::super::progress_map::subagent_event(event, 0, None) else {
+ return;
+ };
+ let mut projector = tinychannels_runtime::adapters::web::WebTurnProjector::new(
+ &self.client_id,
+ &self.thread_id,
+ &self.request_id,
+ );
+ for mut projected in projector.project(output, super::super::progress::unix_epoch_ms()) {
+ // A late event follows the turn's own sequence, which this
+ // projector does not know; leave it unstamped.
+ tinychannels_runtime::adapters::web::context_mut(&mut projected).seq = None;
+ log::debug!(
+ "[web-channel] late sub-agent event event={} request_id={}",
+ projected.event_name(),
+ self.request_id
+ );
+ super::super::event_bus_channel::publish_web_channel_event_in(ctx, projected);
+ }
+ }
+}
+
/// The `inference_heartbeat` liveness beat for the turn's projection.
pub(super) fn spawn_heartbeat(
ctx: &DispatchCtx,
diff --git a/crates/openhuman-core/src/web_chat/turn_host_tests.rs b/crates/openhuman-core/src/web_chat/turn_host_tests.rs
index e996bcff34..892064c600 100644
--- a/crates/openhuman-core/src/web_chat/turn_host_tests.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host_tests.rs
@@ -53,6 +53,7 @@ async fn the_tap_forwards_in_order_brackets_open_work_and_signals_the_drain() {
runner_tx,
activity.clone(),
drained_tx,
+ None,
);
for event in [
@@ -95,6 +96,7 @@ async fn the_drain_is_released_when_the_stream_ends_without_a_completion() {
runner_tx,
TurnActivity::default(),
drained_tx,
+ None,
);
agent_tx.send(AgentProgress::TurnStarted).await.unwrap();
assert_eq!(kind(&runner_rx.recv().await.unwrap()), "other");
@@ -117,6 +119,7 @@ async fn a_dropped_runner_stops_the_tap() {
runner_tx,
TurnActivity::default(),
drained_tx,
+ None,
);
drop(runner_rx);
agent_tx.send(AgentProgress::TurnStarted).await.unwrap();