Skip to content

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

feat(orchestration): add background completions release tests

Steven Enamakel8f49914cf48e
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index b4ae08bc8e..863d55b339 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -706,3 +706,7 @@ pub(crate) fn recover_pending_threads(ctx: &DispatchCtx, workspace_dir: &Path) -
#[cfg(test)]
#[path = "background_completions_tests.rs"]
mod tests;
+
+#[cfg(test)]
+#[path = "background_completions_release_tests.rs"]
+mod release_tests;
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions_release_tests.rs b/crates/openhuman-core/src/agent/orchestration/background_completions_release_tests.rs
new file mode 100644
index 0000000000..4bb91cf551
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions_release_tests.rs
@@ -0,0 +1,111 @@
+//! Releasing the completion routers (`release_all`) when the core server stops.
+
+use super::*;
+use crate::core::runtime::DispatchCtx;
+
+/// The context these tests dispatch under: the calling task's own.
+fn cx() -> DispatchCtx {
+ DispatchCtx::ambient()
+}
+
+fn workspace() -> TestWorkspace {
+ TestWorkspace::new()
+}
+
+#[tokio::test]
+async fn release_all_closes_every_router_and_the_log_stays_replayable() {
+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;
+ let ws = workspace();
+ let w = ws.path();
+ record_completion(
+ &cx(),
+ w,
+ "sess-rel",
+ "sub-1",
+ "researcher",
+ "kept on disk",
+ Some("thread-rel".into()),
+ )
+ .await;
+
+ assert!(release_all().await >= 1);
+ assert!(
+ !state().routers.contains_key(w),
+ "the router (and its log handle) is dropped"
+ );
+ assert!(
+ workspace_for_thread(&cx(), "thread-rel").is_none(),
+ "process-local maps are cleared"
+ );
+
+ // The next boot finds the undelivered completion from the log alone.
+ assert_eq!(recover_pending_threads(&cx(), w), ["thread-rel"]);
+}
+
+#[tokio::test(start_paused = true)]
+async fn release_all_waits_for_a_router_still_in_use() {
+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;
+ let ws = workspace();
+ let w = ws.path();
+ let in_use = router_for_workspace(w);
+ let releasing = tokio::spawn(release_all());
+ // Well inside the drain window: still waiting on the held router.
+ tokio::time::sleep(std::time::Duration::from_secs(1)).await;
+ assert!(
+ !releasing.is_finished(),
+ "waits while a delivery holds the router"
+ );
+ drop(in_use);
+ tokio::time::sleep(std::time::Duration::from_millis(200)).await;
+ assert!(releasing.await.unwrap() >= 1);
+}
+
+#[tokio::test(start_paused = true)]
+async fn release_all_gives_up_waiting_after_the_drain_window() {
+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;
+ let ws = workspace();
+ let w = ws.path();
+ let _held_forever = router_for_workspace(w);
+ let released = release_all().await;
+ assert!(
+ released >= 1,
+ "stragglers are dropped once the window passes"
+ );
+ assert!(!state().routers.contains_key(w));
+}
+
+#[tokio::test]
+async fn release_all_keeps_the_stop_and_delete_gates() {
+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;
+ let ws = workspace();
+ let w = ws.path();
+ discard_pending_for_thread(&cx(), "thread-keep-stop");
+ state().deleted_threads.insert("thread-keep-del".into());
+ release_all().await;
+
+ // A detached child finishing after the release is still refused.
+ record_completion(
+ &cx(),
+ w,
+ "s",
+ "sub-1",
+ "r",
+ "x",
+ Some("thread-keep-stop".into()),
+ )
+ .await;
+ record_completion(
+ &cx(),
+ w,
+ "s",
+ "sub-2",
+ "r",
+ "x",
+ Some("thread-keep-del".into()),
+ )
+ .await;
+ assert!(pending_ids(w, "thread-keep-stop").is_empty());
+ assert!(pending_ids(w, "thread-keep-del").is_empty());
+ resume_stopped_thread(&cx(), "thread-keep-stop");
+ state().deleted_threads.remove("thread-keep-del");
+}
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs b/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
index b2576aca74..7357cf9338 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
@@ -1,14 +1,13 @@
use super::*;
-
-/// The context these tests dispatch under: the calling task's own.
-fn cx() -> crate::core::runtime::DispatchCtx {
- crate::core::runtime::DispatchCtx::ambient()
-}
-
use crate::core::runtime::DispatchCtx;
use tinyagents_tasks::CompletionStatus;
use tokio::sync::MutexGuard;
+/// The context these tests dispatch under: the calling task's own.
+fn cx() -> DispatchCtx {
+ DispatchCtx::ambient()
+}
+
/// Serializes every test that touches the process-wide router registry. We reuse
/// the crate-wide `TEST_ENV_LOCK` because `clear_all` sweeps every open
/// workspace and is also reachable from the `threads::ops` purge test (which
@@ -624,104 +623,6 @@ fn a_late_record_for_a_deleted_thread_is_dropped_by_the_durable_marker_alone() {
assert!(pending_ids(w, "thread-dm").is_empty());
}
-#[tokio::test]
-async fn release_all_closes_every_router_and_the_log_stays_replayable() {
- let _guard = crate::config::TEST_ENV_LOCK.lock().await;
- let ws = workspace();
- let w = ws.path();
- record_completion(
- &cx(),
- w,
- "sess-rel",
- "sub-1",
- "researcher",
- "kept on disk",
- Some("thread-rel".into()),
- )
- .await;
-
- assert!(release_all().await >= 1);
- assert!(
- !state().routers.contains_key(w),
- "the router (and its log handle) is dropped"
- );
- assert!(
- workspace_for_thread(&cx(), "thread-rel").is_none(),
- "process-local maps are cleared"
- );
-
- // The next boot finds the undelivered completion from the log alone.
- assert_eq!(recover_pending_threads(&cx(), w), ["thread-rel"]);
-}
-
-#[tokio::test(start_paused = true)]
-async fn release_all_waits_for_a_router_still_in_use() {
- let _guard = crate::config::TEST_ENV_LOCK.lock().await;
- let ws = workspace();
- let w = ws.path();
- let in_use = router_for_workspace(w);
- let releasing = tokio::spawn(release_all());
- // Well inside the drain window: still waiting on the held router.
- tokio::time::sleep(std::time::Duration::from_secs(1)).await;
- assert!(
- !releasing.is_finished(),
- "waits while a delivery holds the router"
- );
- drop(in_use);
- tokio::time::sleep(std::time::Duration::from_millis(200)).await;
- assert!(releasing.await.unwrap() >= 1);
-}
-
-#[tokio::test(start_paused = true)]
-async fn release_all_gives_up_waiting_after_the_drain_window() {
- let _guard = crate::config::TEST_ENV_LOCK.lock().await;
- let ws = workspace();
- let w = ws.path();
- let _held_forever = router_for_workspace(w);
- let released = release_all().await;
- assert!(
- released >= 1,
- "stragglers are dropped once the window passes"
- );
- assert!(!state().routers.contains_key(w));
-}
-
-#[tokio::test]
-async fn release_all_keeps_the_stop_and_delete_gates() {
- let _guard = crate::config::TEST_ENV_LOCK.lock().await;
- let ws = workspace();
- let w = ws.path();
- discard_pending_for_thread(&cx(), "thread-keep-stop");
- state().deleted_threads.insert("thread-keep-del".into());
- release_all().await;
-
- // A detached child finishing after the release is still refused.
- record_completion(
- &cx(),
- w,
- "s",
- "sub-1",
- "r",
- "x",
- Some("thread-keep-stop".into()),
- )
- .await;
- record_completion(
- &cx(),
- w,
- "s",
- "sub-2",
- "r",
- "x",
- Some("thread-keep-del".into()),
- )
- .await;
- assert!(pending_ids(w, "thread-keep-stop").is_empty());
- assert!(pending_ids(w, "thread-keep-del").is_empty());
- resume_stopped_thread(&cx(), "thread-keep-stop");
- state().deleted_threads.remove("thread-keep-del");
-}
-
#[test]
fn a_completion_for_a_removed_workspace_does_not_recreate_it() {
let _guard = test_guard();