Skip to content

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

fix(channels): cancel remote control callbacks on session end

Steven Enamakelb83b17f53d23
diff --git a/crates/openhuman-core/src/channels/host/remote_control.rs b/crates/openhuman-core/src/channels/host/remote_control.rs
index c19121fbda..a81543a393 100644
--- a/crates/openhuman-core/src/channels/host/remote_control.rs
+++ b/crates/openhuman-core/src/channels/host/remote_control.rs
@@ -6,11 +6,16 @@ use crate::threads::store::{self as conversations, CreateConversationThread};
/// Serves product-owned conversation state to the captured module runtime.
pub(crate) async fn dispatch_callback(
ctx: &ChannelRuntimeContext,
+ tenant: &crate::core::runtime::Tenant,
+ cancellation: &tokio_util::sync::CancellationToken,
request: tinychannels_bus::module_remote::RemoteHostRequest,
) -> tinybus::Result<tinychannels_bus::module_remote::RemoteHostResult> {
use tinychannels_bus::module_remote::{
RemoteHostRequest as Request, RemoteHostResult as Response,
};
+ if cancellation.is_cancelled() {
+ return Err(tinybus::Error::failed("channel session ended"));
+ }
match request {
Request::ListThreads => {
let threads = conversations::list_threads(ctx.workspace_dir.to_path_buf())
@@ -30,6 +35,9 @@ pub(crate) async fn dispatch_callback(
))
}
Request::CreateThread(thread) => {
+ if cancellation.is_cancelled() {
+ return Err(tinybus::Error::failed("channel session ended"));
+ }
conversations::ensure_thread(
ctx.workspace_dir.to_path_buf(),
CreateConversationThread {
@@ -46,11 +54,22 @@ pub(crate) async fn dispatch_callback(
Ok(Response::Completed)
}
Request::ClearHistory { sender_key } => {
+ if cancellation.is_cancelled() {
+ return Err(tinybus::Error::failed("channel session ended"));
+ }
clear_sender_history(ctx, &sender_key);
Ok(Response::Completed)
}
Request::InvalidateThread { thread_id } => {
- crate::web_chat::invalidate_thread_sessions(&thread_id).await;
+ let _removed = crate::web_chat::invalidate_thread_sessions_for_tenant(
+ tenant,
+ &thread_id,
+ cancellation,
+ )
+ .await;
+ if cancellation.is_cancelled() {
+ return Err(tinybus::Error::failed("channel session ended"));
+ }
Ok(Response::Completed)
}
}
diff --git a/crates/openhuman-core/src/channels/routes.rs b/crates/openhuman-core/src/channels/routes.rs
index ec0f19b3d9..18eb4dbda0 100644
--- a/crates/openhuman-core/src/channels/routes.rs
+++ b/crates/openhuman-core/src/channels/routes.rs
@@ -114,23 +114,59 @@ pub(crate) async fn get_or_create_turn_model_source(
struct RouteCallbacks {
context: ChannelRuntimeContext,
+ core_context: Option<Arc<crate::core::runtime::CoreContext>>,
+ tenant: crate::core::runtime::Tenant,
cancellation: tokio_util::sync::CancellationToken,
}
+impl RouteCallbacks {
+ fn new(
+ context: ChannelRuntimeContext,
+ cancellation: tokio_util::sync::CancellationToken,
+ ) -> Self {
+ Self {
+ context,
+ core_context: crate::core::runtime::CoreContext::current(),
+ tenant: crate::core::runtime::tenant::current_tenant_or_isolated(
+ "channel_remote_control",
+ ),
+ cancellation,
+ }
+ }
+}
+
#[tinybus::interface(name = "ai.tinyhumans.tinychannels.ChannelsHost")]
impl RouteCallbacks {
async fn dispatch_controller_backend(
&self,
request: tinychannels_bus::ChannelBackendRequest,
) -> tinybus::Result<tinychannels_bus::ChannelBackendResult> {
- if self.cancellation.is_cancelled() {
- return Err(tinybus::Error::failed("channel session ended"));
- }
match request {
tinychannels_bus::ChannelBackendRequest::RemoteControl(request) => {
- super::host::remote_control::dispatch_callback(&self.context, request)
- .await
- .map(tinychannels_bus::ChannelBackendResult::RemoteControl)
+ let dispatch = async {
+ tokio::select! {
+ biased;
+ _ = self.cancellation.cancelled() => {
+ Err(tinybus::Error::failed("channel session ended"))
+ }
+ result = super::host::remote_control::dispatch_callback(
+ &self.context,
+ &self.tenant,
+ &self.cancellation,
+ request,
+ ) => result.map(tinychannels_bus::ChannelBackendResult::RemoteControl),
+ }
+ };
+ match &self.core_context {
+ Some(core_context) => {
+ crate::core::runtime::CoreContext::scope(
+ Arc::clone(core_context),
+ dispatch,
+ )
+ .await
+ }
+ None => dispatch.await,
+ }
}
_ => Err(tinybus::Error::failed("channel callback unavailable")),
}
@@ -160,10 +196,7 @@ pub(crate) async fn handle_runtime_command_if_needed(
let operation = async {
let lease = crate::modules::channel_runtime::ChannelRuntimeLease::open(
client,
- RouteCallbacks {
- context: ctx.clone(),
- cancellation: super::runtime::session::channel_session(),
- },
+ RouteCallbacks::new(ctx.clone(), super::runtime::session::channel_session()),
)
.await?;
let response: Result<ChannelControlResult, _> = lease