Skip to content

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

feat(subagent_sessions): cancel driver-backed jobs on thread delete

Steven Enamakeldfb5d240b637
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs
index b5cb2a7511..552ae159b4 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs
@@ -5,7 +5,7 @@ mod tiny_port;
mod types;
pub use ops::{
- action_root_key, close, find_reusable, list_for_parent, mark_failed, mark_finished,
+ action_root_key, cancel_driver_jobs_for_thread, close, 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.rs
index ad304db7bf..58cf6ed9ef 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
@@ -13,6 +13,61 @@ use super::types::{
SubagentSessionStore, SubagentSessionUpsert,
};
+/// Requests cancellation of driver-backed children owned by a thread being
+/// removed. The TinyAgents registry owns cancellation and settlement; this
+/// adapter only selects durable rows and resolves their authenticated keys.
+pub fn cancel_driver_jobs_for_thread(
+ store: &SubagentSessionStore,
+ workspace_dir: &Path,
+ jobs: &tinyagents_orchestration::subagent::SubAgentJobRegistry,
+ thread_id: &str,
+) -> usize {
+ let sessions = match store.load() {
+ Ok(sessions) => sessions,
+ Err(error) => {
+ log::warn!("[subagent_sessions] failed to load sessions for thread cancellation: {error}");
+ return 0;
+ }
+ };
+ let mut cancelled = 0;
+ for session in sessions.into_iter().filter(|session| {
+ session.parent_thread_id.as_deref() == Some(thread_id)
+ && matches!(
+ session.status,
+ DurableSubagentStatus::Running | DurableSubagentStatus::AwaitingUser
+ )
+ }) {
+ let Some(task_id) = session.current_task_id.as_deref() else {
+ continue;
+ };
+ let key = session
+ .lifecycle_key
+ .filter(|key| key.task_id == task_id)
+ .or_else(|| {
+ resolve_lifecycle_key(
+ store,
+ &session.subagent_session_id,
+ &session.parent_session,
+ Some(thread_id),
+ &workspace_dir.join(".openhuman/subagent_checkpoints"),
+ )
+ .ok()
+ .filter(|key| key.task_id == task_id)
+ });
+ let Some(key) = key else {
+ continue;
+ };
+ let owner = crate::agent::tinyagents::tool_registration::subagent_owner_key(
+ &session.parent_session,
+ Some(thread_id),
+ );
+ if jobs.cancel_key_owned(&key, owner).is_ok() {
+ cancelled += 1;
+ }
+ }
+ cancelled
+}
+
/// 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/threads/ops/crud.rs b/crates/openhuman-core/src/threads/ops/crud.rs
index 542981c959..ce3dd33336 100644
--- a/crates/openhuman-core/src/threads/ops/crud.rs
+++ b/crates/openhuman-core/src/threads/ops/crud.rs
@@ -489,8 +489,18 @@ async fn thread_delete_inner(
// completion in the gap between the two calls, then discard anything already
// queued for delivery. Both target a thread that's being deleted, so there's
// nowhere left to deliver to — abort + cleanup is the whole behavior.
- let cancelled =
+ let cancelled_legacy =
crate::agent::orchestration::running_subagents::cancel_for_thread(&ctx, &request.thread_id);
+ let jobs = crate::core::runtime::slot_for::<
+ tinyagents_orchestration::subagent::SubAgentJobRegistry,
+ >(&ctx);
+ let cancelled_driver = crate::agent::orchestration::subagent_sessions::cancel_driver_jobs_for_thread(
+ &crate::agent::orchestration::subagent_sessions::SubagentSessionStore::new(dir.clone()),
+ &dir,
+ &jobs,
+ &request.thread_id,
+ );
+ let cancelled = cancelled_legacy + cancelled_driver;
let discarded = crate::agent::orchestration::background_completions::discard_for_thread(
&ctx,
&dir,