Skip to content

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

fix(channels): validate progressive output scope before observer dispatch

Steven Enamakel8d913b3960a6
diff --git a/crates/openhuman-core/src/channels/runtime/startup/callback.rs b/crates/openhuman-core/src/channels/runtime/startup/callback.rs
index 2ac7c681fc..e3d738ffc5 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/callback.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/callback.rs
@@ -284,6 +284,7 @@ impl ChannelHostCallbacks {
request: ProgressiveTransportRequest,
) -> Result<ProgressiveTransportResult> {
if let ProgressiveTransportRequest::OutputEvent {
+ channel: route,
channel_ref,
conversation,
turn_id,
@@ -292,16 +293,20 @@ impl ChannelHostCallbacks {
..
} = &request
{
+ let channel = channel_ref
+ .as_ref()
+ .ok_or_else(|| Error::failed("progressive output channel identity is required"))?;
+ let conversation = conversation.as_ref().ok_or_else(|| {
+ Error::failed("progressive output conversation identity is required")
+ })?;
+ let turn_id = turn_id
+ .as_deref()
+ .ok_or_else(|| Error::failed("progressive output turn identity is required"))?;
+ if conversation.id.is_empty() || turn_id.is_empty() {
+ return Err(Error::failed("progressive output scope is invalid"));
+ }
+ self.progressive_channel(route, Some(channel))?;
if let Some(observer) = &self.output_observer {
- let channel = channel_ref.as_ref().ok_or_else(|| {
- Error::failed("progressive output channel identity is required")
- })?;
- let conversation = conversation.as_ref().ok_or_else(|| {
- Error::failed("progressive output conversation identity is required")
- })?;
- let turn_id = turn_id.as_deref().ok_or_else(|| {
- Error::failed("progressive output turn identity is required")
- })?;
observer
.observe(channel, conversation, turn_id, *sequence, event)
.await?;
diff --git a/crates/openhuman-core/src/channels/runtime/startup/callback_tests.rs b/crates/openhuman-core/src/channels/runtime/startup/callback_tests.rs
index ae72f64254..cac99c352c 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/callback_tests.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/callback_tests.rs
@@ -1,4 +1,4 @@
-use super::ProgressiveChannelTransport;
+use super::{ProgressiveChannelTransport, ProgressiveOutputObserver};
use super::*;
use crate::channels::Channel;
@@ -14,6 +14,32 @@ use tinychannels_bus::module_progressive::{
};
use tinychannels_bus::{ChannelBackendRequest, ChannelBackendResult, SendMessage};
+#[derive(Default)]
+struct RecordingOutputObserver {
+ seen: tokio::sync::Mutex<Vec<(String, String, String, u64)>>,
+}
+
+#[async_trait::async_trait]
+impl ProgressiveOutputObserver for RecordingOutputObserver {
+ async fn observe(
+ &self,
+ channel: &tinychannels_bus::ChannelRef,
+ conversation: &tinychannels_bus::ConversationRef,
+ turn_id: &str,
+ sequence: u64,
+ event: &ChannelOutputEvent,
+ ) -> Result<()> {
+ assert!(matches!(event, ChannelOutputEvent::TextDelta { .. }));
+ self.seen.lock().await.push((
+ channel.account_id.clone().unwrap_or_default(),
+ conversation.id.clone(),
+ turn_id.into(),
+ sequence,
+ ));
+ Ok(())
+ }
+}
+
struct RecordingChannel {
name: String,
sent: tokio::sync::Mutex<Vec<ChannelOutboundMessage>>,
@@ -118,6 +144,7 @@ impl ProgressiveChannelTransport for RecordingChannel {
fn callback_for_channels(
channels: HashMap<String, Arc<dyn Channel>>,
progressive_channels: HashMap<String, Arc<dyn ProgressiveChannelTransport>>,
+ output_observer: Option<Arc<dyn ProgressiveOutputObserver>>,
) -> (ChannelHostCallbacks, Arc<ChannelRuntimeContext>) {
let context = Arc::new(ChannelRuntimeContext {
relay_thread: None,
@@ -154,7 +181,7 @@ fn callback_for_channels(
Arc::new(crate::config::Config::default()),
context_slot,
progressive_slot,
- None,
+ output_observer,
dispatch,
),
context,
@@ -186,7 +213,7 @@ async fn native_progressive_transport_dispatches_typed_content_through_configure
selected_account.clone() as Arc<dyn ProgressiveChannelTransport>,
),
]);
- let (callback, _context) = callback_for_channels(channels, progressive_channels);
+ let (callback, _context) = callback_for_channels(channels, progressive_channels, None);
let parts = vec![OutboundContentPart::Content {
part: tinychannels_bus::ContentPart::Image {
media: tinychannels_bus::MediaReference {
@@ -302,7 +329,7 @@ async fn native_progressive_transport_rejects_unconfigured_account_without_provi
second_account.clone() as Arc<dyn ProgressiveChannelTransport>,
),
]);
- let (callback, _context) = callback_for_channels(channels, progressive_channels);
+ let (callback, _context) = callback_for_channels(channels, progressive_channels, None);
let result = callback
.dispatch_controller_backend(ChannelBackendRequest::ProgressiveTransport(Box::new(
@@ -341,7 +368,19 @@ async fn native_progressive_transport_rejects_unconfigured_account_without_provi
#[tokio::test]
async fn native_progressive_output_event_is_acknowledged_by_optional_host_observer() {
- let (callback, _context) = callback_for_channels(HashMap::new(), HashMap::new());
+ let observer = Arc::new(RecordingOutputObserver::default());
+ let adapter = Arc::new(RecordingChannel::new("telegram:bot-b"));
+ let (callback, _context) = callback_for_channels(
+ HashMap::from([(
+ "telegram:bot-b".into(),
+ adapter.clone() as Arc<dyn Channel>,
+ )]),
+ HashMap::from([(
+ "telegram:bot-b".into(),
+ adapter.clone() as Arc<dyn ProgressiveChannelTransport>,
+ )]),
+ Some(observer.clone() as Arc<dyn ProgressiveOutputObserver>),
+ );
let result = callback
.dispatch_controller_backend(ChannelBackendRequest::ProgressiveTransport(Box::new(
ProgressiveTransportRequest::OutputEvent {
@@ -369,4 +408,9 @@ async fn native_progressive_output_event_is_acknowledged_by_optional_host_observ
panic!("expected progressive transport result");
};
assert!(matches!(*result, ProgressiveTransportResult::Acknowledged));
+ assert_eq!(
+ *observer.seen.lock().await,
+ [("telegram:bot-b".into(), "chat-42".into(), "turn-13".into(), 4)]
+ );
+ assert!(adapter.progressive_requests.lock().await.is_empty());
}
diff --git a/crates/openhuman-core/src/channels/runtime/startup/progressive_transport.rs b/crates/openhuman-core/src/channels/runtime/startup/progressive_transport.rs
index edf789ae27..dbf8da65c1 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/progressive_transport.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/progressive_transport.rs
@@ -31,16 +31,21 @@ impl ProgressiveChannelTransport for ModuleChannel {
parts,
..
} => {
+ let channel_id = channel_ref
+ .as_ref()
+ .map(|reference| reference.id.clone())
+ .unwrap_or(channel);
+ let thread_id = conversation
+ .as_ref()
+ .and_then(|reference| reference.thread_id.clone());
let outbound = tinychannels_bus::channel::ChannelOutboundMessage {
- channel_id: channel,
+ channel_id,
conversation_id: conversation
.as_ref()
.map(|reference| reference.id.clone())
.unwrap_or_default(),
channel_ref,
- thread_id: conversation
- .as_ref()
- .and_then(|reference| reference.thread_id.clone()),
+ thread_id,
conversation_ref: conversation,
reply_to_id: None,
turn_id,
@@ -141,7 +146,7 @@ impl ProgressiveChannelTransport for ModuleChannel {
Some(tinychannels_bus::channel::SendErrorKind::NotFound) => {
Ok(Result::MessageGone)
}
- Some(tinychannels_bus::channel::SendErrorKind::Forbidden) => {
+ Some(error) if error.unsupported => {
Ok(Result::EditUnsupported)
}
_ => Ok(Result::Outcome(outcome)),