The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(web_chat): split subagent event handling out of progress bridge
Steven Enamakel4df76de617bc
diff --git a/crates/openhuman-core/src/web_chat/egress_surface.rs b/crates/openhuman-core/src/web_chat/egress_surface.rsindex 45eb5f28e3..16a728f232 100644--- a/crates/openhuman-core/src/web_chat/egress_surface.rs+++ b/crates/openhuman-core/src/web_chat/egress_surface.rs@@ -12,7 +12,7 @@ use tinybus::{EventHandler, SubscriptionHandle};use crate::core::events::DomainEvent;use crate::web_chat::WebChannelEvent;-use super::event_bus::publish_web_channel_event_in;+use super::event_bus::publish_web_channel_event;static EGRESS_SURFACE_HANDLE: OnceLock<SubscriptionHandle> = OnceLock::new();@@ -94,16 +94,13 @@ impl EventHandler<DomainEvent> for EgressSurfaceSubscriber {descriptor.reason,request_id,);- publish_web_channel_event_in(- &crate::core::runtime::DispatchCtx::ambient(),- WebChannelEvent {- event: "external_transfer_pending".to_string(),- client_id: client_id.clone(),- thread_id: thread_id.clone(),- request_id: request_id.clone().unwrap_or_default(),- args: Some(args),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "external_transfer_pending".to_string(),+ client_id: client_id.clone(),+ thread_id: thread_id.clone(),+ request_id: request_id.clone().unwrap_or_default(),+ args: Some(args),+ ..Default::default()+ });}}diff --git a/crates/openhuman-core/src/web_chat/event_bus.rs b/crates/openhuman-core/src/web_chat/event_bus.rsindex b35566c9bb..757f259386 100644--- a/crates/openhuman-core/src/web_chat/event_bus.rs+++ b/crates/openhuman-core/src/web_chat/event_bus.rs@@ -8,7 +8,6 @@ use std::sync::{Arc, OnceLock};use tokio::sync::broadcast;use crate::core::events::DomainEvent;-use crate::core::runtime::DispatchCtx;use crate::web_chat::WebChannelEvent;use tinybus::EventHandler;use tinybus::SubscriptionHandle;@@ -26,17 +25,11 @@ pub fn subscribe_web_channel_events() -> broadcast::Receiver<WebChannelEvent> {/// `/events` stream, stamping `ts` (epoch ms) when the caller left it unset/// so every emitted event carries a wall-clock time the frontend can use for/// ordering/latency display without guessing at receive time.-pub fn publish_web_channel_event(event: WebChannelEvent) {- publish_web_channel_event_in(&DispatchCtx::ambient(), event);-}--/// [`publish_web_channel_event`] with the tenant stamped from `ctx`: the one-/// the calling turn runs under, rather than the calling task's own scope.-pub fn publish_web_channel_event_in(ctx: &DispatchCtx, mut event: WebChannelEvent) {+pub fn publish_web_channel_event(mut event: WebChannelEvent) {if event.ts.is_none() {event.ts = Some(crate::web_chat::progress_bridge::unix_epoch_ms());}- event.stamp_tenant(ctx.tenant().ok());+ event.stamp_tenant(crate::core::runtime::current_tenant().ok());let _ = EVENT_BUS.send(event);}@@ -137,8 +130,6 @@ impl EventHandler<DomainEvent> for MemoryActivitySurfaceSubscriber {}async fn handle(&self, event: &DomainEvent) {- // Bus events carry no context: act under the calling task's own.- let ctx = DispatchCtx::ambient();let (event_name, thread_id, client_id, args) = match event {DomainEvent::MemoryStored {key,@@ -188,16 +179,13 @@ impl EventHandler<DomainEvent> for MemoryActivitySurfaceSubscriber {log::debug!("[web-channel] memory-activity-surface emitting memory_activity kind={event_name} thread_id={thread_id} client_id={client_id}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "memory_activity".to_string(),- client_id,- thread_id,- args: Some(args),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "memory_activity".to_string(),+ client_id,+ thread_id,+ args: Some(args),+ ..Default::default()+ });}}@@ -246,8 +234,6 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {}async fn handle(&self, event: &DomainEvent) {- // Bus events carry no context: act under the calling task's own.- let ctx = DispatchCtx::ambient();match event {DomainEvent::ThreadGoalUpdated {thread_id, goal, ..@@ -255,60 +241,48 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {log::debug!("[web-channel] agent-surface emitting thread_goal_updated thread_id={thread_id}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "thread_goal_updated".to_string(),- client_id: String::new(),- thread_id: thread_id.clone(),- goal: goal.clone(),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "thread_goal_updated".to_string(),+ client_id: String::new(),+ thread_id: thread_id.clone(),+ goal: goal.clone(),+ ..Default::default()+ });}DomainEvent::ThreadGoalCleared { thread_id } => {log::debug!("[web-channel] agent-surface emitting thread_goal_cleared thread_id={thread_id}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "thread_goal_cleared".to_string(),- client_id: String::new(),- thread_id: thread_id.clone(),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "thread_goal_cleared".to_string(),+ client_id: String::new(),+ thread_id: thread_id.clone(),+ ..Default::default()+ });}DomainEvent::ThreadTodosChanged { thread_id, todos } => {log::debug!("[web-channel] agent-surface emitting thread_todos_changed thread_id={thread_id}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "thread_todos_changed".to_string(),- client_id: String::new(),- thread_id: thread_id.clone(),- todos: Some(todos.clone()),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "thread_todos_changed".to_string(),+ client_id: String::new(),+ thread_id: thread_id.clone(),+ todos: Some(todos.clone()),+ ..Default::default()+ });}DomainEvent::ThreadRunModeChanged { thread_id, mode } => {log::debug!("[web-channel] agent-surface emitting run_mode_changed thread_id={thread_id} mode={mode}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "run_mode_changed".to_string(),- client_id: String::new(),- thread_id: thread_id.clone(),- message: Some(mode.clone()),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "run_mode_changed".to_string(),+ client_id: String::new(),+ thread_id: thread_id.clone(),+ message: Some(mode.clone()),+ ..Default::default()+ });}DomainEvent::RunQueueMessageQueued {thread_id,@@ -328,20 +302,17 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {log::debug!("[web-channel] agent-surface emitting queue_item_queued thread_id={thread_id} item_id={item_id}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "queue_item_queued".to_string(),- client_id: String::new(),- thread_id: thread_id.clone(),- queue_item: Some(crate::web_chat::QueueItemPayload {- id: item_id,- lane: None,- text_preview: text_preview.clone(),- }),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "queue_item_queued".to_string(),+ client_id: String::new(),+ thread_id: thread_id.clone(),+ queue_item: Some(crate::web_chat::QueueItemPayload {+ id: item_id,+ lane: None,+ text_preview: text_preview.clone(),+ }),+ ..Default::default()+ });}DomainEvent::RunQueueMessageDelivered {thread_id,@@ -357,20 +328,17 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {log::debug!("[web-channel] agent-surface emitting queue_item_delivered thread_id={thread_id} item_id={item_id}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "queue_item_delivered".to_string(),- client_id: String::new(),- thread_id: thread_id.clone(),- queue_item: Some(crate::web_chat::QueueItemPayload {- id: item_id,- lane,- text_preview: text_preview.clone(),- }),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "queue_item_delivered".to_string(),+ client_id: String::new(),+ thread_id: thread_id.clone(),+ queue_item: Some(crate::web_chat::QueueItemPayload {+ id: item_id,+ lane,+ text_preview: text_preview.clone(),+ }),+ ..Default::default()+ });}DomainEvent::RunQueueFollowupDispatched {thread_id,@@ -391,20 +359,17 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {log::debug!("[web-channel] agent-surface emitting queue_item_delivered thread_id={thread_id} item_id={item_id}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "queue_item_delivered".to_string(),- client_id: String::new(),- thread_id: thread_id.clone(),- queue_item: Some(crate::web_chat::QueueItemPayload {- id: item_id,- lane,- text_preview: text_preview.clone(),- }),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "queue_item_delivered".to_string(),+ client_id: String::new(),+ thread_id: thread_id.clone(),+ queue_item: Some(crate::web_chat::QueueItemPayload {+ id: item_id,+ lane,+ text_preview: text_preview.clone(),+ }),+ ..Default::default()+ });}_ => {}}@@ -424,8 +389,6 @@ impl EventHandler<DomainEvent> for ArtifactSurfaceSubscriber {}async fn handle(&self, event: &DomainEvent) {- // Bus events carry no context: act under the calling task's own.- let ctx = DispatchCtx::ambient();match event {DomainEvent::ArtifactReady {artifact_id,@@ -448,25 +411,22 @@ impl EventHandler<DomainEvent> for ArtifactSurfaceSubscriber {log::info!("[web-channel] artifact-surface emitting artifact_ready id={artifact_id} kind={kind} thread_id={thread_id} client_id={client_id} tool_call_id={tool_call_id:?}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "artifact_ready".to_string(),- client_id: client_id.clone(),- thread_id: thread_id.clone(),- tool_call_id: tool_call_id.clone(),- turn_request_id: request_id.clone(),- args: Some(serde_json::json!({- "artifact_id": artifact_id,- "kind": kind,- "title": title,- "workspace_dir": workspace_dir,- "path": path,- "size_bytes": size_bytes,- })),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "artifact_ready".to_string(),+ client_id: client_id.clone(),+ thread_id: thread_id.clone(),+ tool_call_id: tool_call_id.clone(),+ turn_request_id: request_id.clone(),+ args: Some(serde_json::json!({+ "artifact_id": artifact_id,+ "kind": kind,+ "title": title,+ "workspace_dir": workspace_dir,+ "path": path,+ "size_bytes": size_bytes,+ })),+ ..Default::default()+ });}DomainEvent::ArtifactFailed {artifact_id,@@ -489,24 +449,21 @@ impl EventHandler<DomainEvent> for ArtifactSurfaceSubscriber {"[web-channel] artifact-surface emitting artifact_failed id={artifact_id} kind={kind} thread_id={thread_id} client_id={client_id} tool_call_id={tool_call_id:?} error_len={}",error.len());- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "artifact_failed".to_string(),- client_id: client_id.clone(),- thread_id: thread_id.clone(),- tool_call_id: tool_call_id.clone(),- turn_request_id: request_id.clone(),- args: Some(serde_json::json!({- "artifact_id": artifact_id,- "kind": kind,- "title": title,- "workspace_dir": workspace_dir,- "error": error,- })),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "artifact_failed".to_string(),+ client_id: client_id.clone(),+ thread_id: thread_id.clone(),+ tool_call_id: tool_call_id.clone(),+ turn_request_id: request_id.clone(),+ args: Some(serde_json::json!({+ "artifact_id": artifact_id,+ "kind": kind,+ "title": title,+ "workspace_dir": workspace_dir,+ "error": error,+ })),+ ..Default::default()+ });}DomainEvent::ArtifactPending {artifact_id,@@ -528,24 +485,21 @@ impl EventHandler<DomainEvent> for ArtifactSurfaceSubscriber {log::info!("[web-channel] artifact-surface emitting artifact_pending id={artifact_id} kind={kind} thread_id={thread_id} client_id={client_id} tool_call_id={tool_call_id:?}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "artifact_pending".to_string(),- client_id: client_id.clone(),- thread_id: thread_id.clone(),- tool_call_id: tool_call_id.clone(),- turn_request_id: request_id.clone(),- args: Some(serde_json::json!({- "artifact_id": artifact_id,- "kind": kind,- "title": title,- "workspace_dir": workspace_dir,- "path": path,- })),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "artifact_pending".to_string(),+ client_id: client_id.clone(),+ thread_id: thread_id.clone(),+ tool_call_id: tool_call_id.clone(),+ turn_request_id: request_id.clone(),+ args: Some(serde_json::json!({+ "artifact_id": artifact_id,+ "kind": kind,+ "title": title,+ "workspace_dir": workspace_dir,+ "path": path,+ })),+ ..Default::default()+ });}_ => {}}@@ -656,8 +610,6 @@ impl EventHandler<DomainEvent> for ApprovalSurfaceSubscriber {}async fn handle(&self, event: &DomainEvent) {- // Bus events carry no context: act under the calling task's own.- let ctx = DispatchCtx::ambient();match event {DomainEvent::ApprovalRequested {request_id,@@ -674,20 +626,17 @@ impl EventHandler<DomainEvent> for ApprovalSurfaceSubscriber {log::info!("[web-channel] approval-surface emitting approval_request request_id={request_id} thread_id={thread_id} client_id={client_id} tool={tool_name}");- publish_web_channel_event_in(- &ctx,- approval_request_event(- request_id,- tool_name,- action_summary,- args_redacted,- thread_id,- client_id,- tool_call_id.as_deref(),- expires_at.as_deref(),- crate::security::approval::is_detached_request(request_id),- ),- );+ publish_web_channel_event(approval_request_event(+ request_id,+ tool_name,+ action_summary,+ args_redacted,+ thread_id,+ client_id,+ tool_call_id.as_deref(),+ expires_at.as_deref(),+ crate::security::approval::is_detached_request(request_id),+ ));}_ => {log::warn!(@@ -711,20 +660,17 @@ impl EventHandler<DomainEvent> for ApprovalSurfaceSubscriber {log::info!("[web-channel] approval-surface emitting approval_decided request_id={request_id} thread_id={thread_id} client_id={client_id} tool={tool_name} decision={decision}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "approval_decided".to_string(),- client_id: client_id.clone(),- thread_id: thread_id.clone(),- request_id: request_id.clone(),- tool_name: Some(tool_name.clone()),- message: Some(decision.clone()),- tool_call_id: tool_call_id.clone(),- cancel_reason: resolution.clone(),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "approval_decided".to_string(),+ client_id: client_id.clone(),+ thread_id: thread_id.clone(),+ request_id: request_id.clone(),+ tool_name: Some(tool_name.clone()),+ message: Some(decision.clone()),+ tool_call_id: tool_call_id.clone(),+ cancel_reason: resolution.clone(),+ ..Default::default()+ });}_ => {log::debug!(@@ -746,18 +692,15 @@ impl EventHandler<DomainEvent> for ApprovalSurfaceSubscriber {"[web-channel] plan-review-surface emitting plan_review_request request_id={request_id} thread_id={thread_id} client_id={client_id} steps={}",steps.len());- publish_web_channel_event_in(- &ctx,- plan_review_request_event(- request_id,- summary,- steps,- thread_id,- client_id,- tool_call_id.as_deref(),- expires_at.as_deref(),- ),- );+ publish_web_channel_event(plan_review_request_event(+ request_id,+ summary,+ steps,+ thread_id,+ client_id,+ tool_call_id.as_deref(),+ expires_at.as_deref(),+ ));}_ => {log::warn!(@@ -779,20 +722,17 @@ impl EventHandler<DomainEvent> for ApprovalSurfaceSubscriber {log::info!("[web-channel] plan-review-surface emitting plan_review_decided request_id={request_id} thread_id={thread_id} client_id={client_id} decision={decision}");- publish_web_channel_event_in(- &ctx,- WebChannelEvent {- event: "plan_review_decided".to_string(),- client_id: client_id.clone(),- thread_id: thread_id.clone(),- request_id: request_id.clone(),- tool_name: Some("request_plan_review".to_string()),- message: Some(decision.clone()),- tool_call_id: tool_call_id.clone(),- cancel_reason: resolution.clone(),- ..Default::default()- },- );+ publish_web_channel_event(WebChannelEvent {+ event: "plan_review_decided".to_string(),+ client_id: client_id.clone(),+ thread_id: thread_id.clone(),+ request_id: request_id.clone(),+ tool_name: Some("request_plan_review".to_string()),+ message: Some(decision.clone()),+ tool_call_id: tool_call_id.clone(),+ cancel_reason: resolution.clone(),+ ..Default::default()+ });}_ => {log::debug!(diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rsindex 963de38e0a..7e71eb09e1 100644--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs@@ -684,13 +684,7 @@ pub(crate) fn spawn_progress_bridge(parent_call_id,..} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_spawned(&ctx,&mut emit_seq,@@ -719,13 +713,7 @@ pub(crate) fn spawn_progress_bridge(dirty_status,..} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_completed(&ctx,&mut emit_seq,@@ -749,13 +737,7 @@ pub(crate) fn spawn_progress_bridge(task_id,error,} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_failed(&ctx,&mut emit_seq,@@ -774,13 +756,7 @@ pub(crate) fn spawn_progress_bridge(worker_thread_id,checkpoint_path,} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_awaiting_user(&ctx,&mut emit_seq,@@ -800,13 +776,7 @@ pub(crate) fn spawn_progress_bridge(max_iterations,extended_policy,} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_iteration_started(&ctx,&mut emit_seq,@@ -828,13 +798,7 @@ pub(crate) fn spawn_progress_bridge(display_label,display_detail,} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_tool_call_started(&ctx,&mut emit_seq,@@ -866,13 +830,7 @@ pub(crate) fn spawn_progress_bridge(display_detail,structured,} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_tool_call_completed(&ctx,&mut emit_seq,@@ -899,13 +857,7 @@ pub(crate) fn spawn_progress_bridge(delta,iteration,} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_text_delta(&ctx,&mut emit_seq,@@ -922,13 +874,7 @@ pub(crate) fn spawn_progress_bridge(delta,iteration,} => {- let ctx = BridgeCtx {- client_id: &client_id,- thread_id: &thread_id,- request_id: &request_id,- config: &config,- dispatch: &dispatch_ctx,- };+ let ctx = BridgeCtx::new(&client_id, &thread_id, &request_id, &config, &dispatch_ctx);subagent_events::on_subagent_thinking_delta(&ctx,&mut emit_seq,diff --git a/crates/openhuman-core/src/web_chat/progress_bridge_subagent_events.rs b/crates/openhuman-core/src/web_chat/progress_bridge_subagent_events.rsindex b62af4f2b8..2a6fab8b35 100644--- a/crates/openhuman-core/src/web_chat/progress_bridge_subagent_events.rs+++ b/crates/openhuman-core/src/web_chat/progress_bridge_subagent_events.rs@@ -32,6 +32,24 @@ pub(super) struct BridgeCtx<'a> {pub(super) dispatch: &'a crate::core::runtime::DispatchCtx,}+impl<'a> BridgeCtx<'a> {+ pub(super) fn new(+ client_id: &'a str,+ thread_id: &'a str,+ request_id: &'a str,+ config: &'a crate::config::Config,+ dispatch: &'a crate::core::runtime::DispatchCtx,+ ) -> Self {+ Self {+ client_id,+ thread_id,+ request_id,+ config,+ dispatch,+ }+ }+}+pub(super) fn on_subagent_spawned(ctx: &BridgeCtx<'_>,emit_seq: &mut u64,