Skip to content

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

fix(background-completions): keep thread gate through stop until next user turn

Steven Enamakeldc3926c6cead
diff --git a/app/src/features/conversations/Conversations.tsx b/app/src/features/conversations/Conversations.tsx
index f161008b16..726b7a2e4b 100644
--- a/app/src/features/conversations/Conversations.tsx
+++ b/app/src/features/conversations/Conversations.tsx
@@ -884,6 +884,7 @@ const Conversations = ({
// clear the timer every time the user switched threads, which is exactly the
// watchdog this PR exists to arm.
useEffect(() => {
+ isMountedRef.current = true;
const timers = sendingTimeoutsRef.current;
return () => {
isMountedRef.current = false;
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 42ab307779..476689813c 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -126,8 +126,7 @@ impl QueueState {
}
}
- fn finish_stop(&mut self, thread_id: &str, task_ids: &[String]) {
- self.stopped_threads.remove(thread_id);
+ fn finish_stop(&mut self, task_ids: &[String]) {
for task_id in task_ids {
if self.stopped_tasks.insert(task_id.clone()) {
self.stopped_task_order.push_back(task_id.clone());
@@ -140,6 +139,10 @@ impl QueueState {
}
}
+ fn resume_thread(&mut self, thread_id: &str) {
+ self.stopped_threads.remove(thread_id);
+ }
+
/// 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) {
@@ -380,15 +383,28 @@ pub(crate) fn discard_pending_for_thread(thread_id: &str) -> usize {
/// Complete a Stop operation after its registered children have been aborted.
///
-/// `discard_pending_for_thread` installs the thread gate first. This function
-/// replaces it with task-specific tombstones, allowing later turns on the
-/// thread while still rejecting any old child that reaches completion after
-/// Tokio observes its cooperative cancellation.
+/// The thread gate stays in place until [`resume_stopped_thread`] starts a new
+/// user turn. That closes the spawn/register race: a child that was spawned
+/// before Stop but registered after the registry sweep still cannot enqueue a
+/// completion. Task tombstones additionally protect that stopped generation
+/// after the next turn reopens the thread.
pub(crate) fn finish_stop_for_thread(thread_id: &str, task_ids: &[String]) {
let mut state = queue()
.lock()
.expect("background_completions queue poisoned");
- state.finish_stop(thread_id, task_ids);
+ state.finish_stop(task_ids);
+}
+
+/// Reopen a thread's completion gate for a newly accepted user turn.
+///
+/// A Stop gate deliberately outlives registry cancellation, because a detached
+/// child may be between `tokio::spawn` and `running_subagents::register` when
+/// Stop is pressed. New task ids remain distinct from the stopped generation.
+pub(crate) fn resume_stopped_thread(thread_id: &str) {
+ queue()
+ .lock()
+ .expect("background_completions queue poisoned")
+ .resume_thread(thread_id);
}
fn remove_pending_for_thread(state: &mut QueueState, thread_id: &str) -> usize {
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 c114c2b74a..9e90a49c78 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions_tests.rs
@@ -402,8 +402,8 @@ fn discard_pending_for_thread_blocks_late_results_until_the_next_turn() {
);
assert_eq!(pending_count("sess-stop"), 0);
- // Completing Stop replaces the thread gate with task-specific tombstones,
- // so later turns can run while this stopped child remains rejected.
+ // Completing Stop keeps the thread gate until a new user turn begins, so a
+ // child that registers after the cancellation sweep is still rejected.
finish_stop_for_thread("thread-stop-live", &["sub-stop-later".into()]);
record_completion(
"sess-stop",
@@ -413,6 +413,8 @@ fn discard_pending_for_thread_blocks_late_results_until_the_next_turn() {
Some("thread-stop-live".into()),
);
assert_eq!(pending_count("sess-stop"), 0);
+
+ resume_stopped_thread("thread-stop-live");
record_completion(
"sess-stop",
"sub-stop-new-turn",
diff --git a/crates/openhuman-core/src/agent/registry/agents/orchestrator/prompt_tests_session_routing_tests.rs b/crates/openhuman-core/src/agent/registry/agents/orchestrator/prompt_tests_session_routing_tests.rs
index b36fc877af..2d0dbf73b4 100644
--- a/crates/openhuman-core/src/agent/registry/agents/orchestrator/prompt_tests_session_routing_tests.rs
+++ b/crates/openhuman-core/src/agent/registry/agents/orchestrator/prompt_tests_session_routing_tests.rs
@@ -113,9 +113,7 @@ fn prompt_routes_workflow_authoring_to_the_builder_not_use_skill() {
"orchestrator prompt must carry the workflow routing rule"
);
assert!(
- ARCHETYPE.contains(
- "skill `workflows` (`build_workflow` to author, `discover_workflows` to find)"
- ),
+ ARCHETYPE.contains("spawn the `workflow_builder` agent with `spawn_async_subagent`"),
"the rule must name the delegate to call"
);
diff --git a/crates/openhuman-core/src/web_chat/ops/start_chat.rs b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
index 6b8db06143..7d712258f8 100644
--- a/crates/openhuman-core/src/web_chat/ops/start_chat.rs
+++ b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
@@ -61,6 +61,11 @@ pub async fn start_chat(
return Err("message is required".to_string());
}
+ // A fresh user request is the explicit boundary after Stop. Keep the gate
+ // installed through registry cancellation so a child registering late
+ // cannot deliver a result into the stopped generation.
+ crate::agent::orchestration::background_completions::resume_stopped_thread(&thread_id);
+
// [pdf/image-attach fix] Process attachments at ingress, BEFORE the message is
// injection-scanned, persisted to history/JSONL, or auto-saved to the memory
// store. Otherwise a multi-MB base64 data URI floods every upstream stage