Skip to content

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

fix(channels): keep debouncer keys until batches are handed off

Steven Enamakel76291676a236
diff --git a/crates/openhuman-core/src/agent/subagent_host/lifecycle_storage_adoption_tests.rs b/crates/openhuman-core/src/agent/subagent_host/lifecycle_storage_adoption_tests.rs
index 6e7bc4733b..18c7e0d645 100644
--- a/crates/openhuman-core/src/agent/subagent_host/lifecycle_storage_adoption_tests.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/lifecycle_storage_adoption_tests.rs
@@ -264,9 +264,10 @@ async fn a_detached_driver_uses_its_carried_actor_for_lifecycle_commits() {
.unwrap()
.expect("detached lifecycle write committed in its carried actor scope");
assert_eq!(durable.output, "detached actor result");
- let bob_dispatch =
- CoreContext::scope(bob.clone(), async { crate::core::runtime::DispatchCtx::ambient() })
- .await;
+ let bob_dispatch = CoreContext::scope(bob.clone(), async {
+ crate::core::runtime::DispatchCtx::ambient()
+ })
+ .await;
let other = super::super::persistence::OpenHumanPersistence::new(checkpoints)
.with_dispatch(bob_dispatch);
assert!(other.load_terminal(&key).await.unwrap().is_none());
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/inbound_batch_tests.rs b/crates/openhuman-core/src/channels/runtime/dispatch/inbound_batch_tests.rs
index cd0703bc88..a4c70f558d 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/inbound_batch_tests.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/inbound_batch_tests.rs
@@ -124,9 +124,18 @@ fn item_traits_classify_media_commands_blanks_and_owned_state() {
caption: None,
alt_text: None,
});
- assert!(typed_media.carries_media(), "a typed image stretches the window");
+ assert!(
+ typed_media.carries_media(),
+ "a typed image stretches the window"
+ );
let mut imported = queued(1, "m", 1, "hello");
- imported.inbound.imported_history = vec![tinyagents_session::transcript::TranscriptMessage::user("old")];
- assert!(!imported.mergeable(), "imported history belongs to one message");
+ imported.inbound.imported_history =
+ vec![tinyagents_session::transcript::TranscriptMessage::user(
+ "old",
+ )];
+ assert!(
+ !imported.mergeable(),
+ "imported history belongs to one message"
+ );
}
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
index fbb96507d6..11541da91a 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
@@ -102,11 +102,11 @@ 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;
pub(crate) use reset::reset_durable_channel_session;
-use admit::admit_runtime_message;
-pub(crate) use admit::AdmittedInbound;
pub(crate) use turn::process_channel_runtime_message;
use turn::run_admitted_turn;
@@ -120,8 +120,8 @@ pub(crate) async fn process_channel_message(ctx: Arc<ChannelRuntimeContext>, msg
/// commands and approval replies), concurrently but released in receive order.
/// Admitted messages are then debounced per conversation (`history_key`): a
/// burst becomes one turn. The debouncer only delays and merges intake. It
-/// releases a conversation as soon as its batch is handed to a turn, so the
-/// shared TinyAgents turn runner still owns queueing and steering for
+/// releases a conversation as soon as its batch is handed to the shared
+/// TinyAgents turn runner, which still owns queueing and steering for
/// messages that arrive while a turn runs. At most `max_in_flight_messages`
/// turns start concurrently, and the loop stops receiving while
/// [`intake_budget`] admitted messages are waiting for a turn, so a sustained
@@ -142,6 +142,7 @@ pub(crate) async fn run_message_dispatch_loop(
let mut admit_seqs: HashMap<tokio::task::Id, u64> = HashMap::new();
let mut admission_order = InOrder::new();
let mut turns = JoinSet::new();
+ let mut handoffs: JoinSet<String> = JoinSet::new();
let mut debouncer: KeyedDebouncer<String, QueuedInbound> = KeyedDebouncer::new();
let mut ready: VecDeque<(String, Vec<QueuedInbound>)> = VecDeque::new();
@@ -152,24 +153,34 @@ pub(crate) async fn run_message_dispatch_loop(
};
let batch_len = batch.len();
held = held.saturating_sub(batch_len);
- // The turn runner serializes and steers per conversation, so the
- // debouncer lets go of the key as soon as the batch is dispatched.
- debouncer.complete(&key);
- if let Some(admitted) = merge_channel_batch(batch) {
- tracing::debug!(
- channel = %admitted.msg.channel,
- message_id = %admitted.msg.id,
- batch_len,
- in_flight = turns.len() + 1,
- held,
- "[dispatch][debounce] starting turn"
- );
- turns.spawn(run_admitted_turn(Arc::clone(&ctx), admitted));
+ match merge_channel_batch(batch) {
+ None => debouncer.complete(&key),
+ Some(admitted) => {
+ tracing::debug!(
+ channel = %admitted.msg.channel,
+ message_id = %admitted.msg.id,
+ batch_len,
+ in_flight = turns.len() + 1,
+ held,
+ "[dispatch][debounce] starting turn"
+ );
+ // The turn runner owns serialization and steering per
+ // conversation. The debouncer keeps a key only until its batch
+ // is handed to the runner, so the next batch of that
+ // conversation is handed over after it, in receive order.
+ let (handoff, handed_off) = tokio::sync::oneshot::channel::<()>();
+ turns.spawn(run_admitted_turn(Arc::clone(&ctx), admitted, Some(handoff)));
+ handoffs.spawn(async move {
+ let _ = handed_off.await;
+ key
+ });
+ }
}
}
if !inbound_open
&& admitting.is_empty()
&& turns.is_empty()
+ && handoffs.is_empty()
&& ready.is_empty()
&& debouncer.is_empty()
{
@@ -213,6 +224,12 @@ pub(crate) async fn run_message_dispatch_loop(
}
}
}
+ Some(joined) = handoffs.join_next(), if !handoffs.is_empty() => {
+ match joined {
+ Ok(key) => debouncer.complete(&key),
+ Err(error) => log_join_error("handoff", Some(error)),
+ }
+ }
Some(joined) = turns.join_next(), if !turns.is_empty() => {
if let Err(error) = joined {
log_join_error("turn", Some(error));
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 f0cfd07821..8699527122 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -30,12 +30,20 @@ pub(crate) async fn process_channel_runtime_message(
runtime_msg: RuntimeChannelMessage,
) {
if let Some(admitted) = admit_runtime_message(Arc::clone(&ctx), runtime_msg).await {
- run_admitted_turn(ctx, admitted).await;
+ run_admitted_turn(ctx, admitted, None).await;
}
}
/// Run the agent turn for one admitted message (or one merged batch).
-pub(crate) async fn run_admitted_turn(ctx: Arc<ChannelRuntimeContext>, admitted: AdmittedInbound) {
+///
+/// `handoff` is released right before the turn is handed to the shared turn
+/// runner (or when the turn ends early), which lets the dispatch loop start
+/// the next batch of the same conversation after this one in receive order.
+pub(crate) async fn run_admitted_turn(
+ ctx: Arc<ChannelRuntimeContext>,
+ admitted: AdmittedInbound,
+ handoff: Option<tokio::sync::oneshot::Sender<()>>,
+) {
let AdmittedInbound {
msg,
envelope,
@@ -306,6 +314,7 @@ pub(crate) async fn run_admitted_turn(ctx: Arc<ChannelRuntimeContext>, admitted:
history_key: Some(history_key.clone()),
channel_session,
};
+ drop(handoff);
let execution = match selected_host {
Some(host) => {
let request_id = format!("{}:{}:{}", msg.channel, msg.id, uuid::Uuid::new_v4());
diff --git a/crates/openhuman-core/src/channels/tests/runtime_debounce.rs b/crates/openhuman-core/src/channels/tests/runtime_debounce.rs
index dda3feeaf9..022ce6832d 100644
--- a/crates/openhuman-core/src/channels/tests/runtime_debounce.rs
+++ b/crates/openhuman-core/src/channels/tests/runtime_debounce.rs
@@ -86,7 +86,11 @@ async fn install_handler(turns: Arc<Turns>, gate: Option<Gate>) -> MockBusGuard
let active = turns.active.fetch_add(1, Ordering::SeqCst) + 1;
turns.peak.fetch_max(active, Ordering::SeqCst);
if let Some(gate) = gate.filter(|gate| gate.all_turns || index == 1) {
- gate.permits.acquire().await.expect("gate stays open").forget();
+ gate.permits
+ .acquire()
+ .await
+ .expect("gate stays open")
+ .forget();
}
turns.active.fetch_sub(1, Ordering::SeqCst);
Ok(AgentTurnResponse::new(format!("reply {index}")))
@@ -218,7 +222,11 @@ async fn two_quick_messages_from_one_sender_become_one_turn() {
sleep_ms(2000).await;
assert_eq!(turns.count(), 1);
- assert!(turns.user(0).ends_with("first\nsecond"), "{}", turns.user(0));
+ assert!(
+ turns.user(0).ends_with("first\nsecond"),
+ "{}",
+ turns.user(0)
+ );
run.finish().await;
assert_eq!(turns.count(), 1);
assert_eq!(channel.sent_messages.lock().await.len(), 1);
@@ -234,7 +242,11 @@ async fn text_after_an_image_joins_it_through_the_media_window() {
run.send("alice", "[IMAGE:https://example.com/a.png]\nlook")
.await;
sleep_ms(3000).await;
- assert_eq!(turns.count(), 0, "the media window outlasts the text window");
+ assert_eq!(
+ turns.count(),
+ 0,
+ "the media window outlasts the text window"
+ );
run.send("alice", "what is this?").await;
sleep_ms(5000).await;
@@ -264,7 +276,11 @@ async fn messages_during_a_running_turn_are_not_held_behind_it() {
// debouncer only merges intake, so the next burst starts its own turn once
// its quiet window closes, without waiting for the running turn.
assert_eq!(turns.count(), 2);
- assert!(turns.user(1).ends_with("second\nthird"), "{}", turns.user(1));
+ assert!(
+ turns.user(1).ends_with("second\nthird"),
+ "{}",
+ turns.user(1)
+ );
gate.open();
run.finish().await;
diff --git a/crates/openhuman-core/src/cron/scheduler_headless_descendants_tests.rs b/crates/openhuman-core/src/cron/scheduler_headless_descendants_tests.rs
index 15abebd5fe..ae5aba372b 100644
--- a/crates/openhuman-core/src/cron/scheduler_headless_descendants_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler_headless_descendants_tests.rs
@@ -296,7 +296,8 @@ async fn scheduled_agent_timeout_cancels_its_blocked_child_before_history_settle
});
let _model =
crate::inference::provider::factory::test_provider_override::install_model(model.clone());
- let (_temp, _storage, _storage_guard, config, core, dispatch, job) = scheduled_agent_fixture(15);
+ let (_temp, _storage, _storage_guard, config, core, dispatch, job) =
+ scheduled_agent_fixture(15);
let inspect_dispatch = dispatch.clone();
let (child_started_tx, child_started_rx) = tokio::sync::oneshot::channel();
*model.child_started.lock().unwrap() = Some(child_started_tx);