Skip to content

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

fix(orchestration): replace thread gate with task tombstones on stop

Steven Enamakela39a0375d69c
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index c0d5285a5c..42ab307779 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -85,6 +85,12 @@ struct QueueState {
stopped_threads: HashSet<String>,
/// Insertion order for `stopped_threads`, used to bound the set.
stopped_order: VecDeque<String>,
+ /// Detached task ids stopped by the user. Once the thread gate is reopened
+ /// for a later turn, this keeps a straggling task from the old generation
+ /// from recording its completion.
+ stopped_tasks: HashSet<String>,
+ /// Insertion order for `stopped_tasks`, used to bound the set.
+ stopped_task_order: VecDeque<String>,
/// Task ids the parent already collected inline via `wait_subagent` and will
/// present in its own turn. A completion for a collected task is dropped by
/// [`record_completion`] (closing the wait/record ordering race) and any
@@ -120,8 +126,18 @@ impl QueueState {
}
}
- fn resume(&mut self, thread_id: &str) {
+ fn finish_stop(&mut self, thread_id: &str, task_ids: &[String]) {
self.stopped_threads.remove(thread_id);
+ for task_id in task_ids {
+ if self.stopped_tasks.insert(task_id.clone()) {
+ self.stopped_task_order.push_back(task_id.clone());
+ while self.stopped_task_order.len() > COLLECTED_TOMBSTONE_CAP {
+ if let Some(evicted) = self.stopped_task_order.pop_front() {
+ self.stopped_tasks.remove(&evicted);
+ }
+ }
+ }
+ }
}
/// Tombstone `task_id` so a completion that records after the parent
@@ -215,6 +231,13 @@ pub(crate) fn record_outcome(
);
return;
}
+ if state.stopped_tasks.contains(&entry.task_id) {
+ log::debug!(
+ "[background_completions] dropping completion task_id={} stopped by user",
+ entry.task_id
+ );
+ return;
+ }
let pending = state.pending.entry(parent_session).or_default();
if pending.iter().any(|c| c.task_id == entry.task_id) {
return;
@@ -337,9 +360,9 @@ pub(crate) fn discard_for_thread(thread_id: &str) -> usize {
/// The Stop-button counterpart of [`discard_for_thread`]: the user halted the
/// thread's work, so results that finished but were not yet delivered must not
/// start a fresh delivery turn behind their back. The thread itself stays
-/// alive; [`resume_for_thread`] reopens it when a later user turn begins, so
-/// sub-agents spawned by that turn deliver normally. Returns the number of
-/// queued completions removed.
+/// alive; after cancellation, old task ids are tombstoned while sub-agents
+/// spawned by later turns use new ids and deliver normally. Returns the number
+/// of queued completions removed.
pub(crate) fn discard_pending_for_thread(thread_id: &str) -> usize {
let mut state = queue()
.lock()
@@ -355,13 +378,17 @@ pub(crate) fn discard_pending_for_thread(thread_id: &str) -> usize {
removed
}
-/// Reopen a thread that was stopped by the user for work started by a later
-/// user request. Deleted-thread tombstones are deliberately not cleared.
-pub(crate) fn resume_for_thread(thread_id: &str) {
+/// 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.
+pub(crate) fn finish_stop_for_thread(thread_id: &str, task_ids: &[String]) {
let mut state = queue()
.lock()
.expect("background_completions queue poisoned");
- state.resume(thread_id);
+ state.finish_stop(thread_id, task_ids);
}
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 46bfc29162..c114c2b74a 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,17 @@ fn discard_pending_for_thread_blocks_late_results_until_the_next_turn() {
);
assert_eq!(pending_count("sess-stop"), 0);
- // A later user turn deliberately reopens the thread for new work.
- resume_for_thread("thread-stop-live");
+ // Completing Stop replaces the thread gate with task-specific tombstones,
+ // so later turns can run while this stopped child remains rejected.
+ finish_stop_for_thread("thread-stop-live", &["sub-stop-later".into()]);
+ record_completion(
+ "sess-stop",
+ "sub-stop-later",
+ "researcher",
+ "late stopped result after the next turn starts",
+ 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/web_chat/ops/channel_ops.rs b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
index f9f034abdb..729de44580 100644
--- a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
+++ b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
@@ -122,17 +122,19 @@ async fn cancel_chat_inner(
crate::agent::orchestration::background_completions::discard_pending_for_thread(
thread_id,
);
- let stopped =
- crate::agent::orchestration::running_subagents::stop_for_thread(thread_id).len();
+ let stopped = crate::agent::orchestration::running_subagents::stop_for_thread(thread_id);
+ crate::agent::orchestration::background_completions::finish_stop_for_thread(
+ thread_id, &stopped,
+ );
log::info!(
"[web-channel] stop thread_id={} turn={:?} parallel={} subagents_cancelled={} completions_discarded={}",
thread_id,
removed_request_id,
cancelled_parallel.len(),
- stopped,
+ stopped.len(),
discarded
);
- stopped
+ stopped.len()
} else {
0
};
@@ -167,9 +169,6 @@ pub async fn channel_web_chat(
queue_mode: Option<String>,
metadata: ChatRequestMetadata,
) -> Result<RpcOutcome<Value>, String> {
- // A Stop gate only suppresses completions from the halted generation. A
- // later user request deliberately starts a new generation on this thread.
- crate::agent::orchestration::background_completions::resume_for_thread(thread_id);
let result = start_chat(
client_id,
thread_id,