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 job when closing a session

Steven Enamakel66bd0853f0f2
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 b7ab58945b..fa04b75886 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,8 @@ mod tiny_port;
mod types;
pub use ops::{
- action_root_key, cancel_driver_jobs_for_thread, close, find_reusable, list_for_parent,
+ action_root_key, cancel_driver_job_for_session, 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 a19dd2c6e4..784198f244 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
@@ -70,6 +70,57 @@ pub fn cancel_driver_jobs_for_thread(
cancelled
}
+/// Cancels the current driver-backed lifecycle for one authenticated durable
+/// session. Legacy rows recover a key only through their validated checkpoint.
+pub fn cancel_driver_job_for_session(
+ store: &SubagentSessionStore,
+ checkpoint_dir: &Path,
+ jobs: &tinyagents_orchestration::subagent::SubAgentJobRegistry,
+ session_id: &str,
+ parent_session: &str,
+ parent_thread_id: Option<&str>,
+) -> bool {
+ let session = match list_for_parent(store, parent_session, parent_thread_id) {
+ Ok(sessions) => sessions
+ .into_iter()
+ .find(|session| session.subagent_session_id == session_id),
+ Err(error) => {
+ log::warn!("[subagent_sessions] session cancellation lookup failed: {error}");
+ None
+ }
+ };
+ let Some(session) = session else {
+ return false;
+ };
+ let Some(task_id) = session.current_task_id.as_deref() else {
+ return false;
+ };
+ let key = session
+ .lifecycle_key
+ .filter(|key| {
+ key.task_id == task_id && key.thread_id.as_deref() == parent_thread_id
+ })
+ .or_else(|| {
+ resolve_lifecycle_key(
+ store,
+ session_id,
+ parent_session,
+ parent_thread_id,
+ checkpoint_dir,
+ )
+ .ok()
+ .filter(|key| key.task_id == task_id)
+ });
+ let Some(key) = key else {
+ return false;
+ };
+ let owner = crate::agent::tinyagents::tool_registration::subagent_owner_key(
+ parent_session,
+ parent_thread_id,
+ );
+ jobs.cancel_key_owned(&key, owner).is_ok()
+}
+
/// 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/orchestration/subagent_sessions/tiny_port.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/tiny_port.rs
index 7c6f041200..6fed66c9a0 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/tiny_port.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/tiny_port.rs
@@ -21,6 +21,7 @@ pub(crate) struct OpenHumanTinySubagentSessionStore {
checkpoint_dir: PathBuf,
parent_session: String,
parent_thread_id: Option<String>,
+ jobs: Option<tinyagents_orchestration::subagent::SubAgentJobRegistry>,
}
impl OpenHumanTinySubagentSessionStore {
@@ -34,9 +35,18 @@ impl OpenHumanTinySubagentSessionStore {
store: SubagentSessionStore::new(workspace_dir),
parent_session: parent_session.into(),
parent_thread_id,
+ jobs: None,
}
}
+ pub(crate) fn with_jobs(
+ mut self,
+ jobs: tinyagents_orchestration::subagent::SubAgentJobRegistry,
+ ) -> Self {
+ self.jobs = Some(jobs);
+ self
+ }
+
fn is_owned(&self, row: &DurableSubagentSession) -> bool {
row.parent_session == self.parent_session && row.parent_thread_id == self.parent_thread_id
}
@@ -198,9 +208,20 @@ impl TinySessionStore for OpenHumanTinySubagentSessionStore {
}
async fn close(&self, id: &str) -> Result<bool, SubagentError> {
- if self.get(id).await?.is_none() {
+ let Some(session) = self.get(id).await? else {
return Ok(false);
+ };
+ if let Some(jobs) = &self.jobs {
+ let _ = super::cancel_driver_job_for_session(
+ &self.store,
+ &self.checkpoint_dir,
+ jobs,
+ id,
+ &self.parent_session,
+ self.parent_thread_id.as_deref(),
+ );
}
+ drop(session);
super::close(&self.store, id).map_err(SubagentError::Persistence)
}