Skip to content

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

refactor(web_chat): simplify event bus internals

Steven Enamakel61a52952610e
diff --git a/crates/openhuman-core/src/web_chat/event_bus.rs b/crates/openhuman-core/src/web_chat/event_bus.rs
index 757f259386..a176ba6843 100644
--- a/crates/openhuman-core/src/web_chat/event_bus.rs
+++ b/crates/openhuman-core/src/web_chat/event_bus.rs
@@ -8,6 +8,7 @@ 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;
@@ -25,11 +26,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(mut event: WebChannelEvent) {
+pub fn publish_web_channel_event(ctx: &DispatchCtx, mut event: WebChannelEvent) {
if event.ts.is_none() {
event.ts = Some(crate::web_chat::progress_bridge::unix_epoch_ms());
}
- event.stamp_tenant(crate::core::runtime::current_tenant().ok());
+ event.stamp_tenant(ctx.tenant().ok());
let _ = EVENT_BUS.send(event);
}
@@ -130,6 +131,8 @@ 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,
@@ -179,7 +182,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "memory_activity".to_string(),
client_id,
thread_id,
@@ -234,6 +237,8 @@ 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, ..
@@ -241,7 +246,7 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {
log::debug!(
"[web-channel] agent-surface emitting thread_goal_updated thread_id={thread_id}"
);
- publish_web_channel_event(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "thread_goal_updated".to_string(),
client_id: String::new(),
thread_id: thread_id.clone(),
@@ -253,7 +258,7 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {
log::debug!(
"[web-channel] agent-surface emitting thread_goal_cleared thread_id={thread_id}"
);
- publish_web_channel_event(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "thread_goal_cleared".to_string(),
client_id: String::new(),
thread_id: thread_id.clone(),
@@ -264,7 +269,7 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {
log::debug!(
"[web-channel] agent-surface emitting thread_todos_changed thread_id={thread_id}"
);
- publish_web_channel_event(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "thread_todos_changed".to_string(),
client_id: String::new(),
thread_id: thread_id.clone(),
@@ -276,7 +281,7 @@ impl EventHandler<DomainEvent> for AgentSurfaceSubscriber {
log::debug!(
"[web-channel] agent-surface emitting run_mode_changed thread_id={thread_id} mode={mode}"
);
- publish_web_channel_event(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "run_mode_changed".to_string(),
client_id: String::new(),
thread_id: thread_id.clone(),
@@ -302,7 +307,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "queue_item_queued".to_string(),
client_id: String::new(),
thread_id: thread_id.clone(),
@@ -328,7 +333,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "queue_item_delivered".to_string(),
client_id: String::new(),
thread_id: thread_id.clone(),
@@ -359,7 +364,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "queue_item_delivered".to_string(),
client_id: String::new(),
thread_id: thread_id.clone(),
@@ -389,6 +394,8 @@ 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,
@@ -411,7 +418,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "artifact_ready".to_string(),
client_id: client_id.clone(),
thread_id: thread_id.clone(),
@@ -449,7 +456,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "artifact_failed".to_string(),
client_id: client_id.clone(),
thread_id: thread_id.clone(),
@@ -485,7 +492,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "artifact_pending".to_string(),
client_id: client_id.clone(),
thread_id: thread_id.clone(),
@@ -610,6 +617,8 @@ 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,
@@ -626,7 +635,7 @@ 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(approval_request_event(
+ publish_web_channel_event(&ctx, approval_request_event(
request_id,
tool_name,
action_summary,
@@ -660,7 +669,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "approval_decided".to_string(),
client_id: client_id.clone(),
thread_id: thread_id.clone(),
@@ -692,7 +701,7 @@ 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(plan_review_request_event(
+ publish_web_channel_event(&ctx, plan_review_request_event(
request_id,
summary,
steps,
@@ -722,7 +731,7 @@ 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(WebChannelEvent {
+ publish_web_channel_event(&ctx, WebChannelEvent {
event: "plan_review_decided".to_string(),
client_id: client_id.clone(),
thread_id: thread_id.clone(),