Skip to content

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

fix(channels): scope transport delivery to the originating channel session

Steven Enamakel61f98f21be3a
diff --git a/crates/openhuman-core/src/channels/bus/delivery.rs b/crates/openhuman-core/src/channels/bus/delivery.rs
index 45ba12ba79..a8a926565a 100644
--- a/crates/openhuman-core/src/channels/bus/delivery.rs
+++ b/crates/openhuman-core/src/channels/bus/delivery.rs
@@ -31,10 +31,13 @@ pub(super) fn map_backend_error(error: anyhow::Error) -> ProgressiveTransportRes
struct ManagedTransport {
client: BackendClient,
credential: BackendCredential,
+ channel: String,
+ session: tokio_util::sync::CancellationToken,
}
impl ManagedTransport {
- fn capture(config: &crate::config::Config) -> anyhow::Result<Self> {
+ fn capture(config: &crate::config::Config, channel: String, session: tokio_util::sync::CancellationToken) -> anyhow::Result<Self> {
+ anyhow::ensure!(!session.is_cancelled(), "Channel session closed");
let credential = match get_session_token(config)
.map_err(|_| anyhow::anyhow!("Hosted channel credential unavailable"))? {
Some(token) if !is_local_session_token(&token) => BackendCredential::Session(token),
@@ -43,10 +46,23 @@ impl ManagedTransport {
Ok(Self {
client: BackendClient::from_config(config)?,
credential,
+ channel,
+ session,
})
}
async fn execute(&self, request: ProgressiveTransportRequest) -> ProgressiveTransportResult {
+ if request_channel(&request) != self.channel {
+ return ProgressiveTransportResult::Failed;
+ }
+ tokio::select! {
+ biased;
+ _ = self.session.cancelled() => ProgressiveTransportResult::Unavailable,
+ result = self.execute_transport(request) => result,
+ }
+ }
+
+ async fn execute_transport(&self, request: ProgressiveTransportRequest) -> ProgressiveTransportResult {
let result = match request {
ProgressiveTransportRequest::Send { channel, message } => {
self.client
@@ -88,6 +104,15 @@ impl ManagedTransport {
}
}
+fn request_channel(request: &ProgressiveTransportRequest) -> &str {
+ match request {
+ ProgressiveTransportRequest::Send { channel, .. }
+ | ProgressiveTransportRequest::Edit { channel, .. }
+ | ProgressiveTransportRequest::Delete { channel, .. }
+ | ProgressiveTransportRequest::Typing { channel } => channel,
+ }
+}
+
#[tinybus::interface(name = "ai.tinyhumans.tinychannels.ChannelsHost")]
impl ManagedTransport {
async fn dispatch_controller_backend(
@@ -113,8 +138,9 @@ impl ModuleProgressiveReply {
pub(super) async fn open(
config: crate::config::Config,
channel: String,
+ session: tokio_util::sync::CancellationToken,
) -> anyhow::Result<Self> {
- let callback = ManagedTransport::capture(&config)?;
+ let callback = ManagedTransport::capture(&config, channel.clone(), session)?;
let runtime = ChannelRuntimeLease::open(ModuleClient::new(config), callback).await?;
let result: ProgressiveResult = runtime
.object()
diff --git a/crates/openhuman-core/src/channels/bus/delivery_tests.rs b/crates/openhuman-core/src/channels/bus/delivery_tests.rs
index 088cab6f17..aa72f830c3 100644
--- a/crates/openhuman-core/src/channels/bus/delivery_tests.rs
+++ b/crates/openhuman-core/src/channels/bus/delivery_tests.rs
@@ -16,5 +16,24 @@ async fn transport_captures_no_local_or_missing_hosted_credential() {
let _env_lock = crate::config::TEST_ENV_LOCK.lock().await;
let workspace = tempfile::tempdir().expect("temporary workspace");
let _workspace_env = EnvVarGuard::workspace_unlocked(workspace.path());
- assert!(ManagedTransport::capture(&crate::config::Config::default()).is_err());
+ assert!(ManagedTransport::capture(&crate::config::Config::default(), "telegram".into(), tokio_util::sync::CancellationToken::new()).is_err());
+}
+
+#[test]
+fn every_transport_operation_carries_the_channel_scoped_by_the_host() {
+ for request in [
+ ProgressiveTransportRequest::Send { channel: "discord:fixture".into(), message: json!({"text":"hello"}) },
+ ProgressiveTransportRequest::Edit { channel: "discord:fixture".into(), message_id: "message".into(), text: "edited".into() },
+ ProgressiveTransportRequest::Delete { channel: "discord:fixture".into(), message_id: "message".into() },
+ ProgressiveTransportRequest::Typing { channel: "discord:fixture".into() },
+ ] { assert_eq!(request_channel(&request), "discord:fixture"); }
+}
+
+#[test]
+fn a_cancelled_identity_cannot_capture_a_replacement_credential() {
+ let session = tokio_util::sync::CancellationToken::new();
+ session.cancel();
+ let error = ManagedTransport::capture(&crate::config::Config::default(), "telegram".into(), session)
+ .err().expect("closed identities must fail before credential reads");
+ assert_eq!(error.to_string(), "Channel session closed");
}
diff --git a/crates/openhuman-core/src/channels/bus/subscriber.rs b/crates/openhuman-core/src/channels/bus/subscriber.rs
index 6a3767f8ea..4582708c38 100644
--- a/crates/openhuman-core/src/channels/bus/subscriber.rs
+++ b/crates/openhuman-core/src/channels/bus/subscriber.rs
@@ -64,6 +64,7 @@ impl EventHandler<DomainEvent> for ChannelInboundSubscriber {
return;
}
+ let session = crate::channels::runtime::session::channel_session();
let config = match crate::config::rpc::load_config_with_timeout().await {
Ok(config) => config,
Err(error) => {
@@ -92,7 +93,7 @@ impl EventHandler<DomainEvent> for ChannelInboundSubscriber {
let client_id = identity.client_id;
let mut event_rx = crate::web_chat::subscribe_web_channel_events();
- let reply = match ModuleProgressiveReply::open(config, channel.clone()).await {
+ let reply = match ModuleProgressiveReply::open(config, channel.clone(), session.clone()).await {
Ok(reply) => reply,
Err(error) => {
tracing::error!("[channel-inbound] progressive runtime unavailable: {error}");
@@ -142,6 +143,8 @@ impl EventHandler<DomainEvent> for ChannelInboundSubscriber {
batch_timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
tokio::select! {
+ biased;
+ _ = session.cancelled() => return,
event = event_rx.recv() => {
match event {
Ok(ev) if ev.request_id == request_id => {