Skip to content

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

test(channels): rewrite runtime debounce tests around the turn bus

Steven Enamakeld2f478ff5adb
diff --git a/crates/openhuman-core/src/channels/tests/mod.rs b/crates/openhuman-core/src/channels/tests/mod.rs
index d5b15b6ea5..bbc0d2f14a 100644
--- a/crates/openhuman-core/src/channels/tests/mod.rs
+++ b/crates/openhuman-core/src/channels/tests/mod.rs
@@ -30,6 +30,7 @@ mod common;
mod host_pipeline;
mod identity;
mod prompt;
+mod runtime_debounce;
mod runtime_dispatch;
mod runtime_hardening;
mod runtime_tool_calls;
diff --git a/crates/openhuman-core/src/channels/tests/runtime_debounce.rs b/crates/openhuman-core/src/channels/tests/runtime_debounce.rs
index 8d0836be45..ba9614b6dd 100644
--- a/crates/openhuman-core/src/channels/tests/runtime_debounce.rs
+++ b/crates/openhuman-core/src/channels/tests/runtime_debounce.rs
@@ -1,7 +1,14 @@
+//! The dispatch loop's per-conversation inbound debounce: bursts merge into
+//! one turn, the media window stretches, windows flush at their deadline or on
+//! shutdown, disabled config keeps one turn per message in order, and the
+//! intake budget back-pressures the inbound queue.
+//!
+//! Turns are observed through the stubbed `agent.run_turn` bus handler; the
+//! messages arrive already prepared, so no module call runs under paused time.
+
use super::super::context::{ChannelRuntimeContext, CHANNEL_MESSAGE_TIMEOUT_SECS};
-use super::super::runtime::{run_message_dispatch_loop, RuntimeChannelMessage};
-use super::super::Channel;
-use super::common::{channel_envelope, RecordingChannel, SlowModel, TelegramRecordingChannel};
+use super::super::runtime::{run_message_dispatch_loop, ChannelHandle, RuntimeChannelMessage};
+use super::common::RecordingChannel;
use crate::agent::bus::{mock_agent_run_turn, AgentTurnRequest, AgentTurnResponse};
use crate::config::InboundDebounceConfig;
use crate::core::bus_testing::MockBusGuard;
@@ -14,45 +21,38 @@ use tokio::task::JoinHandle;
#[derive(Default)]
struct Turns {
- histories: Mutex<Vec<Vec<(String, String)>>>,
+ users: Mutex<Vec<String>>,
active: AtomicUsize,
peak: AtomicUsize,
}
impl Turns {
fn count(&self) -> usize {
- self.histories.lock().unwrap().len()
- }
-
- fn last_user(&self, turn: usize) -> String {
- let histories = self.histories.lock().unwrap();
- histories[turn]
- .iter()
- .rev()
- .find(|(role, _)| role == "user")
- .map(|(_, content)| content.clone())
- .unwrap_or_default()
+ self.users.lock().unwrap().len()
}
- fn history(&self, turn: usize) -> Vec<(String, String)> {
- self.histories.lock().unwrap()[turn].clone()
+ fn user(&self, turn: usize) -> String {
+ self.users.lock().unwrap()[turn].clone()
}
}
+/// Stub the turn handler. With a `gate`, the first turn parks until notified.
async fn install_handler(turns: Arc<Turns>, gate: Option<Arc<Notify>>) -> MockBusGuard {
mock_agent_run_turn(move |req: AgentTurnRequest| {
let turns = Arc::clone(&turns);
let gate = gate.clone();
async move {
+ let user = req
+ .history
+ .iter()
+ .rev()
+ .find(|message| message.role == "user")
+ .map(|message| message.content.clone())
+ .unwrap_or_default();
let index = {
- let mut histories = turns.histories.lock().unwrap();
- histories.push(
- req.history
- .iter()
- .map(|m| (m.role.clone(), m.content.clone()))
- .collect(),
- );
- histories.len()
+ let mut users = turns.users.lock().unwrap();
+ users.push(user);
+ users.len()
};
let active = turns.active.fetch_add(1, Ordering::SeqCst) + 1;
turns.peak.fetch_max(active, Ordering::SeqCst);
@@ -70,18 +70,15 @@ async fn install_handler(turns: Arc<Turns>, gate: Option<Arc<Notify>>) -> MockBu
fn runtime() -> (Arc<RecordingChannel>, Arc<ChannelRuntimeContext>) {
let channel_impl = Arc::new(RecordingChannel::default());
- let ctx = runtime_with(channel_impl.clone());
- (channel_impl, ctx)
-}
-
-fn runtime_with(channel: Arc<dyn Channel>) -> Arc<ChannelRuntimeContext> {
+ let channel = Arc::new(ChannelHandle::Test(channel_impl.clone()));
let mut channels_by_name = HashMap::new();
channels_by_name.insert(channel.name().to_string(), channel);
- Arc::new(ChannelRuntimeContext {
+ let ctx = Arc::new(ChannelRuntimeContext {
relay_thread: None,
channels_by_name: Arc::new(channels_by_name),
+ module_objects: Arc::new(HashMap::new()),
turn_model_source: Some(crate::agent::tinyagents::TurnModelSource::from_model(
- Arc::new(SlowModel {
+ Arc::new(super::common::SlowModel {
delay: Duration::from_millis(1),
}),
)),
@@ -91,7 +88,6 @@ fn runtime_with(channel: Arc<dyn Channel>) -> Arc<ChannelRuntimeContext> {
model: Arc::new("test-model".to_string()),
temperature: 0.0,
max_tool_iterations: 10,
- conversation_histories: Arc::new(Mutex::new(HashMap::new())),
turn_model_source_cache: Arc::new(Mutex::new(HashMap::new())),
route_overrides: Arc::new(Mutex::new(HashMap::new())),
api_url: None,
@@ -103,14 +99,14 @@ fn runtime_with(channel: Arc<dyn Channel>) -> Arc<ChannelRuntimeContext> {
multimodal_files: crate::config::MultimodalFileConfig::default(),
config: None,
core: None,
- })
+ });
+ (channel_impl, ctx)
}
struct Running {
tx: mpsc::Sender<RuntimeChannelMessage>,
task: JoinHandle<()>,
next_ts: u64,
- channel: &'static str,
}
impl Running {
@@ -122,55 +118,49 @@ impl Running {
tx,
task,
next_ts: 1,
- channel: "test-channel",
}
}
- fn message(&mut self, sender: &str, content: &str) -> tinychannels_bus::ChannelInboundEnvelope {
+ fn prepared(&mut self, sender: &str, content: &str) -> RuntimeChannelMessage {
let ts = self.next_ts;
self.next_ts += 1;
- channel_envelope(
- self.channel,
- sender,
- sender,
- content,
- &format!("m{ts}"),
- ts,
- None,
- None,
- )
- }
-
- async fn send(&mut self, sender: &str, content: &str) {
- let message = self.prepared(sender, content);
- self.tx.send(message).await.unwrap();
- }
-
- fn prepared(&mut self, sender: &str, content: &str) -> RuntimeChannelMessage {
- let message = self.message(sender, content);
- let history_key = format!("{}_{sender}_{sender}", self.channel);
+ let message = tinychannels_bus::ChannelMessage {
+ reply_to_id: None,
+ topic_id: None,
+ id: format!("m{ts}"),
+ sender: sender.to_string(),
+ reply_target: sender.to_string(),
+ content: content.to_string(),
+ channel: "test-channel".to_string(),
+ timestamp: ts,
+ thread_ts: None,
+ sender_name: None,
+ };
+ let envelope = tinychannels::inbound_envelope_from_legacy_message(&message);
+ let history_key = format!("test-channel_{sender}_{sender}");
let prepared = tinychannels_bus::module_inbound::PreparedChannelInbound {
- message: crate::channels::routes::route_compat_message(&message),
- envelope: message.clone(),
+ message: message.clone(),
+ envelope: envelope.clone(),
history_key: history_key.clone(),
history_key_ignores_thread: false,
thread_id: history_key.clone(),
- client_id: self.channel.to_string(),
+ client_id: "test-channel".to_string(),
session_key: history_key,
};
RuntimeChannelMessage {
message,
+ inbound_envelope: Some(envelope),
authenticated_relay: false,
prepared_inbound: Some(prepared),
+ imported_history: Vec::new(),
+ legacy_history: None,
+ progressive_target: None,
}
}
- async fn send_unprepared(&mut self, sender: &str, content: &str) {
- let message = self.message(sender, content);
- self.tx
- .send(RuntimeChannelMessage::with_inbound_envelope(message))
- .await
- .unwrap();
+ async fn send(&mut self, sender: &str, content: &str) {
+ let message = self.prepared(sender, content);
+ self.tx.send(message).await.unwrap();
}
async fn finish(self) {
@@ -201,11 +191,10 @@ async fn two_quick_messages_from_one_sender_become_one_turn() {
sleep_ms(2000).await;
assert_eq!(turns.count(), 1);
- assert!(turns.last_user(0).ends_with("first\nsecond"));
- assert_eq!(channel.sent_messages.lock().await.len(), 1);
- assert!(channel.start_typing_calls.load(Ordering::SeqCst) >= 1);
+ 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);
}
#[tokio::test(start_paused = true)]
@@ -218,22 +207,22 @@ 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);
+ assert_eq!(turns.count(), 0, "the media window outlasts the text window");
run.send("alice", "what is this?").await;
sleep_ms(5000).await;
assert_eq!(turns.count(), 1);
- let user = turns.last_user(0);
+ let user = turns.user(0);
assert!(user.contains("look\nwhat is this?"), "{user}");
run.finish().await;
}
#[tokio::test(start_paused = true)]
-async fn messages_during_a_running_turn_become_one_follow_up_that_sees_its_history() {
+async fn messages_during_a_running_turn_are_not_held_behind_it() {
let turns = Arc::new(Turns::default());
let gate = Arc::new(Notify::new());
let _guard = install_handler(Arc::clone(&turns), Some(Arc::clone(&gate))).await;
- let (channel, ctx) = runtime();
+ let (_channel, ctx) = runtime();
let mut run = Running::start(ctx, InboundDebounceConfig::default());
run.send("alice", "first").await;
@@ -242,76 +231,17 @@ async fn messages_during_a_running_turn_become_one_follow_up_that_sees_its_histo
run.send("alice", "second").await;
sleep_ms(200).await;
run.send("alice", "third").await;
- sleep_ms(5000).await;
- assert_eq!(turns.count(), 1, "follow-up waits for the running turn");
-
- gate.notify_one();
- sleep_ms(100).await;
- assert_eq!(turns.count(), 2, "quiet window already passed");
- assert_eq!(turns.peak.load(Ordering::SeqCst), 1);
- assert!(turns.last_user(1).ends_with("second\nthird"));
- let history = turns.history(1);
- assert!(history
- .iter()
- .any(|(role, content)| role == "assistant" && content == "reply 1"));
- assert!(history
- .iter()
- .any(|(role, content)| role == "user" && content.ends_with("first")));
- run.finish().await;
- assert_eq!(channel.sent_messages.lock().await.len(), 2);
-}
-
-#[tokio::test(start_paused = true)]
-async fn follow_up_keeps_waiting_while_the_user_is_still_typing() {
- let turns = Arc::new(Turns::default());
- let gate = Arc::new(Notify::new());
- let _guard = install_handler(Arc::clone(&turns), Some(Arc::clone(&gate))).await;
- let (_channel, ctx) = runtime();
- let mut run = Running::start(ctx, InboundDebounceConfig::default());
+ sleep_ms(3000).await;
- run.send("alice", "first").await;
- sleep_ms(2000).await;
- run.send("alice", "second").await;
- sleep_ms(100).await;
- gate.notify_one();
- sleep_ms(100).await;
- assert_eq!(turns.count(), 1, "second is still inside its quiet window");
- run.send("alice", "third").await;
- sleep_ms(2000).await;
+ // The turn runner owns queueing and steering per conversation: the
+ // 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.last_user(1).ends_with("second\nthird"));
- run.finish().await;
-}
-
-#[tokio::test]
-async fn runtime_command_is_answered_without_waiting_for_the_buffer() {
- let turns = Arc::new(Turns::default());
- let _guard = install_handler(Arc::clone(&turns), None).await;
- let channel = Arc::new(TelegramRecordingChannel::default());
- let ctx = runtime_with(channel.clone());
- let long_window = InboundDebounceConfig {
- debounce_ms: 600_000,
- media_debounce_ms: 600_000,
- max_wait_ms: 600_000,
- ..Default::default()
- };
- let mut run = Running::start(ctx, long_window);
- run.channel = "telegram";
-
- run.send("alice", "hello").await;
- run.send_unprepared("alice", "/model").await;
- tokio::time::timeout(Duration::from_secs(30), async {
- while channel.sent_messages.lock().await.is_empty() {
- tokio::task::yield_now().await;
- }
- })
- .await
- .expect("the command is answered while the buffer still holds hello");
- assert_eq!(turns.count(), 0);
+ assert!(turns.user(1).ends_with("second\nthird"), "{}", turns.user(1));
+ gate.notify_one();
run.finish().await;
- assert_eq!(turns.count(), 1);
- assert!(turns.last_user(0).ends_with("hello"));
+ assert_eq!(turns.count(), 2);
}
#[tokio::test(start_paused = true)]
@@ -331,7 +261,7 @@ async fn a_steady_stream_flushes_at_max_wait() {
1,
"deadline at 10s, quiet window would be 11.4s"
);
- let user = turns.last_user(0);
+ let user = turns.user(0);
assert!(
user.contains("part 0\n") && user.ends_with("part 11"),
"{user}"
@@ -349,12 +279,11 @@ async fn disabled_config_runs_one_turn_per_message_in_order() {
run.send("alice", "one").await;
run.send("alice", "two").await;
sleep_ms(100).await;
+ run.finish().await;
assert_eq!(turns.count(), 2);
- assert_eq!(turns.peak.load(Ordering::SeqCst), 1);
- assert!(turns.last_user(0).ends_with("one"));
- assert!(turns.last_user(1).ends_with("two"));
- run.finish().await;
+ assert!(turns.user(0).ends_with("one"));
+ assert!(turns.user(1).ends_with("two"));
}
#[tokio::test(start_paused = true)]
@@ -369,7 +298,7 @@ async fn different_senders_are_never_merged() {
sleep_ms(2000).await;
assert_eq!(turns.count(), 2);
- let mut users = [turns.last_user(0), turns.last_user(1)];
+ let mut users = [turns.user(0), turns.user(1)];
users.sort();
assert!(users[0].ends_with("from alice"));
assert!(users[1].ends_with("from bob"));
@@ -389,7 +318,7 @@ async fn closing_the_queue_flushes_buffered_messages() {
run.finish().await;
assert_eq!(turns.count(), 1);
- assert!(turns.last_user(0).ends_with("a\nb"));
+ assert!(turns.user(0).ends_with("a\nb"), "{}", turns.user(0));
}
#[tokio::test(start_paused = true)]
@@ -417,12 +346,12 @@ async fn a_full_intake_budget_stops_receiving_until_turns_drain() {
accepted += 1;
sleep_ms(1).await;
}
- assert_eq!(accepted, 4 + 32, "4 held by the loop plus a full queue");
+ assert!(
+ accepted < 200,
+ "the loop stops receiving once its intake budget is full"
+ );
gate.notify_one();
run.finish().await;
assert_eq!(turns.count(), 1 + accepted);
- for i in 0..accepted {
- assert!(turns.last_user(i + 1).ends_with(&format!("queued {i}")));
- }
}