Skip to content

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

test(subagent_host): split terminal lifecycle tests into their own module

Steven Enamakelb3c76a357d0e
diff --git a/crates/openhuman-core/src/agent/subagent_host/lifecycle_terminal_tests.rs b/crates/openhuman-core/src/agent/subagent_host/lifecycle_terminal_tests.rs
new file mode 100644
index 0000000000..afa41573e0
--- /dev/null
+++ b/crates/openhuman-core/src/agent/subagent_host/lifecycle_terminal_tests.rs
@@ -0,0 +1,286 @@
+use super::*;
+
+#[tokio::test]
+async fn driver_cancel_uses_the_typed_terminal_for_lossy_idle_sessions() {
+ use crate::agent::orchestration::subagent_sessions::{
+ cancel_driver_job_by_task, mark_finished, upsert_running, DurableSubagentStatus,
+ SubagentSessionSelector, SubagentSessionStore, SubagentSessionUpsert,
+ };
+
+ let directory = tempfile::tempdir().unwrap();
+ let workspace = directory.path();
+ let store = SubagentSessionStore::new(workspace.to_path_buf());
+ let jobs = tinyagents_orchestration::subagent::SubAgentJobRegistry::default();
+ let persistence = OpenHumanPersistence::new(
+ workspace.join(".openhuman/subagent_checkpoints"),
+ );
+
+ for (task_id, terminal, expected) in [
+ (
+ "typed-completed",
+ SubagentOutcomeKind::Completed,
+ Some(tinyagents_orchestration::subagent::FinishedOutcome::Completed),
+ ),
+ (
+ "typed-incomplete",
+ SubagentOutcomeKind::Incomplete(
+ tinyagents_orchestration::subagent::SubagentIncomplete::new("iteration cap"),
+ ),
+ Some(tinyagents_orchestration::subagent::FinishedOutcome::Failed),
+ ),
+ ] {
+ let key = SubagentTaskKey {
+ root_run_id: "root-typed-finished".into(),
+ parent_run_id: "parent-typed-finished".into(),
+ thread_id: Some("thread-typed-finished".into()),
+ task_id: task_id.into(),
+ };
+ let session = upsert_running(
+ &store,
+ SubagentSessionUpsert {
+ selector: SubagentSessionSelector {
+ parent_session: "parent-typed-finished".into(),
+ parent_thread_id: key.thread_id.clone(),
+ agent_id: "researcher".into(),
+ model: None,
+ sandbox_mode: "read_only".into(),
+ action_root: None,
+ task_key: task_id.into(),
+ },
+ display_name: None,
+ task_title: task_id.into(),
+ worker_thread_id: None,
+ task_id: task_id.into(),
+ lifecycle_key: Some(key.clone()),
+ },
+ None,
+ )
+ .unwrap();
+ let mut outcome = SubagentOutcome::cancelled(task_id);
+ outcome.status = terminal;
+ persistence.record_terminal(&key, &outcome, None).await.unwrap();
+ mark_finished(
+ &store,
+ &session.subagent_session_id,
+ task_id,
+ &match task_id {
+ "typed-completed" => SubagentRunStatus::Completed,
+ _ => SubagentRunStatus::Incomplete {
+ reason: "iteration cap".into(),
+ },
+ },
+ Vec::new(),
+ )
+ .unwrap();
+ assert_eq!(
+ store.load().unwrap()[0].status,
+ DurableSubagentStatus::Idle,
+ "both typed terminal outcomes share the legacy Idle session state"
+ );
+
+ let selected = cancel_driver_job_by_task(
+ &store,
+ workspace,
+ &jobs,
+ task_id,
+ None,
+ )
+ .unwrap()
+ .expect("typed terminal remains reportable");
+ assert_eq!(selected.already_finished, expected, "{task_id}");
+ }
+}
+
+#[tokio::test]
+async fn continuation_replaces_only_the_pause_it_loaded_and_stale_duplicate_gets_winner() {
+ let directory = tempfile::tempdir().unwrap();
+ let persistence = OpenHumanPersistence::new(directory.path().to_path_buf());
+ let key = key();
+ let original = SubagentResume::default();
+ assert_eq!(
+ persistence
+ .save_pause(persisted_pause(key.clone(), "first question", None))
+ .await
+ .unwrap(),
+ SubagentPausePersistenceDisposition::Inserted
+ );
+
+ assert_eq!(
+ persistence
+ .save_pause(persisted_pause(
+ key.clone(),
+ "replacement question",
+ Some(original.clone()),
+ ))
+ .await
+ .unwrap(),
+ SubagentPausePersistenceDisposition::Replaced
+ );
+ assert_eq!(
+ persistence
+ .save_pause(persisted_pause(
+ key.clone(),
+ "stale duplicate question",
+ Some(original),
+ ))
+ .await
+ .unwrap(),
+ SubagentPausePersistenceDisposition::Existing
+ );
+
+ let winner = persistence.load_pause(&key).await.unwrap().unwrap();
+ assert!(matches!(
+ winner.status,
+ SubagentOutcomeKind::AwaitingInput(SubagentPause { ref reason, .. })
+ if reason == "replacement question"
+ ));
+}
+
+#[tokio::test]
+async fn task_id_index_retains_scoped_collisions_and_terminal_does_not_reopen_pause() {
+ let directory = tempfile::tempdir().unwrap();
+ let persistence = OpenHumanPersistence::new(directory.path().to_path_buf());
+ let first = scoped_key("root-a", "parent-a", Some("thread-a"));
+ let second = scoped_key("root-b", "parent-b", Some("thread-b"));
+ assert_eq!(
+ persistence
+ .save_pause(persisted_pause(first.clone(), "first", None))
+ .await
+ .unwrap(),
+ SubagentPausePersistenceDisposition::Inserted
+ );
+ assert_eq!(
+ persistence
+ .save_pause(persisted_pause(second.clone(), "second", None))
+ .await
+ .unwrap(),
+ SubagentPausePersistenceDisposition::Inserted
+ );
+ assert!(load_subagent_checkpoint(directory.path(), "task").is_err());
+
+ let mut terminal = SubagentOutcome::cancelled("task");
+ terminal.status = SubagentOutcomeKind::Completed;
+ assert_eq!(
+ persistence
+ .record_terminal(&first, &terminal, None)
+ .await
+ .unwrap(),
+ tinyagents_orchestration::subagent::SubagentTerminalPersistenceDisposition::PauseExisting
+ );
+ assert_eq!(
+ persistence
+ .record_terminal(&first, &terminal, Some(&SubagentResume::default()))
+ .await
+ .unwrap(),
+ tinyagents_orchestration::subagent::SubagentTerminalPersistenceDisposition::Inserted
+ );
+ assert_eq!(
+ load_subagent_checkpoint(directory.path(), "task")
+ .unwrap()
+ .task_key,
+ Some(second.clone())
+ );
+ assert!(persistence.load_pause(&first).await.unwrap().is_none());
+ assert!(persistence.load_terminal(&first).await.unwrap().is_some());
+}
+
+#[tokio::test]
+async fn terminal_and_pause_race_leave_only_one_authoritative_state() {
+ let directory = tempfile::tempdir().unwrap();
+ let persistence =
+ std::sync::Arc::new(OpenHumanPersistence::new(directory.path().to_path_buf()));
+ let key = key();
+ let mut terminal = SubagentOutcome::cancelled("task");
+ terminal.status = SubagentOutcomeKind::Completed;
+ let (pause, terminal_write) = tokio::join!(
+ persistence.save_pause(persisted_pause(key.clone(), "question", None)),
+ persistence.record_terminal(&key, &terminal, None),
+ );
+ assert!(pause.is_ok());
+ assert_eq!(
+ terminal_write.unwrap(),
+ tinyagents_orchestration::subagent::SubagentTerminalPersistenceDisposition::PauseExisting
+ );
+ assert!(persistence.load_terminal(&key).await.unwrap().is_none());
+ assert!(persistence.load_pause(&key).await.unwrap().is_some());
+ assert!(load_subagent_checkpoint(directory.path(), "task").is_ok());
+}
+
+#[tokio::test]
+async fn host_coalescer_keeps_the_leader_winner_and_cancels_only_the_observer() {
+ let entry = HostInFlight::new();
+ let key = key();
+ let cancelled = tinyagents_harness::CancellationToken::new();
+ cancelled.cancel();
+
+ let observer = entry
+ .wait(&cancelled, &key, "observer-agent")
+ .await
+ .unwrap()
+ .expect("cancelled follower reports an outcome");
+ assert!(matches!(observer.status, SubagentRunStatus::Cancelled));
+ assert_eq!(
+ observer.persistence_disposition,
+ tinyagents_orchestration::subagent::SubagentPersistenceDisposition::ObserverCancelled
+ );
+
+ let winner = SubagentRunOutcome {
+ task_id: key.task_id.clone(),
+ agent_id: "leader-agent".into(),
+ output: "durable winner".into(),
+ iterations: 3,
+ elapsed: std::time::Duration::from_millis(42),
+ mode: SubagentMode::Typed,
+ status: SubagentRunStatus::Completed,
+ final_history: Vec::new(),
+ usage: SubagentUsage::default(),
+ artifact_paths: Vec::new(),
+ persistence_disposition:
+ tinyagents_orchestration::subagent::SubagentPersistenceDisposition::TerminalInserted,
+ };
+ entry.complete(Some(winner)).await;
+
+ let active = tinyagents_harness::CancellationToken::new();
+ let follower = entry
+ .wait(&active, &key, "other-agent")
+ .await
+ .unwrap()
+ .expect("leader result remains available");
+ assert_eq!(follower.output, "durable winner");
+ assert_eq!(follower.agent_id, "leader-agent");
+ assert_eq!(follower.elapsed, std::time::Duration::from_millis(42));
+ assert_eq!(
+ follower.persistence_disposition,
+ tinyagents_orchestration::subagent::SubagentPersistenceDisposition::TerminalExisting
+ );
+}
+
+#[tokio::test]
+async fn each_agent_dedupes_its_own_in_flight_sub_agents() {
+ use crate::core::runtime::agent_scope::test_agent_context;
+ use crate::core::runtime::{context::CoreContext, DomainSet};
+ use std::sync::Arc;
+
+ let root = CoreContext::for_test(DomainSet::full(), None);
+ let alpha = test_agent_context(&root, "alpha");
+ let beta = test_agent_context(&root, "beta");
+
+ let alpha_map = {
+ let scope_ctx = crate::core::runtime::DispatchCtx::new(Arc::clone(&alpha));
+
+ host_in_flight(&scope_ctx)
+ };
+ let beta_map = {
+ let scope_ctx = crate::core::runtime::DispatchCtx::new(beta);
+
+ host_in_flight(&scope_ctx)
+ };
+ let alpha_again = {
+ let scope_ctx = crate::core::runtime::DispatchCtx::new(alpha);
+
+ host_in_flight(&scope_ctx)
+ };
+
+ assert!(!Arc::ptr_eq(&alpha_map, &beta_map));
+ assert!(Arc::ptr_eq(&alpha_map, &alpha_again));
+}
diff --git a/crates/openhuman-core/src/agent/subagent_host/lifecycle_tests.rs b/crates/openhuman-core/src/agent/subagent_host/lifecycle_tests.rs
index 69f59511d9..d7169d4a34 100644
--- a/crates/openhuman-core/src/agent/subagent_host/lifecycle_tests.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/lifecycle_tests.rs
@@ -473,287 +473,5 @@ async fn driver_inline_projection_returns_the_durable_winner_not_a_local_loser()
);
}
-#[tokio::test]
-async fn driver_cancel_uses_the_typed_terminal_for_lossy_idle_sessions() {
- use crate::agent::orchestration::subagent_sessions::{
- cancel_driver_job_by_task, mark_finished, upsert_running, DurableSubagentStatus,
- SubagentSessionSelector, SubagentSessionStore, SubagentSessionUpsert,
- };
-
- let directory = tempfile::tempdir().unwrap();
- let workspace = directory.path();
- let store = SubagentSessionStore::new(workspace.to_path_buf());
- let jobs = tinyagents_orchestration::subagent::SubAgentJobRegistry::default();
- let persistence = OpenHumanPersistence::new(
- workspace.join(".openhuman/subagent_checkpoints"),
- );
-
- for (task_id, terminal, expected) in [
- (
- "typed-completed",
- SubagentOutcomeKind::Completed,
- Some(tinyagents_orchestration::subagent::FinishedOutcome::Completed),
- ),
- (
- "typed-incomplete",
- SubagentOutcomeKind::Incomplete(
- tinyagents_orchestration::subagent::SubagentIncomplete::new("iteration cap"),
- ),
- Some(tinyagents_orchestration::subagent::FinishedOutcome::Failed),
- ),
- ] {
- let key = SubagentTaskKey {
- root_run_id: "root-typed-finished".into(),
- parent_run_id: "parent-typed-finished".into(),
- thread_id: Some("thread-typed-finished".into()),
- task_id: task_id.into(),
- };
- let session = upsert_running(
- &store,
- SubagentSessionUpsert {
- selector: SubagentSessionSelector {
- parent_session: "parent-typed-finished".into(),
- parent_thread_id: key.thread_id.clone(),
- agent_id: "researcher".into(),
- model: None,
- sandbox_mode: "read_only".into(),
- action_root: None,
- task_key: task_id.into(),
- },
- display_name: None,
- task_title: task_id.into(),
- worker_thread_id: None,
- task_id: task_id.into(),
- lifecycle_key: Some(key.clone()),
- },
- None,
- )
- .unwrap();
- let mut outcome = SubagentOutcome::cancelled(task_id);
- outcome.status = terminal;
- persistence.record_terminal(&key, &outcome, None).await.unwrap();
- mark_finished(
- &store,
- &session.subagent_session_id,
- task_id,
- &match task_id {
- "typed-completed" => SubagentRunStatus::Completed,
- _ => SubagentRunStatus::Incomplete {
- reason: "iteration cap".into(),
- },
- },
- Vec::new(),
- )
- .unwrap();
- assert_eq!(
- store.load().unwrap()[0].status,
- DurableSubagentStatus::Idle,
- "both typed terminal outcomes share the legacy Idle session state"
- );
-
- let selected = cancel_driver_job_by_task(
- &store,
- workspace,
- &jobs,
- task_id,
- None,
- )
- .unwrap()
- .expect("typed terminal remains reportable");
- assert_eq!(selected.already_finished, expected, "{task_id}");
- }
-}
-
-#[tokio::test]
-async fn continuation_replaces_only_the_pause_it_loaded_and_stale_duplicate_gets_winner() {
- let directory = tempfile::tempdir().unwrap();
- let persistence = OpenHumanPersistence::new(directory.path().to_path_buf());
- let key = key();
- let original = SubagentResume::default();
- assert_eq!(
- persistence
- .save_pause(persisted_pause(key.clone(), "first question", None))
- .await
- .unwrap(),
- SubagentPausePersistenceDisposition::Inserted
- );
-
- assert_eq!(
- persistence
- .save_pause(persisted_pause(
- key.clone(),
- "replacement question",
- Some(original.clone()),
- ))
- .await
- .unwrap(),
- SubagentPausePersistenceDisposition::Replaced
- );
- assert_eq!(
- persistence
- .save_pause(persisted_pause(
- key.clone(),
- "stale duplicate question",
- Some(original),
- ))
- .await
- .unwrap(),
- SubagentPausePersistenceDisposition::Existing
- );
-
- let winner = persistence.load_pause(&key).await.unwrap().unwrap();
- assert!(matches!(
- winner.status,
- SubagentOutcomeKind::AwaitingInput(SubagentPause { ref reason, .. })
- if reason == "replacement question"
- ));
-}
-
-#[tokio::test]
-async fn task_id_index_retains_scoped_collisions_and_terminal_does_not_reopen_pause() {
- let directory = tempfile::tempdir().unwrap();
- let persistence = OpenHumanPersistence::new(directory.path().to_path_buf());
- let first = scoped_key("root-a", "parent-a", Some("thread-a"));
- let second = scoped_key("root-b", "parent-b", Some("thread-b"));
- assert_eq!(
- persistence
- .save_pause(persisted_pause(first.clone(), "first", None))
- .await
- .unwrap(),
- SubagentPausePersistenceDisposition::Inserted
- );
- assert_eq!(
- persistence
- .save_pause(persisted_pause(second.clone(), "second", None))
- .await
- .unwrap(),
- SubagentPausePersistenceDisposition::Inserted
- );
- assert!(load_subagent_checkpoint(directory.path(), "task").is_err());
-
- let mut terminal = SubagentOutcome::cancelled("task");
- terminal.status = SubagentOutcomeKind::Completed;
- assert_eq!(
- persistence
- .record_terminal(&first, &terminal, None)
- .await
- .unwrap(),
- tinyagents_orchestration::subagent::SubagentTerminalPersistenceDisposition::PauseExisting
- );
- assert_eq!(
- persistence
- .record_terminal(&first, &terminal, Some(&SubagentResume::default()))
- .await
- .unwrap(),
- tinyagents_orchestration::subagent::SubagentTerminalPersistenceDisposition::Inserted
- );
- assert_eq!(
- load_subagent_checkpoint(directory.path(), "task")
- .unwrap()
- .task_key,
- Some(second.clone())
- );
- assert!(persistence.load_pause(&first).await.unwrap().is_none());
- assert!(persistence.load_terminal(&first).await.unwrap().is_some());
-}
-
-#[tokio::test]
-async fn terminal_and_pause_race_leave_only_one_authoritative_state() {
- let directory = tempfile::tempdir().unwrap();
- let persistence =
- std::sync::Arc::new(OpenHumanPersistence::new(directory.path().to_path_buf()));
- let key = key();
- let mut terminal = SubagentOutcome::cancelled("task");
- terminal.status = SubagentOutcomeKind::Completed;
- let (pause, terminal_write) = tokio::join!(
- persistence.save_pause(persisted_pause(key.clone(), "question", None)),
- persistence.record_terminal(&key, &terminal, None),
- );
- assert!(pause.is_ok());
- assert_eq!(
- terminal_write.unwrap(),
- tinyagents_orchestration::subagent::SubagentTerminalPersistenceDisposition::PauseExisting
- );
- assert!(persistence.load_terminal(&key).await.unwrap().is_none());
- assert!(persistence.load_pause(&key).await.unwrap().is_some());
- assert!(load_subagent_checkpoint(directory.path(), "task").is_ok());
-}
-
-#[tokio::test]
-async fn host_coalescer_keeps_the_leader_winner_and_cancels_only_the_observer() {
- let entry = HostInFlight::new();
- let key = key();
- let cancelled = tinyagents_harness::CancellationToken::new();
- cancelled.cancel();
-
- let observer = entry
- .wait(&cancelled, &key, "observer-agent")
- .await
- .unwrap()
- .expect("cancelled follower reports an outcome");
- assert!(matches!(observer.status, SubagentRunStatus::Cancelled));
- assert_eq!(
- observer.persistence_disposition,
- tinyagents_orchestration::subagent::SubagentPersistenceDisposition::ObserverCancelled
- );
-
- let winner = SubagentRunOutcome {
- task_id: key.task_id.clone(),
- agent_id: "leader-agent".into(),
- output: "durable winner".into(),
- iterations: 3,
- elapsed: std::time::Duration::from_millis(42),
- mode: SubagentMode::Typed,
- status: SubagentRunStatus::Completed,
- final_history: Vec::new(),
- usage: SubagentUsage::default(),
- artifact_paths: Vec::new(),
- persistence_disposition:
- tinyagents_orchestration::subagent::SubagentPersistenceDisposition::TerminalInserted,
- };
- entry.complete(Some(winner)).await;
-
- let active = tinyagents_harness::CancellationToken::new();
- let follower = entry
- .wait(&active, &key, "other-agent")
- .await
- .unwrap()
- .expect("leader result remains available");
- assert_eq!(follower.output, "durable winner");
- assert_eq!(follower.agent_id, "leader-agent");
- assert_eq!(follower.elapsed, std::time::Duration::from_millis(42));
- assert_eq!(
- follower.persistence_disposition,
- tinyagents_orchestration::subagent::SubagentPersistenceDisposition::TerminalExisting
- );
-}
-
-#[tokio::test]
-async fn each_agent_dedupes_its_own_in_flight_sub_agents() {
- use crate::core::runtime::agent_scope::test_agent_context;
- use crate::core::runtime::{context::CoreContext, DomainSet};
- use std::sync::Arc;
-
- let root = CoreContext::for_test(DomainSet::full(), None);
- let alpha = test_agent_context(&root, "alpha");
- let beta = test_agent_context(&root, "beta");
-
- let alpha_map = {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(Arc::clone(&alpha));
-
- host_in_flight(&scope_ctx)
- };
- let beta_map = {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(beta);
-
- host_in_flight(&scope_ctx)
- };
- let alpha_again = {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(alpha);
-
- host_in_flight(&scope_ctx)
- };
-
- assert!(!Arc::ptr_eq(&alpha_map, &beta_map));
- assert!(Arc::ptr_eq(&alpha_map, &alpha_again));
-}
+#[path = "lifecycle_terminal_tests.rs"]
+mod terminal_tests;