Skip to content

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

feat(running_subagents): add stop_for_thread to cancel sub-agents on Stop

Steven Enamakelb15d41ecd589
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
index 5517c91bb4..af402bce56 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
@@ -65,6 +65,7 @@ mod tests;
pub(crate) use cancel::prune;
pub(crate) use cancel::{
cancel_all, cancel_by_session_in_workspace, cancel_by_task, cancel_for_thread,
+ stop_for_thread,
};
pub(crate) use registry::{register, status_channel, SubagentResumeRef, SubagentStatus};
pub(crate) use resolve::{resume_ref_for_task_in_workspace, task_id_for_session_in_workspace};
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
index 46bfd00cff..c127ca83a6 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -94,6 +94,55 @@ pub(crate) fn cancel_for_thread(thread_id: &str) -> usize {
count
}
+/// Abort every running detached sub-agent spawned from chat thread
+/// `thread_id` because the user pressed Stop on that thread.
+///
+/// Unlike [`cancel_for_thread`] (thread deletion) the thread survives, so each
+/// child's durable sub-agent session is marked failed ("cancelled by user")
+/// rather than left looking resumable. No "you cancelled" completion is
+/// recorded: delivering one would start a fresh system turn on the thread,
+/// which is exactly what Stop is meant to prevent. Returns the cancelled task
+/// ids.
+pub(crate) fn stop_for_thread(thread_id: &str) -> Vec<String> {
+ let cancelled = registry()
+ .cancel_where(|metadata| metadata.parent_thread_id.as_deref() == Some(thread_id))
+ .expect("detached task registry lock poisoned");
+ let mut task_ids = Vec::with_capacity(cancelled.len());
+ for entry in cancelled {
+ let task_id = entry.task_id.as_str().to_string();
+ record_cancelled(&entry.metadata.workspace_dir, &task_id);
+ if let Some(subagent_session_id) = entry.metadata.subagent_session_id.as_deref() {
+ let store = crate::agent::orchestration::subagent_sessions::SubagentSessionStore::new(
+ entry.metadata.workspace_dir.clone(),
+ );
+ if let Err(err) = crate::agent::orchestration::subagent_sessions::mark_failed(
+ &store,
+ subagent_session_id,
+ &task_id,
+ "cancelled by user".to_string(),
+ ) {
+ log::warn!(
+ "[running_subagents] stop_for_thread mark_failed failed thread_id={} task_id={} subagent_session_id={} error={}",
+ thread_id,
+ task_id,
+ subagent_session_id,
+ err
+ );
+ }
+ }
+ task_ids.push(task_id);
+ }
+ log::info!(
+ "[running_subagents] stop_for_thread thread_id={} cancelled={} live_entries={}",
+ thread_id,
+ task_ids.len(),
+ registry()
+ .len()
+ .expect("detached task registry lock poisoned")
+ );
+ task_ids
+}
+
/// Abort and drop **every** registered sub-agent. Called on a full thread purge
/// where no parent thread survives. Returns the **distinct parent thread ids**
/// that had sub-agents, so the purge path can tombstone them in