Skip to content

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

test(web_chat): add turn activity tests

Steven Enamakel07782c79adb6
diff --git a/crates/openhuman-core/src/web_chat/turn_activity_tests.rs b/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
index ad55b85e86..c975c4960e 100644
--- a/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
+++ b/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
@@ -59,147 +59,97 @@ fn text(delta: &str) -> AgentProgress {
}
}
-const WINDOW: Duration = Duration::from_secs(900);
+use std::time::Duration;
#[test]
fn parse_idle_window_defaults_and_disables() {
- assert_eq!(parse_idle_window(None), Some(Duration::from_secs(900)));
- assert_eq!(parse_idle_window(Some("x")), Some(Duration::from_secs(900)));
- assert_eq!(
- parse_idle_window(Some(" 60 ")),
- Some(Duration::from_secs(60))
- );
- assert_eq!(parse_idle_window(Some("0")), None);
+ assert_eq!(parse_idle_window(None), Duration::from_secs(900));
+ assert_eq!(parse_idle_window(Some("x")), Duration::from_secs(900));
+ assert_eq!(parse_idle_window(Some(" 60 ")), Duration::from_secs(60));
+ // Disabled stands for a window no turn reaches.
+ assert!(parse_idle_window(Some("0")) > Duration::from_secs(365 * 24 * 3600));
}
-#[tokio::test(start_paused = true)]
-async fn every_event_restarts_the_idle_clock() {
- let activity = TurnActivity::new();
- tokio::time::advance(Duration::from_secs(300)).await;
- assert_eq!(
- activity.idle_for(Instant::now()),
- Some(Duration::from_secs(300))
- );
- activity.observe(&text("hi"));
- assert_eq!(activity.idle_for(Instant::now()), Some(Duration::ZERO));
+fn signal(tracker: &mut WorkTracker, event: &AgentProgress) -> Option<&'static str> {
+ tracker.observe(event).map(|signal| match signal {
+ TurnSignal::WorkStarted => "started",
+ TurnSignal::WorkFinished => "finished",
+ TurnSignal::Progress(_) => "progress",
+ })
}
-/// A provider retrying a failed call is silent for minutes (289s observed)
-/// while the turn is healthy: an open model call is never idle.
-#[tokio::test(start_paused = true)]
-async fn an_open_model_call_is_never_idle() {
- let activity = TurnActivity::new();
- activity.observe(&iteration_started());
- tokio::time::advance(Duration::from_secs(1_500)).await;
- assert_eq!(activity.idle_for(Instant::now()), None);
- activity.observe(&model_call_completed(None));
- assert_eq!(activity.idle_for(Instant::now()), Some(Duration::ZERO));
+/// A provider retrying a failed call is silent for minutes while the turn is
+/// healthy: an open model call is work in flight until it completes.
+#[test]
+fn an_open_model_call_is_work_until_it_completes() {
+ let mut tracker = WorkTracker::default();
+ assert_eq!(signal(&mut tracker, &iteration_started()), Some("started"));
+ assert_eq!(signal(&mut tracker, &text("hi")), None);
+ assert_eq!(
+ signal(&mut tracker, &model_call_completed(None)),
+ Some("finished")
+ );
}
-#[tokio::test(start_paused = true)]
-async fn a_subagent_model_call_does_not_close_the_parent_call() {
- let activity = TurnActivity::new();
- activity.observe(&iteration_started());
- activity.observe(&model_call_completed(Some("task-1")));
- assert_eq!(activity.idle_for(Instant::now()), None);
+#[test]
+fn a_subagent_model_call_does_not_close_the_parent_call() {
+ let mut tracker = WorkTracker::default();
+ signal(&mut tracker, &iteration_started());
+ assert_eq!(
+ signal(&mut tracker, &model_call_completed(Some("task-1"))),
+ None
+ );
}
/// A long, silent shell call is in flight, not idle, and parallel calls stay
/// open until the last one completes.
-#[tokio::test(start_paused = true)]
-async fn open_tool_calls_are_never_idle() {
- let activity = TurnActivity::new();
- activity.observe(&tool_started("a"));
- activity.observe(&tool_started("b"));
- tokio::time::advance(Duration::from_secs(1_500)).await;
- activity.observe(&tool_completed("a"));
- assert_eq!(activity.idle_for(Instant::now()), None);
- activity.observe(&tool_completed("b"));
- assert_eq!(activity.idle_for(Instant::now()), Some(Duration::ZERO));
-}
-
-#[tokio::test(start_paused = true)]
-async fn turn_completed_closes_everything_still_open() {
- let activity = TurnActivity::new();
- activity.observe(&iteration_started());
- activity.observe(&tool_started("a"));
- activity.observe(&AgentProgress::TurnCompleted {
- iterations: 1,
- stop: None,
- });
- assert_eq!(activity.idle_for(Instant::now()), Some(Duration::ZERO));
-}
-
-#[tokio::test(start_paused = true)]
-async fn watchdog_fires_after_the_window_of_silence() {
- let activity = TurnActivity::new();
- let idle = idle_watchdog(activity, WINDOW).await;
- assert!(idle >= WINDOW);
+#[test]
+fn open_tool_calls_are_work_until_the_last_one_completes() {
+ let mut tracker = WorkTracker::default();
+ assert_eq!(signal(&mut tracker, &tool_started("a")), Some("started"));
+ assert_eq!(signal(&mut tracker, &tool_started("b")), None);
+ assert_eq!(signal(&mut tracker, &tool_completed("a")), None);
+ assert_eq!(signal(&mut tracker, &tool_completed("b")), Some("finished"));
}
-/// The turn that motivated this: 53 model rounds over 15 minutes, one every
-/// ~17s, each with a tool call. It must outlive the old 900s backstop.
-#[tokio::test(start_paused = true)]
-async fn a_busy_turn_outlives_the_window() {
- let activity = TurnActivity::new();
- let watchdog = tokio::spawn(idle_watchdog(activity.clone(), WINDOW));
- for round in 0..120 {
- let call = format!("call-{round}");
- activity.observe(&iteration_started());
- tokio::time::sleep(Duration::from_secs(10)).await;
- activity.observe(&model_call_completed(None));
- activity.observe(&tool_started(&call));
- tokio::time::sleep(Duration::from_secs(7)).await;
- activity.observe(&tool_completed(&call));
- }
- // 120 rounds × 17s = 34 minutes.
- assert!(!watchdog.is_finished());
- watchdog.abort();
+/// The model call that asked for a tool returned: a tool starting hands the
+/// open work over without a gap.
+#[test]
+fn a_tool_start_closes_the_model_call_that_asked_for_it() {
+ let mut tracker = WorkTracker::default();
+ signal(&mut tracker, &iteration_started());
+ assert_eq!(signal(&mut tracker, &tool_started("a")), None);
+ assert_eq!(signal(&mut tracker, &tool_completed("a")), Some("finished"));
}
-#[tokio::test(start_paused = true)]
-async fn a_long_tool_call_holds_off_the_watchdog_until_it_closes() {
- let activity = TurnActivity::new();
- activity.observe(&tool_started("build"));
- let watchdog = tokio::spawn(idle_watchdog(activity.clone(), WINDOW));
- tokio::time::sleep(Duration::from_secs(1_500)).await;
- assert!(!watchdog.is_finished());
- activity.observe(&tool_completed("build"));
- let idle = watchdog.await.expect("watchdog task");
- assert!(idle >= WINDOW);
+/// A lost `ToolCallCompleted` does not leave the call open: the next model
+/// call proves the tool round finished.
+#[test]
+fn the_next_model_call_closes_a_tool_whose_completion_was_lost() {
+ let mut tracker = WorkTracker::default();
+ signal(&mut tracker, &tool_started("a"));
+ // `tool_completed("a")` is dropped under backpressure.
+ assert_eq!(signal(&mut tracker, &iteration_started()), None);
+ assert_eq!(
+ signal(&mut tracker, &model_call_completed(None)),
+ Some("finished")
+ );
}
-#[tokio::test]
-async fn tap_records_and_forwards_in_order() {
- let activity = TurnActivity::new();
- let (tx, rx) = mpsc::channel(4);
- let mut bridge_rx = tap(
- &crate::core::runtime::DispatchCtx::ambient(),
- activity.clone(),
- rx,
- 4,
- );
- tx.send(tool_started("a")).await.unwrap();
- tx.send(text("x")).await.unwrap();
- drop(tx);
- assert!(matches!(
- bridge_rx.recv().await,
- Some(AgentProgress::ToolCallStarted { .. })
- ));
- assert!(matches!(
- bridge_rx.recv().await,
- Some(AgentProgress::TextDelta { .. })
- ));
- assert!(
- bridge_rx.recv().await.is_none(),
- "closes with the agent side"
+#[test]
+fn turn_completed_closes_everything_still_open() {
+ let mut tracker = WorkTracker::default();
+ signal(&mut tracker, &iteration_started());
+ signal(&mut tracker, &tool_started("a"));
+ assert_eq!(
+ signal(&mut tracker, &AgentProgress::turn_completed(1)),
+ Some("finished")
);
- assert_eq!(activity.idle_for(Instant::now()), None, "call a still open");
}
-#[tokio::test]
-async fn stuck_point_names_where_the_turn_went_quiet() {
- let activity = TurnActivity::new();
+#[test]
+fn stuck_point_names_where_the_turn_went_quiet() {
+ let activity = TurnActivity::default();
activity.observe(&AgentProgress::TurnContent {
input: Some("hi".into()),
output: None,
@@ -242,7 +192,7 @@ async fn stuck_point_names_where_the_turn_went_quiet() {
#[test]
fn the_turn_context_is_what_the_turn_recorded() {
- let activity = TurnActivity::new();
+ let activity = TurnActivity::default();
assert!(activity.turn_context().0.is_none());
let config = Arc::new(crate::config::Config::default());
activity.remember_turn(config.clone(), Some("de".into()));
@@ -251,59 +201,3 @@ fn the_turn_context_is_what_the_turn_recorded() {
assert_eq!(locale.as_deref(), Some("de"));
}
-/// A lost `ToolCallCompleted` does not leave the call open: the next model
-/// call proves the tool round finished.
-#[tokio::test(start_paused = true)]
-async fn the_next_model_call_closes_a_tool_whose_completion_was_lost() {
- let activity = TurnActivity::new();
- activity.observe(&tool_started("a"));
- // `tool_completed("a")` is dropped under backpressure.
- activity.observe(&iteration_started());
- activity.observe(&model_call_completed(None));
- assert_eq!(activity.idle_for(Instant::now()), Some(Duration::ZERO));
-}
-
-/// A lost `ModelCallCompleted` does not leave the model call open: the tool
-/// call it requested proves it returned.
-#[tokio::test(start_paused = true)]
-async fn a_tool_call_closes_a_model_call_whose_completion_was_lost() {
- let activity = TurnActivity::new();
- activity.observe(&iteration_started());
- // `model_call_completed(None)` is dropped under backpressure.
- activity.observe(&tool_started("a"));
- activity.observe(&tool_completed("a"));
- assert_eq!(activity.idle_for(Instant::now()), Some(Duration::ZERO));
-}
-
-/// When a lost completion is the turn's last event, the call stops counting
-/// as in flight after the expiry, so the idle guard still stops the turn.
-#[tokio::test(start_paused = true)]
-async fn a_call_left_open_by_a_lost_completion_expires() {
- let activity = TurnActivity::new();
- activity.observe(&tool_started("a"));
- // `tool_completed("a")` is dropped, and the turn then goes silent.
- tokio::time::advance(OPEN_CALL_EXPIRY - Duration::from_secs(1)).await;
- assert_eq!(activity.idle_for(Instant::now()), None);
- tokio::time::advance(Duration::from_secs(1)).await;
- assert_eq!(activity.idle_for(Instant::now()), Some(OPEN_CALL_EXPIRY));
- let idle = idle_watchdog(activity, WINDOW).await;
- assert!(idle >= WINDOW);
-}
-
-/// A long shell call keeps reporting progress, so it stays active past the
-/// expiry of its open-call record.
-#[tokio::test(start_paused = true)]
-async fn a_long_call_that_reports_progress_stays_active_past_the_expiry() {
- let activity = TurnActivity::new();
- activity.observe(&tool_started("build"));
- let watchdog = tokio::spawn(idle_watchdog(activity.clone(), WINDOW));
- for _ in 0..(3_600 / 15) {
- tokio::time::sleep(Duration::from_secs(15)).await;
- activity.observe(&AgentProgress::ToolCallProgress {
- call_id: "build".into(),
- message: "running".into(),
- });
- }
- assert!(!watchdog.is_finished(), "an hour of progress beats");
- watchdog.abort();
-}