The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(channels): serialize and debounce dispatch per conversation
oxoxDev4c9a3881d361
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rsindex 58a988bc8d..ba5553d7f1 100644--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs@@ -12,18 +12,25 @@//! * [`bus_turn`] — the orchestrator path: an unbound channel's turn over//! the native bus. A channel bound to a host agent runs through//! [`super::host_agent`] instead.-//! * [`run_message_dispatch_loop`] — feeds messages into-//! [`process_channel_runtime_message`] with a bounded set of host workers-//! and joins them when the inbound queue closes.+//! * [`run_message_dispatch_loop`] — admits inbound messages, debounces them+//! per conversation through [`crate::inbound_debounce`], runs one turn per+//! batch with a bounded set of host workers, and drains everything when the+//! inbound queue closes.mod admit;mod approval;mod bus_turn;mod turn;+use super::inbound_batch::{merge_channel_batch, QueuedInbound};use crate::channels::context::ChannelRuntimeContext;+use crate::config::InboundDebounceConfig;+use crate::inbound_debounce::{KeyedDebouncer, Push};+use std::collections::{HashMap, VecDeque};use std::sync::Arc;use tinychannels_bus::{ChannelInboundEnvelope, ChannelMessage};+use tokio::task::JoinSet;+use tokio::time::Instant;/// Host-side inbound work item. Relay authentication remains out-of-band and/// can only be set by the native callback after receiving the trusted module@@ -74,36 +81,151 @@ impl From<ChannelMessage> for RuntimeChannelMessage {}}+use admit::admit_runtime_message;pub(crate) use admit::AdmittedInbound;#[cfg(test)]pub(crate) use approval::channel_has_approval_surface;+use turn::run_admitted_turn;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.+/// Run every inbound message through the agent pipeline until `rx` closes.+///+/// Admitted messages are debounced per conversation (`history_key`): a burst+/// becomes one turn, and messages that arrive while a conversation's turn is+/// running are collected into one follow-up turn. At most+/// `max_in_flight_messages` turns run at once.pub(crate) async fn run_message_dispatch_loop(rx: tokio::sync::mpsc::Receiver<RuntimeChannelMessage>,ctx: Arc<ChannelRuntimeContext>,max_in_flight_messages: usize,+ debounce: InboundDebounceConfig,) {+ let max_in_flight = max_in_flight_messages.max(1);let mut rx = rx;- let mut workers = tokio::task::JoinSet::new();- while let Some(message) = rx.recv().await {- while workers.len() >= max_in_flight_messages.max(1) {- if let Some(Err(error)) = workers.join_next().await {- tracing::warn!("channel dispatch worker failed: {error}");+ let mut inbound_open = true;+ let mut next_seq: u64 = 0;+ let mut admitting = JoinSet::new();+ let mut turns = JoinSet::new();+ let mut turn_keys: HashMap<tokio::task::Id, String> = HashMap::new();+ let mut typing = JoinSet::new();+ let mut debouncer: KeyedDebouncer<String, QueuedInbound> = KeyedDebouncer::new();+ let mut ready: VecDeque<(String, Vec<QueuedInbound>)> = VecDeque::new();++ loop {+ while turns.len() < max_in_flight {+ let Some((key, batch)) = ready.pop_front() else {+ break;+ };+ let batch_len = batch.len();+ match merge_channel_batch(batch) {+ Some(admitted) => {+ tracing::debug!(+ channel = %admitted.msg.channel,+ message_id = %admitted.msg.id,+ batch_len,+ in_flight = turns.len() + 1,+ "[dispatch][debounce] starting turn"+ );+ let handle = turns.spawn(run_admitted_turn(Arc::clone(&ctx), admitted));+ turn_keys.insert(handle.id(), key);+ }+ None => debouncer.complete(&key),}}- workers.spawn(process_channel_runtime_message(Arc::clone(&ctx), message));- while let Some(result) = workers.try_join_next() {- if let Err(error) = result {- tracing::warn!("channel dispatch worker failed: {error}");+ while let Some(result) = typing.try_join_next() {+ log_join_error("typing", result.err());+ }+ if !inbound_open+ && admitting.is_empty()+ && turns.is_empty()+ && ready.is_empty()+ && debouncer.is_empty()+ {+ break;+ }++ let wakeup = debouncer.next_wakeup();+ tokio::select! {+ received = rx.recv(), if inbound_open && admitting.len() < max_in_flight => {+ match received {+ Some(message) => {+ let seq = next_seq;+ next_seq += 1;+ let ctx = Arc::clone(&ctx);+ admitting.spawn(async move { (seq, admit_runtime_message(ctx, message).await) });+ }+ None => {+ tracing::debug!(+ buffered_keys = debouncer.tracked(),+ "[dispatch][debounce] inbound queue closed; flushing buffered messages"+ );+ inbound_open = false;+ }+ }+ }+ Some(joined) = admitting.join_next(), if !admitting.is_empty() => {+ match joined {+ Ok((seq, Some(inbound))) => {+ let settings = debounce.settings_for(&inbound.msg.channel);+ let channel = inbound.msg.channel.clone();+ let reply_target = inbound.msg.reply_target.clone();+ let key = inbound.history_key.clone();+ let push = debouncer.push(+ key,+ QueuedInbound { seq, inbound },+ &settings,+ Instant::now(),+ );+ tracing::debug!(+ channel = %channel,+ seq,+ outcome = ?push,+ tracked = debouncer.tracked(),+ "[dispatch][debounce] buffered inbound message"+ );+ if push == Push::Opened {+ if let Some(target) = ctx.channels_by_name.get(&channel).cloned() {+ typing.spawn(async move {+ if let Err(error) = target.start_typing(&reply_target).await {+ tracing::debug!(+ "[dispatch][debounce] typing start failed on {}: {error}",+ target.name()+ );+ }+ });+ }+ }+ }+ Ok((_, None)) => {}+ Err(error) => log_join_error("admission", Some(error)),+ }+ }+ Some(joined) = turns.join_next_with_id(), if !turns.is_empty() => {+ let id = match joined {+ Ok((id, ())) => id,+ Err(error) => {+ let id = error.id();+ log_join_error("turn", Some(error));+ id+ }+ };+ if let Some(key) = turn_keys.remove(&id) {+ debouncer.complete(&key);+ }}+ _ = tokio::time::sleep_until(wakeup.unwrap_or_else(Instant::now)), if wakeup.is_some() => {}}- }- while let Some(result) = workers.join_next().await {- if let Err(error) = result {- tracing::warn!("channel dispatch worker failed: {error}");++ let now = Instant::now();+ if !inbound_open && admitting.is_empty() {+ debouncer.flush_all(now);}+ ready.extend(debouncer.poll_ready(now));+ }+}++fn log_join_error(stage: &str, error: Option<tokio::task::JoinError>) {+ if let Some(error) = error {+ tracing::warn!("channel dispatch {stage} worker failed: {error}");}}diff --git a/crates/openhuman-core/src/channels/runtime/startup/start_channels.rs b/crates/openhuman-core/src/channels/runtime/startup/start_channels.rsindex 6ea0cb8505..4bd704df8c 100644--- a/crates/openhuman-core/src/channels/runtime/startup/start_channels.rs+++ b/crates/openhuman-core/src/channels/runtime/startup/start_channels.rs@@ -295,7 +295,12 @@ async fn start_channels_inner(mut config: Config, session: CancellationToken) ->tokio::select! {biased;_ = session.cancelled() => {},- _ = run_message_dispatch_loop(rx, runtime_ctx, max_in_flight_messages) => {},+ _ = run_message_dispatch_loop(+ rx,+ runtime_ctx,+ max_in_flight_messages,+ config.inbound_debounce.clone(),+ ) => {},}if let Some(runtime) = module_runtime.as_mut() {runtime.close().await?;diff --git a/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs b/crates/openhuman-core/src/channels/tests/runtime_dispatch.rsindex 260427a951..dd2e0dcd36 100644--- a/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs+++ b/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs@@ -176,7 +176,13 @@ async fn message_dispatch_processes_messages_in_parallel() {.unwrap();drop(tx);- run_message_dispatch_loop(rx, parallel_ctx, 2).await;+ run_message_dispatch_loop(+ rx,+ parallel_ctx,+ 2,+ crate::config::InboundDebounceConfig::disabled(),+ )+ .await;assert_eq!(peak_in_flight.load(Ordering::SeqCst), 2);let sent_messages = parallel_channel.sent_messages.lock().await;diff --git a/crates/openhuman-core/src/config/mod.rs b/crates/openhuman-core/src/config/mod.rsindex b58926774d..69a9cadaa0 100644--- a/crates/openhuman-core/src/config/mod.rs+++ b/crates/openhuman-core/src/config/mod.rs@@ -71,6 +71,7 @@ pub use schema::{// Kept as a separate re-export (issue #4117) so the large alphabetized group// above stays byte-identical and rustfmt-stable.pub use schema::RequiredOutputContract;+pub use schema::{InboundDebounceConfig, InboundDebounceOverride};pub use schemas::{all_controller_schemas as all_config_controller_schemas,all_registered_controllers as all_config_registered_controllers,