Skip to content

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

fix(orchestration): resolve subagent cancellation across all parent owners

Steven Enamakel36a3fae3a52e
diff --git a/crates/openhuman-core/src/agent/orchestration/completion_owners.rs b/crates/openhuman-core/src/agent/orchestration/completion_owners.rs
index 45baaed9c5..8de5ca31d3 100644
--- a/crates/openhuman-core/src/agent/orchestration/completion_owners.rs
+++ b/crates/openhuman-core/src/agent/orchestration/completion_owners.rs
@@ -34,6 +34,7 @@ struct Owners {
order: VecDeque<String>,
driver_tasks: HashMap<String, DriverTaskOwner>,
driver_task_order: VecDeque<String>,
+ driver_cancel_reasons: HashMap<String, String>,
}
#[derive(Clone)]
@@ -103,6 +104,35 @@ pub(crate) fn driver_task_owner(
owners().driver_tasks.get(&driver_task_key(ctx, task_id)).cloned()
}
+pub(crate) fn note_driver_cancel_reason(
+ ctx: &crate::core::runtime::DispatchCtx,
+ task_id: &str,
+ reason: &str,
+) {
+ owners().driver_cancel_reasons.insert(
+ driver_task_key(ctx, task_id),
+ reason.to_owned(),
+ );
+}
+
+pub(crate) fn take_driver_cancel_reason(
+ ctx: &crate::core::runtime::DispatchCtx,
+ task_id: &str,
+) -> Option<String> {
+ owners()
+ .driver_cancel_reasons
+ .remove(&driver_task_key(ctx, task_id))
+}
+
+pub(crate) fn clear_driver_cancel_reason(
+ ctx: &crate::core::runtime::DispatchCtx,
+ task_id: &str,
+) {
+ owners()
+ .driver_cancel_reasons
+ .remove(&driver_task_key(ctx, task_id));
+}
+
/// Return registered task scopes for one exact workspace/thread pair.
pub(crate) fn driver_tasks_for_thread(
ctx: &crate::core::runtime::DispatchCtx,
@@ -122,6 +152,24 @@ pub(crate) fn driver_tasks_for_thread(
.collect()
}
+pub(crate) fn driver_tasks_for_parent(
+ ctx: &crate::core::runtime::DispatchCtx,
+ workspace_dir: &std::path::Path,
+ parent_session: &str,
+) -> Vec<(String, DriverTaskOwner)> {
+ let profile = ctx.tenant().ok().and_then(|tenant| tenant.profile);
+ owners()
+ .driver_tasks
+ .iter()
+ .filter_map(|(_, owner)| {
+ (owner.profile == profile
+ && owner.workspace_dir == workspace_dir
+ && owner.parent_session == parent_session)
+ .then(|| (owner.task_id.clone(), owner.clone()))
+ })
+ .collect()
+}
+
/// Note the calling task's profile as an owner of each id. A no-op outside a
/// profile (the desktop, embedded agents).
pub(crate) fn note(ctx: &crate::core::runtime::DispatchCtx, ids: &[&str]) {
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 a5bcbd1625..565d5ca245 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -8,9 +8,6 @@ use tinyagents_orchestration::subagent::{
FinishedOutcome, ParentJobScope, SubAgentJobStatus,
};
-use super::registry::admit_workspace;
-use super::task_ledger::scoped_task_store;
-
/// Metadata captured when a sub-agent is cancelled, so the caller can surface
/// the cancellation back in the parent chat (record a "cancelled" completion
/// for idle-gated delivery).
@@ -132,9 +129,7 @@ pub(crate) async fn cancel_by_task_with_workspace_reason(
}
}
if jobs.cancel_owned(job.id.as_str(), &owner).is_err() {
- crate::agent::orchestration::completion_owners::clear_driver_cancel_reason(
- ctx, task_id,
- );
+ crate::agent::orchestration::completion_owners::clear_driver_cancel_reason(ctx, task_id);
return Ok(None);
}
}
@@ -170,29 +165,33 @@ pub(crate) async fn cancel_by_session_in_workspace(
parent_session: &str,
workspace_dir: &Path,
) -> Option<CancelledSubagent> {
- let owner_scope = crate::agent::orchestration::completion_owners::driver_tasks_for_thread(
+ let owners = crate::agent::orchestration::completion_owners::driver_tasks_for_parent(
ctx,
workspace_dir,
- "",
- )
- .into_iter()
- .map(|(_, owner)| owner)
- .find(|owner| owner.parent_session == parent_session);
- let owner_scope = owner_scope?;
- let task_id = crate::agent::orchestration::subagent_sessions::open_for_context(
- ctx,
- workspace_dir.to_path_buf(),
parent_session,
- owner_scope.parent_thread_id.clone(),
- None,
- )
- .ok()?
- .list_for_parent(parent_session, owner_scope.parent_thread_id.as_deref())
- .await
- .ok()?
- .into_iter()
- .find(|session| session.subagent_session_id == subagent_session_id)?
- .current_task_id?;
+ );
+ let mut task_id = None;
+ for (candidate, owner) in owners {
+ let sessions = crate::agent::orchestration::subagent_sessions::open_for_context(
+ ctx,
+ workspace_dir.to_path_buf(),
+ parent_session,
+ owner.parent_thread_id.clone(),
+ None,
+ )
+ .ok()?
+ .list_for_parent(parent_session, owner.parent_thread_id.as_deref())
+ .await
+ .ok()?;
+ if sessions.iter().any(|session| {
+ session.subagent_session_id == subagent_session_id
+ && session.current_task_id.as_deref() == Some(candidate.as_str())
+ }) {
+ task_id = Some(candidate);
+ break;
+ }
+ }
+ let task_id = task_id?;
cancel_by_task_with_workspace_reason(ctx, &task_id, Some(workspace_dir), None)
.await
.ok()
@@ -223,7 +222,6 @@ pub(crate) fn stop_for_thread(
ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
) -> Vec<String> {
- let mut task_ids = Vec::new();
if let Some(core_context) = ctx.resolved() {
if let Ok(workspace_dir) = core_context.workspace_dir() {
let jobs = crate::core::runtime::slot_for::<
@@ -278,14 +276,12 @@ pub(crate) fn stop_for_thread(
log::warn!("[running_subagents] driver thread stop failed: {error}");
}
}
- for task_id in task_ids {
- if !task_ids.contains(&task_id) {
- task_ids.push(task_id);
- }
- }
+ task_ids.sort();
+ task_ids.dedup();
+ return task_ids;
}
}
- task_ids
+ Vec::new()
}
/// Abort and drop **every** registered sub-agent. Called on a full thread purge