Skip to content

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

fix(agent): drop background completions before touching a removed workspace

Steven Enamakelf3c850957489
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index c38559bc24..2422cabdba 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -281,6 +281,14 @@ pub(crate) async fn record_outcome(
log::warn!("[background_completions] dropping headless completion task_id={task_id}");
return;
};
+ if !workspace_dir.is_dir() {
+ // The workspace was removed (a data reset) while the child ran; do not
+ // recreate it just to log a result nobody can receive.
+ log::warn!(
+ "[background_completions] dropping completion task_id={task_id}: workspace is gone"
+ );
+ return;
+ }
{
// The in-memory gates back up the router's durable cancelled-parent
// marker, so a failed `cancel_parent` write cannot let a late result in.
@@ -316,14 +324,6 @@ pub(crate) async fn record_outcome(
} else {
record
};
- if !workspace_dir.is_dir() {
- // The workspace was removed (a data reset) while the child ran; do not
- // recreate it just to log a result nobody can receive.
- log::warn!(
- "[background_completions] dropping completion task_id={task_id}: workspace is gone"
- );
- return;
- }
let router = router_for_workspace(workspace_dir);
let mut outcome_of_record = router
.record_with_retries(record.clone(), RECORD_RETRIES)
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 64c77725e2..6e931d542a 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
@@ -634,7 +634,10 @@ async fn release_all_waits_for_a_router_still_in_use() {
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");
+ 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);
@@ -647,7 +650,10 @@ async fn release_all_gives_up_waiting_after_the_drain_window() {
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!(
+ released >= 1,
+ "stragglers are dropped once the window passes"
+ );
assert!(!state().routers.contains_key(w));
}
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs
index 05e8de2cbc..acaef81409 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs
@@ -623,3 +623,66 @@ async fn a_dropped_delivery_releases_its_slot_and_lease() {
);
assert!(pending_for(w, "thread-drop").is_empty());
}
+
+#[tokio::test]
+async fn a_failed_turn_under_the_ceiling_asks_for_a_backoff_retry() {
+ // A checkout failure publishes no AgentError, so nothing else would retry a
+ // quiet thread; the loop hands back the delay to try again after.
+ let _g = test_guard().await;
+ let ws = workspace();
+ let w = ws.path();
+ record(w, "bd-retry", "sub-1", "alpha", "thread-retry").await;
+ let router = router_for_workspace(w);
+
+ let retry = try_deliver_with(
+ "thread-retry".into(),
+ router.clone(),
+ |_t, _n| async move { Err::<String, String>("session checkout failed".to_string()) },
+ |_t, _n| async move { unreachable!("one failure must not give up") },
+ )
+ .await;
+ assert_eq!(retry, Some(retry_backoff(1)));
+
+ let delivered = try_deliver_with(
+ "thread-retry".into(),
+ router,
+ |_t, _n| async move { Ok::<String, String>("presented".to_string()) },
+ |_t, _n| async move { unreachable!("a success must not give up") },
+ )
+ .await;
+ assert_eq!(delivered, None, "a delivered batch needs no retry");
+}
+
+#[test]
+fn the_retry_backoff_grows_and_is_capped() {
+ assert!(retry_backoff(2) > retry_backoff(1));
+ assert_eq!(retry_backoff(40), retry_backoff(5));
+}
+
+#[tokio::test]
+async fn boot_recovery_schedules_a_delivery_for_every_thread_with_undelivered_results() {
+ let _g = test_guard().await;
+ let ws = workspace();
+ let w = ws.path();
+ record(
+ w,
+ "bd-boot",
+ "sub-1",
+ "finished before the crash",
+ "thread-boot-sched",
+ )
+ .await;
+ forget_workspace_for_test(w);
+ scheduled_for_test().lock().expect("scheduled").clear();
+
+ assert_eq!(recover_on_boot(w), 1);
+ assert!(scheduled_for_test()
+ .lock()
+ .expect("scheduled")
+ .contains(&("thread-boot-sched".to_string(), RECOVERY_DELAY)));
+ assert_eq!(
+ recover_on_boot(w),
+ 0,
+ "a workspace is scanned once per process"
+ );
+}