Skip to content

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

fix(background_completions): handle late subagent registration after thread stop

Steven Enamakele3d12830154d
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 476689813c..0e56abb006 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -143,6 +143,14 @@ impl QueueState {
self.stopped_threads.remove(thread_id);
}
+ fn mark_stopped_task_if_thread_stopped(&mut self, thread_id: &str, task_id: &str) -> bool {
+ if !self.stopped_threads.contains(thread_id) {
+ return false;
+ }
+ self.finish_stop(&[task_id.to_string()]);
+ true
+ }
+
/// Tombstone `task_id` so a completion that records after the parent
/// collected it inline is dropped rather than delivered again.
fn tombstone_collected(&mut self, task_id: &str) {
@@ -407,6 +415,18 @@ pub(crate) fn resume_stopped_thread(thread_id: &str) {
.resume_thread(thread_id);
}
+/// Record a child that registers while its parent thread is stopped.
+///
+/// Registration happens after the detached task is spawned. If Stop races that
+/// narrow interval, the registry sweep cannot see the child; marking its task
+/// id here keeps it rejected even after a later user turn reopens the thread.
+pub(crate) fn mark_stopped_task_if_thread_stopped(thread_id: &str, task_id: &str) -> bool {
+ queue()
+ .lock()
+ .expect("background_completions queue poisoned")
+ .mark_stopped_task_if_thread_stopped(thread_id, task_id)
+}
+
fn remove_pending_for_thread(state: &mut QueueState, thread_id: &str) -> usize {
let mut removed = 0;
for pending in state.pending.values_mut() {
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 9e90a49c78..10a187c9ef 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
@@ -414,7 +414,22 @@ fn discard_pending_for_thread_blocks_late_results_until_the_next_turn() {
);
assert_eq!(pending_count("sess-stop"), 0);
+ // A child that was spawned before Stop but registers after the registry
+ // sweep gets its own tombstone before the thread can be reopened.
+ assert!(mark_stopped_task_if_thread_stopped(
+ "thread-stop-live",
+ "sub-stop-registered-late"
+ ));
+
resume_stopped_thread("thread-stop-live");
+ record_completion(
+ "sess-stop",
+ "sub-stop-registered-late",
+ "researcher",
+ "late registration result",
+ Some("thread-stop-live".into()),
+ );
+ assert_eq!(pending_count("sess-stop"), 0);
record_completion(
"sess-stop",
"sub-stop-new-turn",
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
index 76424b9f8c..f1acf14141 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
@@ -127,6 +127,22 @@ pub(crate) fn register(
abort: AbortHandle,
status: watch::Receiver<SubagentStatus>,
) {
+ if let Some(thread_id) = parent_thread_id.as_deref()
+ && crate::agent::orchestration::background_completions::mark_stopped_task_if_thread_stopped(
+ thread_id, &task_id,
+ )
+ {
+ // Stop landed after the child was spawned but before this registry
+ // entry existed. The completion is tombstoned above; abort promptly so
+ // the detached work does not keep consuming resources either.
+ abort.abort();
+ log::debug!(
+ "[running_subagents] aborted late registration task_id={} thread_id={}",
+ task_id,
+ thread_id
+ );
+ }
+
// Typed lifecycle ledger: record the spawn and mirror the child's terminal
// status into the store via a lightweight watcher (issue #4249). Done before
// the entry is moved into the map so the metadata is still in scope.