Skip to content

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

feat(agent): add background completion notice tests

Steven Enamakel71d7df542e09
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 3bba23ab53..6a84ed5c3a 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -682,6 +682,29 @@ pub(crate) fn forget_workspace_for_test(workspace_dir: &Path) {
st.session_order.clear();
}
+/// A temporary workspace that forgets its router when dropped, so the
+/// process-wide registry does not accumulate entries for deleted directories.
+#[cfg(test)]
+pub(crate) struct TestWorkspace(tempfile::TempDir);
+
+#[cfg(test)]
+impl TestWorkspace {
+ pub(crate) fn new() -> Self {
+ Self(tempfile::tempdir().expect("tempdir"))
+ }
+
+ pub(crate) fn path(&self) -> &Path {
+ self.0.path()
+ }
+}
+
+#[cfg(test)]
+impl Drop for TestWorkspace {
+ fn drop(&mut self) {
+ forget_workspace_for_test(self.0.path());
+ }
+}
+
#[cfg(test)]
#[path = "background_completions_tests.rs"]
mod tests;
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 670054ea8a..dedbc61936 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
@@ -11,8 +11,8 @@ fn test_guard() -> MutexGuard<'static, ()> {
crate::config::TEST_ENV_LOCK.blocking_lock()
}
-fn workspace() -> tempfile::TempDir {
- tempfile::tempdir().expect("tempdir")
+fn workspace() -> TestWorkspace {
+ TestWorkspace::new()
}
fn record(ws: &Path, session: &str, task: &str, summary: &str, thread: Option<&str>) {
@@ -285,7 +285,7 @@ fn record_awaiting_input_queues_a_framed_needs_input_for_delivery() {
let pending = pending_for(w, "thread-ask");
assert_eq!(pending.len(), 1);
assert_eq!(
- BackgroundAgentOutcome::from_status(pending[0].status),
+ BackgroundAgentOutcome::of(&pending[0]),
BackgroundAgentOutcome::AwaitingInput
);
assert!(pending[0].result.text.contains("which repo?"));
@@ -309,7 +309,7 @@ fn the_outcome_survives_a_claim() {
.claim_pending("thread-out", usize::MAX)
.unwrap();
assert_eq!(
- BackgroundAgentOutcome::from_status(claimed[0].status),
+ BackgroundAgentOutcome::of(&claimed[0]),
BackgroundAgentOutcome::Failed
);
}
@@ -462,3 +462,71 @@ fn undelivered_completions_are_found_again_after_a_restart() {
);
assert!(router_for_thread("thread-boot").is_some());
}
+
+#[test]
+fn a_delete_survives_a_restart_and_is_not_lifted_by_a_new_spawn() {
+ let _guard = test_guard();
+ let ws = workspace();
+ let w = ws.path();
+ record(w, "sess-dr", "sub-1", "x", Some("thread-del-restart"));
+ assert_eq!(discard_for_thread(w, "thread-del-restart"), 1);
+
+ // A restart: memory is gone, the durable markers are not. A late child for
+ // the deleted thread must be aborted rather than treated as a user returning
+ // to a stopped thread, and its result must stay dropped.
+ forget_workspace_for_test(w);
+ assert!(mark_stopped_task_if_thread_stopped(w, "thread-del-restart", "sub-late"));
+ record(w, "sess-dr", "sub-late", "stale", Some("thread-del-restart"));
+ assert!(pending_ids(w, "thread-del-restart").is_empty());
+}
+
+#[test]
+fn in_memory_gates_drop_a_result_even_without_a_durable_marker() {
+ let _guard = test_guard();
+ let ws = workspace();
+ let w = ws.path();
+ // The gates are what protects the process if the durable cancel write fails.
+ state().deleted_threads.insert("thread-gate-del".into());
+ state().stopped_threads.insert("thread-gate-stop".into());
+ record(w, "sess-g", "sub-1", "x", Some("thread-gate-del"));
+ record(w, "sess-g", "sub-2", "y", Some("thread-gate-stop"));
+ assert!(pending_ids(w, "thread-gate-del").is_empty());
+ assert!(pending_ids(w, "thread-gate-stop").is_empty());
+ state().deleted_threads.remove("thread-gate-del");
+ state().stopped_threads.remove("thread-gate-stop");
+}
+
+#[test]
+fn clear_all_also_withdraws_a_completion_a_delivery_has_leased() {
+ let _guard = test_guard();
+ let ws = workspace();
+ let w = ws.path();
+ record(w, "sess-l", "sub-1", "x", Some("thread-leased"));
+ let router = router_for_workspace(w);
+ // A delivery claimed it: the record is leased but still pending in the store.
+ assert_eq!(router.claim_pending("thread-leased", usize::MAX).unwrap().len(), 1);
+
+ assert_eq!(clear_all(w), 1);
+
+ assert!(pending_ids(w, "thread-leased").is_empty());
+ assert_eq!(router.mark_delivered(&["sub-1"]).unwrap(), 0, "nothing left to settle");
+}
+
+#[test]
+fn the_session_cache_evicts_its_oldest_mapping_only() {
+ let _guard = test_guard();
+ for i in 0..(SESSION_THREADS_CAP + 5) {
+ note_session_thread(&format!("evict-sess-{i}"), "evict-thread");
+ }
+ assert_eq!(thread_for_session("evict-sess-0"), None, "oldest evicted");
+ assert_eq!(
+ thread_for_session(&format!("evict-sess-{}", SESSION_THREADS_CAP + 4)).as_deref(),
+ Some("evict-thread"),
+ "newest kept"
+ );
+ assert_eq!(
+ thread_for_session(&format!("evict-sess-{}", SESSION_THREADS_CAP / 2)).as_deref(),
+ Some("evict-thread"),
+ "the cache is not cleared wholesale"
+ );
+}
diff --git a/crates/openhuman-core/src/agent/orchestration/completion_notice_tests.rs b/crates/openhuman-core/src/agent/orchestration/completion_notice_tests.rs
index 0d16b80b76..c91ef15858 100644
--- a/crates/openhuman-core/src/agent/orchestration/completion_notice_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/completion_notice_tests.rs
@@ -166,6 +166,10 @@ fn a_malicious_summary_cannot_forge_or_escape_its_envelope() {
!notice.contains("</background_agent_result>\n<background_agent_result id=\"forged\""),
"a summary must not be able to close its envelope and open a forged one; got: {notice}"
);
+ assert!(
+ !notice.contains("<background_agent_result id=\"forged\""),
+ "no forged result may open inside the persisted notice; got: {notice}"
+ );
assert_eq!(
notice.matches("</background_agent_result>").count(),
1,