Skip to content

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

fix(web_chat): project a detached sub-agent's late completion

M3gA-Mindc5f41ea6dd77
diff --git a/crates/openhuman-core/src/web_chat/turn_host.rs b/crates/openhuman-core/src/web_chat/turn_host.rs
index 13edb1ad1e..e3bd13771f 100644
--- a/crates/openhuman-core/src/web_chat/turn_host.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host.rs
@@ -267,6 +267,7 @@ impl WebHost {
client_id: payload.client_id.clone(),
thread_id: thread_id.to_owned(),
request_id: id.to_owned(),
+ config: config.clone(),
}),
);
let _beat = AbortOnDrop(spawn_heartbeat(&ctx, projection.clone()));
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 e902e7294e..99c3a1549e 100644
--- a/crates/openhuman-core/src/web_chat/turn_host_progress.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host_progress.rs
@@ -78,7 +78,7 @@ pub(super) fn tap_progress(
while let Some(event) = rx.recv().await {
if runner_gone {
if let Some(late) = late.as_ref() {
- late.publish(&publish_ctx, event);
+ late.publish(&publish_ctx, event).await;
}
continue;
}
@@ -105,7 +105,7 @@ pub(super) fn tap_progress(
}
if runner_gone {
if let Some(late) = late.as_ref() {
- late.publish(&publish_ctx, event);
+ late.publish(&publish_ctx, event).await;
}
}
if completed {
@@ -123,44 +123,46 @@ pub(super) struct LateSubagentSink {
pub(super) client_id: String,
pub(super) thread_id: String,
pub(super) request_id: String,
+ pub(super) config: crate::config::Config,
}
impl LateSubagentSink {
- fn publish(&self, ctx: &DispatchCtx, event: AgentProgress) {
- // Only the event that settles a sub-agent card can matter after the
- // turn: a child aborted by `subagent_cancel` (or one that failed late)
- // still reports its failure. Everything else is turn-scoped output
- // the finished turn no longer renders.
- let AgentProgress::SubagentFailed {
- agent_id,
- task_id,
- error,
- ..
- } = event
- else {
- return;
+ async fn publish(&self, ctx: &DispatchCtx, event: AgentProgress) {
+ // Only the events that settle a sub-agent card can matter after the
+ // turn: a detached child usually finishes after its parent replied,
+ // and one aborted by `subagent_cancel` still reports its failure.
+ // Everything else is turn-scoped output the finished turn no longer
+ // renders.
+ let kind = match &event {
+ AgentProgress::SubagentCompleted { task_id, .. } => ("completed", task_id),
+ AgentProgress::SubagentFailed { task_id, .. } => ("failed", task_id),
+ AgentProgress::SubagentAwaitingUser { task_id, .. } => ("awaiting_user", task_id),
+ _ => return,
};
log::debug!(
- "[web-channel] late subagent_failed task_id={task_id} request_id={}",
+ "[web-channel] late subagent_{} task_id={} request_id={}",
+ kind.0,
+ kind.1,
self.request_id
);
- let context = super::super::event_context(
- self.client_id.clone(),
- self.thread_id.clone(),
- self.request_id.clone(),
- );
- super::super::event_bus_channel::publish_web_channel_event_in(
+ let Some(output) = super::super::progress_map::subagent_event(event, 0, None) else {
+ return;
+ };
+ // The turn's own projection is closed, so the event gets a projection
+ // turn of its own: the module still owns the wire form, including the
+ // child's output and spend a detached completion carries.
+ let surface = super::super::surface::Surface::new(
ctx,
- tinychannels_bus::web::WebChannelEvent::SubagentFailed {
- context,
- message: Some(error),
- tool_name: Some(agent_id),
- skill_id: Some(task_id),
- success: Some(false),
- subagent: None,
- round: None,
+ super::super::surface::TurnIds {
+ client_id: self.client_id.clone(),
+ thread_id: self.thread_id.clone(),
+ request_id: self.request_id.clone(),
},
- );
+ self.config.clone(),
+ )
+ .await;
+ surface.output(output).await;
+ surface.close().await;
}
}
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 3ea955d686..1ec05629c5 100644
--- a/crates/openhuman-core/src/web_chat/turn_host_tests.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host_tests.rs
@@ -133,6 +133,7 @@ async fn a_dropped_runner_stops_the_tap() {
/// A detached sub-agent that fails after its parent turn has finished (a child
/// aborted by `subagent_cancel`) still settles its card: the tap keeps running
/// past the runner and publishes the failure under the turn's ids.
+#[cfg(all(feature = "channels", feature = "modules"))]
#[tokio::test]
async fn a_late_subagent_failure_is_published_after_the_runner_is_gone() {
let mut bus = crate::web_chat::subscribe_web_channel_events();
@@ -149,6 +150,7 @@ async fn a_late_subagent_failure_is_published_after_the_runner_is_gone() {
client_id: "late-client".into(),
thread_id: "late-thread".into(),
request_id: "late-request".into(),
+ config: crate::config::Config::default(),
}),
);
drop(runner_rx);
@@ -179,6 +181,66 @@ async fn a_late_subagent_failure_is_published_after_the_runner_is_gone() {
assert_eq!(wire["success"], false);
}
+/// A detached sub-agent usually finishes after its parent turn replied. Its
+/// completion still reaches the web stream, with the child's output, so the
+/// card settles instead of staying on "Working".
+#[cfg(all(feature = "channels", feature = "modules"))]
+#[tokio::test]
+async fn a_late_subagent_completion_is_published_after_the_runner_is_gone() {
+ let mut bus = crate::web_chat::subscribe_web_channel_events();
+ let (agent_tx, agent_rx) = mpsc::channel(8);
+ let (runner_tx, runner_rx) = mpsc::channel(1);
+ let (drained_tx, _drained_rx) = watch::channel(false);
+ let _tap = tap_progress(
+ &DispatchCtx::ambient(),
+ agent_rx,
+ runner_tx,
+ TurnActivity::default(),
+ drained_tx,
+ Some(LateSubagentSink {
+ client_id: "late-done-client".into(),
+ thread_id: "late-done-thread".into(),
+ request_id: "late-done-request".into(),
+ config: crate::config::Config::default(),
+ }),
+ );
+ drop(runner_rx);
+ agent_tx.send(AgentProgress::TurnStarted).await.unwrap();
+ agent_tx
+ .send(AgentProgress::SubagentCompleted {
+ agent_id: "workflow_builder".into(),
+ task_id: "task-late-done".into(),
+ elapsed_ms: 10,
+ iterations: 2,
+ output_chars: 12,
+ usage: None,
+ output: "final answer".into(),
+ worktree_path: None,
+ changed_files: Vec::new(),
+ dirty_status: None,
+ stop: None,
+ })
+ .await
+ .unwrap();
+ let event = tokio::time::timeout(Duration::from_secs(5), async {
+ loop {
+ let event = bus.recv().await.expect("event bus");
+ if event.context().request_id == "late-done-request" {
+ return event;
+ }
+ }
+ })
+ .await
+ .expect("the late completion is published");
+ assert_eq!(event.event_name(), "subagent_completed");
+ assert_eq!(event.context().client_id, "late-done-client");
+ assert_eq!(event.context().thread_id, "late-done-thread");
+ let wire = serde_json::to_value(&event).expect("wire event");
+ assert_eq!(wire["skill_id"], "task-late-done");
+ assert_eq!(wire["success"], true);
+ assert_eq!(wire["subagent"]["output"], "final answer");
+}
+
#[tokio::test]
async fn the_heartbeat_beats_only_for_a_live_turn() {
use super::super::surface::TurnIds;