Skip to content

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

fix(subagent): propagate cancellation store errors to the RPC

Steven Enamakelfb8bfe77a64e
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 a15e89096d..0156913592 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -43,7 +43,9 @@ pub(crate) fn cancel_by_task(
ctx: &crate::core::runtime::DispatchCtx,
task_id: &str,
) -> Option<CancelledSubagent> {
- cancel_by_task_with_workspace(ctx, task_id, None)
+ cancel_by_task_with_workspace_reason(ctx, task_id, None, None)
+ .ok()
+ .flatten()
}
/// Cancels a child by public task id using the authenticated active workspace
@@ -55,12 +57,28 @@ pub(crate) fn cancel_by_task_with_workspace(
task_id: &str,
fallback_workspace: Option<&Path>,
) -> Option<CancelledSubagent> {
+ cancel_by_task_with_workspace_reason(ctx, task_id, fallback_workspace, None)
+ .ok()
+ .flatten()
+}
+
+/// Cancel with an optional user-provided reason for the driver-owned
+/// completion callback. Store failures are returned so the RPC does not report
+/// an unknown task when its durable selector could not be read.
+pub(crate) fn cancel_by_task_with_workspace_reason(
+ ctx: &crate::core::runtime::DispatchCtx,
+ task_id: &str,
+ fallback_workspace: Option<&Path>,
+ reason: Option<&str>,
+) -> Result<Option<CancelledSubagent>, String> {
let id = TaskId::new(task_id);
if let Ok(snapshot) = registry().snapshot_trusted(&id) {
if !control_admits(ctx, &snapshot.metadata) {
- return None;
+ return Ok(None);
}
- let cancelled = registry().cancel(&id, &snapshot.owner_id).ok()?;
+ let Ok(cancelled) = registry().cancel(&id, &snapshot.owner_id) else {
+ return Ok(None);
+ };
// `AwaitingUser` is paused, not finished: cancelling it is a real cancel.
let already_finished = cancelled.status.finished_outcome();
let metadata = cancelled.metadata;
@@ -76,7 +94,7 @@ pub(crate) fn cancel_by_task_with_workspace(
.len()
.expect("detached task registry lock poisoned")
);
- return Some(CancelledSubagent {
+ return Ok(Some(CancelledSubagent {
agent_id: metadata.agent_id,
parent_session: metadata.parent_session,
subagent_session_id: metadata.subagent_session_id,
@@ -84,13 +102,16 @@ pub(crate) fn cancel_by_task_with_workspace(
parent_thread_id: metadata.parent_thread_id,
already_finished,
driver_managed: false,
- });
+ }));
}
let workspace_dir = match ctx.resolved() {
- Some(context) => context.workspace_dir().ok()?,
- None if !crate::core::runtime::is_saas() => fallback_workspace?.to_path_buf(),
- None => return None,
+ Some(context) => context.workspace_dir().ok(),
+ None if !crate::core::runtime::is_saas() => fallback_workspace.map(Path::to_path_buf),
+ None => None,
+ };
+ let Some(workspace_dir) = workspace_dir else {
+ return Ok(None);
};
let store = crate::agent::orchestration::subagent_sessions::SubagentSessionStore::new(
workspace_dir.clone(),
@@ -103,8 +124,9 @@ pub(crate) fn cancel_by_task_with_workspace(
&workspace_dir.join(".openhuman/subagent_checkpoints"),
&jobs,
task_id,
+ reason,
)?;
- Some(CancelledSubagent {
+ Ok(selected.map(|selected| CancelledSubagent {
agent_id: selected.agent_id,
parent_session: selected.parent_session,
subagent_session_id: Some(selected.subagent_session_id),
@@ -112,7 +134,7 @@ pub(crate) fn cancel_by_task_with_workspace(
parent_thread_id: selected.parent_thread_id,
already_finished: selected.already_finished,
driver_managed: true,
- })
+ }))
}
pub(crate) fn cancel_by_session_in_workspace(
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 26c7c19e2f..2c2a0aca80 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
@@ -188,15 +188,17 @@ pub fn cancel_driver_job_by_task(
jobs: &tinyagents_orchestration::subagent::SubAgentJobRegistry,
task_id: &str,
reason: Option<&str>,
-) -> Option<DriverTaskCancellation> {
+) -> Result<Option<DriverTaskCancellation>, String> {
let mut matches = store
.load()
- .ok()?
+ .map_err(|error| format!("failed to load subagent sessions for cancellation: {error}"))?
.into_iter()
.filter(|session| session.current_task_id.as_deref() == Some(task_id));
- let session = matches.next()?;
+ let Some(session) = matches.next() else {
+ return Ok(None);
+ };
if matches.next().is_some() {
- return None;
+ return Ok(None);
}
let already_finished = match session.status {
DurableSubagentStatus::Idle => {
@@ -206,7 +208,7 @@ pub fn cancel_driver_job_by_task(
Some(tinyagents_orchestration::subagent::FinishedOutcome::Failed)
}
DurableSubagentStatus::Running | DurableSubagentStatus::AwaitingUser => None,
- DurableSubagentStatus::Closed => return None,
+ DurableSubagentStatus::Closed => return Ok(None),
};
if already_finished.is_none()
&& reason.is_some()
@@ -217,7 +219,7 @@ pub fn cancel_driver_job_by_task(
reason.expect("checked above").trim(),
)
{
- return None;
+ return Ok(None);
}
if already_finished.is_none()
&& !cancel_driver_job_for_observed_session(
@@ -232,16 +234,16 @@ pub fn cancel_driver_job_by_task(
if reason.is_some() {
clear_cancel_reason(store, &session.subagent_session_id, task_id);
}
- return None;
+ return Ok(None);
}
- Some(DriverTaskCancellation {
+ Ok(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,
already_finished,
- })
+ }))
}
/// Persist an optional RPC cancellation reason before signaling TinyAgents so
diff --git a/crates/openhuman-core/src/agent_runs/subagent_control.rs b/crates/openhuman-core/src/agent_runs/subagent_control.rs
index 1c34ac5538..672beeae38 100644
--- a/crates/openhuman-core/src/agent_runs/subagent_control.rs
+++ b/crates/openhuman-core/src/agent_runs/subagent_control.rs
@@ -115,7 +115,12 @@ fn handle_subagent_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> Contr
// `outcome` is set only when nothing was cancelled: how the run ended
// if the registry still knew it, else "unknown" (never registered, or
// already swept). The card settles on it instead of assuming success.
- let mut cancelled_subagent = running_subagents::cancel_by_task(&ctx, &task_id);
+ let mut cancelled_subagent = running_subagents::cancel_by_task_with_workspace_reason(
+ &ctx,
+ &task_id,
+ None,
+ reason.as_deref(),
+ )?;
if cancelled_subagent.is_none()
&& ctx.resolved().is_none()
&& !crate::core::runtime::is_saas()
@@ -123,11 +128,12 @@ fn handle_subagent_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> Contr
let config = crate::config::ops::load_config_in(&ctx)
.await
.map_err(|error| format!("subagent_cancel: failed to resolve active workspace: {error}"))?;
- cancelled_subagent = running_subagents::cancel_by_task_with_workspace(
+ cancelled_subagent = running_subagents::cancel_by_task_with_workspace_reason(
&ctx,
&task_id,
Some(&config.workspace_dir),
- );
+ reason.as_deref(),
+ )?;
}
let (cancelled, outcome) = match cancelled_subagent {
// Finished before the click landed (it stays registered until the