Skip to content

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

fix(background_delivery): keep recovered drains from overlapping a busy turn

Steven Enamakel27173ee8f7a4
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index e555f0736c..699e0d54ec 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -216,9 +216,20 @@ pub(crate) fn claim_recovery(workspace_dir: &Path) -> bool {
.insert(workspace_dir.to_path_buf())
}
-/// Let `workspace_dir` be recovered again (a profile re-leased after release).
+/// Let `workspace_dir` be recovered again (a profile re-leased after release),
+/// and drop its cached router when nothing is using it so the next scan replays
+/// the log another holder may have appended to. A router still in use stays,
+/// so a log never has two writers.
pub(crate) fn forget_recovery(workspace_dir: &Path) {
- state().recovered_workspaces.remove(workspace_dir);
+ let mut st = state();
+ st.recovered_workspaces.remove(workspace_dir);
+ let idle = st
+ .routers
+ .get(workspace_dir)
+ .is_some_and(|e| Arc::strong_count(e) == 1 && Arc::strong_count(&e.router) == 1);
+ if idle {
+ st.routers.remove(workspace_dir);
+ }
}
/// Remember that `session_id` is a turn on `thread_id`.
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index 424ecdcc04..7cf9106d7b 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -353,6 +353,16 @@ fn schedule_delivery_inner(thread_id: String, delay: Duration, wait_idle: bool)
polls += 1;
tokio::time::sleep(RECOVERY_BUSY_POLL).await;
}
+ if is_busy(&thread_id) {
+ // Still running past the ceiling: leave the record pending and
+ // wait again rather than overlap the user's turn.
+ log::debug!(
+ "[background_delivery] recovered drain still busy; waiting again \
+ thread_id={thread_id}"
+ );
+ schedule_delivery_inner(thread_id, RECOVERY_BUSY_POLL, true);
+ return;
+ }
}
try_deliver(thread_id).await;
});
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery_slots_tests.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery_slots_tests.rs
index 79dc1f50af..7d095c420c 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery_slots_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery_slots_tests.rs
@@ -180,7 +180,8 @@ async fn desktop_keys_are_the_bare_ids() {
let desktop = CoreContext::for_test(DomainSet::full(), None);
let ws = TestWorkspace::new();
let thread = unique("t-desk");
- let session = web_session(&thread);
+ let session = unique("desk-session");
+ background_completions::note_session_thread(&session, &thread);
let turn = in_scope(&desktop, || TurnBusy::start(&session));
assert_eq!(turn.key, session);
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 f80107e49c..37853deb96 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs
@@ -681,6 +681,20 @@ async fn boot_recovery_schedules_a_delivery_for_every_thread_with_undelivered_re
);
}
+#[tokio::test]
+async fn recover_on_open_rescans_a_workspace_already_recovered() {
+ let _g = test_guard().await;
+ let ws = workspace();
+ let w = ws.path();
+ record(w, "bd-open", "sub-1", "left pending", "thread-open-resc").await;
+ forget_workspace_for_test(w);
+ scheduled_for_test().lock().expect("scheduled").clear();
+
+ assert_eq!(recover_on_boot(w), 1);
+ assert_eq!(recover_on_boot(w), 0, "boot recovery claims once");
+ assert_eq!(recover_on_open(w), 1, "a re-open scans again");
+}
+
#[tokio::test]
async fn a_cancelled_turn_does_not_leave_the_thread_busy() {
// A cooperatively cancelled turn publishes no terminal event; Stop clears it.