Skip to content

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

refactor(core): extract dispatch loop and session into tinychannels runtime

Steven Enamakelbce997dd19c1
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
index f41a4bd2d5..6d8decab14 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
@@ -7,16 +7,30 @@
//! approval replies before dispatching a fresh agent turn.
//! * [`process_channel_message`] — full per-message pipeline: typing, ACK
//! reaction, history, agent turn, draft updates, reply.
-//! * [`run_message_dispatch_loop`] — bounded-concurrency worker loop that feeds
-//! messages into [`process_channel_message`].
+//! * [`run_message_dispatch_loop`] — feeds messages into
+//! [`process_channel_runtime_message`] through the bounded-concurrency
+//! `tinychannels::runtime::run_dispatch_loop`.
mod approval;
-mod dispatch_loop;
-mod message;
mod turn;
+use crate::channels::context::ChannelRuntimeContext;
+use std::sync::Arc;
+
#[cfg(test)]
pub(crate) use approval::channel_has_approval_surface;
-pub(crate) use dispatch_loop::run_message_dispatch_loop;
-pub(crate) use message::RuntimeChannelMessage;
+pub(crate) use tinychannels::runtime::RuntimeChannelMessage;
pub(crate) use turn::{process_channel_message, process_channel_runtime_message};
+
+/// Run every inbound message through the agent pipeline, at most
+/// `max_in_flight_messages` at a time, until `rx` closes.
+pub(crate) async fn run_message_dispatch_loop(
+ rx: tokio::sync::mpsc::Receiver<RuntimeChannelMessage>,
+ ctx: Arc<ChannelRuntimeContext>,
+ max_in_flight_messages: usize,
+) {
+ tinychannels::runtime::run_dispatch_loop(rx, max_in_flight_messages, move |msg| {
+ process_channel_runtime_message(Arc::clone(&ctx), msg)
+ })
+ .await;
+}
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
index 5f55f05fe5..b9a427283d 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -30,7 +30,7 @@ use super::super::helpers::{
};
use super::super::routing::resolve_target_agent;
use super::approval::{channel_has_approval_surface, try_route_approval_reply};
-use super::message::RuntimeChannelMessage;
+use super::RuntimeChannelMessage;
pub(crate) async fn process_channel_message(
ctx: Arc<ChannelRuntimeContext>,
diff --git a/crates/openhuman-core/src/channels/runtime/session.rs b/crates/openhuman-core/src/channels/runtime/session.rs
index d555272c4a..361f563d63 100644
--- a/crates/openhuman-core/src/channels/runtime/session.rs
+++ b/crates/openhuman-core/src/channels/runtime/session.rs
@@ -1,34 +1,21 @@
//! Explicit logout invalidates channel runtimes, independently of model auth.
-use std::future::Future;
-use std::sync::{LazyLock, Mutex};
+//!
+//! The session mechanics live in `tinychannels::runtime`; this module owns the
+//! process's single session.
+use std::sync::LazyLock;
+use tinychannels::runtime::ChannelSession;
use tokio_util::sync::CancellationToken;
-static SESSION: LazyLock<Mutex<CancellationToken>> =
- LazyLock::new(|| Mutex::new(CancellationToken::new()));
+pub(super) use tinychannels::runtime::run_in_session;
+
+static SESSION: LazyLock<ChannelSession> = LazyLock::new(ChannelSession::new);
pub(crate) fn channel_session() -> CancellationToken {
- SESSION.lock().unwrap_or_else(|e| e.into_inner()).clone()
+ SESSION.current()
}
pub(crate) fn invalidate_channel_session() {
- let mut session = SESSION.lock().unwrap_or_else(|e| e.into_inner());
- session.cancel();
- *session = CancellationToken::new();
- tracing::debug!("[channels] invalidated channel runtimes on logout");
-}
-
-pub(super) async fn run_in_session(
- session: CancellationToken,
- runtime: impl Future<Output = anyhow::Result<()>>,
-) -> anyhow::Result<()> {
- tokio::select! {
- biased;
- _ = session.cancelled() => {
- tracing::info!("[channels] stopping runtime after logout");
- Ok(())
- }
- result = runtime => result,
- }
+ SESSION.invalidate();
}
#[cfg(test)]