Skip to content

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

refactor(web_chat): move event_belongs_to onto WebChannelEvent

Steven Enamakel1653e04bf6b3
diff --git a/crates/openhuman-core/src/web_chat/channel_event.rs b/crates/openhuman-core/src/web_chat/channel_event.rs
index 89d747ae47..ee37954088 100644
--- a/crates/openhuman-core/src/web_chat/channel_event.rs
+++ b/crates/openhuman-core/src/web_chat/channel_event.rs
@@ -224,6 +224,15 @@ pub struct WebChannelEvent {
pub superseded_by: Option<String>,
}
+impl WebChannelEvent {
+ /// Whether a SaaS user agent's stream may carry this event: only one
+ /// stamped with that agent. An unstamped event (published outside any
+ /// agent scope) is dropped rather than guessed at.
+ pub fn belongs_to(&self, agent: &str) -> bool {
+ self.agent.as_deref() == Some(agent)
+ }
+}
+
/// Time-to-first-visible timing summary for a completed turn. See
/// `web_chat::turn_timing::TurnTiming`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
diff --git a/crates/openhuman-core/src/web_chat/event_bus.rs b/crates/openhuman-core/src/web_chat/event_bus.rs
index 9d6191512e..da9451346a 100644
--- a/crates/openhuman-core/src/web_chat/event_bus.rs
+++ b/crates/openhuman-core/src/web_chat/event_bus.rs
@@ -36,12 +36,6 @@ pub fn publish_web_channel_event(mut event: WebChannelEvent) {
let _ = EVENT_BUS.send(event);
}
-/// Whether a SaaS user agent's stream may carry `event`: only events stamped
-/// with that agent. An unstamped event (published outside any agent scope) is
-/// dropped rather than guessed at.
-pub fn event_belongs_to(event: &WebChannelEvent, agent: &str) -> bool {
- event.agent.as_deref() == Some(agent)
-}
static APPROVAL_SURFACE_HANDLE: OnceLock<SubscriptionHandle> = OnceLock::new();
diff --git a/crates/openhuman-core/src/web_chat/event_bus_tests.rs b/crates/openhuman-core/src/web_chat/event_bus_tests.rs
index ee91ac2bdd..3f92e2a7bf 100644
--- a/crates/openhuman-core/src/web_chat/event_bus_tests.rs
+++ b/crates/openhuman-core/src/web_chat/event_bus_tests.rs
@@ -572,3 +572,47 @@ async fn publish_web_channel_event_preserves_an_explicit_ts() {
.await;
assert_eq!(ev.ts, Some(123));
}
+
+/// A SaaS user's stream carries only events stamped with that user's agent:
+/// `publish_web_channel_event` stamps the publishing scope's agent, and an
+/// event published outside any agent scope belongs to no user.
+#[tokio::test]
+async fn published_events_carry_the_publishing_agent() {
+ use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};
+ let mut rx = subscribe_web_channel_events();
+ let marker = format!("agent-stamp-{}", uuid::Uuid::new_v4());
+ let ctx = CoreContext::for_test(DomainSet::full(), None).derive_with(
+ ContextOverlay::new(crate::config::Config::default(), DomainSet::full(), Default::default())
+ .session_agent("u-alice"),
+ );
+ CoreContext::scope(ctx, async {
+ publish_web_channel_event(WebChannelEvent {
+ event: "chat_done".into(),
+ thread_id: marker.clone(),
+ ..Default::default()
+ });
+ })
+ .await;
+ publish_web_channel_event(WebChannelEvent {
+ event: "chat_done".into(),
+ thread_id: format!("{marker}-unscoped"),
+ ..Default::default()
+ });
+
+ let mut seen = Vec::new();
+ while seen.len() < 2 {
+ let ev = rx.recv().await.unwrap();
+ if ev.thread_id.starts_with(&marker) {
+ seen.push(ev);
+ }
+ }
+ let scoped = seen.iter().find(|e| e.thread_id == marker).unwrap();
+ assert!(scoped.belongs_to("u-alice"));
+ assert!(!scoped.belongs_to("u-bob"));
+ let unscoped = seen.iter().find(|e| e.thread_id != marker).unwrap();
+ assert!(!unscoped.belongs_to("u-alice"), "unstamped events belong to no user");
+ assert!(
+ !serde_json::to_string(scoped).unwrap().contains("u-alice"),
+ "the stamp is not serialized"
+ );
+}
diff --git a/crates/openhuman-core/src/web_chat/mod.rs b/crates/openhuman-core/src/web_chat/mod.rs
index d5b5c03d90..0d85abf472 100644
--- a/crates/openhuman-core/src/web_chat/mod.rs
+++ b/crates/openhuman-core/src/web_chat/mod.rs
@@ -60,8 +60,8 @@ pub use egress_surface::register_egress_surface_subscriber;
pub use event_bus::{
approval_request_event, plan_review_request_event, publish_web_channel_event,
register_agent_surface_subscriber, register_approval_surface_subscriber,
- event_belongs_to, register_artifact_surface_subscriber,
- register_memory_activity_surface_subscriber, subscribe_web_channel_events,
+ register_artifact_surface_subscriber, register_memory_activity_surface_subscriber,
+ subscribe_web_channel_events,
};
pub use progress_bridge::unix_epoch_ms;
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index 8d24e1fc18..77fe2d2ced 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -333,8 +333,6 @@ pub(crate) fn spawn_progress_bridge(
std::sync::Mutex<Option<super::turn_timing::TurnTimingSnapshot>>,
> = std::sync::Arc::new(std::sync::Mutex::new(None));
let timing_snapshot_for_task = timing_snapshot.clone();
- // Scoped: every event this bridge publishes must carry the agent of the
- // turn it reports on, or a SaaS user's stream would drop it.
crate::core::runtime::spawn_scoped(async move {
log::debug!(
"[web_channel][bridge] spawned client_id={} thread_id={} request_id={} speak_reply={:?} source={:?} session_id={:?}",
diff --git a/crates/openhuman-rpc/src/server/http/events.rs b/crates/openhuman-rpc/src/server/http/events.rs
index d9015acdcc..3c0ad11323 100644
--- a/crates/openhuman-rpc/src/server/http/events.rs
+++ b/crates/openhuman-rpc/src/server/http/events.rs
@@ -118,7 +118,7 @@ pub(super) async fn events_handler(
return None;
}
if let Some(agent) = saas_agent.as_deref() {
- if !openhuman_core::web_chat::event_belongs_to(&event, agent) {
+ if !event.belongs_to(agent) {
return None;
}
}