The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(subagent): cancel driver-backed subagents by task id
Steven Enamakelbe1bd63f1961
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rsindex b8cc75aaba..23cf0339ae 100644--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs@@ -28,6 +28,9 @@ pub(crate) struct CancelledSubagent {/// "Cancel" still finds it; the caller must not rewrite that outcome as a/// user cancellation, and reports it instead.pub(crate) already_finished: Option<FinishedOutcome>,+ /// TinyAgents' awaited completion handler owns terminal delivery for this+ /// driver-backed child; the RPC handler must not publish a duplicate.+ pub(crate) driver_managed: bool,}/// Abort and drop the sub-agent with `task_id`, returning its metadata so the@@ -41,33 +44,59 @@ pub(crate) fn cancel_by_task(task_id: &str,) -> Option<CancelledSubagent> {let id = TaskId::new(task_id);- let snapshot = registry().snapshot_trusted(&id).ok()?;- if !control_admits(ctx, &snapshot.metadata) {- return None;- }- let cancelled = registry().cancel(&id, &snapshot.owner_id).ok()?;- // `AwaitingUser` is paused, not finished: cancelling it is a real cancel.- let already_finished = cancelled.status.finished_outcome();- let metadata = cancelled.metadata;- if already_finished.is_none() {- record_cancelled(&metadata.workspace_dir, task_id);+ if let Ok(snapshot) = registry().snapshot_trusted(&id) {+ if !control_admits(ctx, &snapshot.metadata) {+ return None;+ }+ let cancelled = registry().cancel(&id, &snapshot.owner_id).ok()?;+ // `AwaitingUser` is paused, not finished: cancelling it is a real cancel.+ let already_finished = cancelled.status.finished_outcome();+ let metadata = cancelled.metadata;+ if already_finished.is_none() {+ record_cancelled(&metadata.workspace_dir, task_id);+ }+ log::debug!(+ "[running_subagents] cancel_by_task task_id={} agent_id={} parent_thread_id={:?} live_entries={}",+ task_id,+ metadata.agent_id,+ metadata.parent_thread_id,+ registry()+ .len()+ .expect("detached task registry lock poisoned")+ );+ return Some(CancelledSubagent {+ agent_id: metadata.agent_id,+ parent_session: metadata.parent_session,+ subagent_session_id: metadata.subagent_session_id,+ workspace_dir: metadata.workspace_dir,+ parent_thread_id: metadata.parent_thread_id,+ already_finished,+ driver_managed: false,+ });}- log::debug!(- "[running_subagents] cancel_by_task task_id={} agent_id={} parent_thread_id={:?} live_entries={}",- task_id,- metadata.agent_id,- metadata.parent_thread_id,- registry()- .len()- .expect("detached task registry lock poisoned")++ let context = ctx.resolved()?;+ let workspace_dir = context.workspace_dir().ok()?;+ let store = crate::agent::orchestration::subagent_sessions::SubagentSessionStore::new(+ workspace_dir.clone(),);+ let jobs = crate::core::runtime::slot_for::<+ tinyagents_orchestration::subagent::SubAgentJobRegistry,+ >(ctx);+ let selected = crate::agent::orchestration::subagent_sessions::cancel_driver_job_by_task(+ &store,+ &workspace_dir.join(".openhuman/subagent_checkpoints"),+ jobs,+ task_id,+ )?;Some(CancelledSubagent {- agent_id: metadata.agent_id,- parent_session: metadata.parent_session,- subagent_session_id: metadata.subagent_session_id,- workspace_dir: metadata.workspace_dir,- parent_thread_id: metadata.parent_thread_id,- already_finished,+ agent_id: selected.agent_id,+ parent_session: selected.parent_session,+ subagent_session_id: Some(selected.subagent_session_id),+ workspace_dir: selected.workspace_dir,+ parent_thread_id: selected.parent_thread_id,+ already_finished: None,+ driver_managed: true,})}diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rsindex c277054c40..d1a60483d1 100644--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs@@ -5,8 +5,8 @@ mod tiny_port;mod types;pub use ops::{- action_root_key, cancel_driver_job_for_observed_session, cancel_driver_jobs_for_thread,- close_if_current, find_reusable, list_for_parent, mark_failed, mark_finished,+ action_root_key, cancel_driver_job_by_task, cancel_driver_job_for_observed_session,+ cancel_driver_jobs_for_thread, close_if_current, find_reusable, list_for_parent, mark_failed, mark_finished,normalize_task_key, resolve_lifecycle_key, reuse_decision, task_title_from_prompt,upsert_running,};diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rsindex 080c7ce807..195ba5119c 100644--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs@@ -164,6 +164,59 @@ pub fn cancel_driver_job_for_observed_session(jobs.cancel_key_owned(&key, owner).is_ok()}+/// A driver-backed child selected and cancelled by its public task id.+///+/// The selector is scoped to the caller's durable session store and refuses+/// ambiguous task IDs. The actual cancellation remains keyed by the full+/// authenticated TinyAgents lifecycle key.+#[derive(Debug, Clone)]+pub struct DriverTaskCancellation {+ pub agent_id: String,+ pub parent_session: String,+ pub subagent_session_id: String,+ pub workspace_dir: std::path::PathBuf,+ pub parent_thread_id: Option<String>,+}++pub fn cancel_driver_job_by_task(+ store: &SubagentSessionStore,+ checkpoint_dir: &Path,+ jobs: &tinyagents_orchestration::subagent::SubAgentJobRegistry,+ task_id: &str,+) -> Option<DriverTaskCancellation> {+ let mut matches = store+ .load()+ .ok()?+ .into_iter()+ .filter(|session| {+ session.current_task_id.as_deref() == Some(task_id)+ && matches!(+ session.status,+ DurableSubagentStatus::Running | DurableSubagentStatus::AwaitingUser+ )+ });+ let session = matches.next()?;+ if matches.next().is_some()+ || !cancel_driver_job_for_observed_session(+ store,+ checkpoint_dir,+ jobs,+ &session,+ &session.parent_session,+ session.parent_thread_id.as_deref(),+ )+ {+ return None;+ }+ Some(DriverTaskCancellation {+ agent_id: session.agent_id,+ parent_session: session.parent_session,+ subagent_session_id: session.subagent_session_id,+ workspace_dir: store.workspace_dir.clone(),+ parent_thread_id: session.parent_thread_id,+ })+}+/// Resolve a saved product session to its exact TinyAgents lifecycle key.////// The product session lookup is scoped by the authenticated parent session,diff --git a/crates/openhuman-core/src/agent_runs/subagent_control.rs b/crates/openhuman-core/src/agent_runs/subagent_control.rsindex 0b04504c88..2d2d7a6a11 100644--- a/crates/openhuman-core/src/agent_runs/subagent_control.rs+++ b/crates/openhuman-core/src/agent_runs/subagent_control.rs@@ -133,6 +133,13 @@ fn handle_subagent_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> Contr(false, Some(finished.as_str()))}Some(meta) => {+ if meta.driver_managed {+ // The driver's awaited host-completion hook records the+ // terminal session state and delivers the failed event.+ // Publishing the legacy pseudo-completion here would+ // duplicate both effects.+ (true, None)+ } else {let summary = match reason.as_deref().map(str::trim).filter(|r| !r.is_empty()) {Some(r) => format!("You cancelled this background sub-agent before it finished. Reason: {r}"@@ -169,6 +176,7 @@ fn handle_subagent_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> Contr}}(true, None)+ }}None => (false, Some("unknown")),};