Skip to content

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

feat(agent): track parent epoch during subagent cancellation

Steven Enamakelc5586a60ae92
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 c013a3e7f7..df66722729 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -23,6 +23,9 @@ pub(crate) struct CancelledSubagent {
pub(crate) subagent_session_id: Option<String>,
pub(crate) workspace_dir: PathBuf,
pub(crate) parent_thread_id: Option<String>,
+ /// Admission generation captured for this child, if it originated in a
+ /// thread. Completion after a later stop/resume must retain this epoch.
+ pub(crate) parent_epoch: Option<u64>,
/// How the run had already ended when the cancel arrived, if it had. A
/// finished entry stays registered until `sweep_terminal`, so a late
/// "Cancel" still finds it; the caller must not rewrite that outcome as a
@@ -90,6 +93,7 @@ pub(crate) fn cancel_by_task_with_workspace_reason(
subagent_session_id: metadata.subagent_session_id,
workspace_dir: metadata.workspace_dir,
parent_thread_id: metadata.parent_thread_id,
+ parent_epoch: metadata.parent_epoch,
already_finished,
driver_pause: None,
driver_managed: false,
@@ -117,6 +121,27 @@ pub(crate) fn cancel_by_task_with_workspace_reason(
task_id,
reason,
)?;
+ let parent_epoch = match selected.as_ref().and_then(|selected| {
+ selected
+ .was_paused
+ .then_some((selected.parent_thread_id.as_deref(), selected.task_id.as_str()))
+ }) {
+ Some((Some(thread_id), selected_task_id)) => {
+ match crate::agent::orchestration::background_completions::paused_task_parent_epoch(
+ ctx,
+ &workspace_dir,
+ thread_id,
+ selected_task_id,
+ ) {
+ Ok(epoch) => epoch,
+ Err(error) => {
+ log::warn!("[running_subagents] could not recover paused task parent epoch task_id={selected_task_id}: {error}");
+ None
+ }
+ }
+ }
+ _ => None,
+ };
let driver_pause = selected
.as_ref()
.filter(|selected| selected.was_paused)
@@ -127,6 +152,7 @@ pub(crate) fn cancel_by_task_with_workspace_reason(
subagent_session_id: Some(selected.subagent_session_id),
workspace_dir: selected.workspace_dir,
parent_thread_id: selected.parent_thread_id,
+ parent_epoch,
already_finished: selected.already_finished,
driver_pause,
driver_managed: true,
diff --git a/crates/openhuman-core/src/agent_runs/subagent_control.rs b/crates/openhuman-core/src/agent_runs/subagent_control.rs
index 0ee37ecef7..d83b30d6f3 100644
--- a/crates/openhuman-core/src/agent_runs/subagent_control.rs
+++ b/crates/openhuman-core/src/agent_runs/subagent_control.rs
@@ -160,7 +160,13 @@ fn handle_subagent_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> Contr
// A paused job has already returned from that hook, so this
// handler settles and reports that one terminal transition.
if let Some(paused) = meta.driver_pause.as_ref() {
- settle_paused_driver_cancel(&ctx, paused, reason.as_deref()).await?;
+ settle_paused_driver_cancel(
+ &ctx,
+ paused,
+ meta.parent_epoch,
+ reason.as_deref(),
+ )
+ .await?;
}
(true, None)
} else {
@@ -174,7 +180,7 @@ fn handle_subagent_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> Contr
// The thread is still alive (unlike the delete path), so we
// record a completion that flows through the same idle-gated
// delivery and surfaces the cancellation in chat.
- background_completions::record_completion(
+ background_completions::record_outcome_with_parent_epoch(
&ctx,
&meta.workspace_dir,
&meta.parent_session,
@@ -182,6 +188,8 @@ fn handle_subagent_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> Contr
meta.agent_id.clone(),
summary,
meta.parent_thread_id.clone(),
+ meta.parent_epoch,
+ background_completions::BackgroundAgentOutcome::Completed,
)
.await;
if let Some(subagent_session_id) = meta.subagent_session_id.as_deref() {
@@ -220,8 +228,12 @@ fn handle_subagent_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> Contr
async fn settle_paused_driver_cancel(
ctx: &DispatchCtx,
paused: &subagent_sessions::DriverTaskCancellation,
+ parent_epoch: Option<u64>,
reason: Option<&str>,
) -> Result<bool, String> {
+ let Some(parent_epoch) = parent_epoch else {
+ return Err("subagent_cancel: paused task has no captured parent generation".into());
+ };
let store = subagent_sessions::SubagentSessionStore::new(paused.workspace_dir.clone());
let transitioned = subagent_sessions::mark_driver_pause_cancelled(&store, paused)
.map_err(|error| format!("subagent_cancel: failed to settle paused child: {error}"))?;
@@ -230,7 +242,7 @@ async fn settle_paused_driver_cancel(
Some(reason) => format!("cancelled by user. Reason: {reason}"),
None => "cancelled by user".to_string(),
};
- background_completions::record_failure(
+ background_completions::record_outcome_with_parent_epoch(
ctx,
&paused.workspace_dir,
&paused.parent_session,
@@ -238,6 +250,8 @@ async fn settle_paused_driver_cancel(
&paused.agent_id,
&error,
paused.parent_thread_id.clone(),
+ Some(parent_epoch),
+ background_completions::BackgroundAgentOutcome::Failed,
)
.await;
}