Skip to content

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

fix(subagent_sessions): project cancellation for paused driver children

Steven Enamakele9a0786aea5a
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 ae172cf417..b9e28473e8 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>,
+ /// The selected durable driver row when a paused child needs host-side
+ /// terminal projection (its driver callback already returned on pause).
+ pub(crate) driver_pause: Option<crate::agent::orchestration::subagent_sessions::DriverTaskCancellation>,
/// 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,
@@ -87,6 +90,7 @@ pub(crate) fn cancel_by_task_with_workspace_reason(
workspace_dir: metadata.workspace_dir,
parent_thread_id: metadata.parent_thread_id,
already_finished,
+ driver_pause: None,
driver_managed: false,
}));
}
@@ -112,6 +116,10 @@ pub(crate) fn cancel_by_task_with_workspace_reason(
task_id,
reason,
)?;
+ let driver_pause = selected
+ .as_ref()
+ .filter(|selected| selected.was_paused)
+ .cloned();
Ok(selected.map(|selected| CancelledSubagent {
agent_id: selected.agent_id,
parent_session: selected.parent_session,
@@ -119,6 +127,7 @@ pub(crate) fn cancel_by_task_with_workspace_reason(
workspace_dir: selected.workspace_dir,
parent_thread_id: selected.parent_thread_id,
already_finished: selected.already_finished,
+ driver_pause,
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.rs
index f1cdb981f5..fbf6245ac7 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs
@@ -6,8 +6,9 @@ mod types;
pub use ops::{
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,
+ cancel_driver_jobs_for_thread, close_if_current, find_reusable, list_for_parent,
+ mark_driver_pause_cancelled, mark_failed, mark_finished, normalize_task_key,
+ resolve_lifecycle_key, reuse_decision, DriverTaskCancellation,
task_title_from_prompt, upsert_running,
};
pub(crate) use tiny_port::OpenHumanTinySubagentSessionStore;
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 60a8984146..c942e0afde 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
@@ -174,12 +174,15 @@ pub fn cancel_driver_job_for_observed_session(
/// authenticated TinyAgents lifecycle key.
#[derive(Debug, Clone)]
pub struct DriverTaskCancellation {
+ pub task_id: String,
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 already_finished: Option<tinyagents_orchestration::subagent::FinishedOutcome>,
+ pub was_paused: bool,
+ pub lifecycle_key: Option<tinyagents_orchestration::subagent::SubagentTaskKey>,
}
pub fn cancel_driver_job_by_task(
@@ -200,6 +203,7 @@ pub fn cancel_driver_job_by_task(
if matches.next().is_some() {
return Ok(None);
}
+ let was_paused = session.status == DurableSubagentStatus::AwaitingUser;
let already_finished = match session.status {
DurableSubagentStatus::Idle => {
Some(tinyagents_orchestration::subagent::FinishedOutcome::Completed)
@@ -210,6 +214,20 @@ pub fn cancel_driver_job_by_task(
DurableSubagentStatus::Running | DurableSubagentStatus::AwaitingUser => None,
DurableSubagentStatus::Closed => return Ok(None),
};
+ let lifecycle_key = if already_finished.is_none() {
+ Some(
+ resolve_lifecycle_key(
+ store,
+ task_id,
+ &session.parent_session,
+ session.parent_thread_id.as_deref(),
+ checkpoint_dir,
+ )
+ .map_err(|error| format!("failed to resolve subagent lifecycle for cancellation: {error}"))?,
+ )
+ } else {
+ session.lifecycle_key.clone()
+ };
if already_finished.is_none()
&& reason.is_some()
&& !record_cancel_reason(store, &session, reason.expect("checked above").trim())?
@@ -232,15 +250,54 @@ pub fn cancel_driver_job_by_task(
return Ok(None);
}
Ok(Some(DriverTaskCancellation {
+ task_id: task_id.to_owned(),
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,
already_finished,
+ was_paused,
+ lifecycle_key,
}))
}
+/// Mark a paused driver's durable product row cancelled only if it is still
+/// the same lifecycle selected by the RPC. Paused driver completion has
+/// already returned, so there is no live callback left to project this host
+/// state transition.
+pub fn mark_driver_pause_cancelled(
+ store: &SubagentSessionStore,
+ observed: &DriverTaskCancellation,
+) -> Result<bool, String> {
+ if !observed.was_paused {
+ return Ok(false);
+ }
+ let _guard = store_write_lock()
+ .lock()
+ .map_err(|_| "subagent session store lock poisoned".to_string())?;
+ let mut sessions = store.load()?;
+ let Some(session) = sessions.iter_mut().find(|session| {
+ session.subagent_session_id == observed.subagent_session_id
+ }) else {
+ return Ok(false);
+ };
+ if session.parent_session != observed.parent_session
+ || session.parent_thread_id != observed.parent_thread_id
+ || session.current_task_id.as_deref() != Some(observed.task_id.as_str())
+ || session.lifecycle_key != observed.lifecycle_key
+ || session.status != DurableSubagentStatus::AwaitingUser
+ {
+ return Ok(false);
+ }
+ session.status = DurableSubagentStatus::Failed;
+ session.latest_error = Some("cancelled by user".into());
+ session.updated_at = chrono::Utc::now().to_rfc3339();
+ session.last_used_at = session.updated_at.clone();
+ store.save(&sessions)?;
+ Ok(true)
+}
+
/// Persist an optional RPC cancellation reason before signaling TinyAgents so
/// the awaited completion callback can include it in the single cancellation
/// notice. The current task check prevents a stale cancel from annotating a