Skip to content

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

feat(orchestration): cancel driver subagents when a thread is deleted

Steven Enamakel79b0ce3f398e
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 62b16bdfc5..e04afdb9a7 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -294,10 +294,36 @@ pub(crate) async fn cancel_by_session_in_workspace(
/// keep running (and later try to deliver) against a thread that no longer
/// exists. Returns the number of sub-agents cancelled.
pub(crate) fn cancel_for_thread(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str) -> usize {
- let _ = (ctx, thread_id);
- // The production job-registry thread selector is added by the cancellation
- // owner leaf; the legacy detached-task registry was always empty in hosts.
- 0
+ let Some(core_context) = ctx.resolved() else {
+ return 0;
+ };
+ let Ok(workspace_dir) = core_context.workspace_dir() else {
+ return 0;
+ };
+ let scopes = crate::agent::orchestration::completion_owners::driver_tasks_for_thread(
+ ctx,
+ &workspace_dir,
+ thread_id,
+ );
+ let jobs = crate::core::runtime::slot_for::<
+ tinyagents_harness_tools::subagents::SubAgentJobRegistry,
+ >(ctx);
+ let mut cancelled = 0;
+ let mut scopes_seen = Vec::new();
+ for (_, owner) in scopes {
+ let scope = ParentJobScope::new(owner.workspace, thread_id);
+ if scopes_seen.contains(&scope) {
+ continue;
+ }
+ scopes_seen.push(scope.clone());
+ match jobs.cancel_for_thread(&scope) {
+ Ok(count) => cancelled += count,
+ Err(error) => log::warn!(
+ "[running_subagents] driver thread deletion cancellation failed: {error}"
+ ),
+ }
+ }
+ cancelled
}
/// Abort every running detached sub-agent spawned from chat thread
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_driver_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_driver_tests.rs
index 9bc492542c..a9cee6bfc7 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_driver_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_driver_tests.rs
@@ -313,12 +313,10 @@ async fn force_fresh_cancels_old_driver_before_creating_a_new_session() {
#[tokio::test]
async fn deleting_parent_thread_cancels_driver_child_before_discarding_completions() {
- use crate::agent::orchestration::background_completions::{
- self, BackgroundAgentOutcome,
- };
+ use crate::agent::orchestration::background_completions::{self, BackgroundAgentOutcome};
use crate::agent::tinyagents::host::OpenHumanRunContext;
use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};
- use crate::threads::store::{CreateConversationThread, blocking};
+ use crate::threads::store::{blocking, CreateConversationThread};
use crate::tools::toolpacks::ToolGroups;
use tinyagents_harness::context::RunContext;
use tinyagents_harness_tools::subagents::SubAgentJobRegistry;
@@ -445,9 +443,7 @@ async fn deleting_parent_thread_cancels_driver_child_before_discarding_completio
let job = jobs
.list()
.into_iter()
- .find(|job| {
- job.status == SubAgentJobStatus::Running && job.agent == "task_manager_agent"
- })
+ .find(|job| job.status == SubAgentJobStatus::Running && job.agent == "task_manager_agent")
.expect("spawned child is live in the TinyAgents job registry");
background_completions::record_outcome(
&dispatch,