Skip to content

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

fix(relay): scope progressive web events to the dispatch context

Steven Enamakelc0869f42579c
diff --git a/crates/openhuman-core/src/channels/providers/relay/channel.rs b/crates/openhuman-core/src/channels/providers/relay/channel.rs
index 862a7f31aa..e93b3aa381 100644
--- a/crates/openhuman-core/src/channels/providers/relay/channel.rs
+++ b/crates/openhuman-core/src/channels/providers/relay/channel.rs
@@ -10,7 +10,7 @@ use std::sync::Mutex;
use async_trait::async_trait;
use serde_json::json;
-use crate::web_chat::{publish_web_channel_event, WebChannelEvent};
+use crate::web_chat::{publish_web_channel_event, publish_web_channel_event_in, WebChannelEvent};
use tinychannels_bus::{ChannelMessage, SendMessage};
use tinychannels_runtime::Channel;
@@ -84,6 +84,7 @@ impl RelayChannel {
/// separately from the gateway and are never fabricated.
pub(crate) fn publish_typed_reply(
&self,
+ ctx: &crate::core::runtime::DispatchCtx,
channel: &tinychannels_bus::ChannelRef,
conversation: &tinychannels_bus::ConversationRef,
turn_id: &str,
@@ -97,7 +98,7 @@ impl RelayChannel {
.lock()
.unwrap_or_else(|e| e.into_inner())
.push(text.to_owned());
- publish_web_channel_event(WebChannelEvent::ChannelOutbound {
+ publish_web_channel_event_in(ctx, WebChannelEvent::ChannelOutbound {
context: crate::web_chat::event_context(
self.client_id.clone(),
self.thread_id.clone(),
diff --git a/crates/openhuman-core/src/channels/providers/relay/progressive.rs b/crates/openhuman-core/src/channels/providers/relay/progressive.rs
index d5b0b8abd9..57cf5308ec 100644
--- a/crates/openhuman-core/src/channels/providers/relay/progressive.rs
+++ b/crates/openhuman-core/src/channels/providers/relay/progressive.rs
@@ -14,6 +14,7 @@ use tinychannels_bus::{
use super::channel::RelayChannel;
pub(crate) struct RelayProgressiveTransport {
+ ctx: crate::core::runtime::DispatchCtx,
channel: Arc<RelayChannel>,
channel_ref: tinychannels_bus::ChannelRef,
conversation: tinychannels_bus::ConversationRef,
@@ -23,6 +24,7 @@ pub(crate) struct RelayProgressiveTransport {
impl RelayProgressiveTransport {
pub(crate) fn new(
+ ctx: crate::core::runtime::DispatchCtx,
channel: Arc<RelayChannel>,
channel_ref: tinychannels_bus::ChannelRef,
conversation: tinychannels_bus::ConversationRef,
@@ -30,6 +32,7 @@ impl RelayProgressiveTransport {
projection: Arc<crate::web_chat::Surface>,
) -> Self {
Self {
+ ctx,
channel,
channel_ref,
conversation,
@@ -107,6 +110,7 @@ impl RelayProgressiveTransport {
return Err(Error::failed("hosted relay send identity is invalid"));
}
self.channel.publish_typed_reply(
+ &self.ctx,
&self.channel_ref,
&self.conversation,
&self.turn_id,
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/progressive.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/progressive.rs
index 54c16a88e2..5db81920ba 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/progressive.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/progressive.rs
@@ -70,6 +70,7 @@ impl NativeProgressiveTarget {
);
let callback =
crate::channels::providers::relay::progressive::RelayProgressiveTransport::new(
+ ctx.clone(),
Arc::clone(channel),
self.channel.clone(),
self.conversation.clone(),
diff --git a/crates/openhuman-core/src/channels/runtime/startup/module_providers_tests.rs b/crates/openhuman-core/src/channels/runtime/startup/module_providers_tests.rs
index cfe6d3bdbc..b495100560 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/module_providers_tests.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/module_providers_tests.rs
@@ -261,6 +261,8 @@ async fn compiled_module_progressive_send_reenters_typed_callback_without_deadlo
#[tokio::test]
async fn compiled_module_hosted_progressive_callback_is_scoped_and_typed() {
+ use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx, DomainSet};
+ use crate::tools::toolpacks::ToolGroups;
let _env_lock = EnvVarGuard::locked_async().await;
let _storage = crate::channels::module_fixture::install_memory_storage();
let client = crate::channels::module_fixture::client().await;
@@ -284,19 +286,26 @@ async fn compiled_module_hosted_progressive_callback_is_scoped_and_typed() {
..Default::default()
};
let mut web_events = crate::web_chat::subscribe_web_channel_events();
+ let config = Config::default();
+ let ctx = DispatchCtx::new(CoreContext::for_test(DomainSet::full(), None).derive_with(
+ ContextOverlay::new(config.clone(), DomainSet::kernel(), ToolGroups::none())
+ .session_agent("relay-agent")
+ .session_profile("relay-profile"),
+ ));
let projection = Arc::new(
crate::web_chat::Surface::new(
- &crate::core::runtime::DispatchCtx::ambient(),
+ &ctx,
crate::web_chat::TurnIds {
client_id: "relay-fixture-client".into(),
thread_id: "channel:telegram/sender-7/chat-42".into(),
request_id: "relay-fixture-request".into(),
},
- Config::default(),
+ config,
)
.await,
);
let callback = crate::channels::providers::relay::progressive::RelayProgressiveTransport::new(
+ ctx,
Arc::clone(&relay),
channel.clone(),
conversation.clone(),
@@ -360,18 +369,26 @@ async fn compiled_module_hosted_progressive_callback_is_scoped_and_typed() {
assert_eq!(outcome.receipt.conversation.as_ref(), Some(&conversation));
assert_eq!(outcome.receipt.thread_id.as_deref(), Some("topic-3"));
assert_eq!(relay.sent(), vec!["typed hosted relay reply".to_owned()]);
- let projected = tokio::time::timeout(Duration::from_secs(5), async {
- loop {
+ let mut projected = None;
+ let mut terminal = None;
+ tokio::time::timeout(Duration::from_secs(5), async {
+ while projected.is_none() || terminal.is_none() {
let event = web_events.recv().await.expect("web event bus");
- if event.context().request_id == "relay-fixture-request"
- && event.event_name() == "chat_done"
- {
- break event;
+ if event.context().request_id != "relay-fixture-request" {
+ continue;
+ }
+ match event.event_name() {
+ "chat_done" => projected = Some(event),
+ crate::channels::providers::relay::CHANNEL_OUTBOUND_EVENT => {
+ terminal = Some(event)
+ }
+ _ => panic!("unexpected relayed output event: {}", event.event_name()),
}
}
})
.await
- .expect("compiled relay output is projected through the web adapter");
+ .expect("web projection and SaaS terminal both arrive without dropping either");
+ let projected = projected.expect("compiled relay output is projected through the web adapter");
let projected_wire = serde_json::to_value(&projected).expect("projected wire event");
assert_eq!(projected.context().client_id, "relay-fixture-client");
assert_eq!(
@@ -379,21 +396,13 @@ async fn compiled_module_hosted_progressive_callback_is_scoped_and_typed() {
"channel:telegram/sender-7/chat-42"
);
assert_eq!(projected.context().seq, Some(1));
+ assert!(projected.belongs_to("relay-agent"));
+ assert!(projected.belongs_to_profile("relay-profile"));
assert_eq!(projected_wire["full_response"], "typed hosted relay reply");
-
- let terminal = tokio::time::timeout(Duration::from_secs(5), async {
- loop {
- let event = web_events.recv().await.expect("web event bus");
- if event.context().request_id == "relay-fixture-request"
- && event.event_name() == crate::channels::providers::relay::CHANNEL_OUTBOUND_EVENT
- {
- break event;
- }
- }
- })
- .await
- .expect("SaaS terminal delivery remains available");
+ let terminal = terminal.expect("SaaS terminal delivery remains available");
assert_eq!(terminal.context().client_id, "relay-fixture-client");
+ assert!(terminal.belongs_to("relay-agent"));
+ assert!(terminal.belongs_to_profile("relay-profile"));
assert_eq!(relay.sent().len(), 1, "terminal output is not duplicated");
runtime
.close()
@@ -403,6 +412,8 @@ async fn compiled_module_hosted_progressive_callback_is_scoped_and_typed() {
#[tokio::test]
async fn hosted_target_projects_to_the_canonical_thread_and_keeps_one_gateway_terminal() {
+ use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx, DomainSet};
+ use crate::tools::toolpacks::ToolGroups;
let _env_lock = EnvVarGuard::locked_async().await;
let _storage = crate::channels::module_fixture::install_memory_storage();
crate::channels::module_fixture::client().await;
@@ -429,10 +440,15 @@ async fn hosted_target_projects_to_the_canonical_thread_and_keeps_one_gateway_te
topic_id: Some("topic-3".into()),
..Default::default()
};
- let ctx = crate::core::runtime::DispatchCtx::ambient();
+ let config = Config::default();
+ let ctx = DispatchCtx::new(CoreContext::for_test(DomainSet::full(), None).derive_with(
+ ContextOverlay::new(config.clone(), DomainSet::kernel(), ToolGroups::none())
+ .session_agent("relay-target-agent")
+ .session_profile("relay-target-profile"),
+ ));
let target = crate::channels::runtime::dispatch::hosted_relay_target(
&ctx,
- &Config::default(),
+ &config,
Arc::clone(&relay),
channel,
conversation,
@@ -478,12 +494,16 @@ async fn hosted_target_projects_to_the_canonical_thread_and_keeps_one_gateway_te
assert_eq!(projected.context().client_id, client_id);
assert_eq!(projected.context().thread_id, thread_id);
assert_eq!(projected.context().seq, Some(1));
+ assert!(projected.belongs_to("relay-target-agent"));
+ assert!(projected.belongs_to_profile("relay-target-profile"));
assert_eq!(projected_wire["full_response"], "projected hosted reply");
let gateway_terminal = gateway_terminal.expect("legacy SaaS terminal");
let terminal_wire = serde_json::to_value(&gateway_terminal).expect("gateway wire");
assert_eq!(gateway_terminal.context().client_id, client_id);
assert_eq!(gateway_terminal.context().thread_id, thread_id);
+ assert!(gateway_terminal.belongs_to("relay-target-agent"));
+ assert!(gateway_terminal.belongs_to_profile("relay-target-profile"));
assert_eq!(terminal_wire["structured"]["kind"], "channel_outbound");
assert_eq!(relay.sent().len(), 1, "one provider terminal is published");