Skip to content

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

feat(subagent_sessions): persist parent epoch on durable sessions

Steven Enamakelf2febcc6ccdc
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 21941d7219..6a13551ef3 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -304,25 +304,6 @@ pub(crate) async fn record_outcome_with_parent_epoch(
}
}
-/// Recover the admission epoch from a task's persisted pause notice. Paused
-/// driver jobs have already returned from their completion callback, so their
-/// host-side cancellation must reuse this epoch rather than admitting a new
-/// child generation.
-pub(crate) fn paused_task_parent_epoch(
- ctx: &DispatchCtx,
- workspace_dir: &Path,
- thread_id: &str,
- task_id: &str,
-) -> anyhow::Result<Option<u64>> {
- let prefix = awaiting_input_record_prefix(task_id);
- Ok(entry_for_ctx(ctx, workspace_dir)
- .store
- .try_list(Some(thread_id))?
- .into_iter()
- .find(|record| record.task_id.starts_with(&prefix))
- .map(|record| record.parent_epoch)
- .filter(|epoch| *epoch != 0))
-}
async fn record_completion_record(
ctx: &DispatchCtx,
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 5a8cd0c708..a2485f709a 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -121,28 +121,7 @@ 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 parent_epoch = selected.as_ref().and_then(|selected| selected.parent_epoch);
let driver_pause = selected
.as_ref()
.filter(|selected| selected.was_paused)
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs
index 4e8237baa3..e978bd6517 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs
@@ -4,7 +4,8 @@ use tinyagents_orchestration::steering::SteerError;
/// The context these tests dispatch under: the calling task's own.
fn cx() -> crate::core::runtime::DispatchCtx {
crate::core::runtime::DispatchCtx::ambient()
-}
+ parent_epoch: None,
+ }
use crate::agent::orchestration::fleet_tools::FleetToolSet;
use crate::agent::orchestration::running_subagents::resolve::resume_ref_for_task;
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 6093af3359..7a28c0ca82 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
@@ -139,6 +139,7 @@ pub fn upsert_running(
task_title: upsert.task_title.clone(),
current_task_id: None,
lifecycle_key: None,
+ parent_epoch: None,
status: DurableSubagentStatus::Running,
reusable: true,
latest_history: None,
@@ -155,6 +156,8 @@ pub fn upsert_running(
session.display_name = upsert.display_name.or(session.display_name);
session.current_task_id = Some(upsert.task_id);
session.lifecycle_key = upsert.lifecycle_key.or(session.lifecycle_key);
+ // This belongs to the new task, not the reusable session's previous run.
+ session.parent_epoch = upsert.parent_epoch;
session.status = DurableSubagentStatus::Running;
session.reusable = true;
session.latest_error = None;
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops_driver.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops_driver.rs
index 5796ea700e..02e7ae6c23 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops_driver.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops_driver.rs
@@ -118,6 +118,7 @@ pub struct DriverTaskCancellation {
pub subagent_session_id: String,
pub workspace_dir: std::path::PathBuf,
pub parent_thread_id: Option<String>,
+ pub parent_epoch: Option<u64>,
pub already_finished: Option<tinyagents_orchestration::subagent::FinishedOutcome>,
pub was_paused: bool,
pub lifecycle_key: Option<tinyagents_orchestration::subagent::SubagentTaskKey>,
@@ -245,6 +246,7 @@ pub fn cancel_driver_job_by_task(
subagent_session_id: session.subagent_session_id,
workspace_dir: store.workspace_dir.clone(),
parent_thread_id: session.parent_thread_id,
+ parent_epoch: session.parent_epoch,
already_finished,
was_paused,
lifecycle_key,
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops_tests.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops_tests.rs
index 5f1f932993..389eacad39 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops_tests.rs
@@ -1,7 +1,17 @@
use super::*;
use crate::agent::orchestration::subagent_sessions::types::SubagentSessionUpsert;
use tinyagents_harness_tools::subagents::SubAgentJobRegistry;
-use tinyagents_orchestration::subagent::{FinishedOutcome, SubagentTaskKey};
+use tinyagents_orchestration::subagent::{FinishedOutcome, SubagentTaskKey parent_epoch: None,
+ parent_epoch: None,
+ let first = upsert_r parent_epoch: None,
+ let store = SubagentSessionStore::new(dir.p parent_epoch: None,
+ parent_run_id: "turn-run" parent_epoch: None,
+ task_id: " parent_epoch: None,
+ let d parent_epoch: None,
+fn cancellation_reason_is_written_only_fo parent_epoch: None,
+ task_tit parent_epoch: None,
+#[te parent_epoch: None,
+ normalize};
fn selector(task_key: &str) -> SubagentSessionSelector {
SubagentSessionSelector {
@@ -12,7 +22,13 @@ fn selector(task_key: &str) -> SubagentSessionSelector {
sandbox_mode: "read_only".into(),
action_root: Some("/tmp/work".into()),
task_key: task_key.into(),
- }
+ parent_epoch: None,
+ assert parent_epoch: None,
+ assert_eq!(current.lifecycle_key, Some(secon parent_epoch: None,
+ parent_epoch: None,
+ life parent_epoch: None,
+ &se parent_epoch: None,
+ .}
}
#[test]
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/storage_schema.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/storage_schema.rs
index 9f386758b7..85ecca7f6d 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/storage_schema.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/storage_schema.rs
@@ -38,7 +38,7 @@ pub(super) fn legacy_collections() -> Vec<CollectionSpec> {
}
/// The camelCase v1 field and its snake_case v2 name, for the plain copies.
-const COPIES: [(&str, &str); 17] = [
+const COPIES: [(&str, &str); 18] = [
("subagentSessionId", "subagent_session_id"),
("parentSession", "parent_session"),
("parentThreadId", "parent_thread_id"),
@@ -52,6 +52,7 @@ const COPIES: [(&str, &str); 17] = [
("taskTitle", "task_title"),
("currentTaskId", "current_task_id"),
("lifecycleKey", "lifecycle_key"),
+ ("parentEpoch", "parent_epoch"),
("status", "status"),
("reusable", "reusable"),
("latestHistory", "latest_history"),
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
index 72cbe0dfee..4cc7c5ec8b 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
@@ -62,6 +62,8 @@ fn collections() -> Vec<CollectionSpec> {
struct StoredSession {
#[serde(default, skip_serializing_if = "Option::is_none")]
lifecycle_key: Option<tinyagents_orchestration::subagent::SubagentTaskKey>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ parent_epoch: Option<u64>,
subagent_session_id: String,
parent_session: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -101,6 +103,7 @@ impl From<&DurableSubagentSession> for StoredSession {
let updated_ms = rfc3339_ms(&s.updated_at).unwrap_or(created_ms);
Self {
lifecycle_key: s.lifecycle_key.clone(),
+ parent_epoch: s.parent_epoch,
subagent_session_id: s.subagent_session_id.clone(),
parent_session: s.parent_session.clone(),
parent_thread_id: s.parent_thread_id.clone(),
@@ -128,6 +131,7 @@ impl From<StoredSession> for DurableSubagentSession {
fn from(s: StoredSession) -> Self {
Self {
lifecycle_key: s.lifecycle_key,
+ parent_epoch: s.parent_epoch,
subagent_session_id: s.subagent_session_id,
parent_session: s.parent_session,
parent_thread_id: s.parent_thread_id,
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 f50c0d3d7f..d71eb2719c 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
@@ -224,6 +224,7 @@ impl TinySessionStore for OpenHumanTinySubagentSessionStore {
worker_thread_id: upsert.worker_thread_id,
task_id: upsert.task_id,
lifecycle_key: Some(upsert.lifecycle_key),
+ parent_epoch: upsert.parent_epoch,
},
reuse.as_ref(),
)
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/types.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/types.rs
index dd9ed64491..0b7979e389 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/types.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/types.rs
@@ -65,6 +65,11 @@ pub struct DurableSubagentSession {
/// validated checkpoint before resuming or controlling a live child.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lifecycle_key: Option<tinyagents_orchestration::subagent::SubagentTaskKey>,
+ /// Parent lifecycle generation admitted for `current_task_id`. Older rows
+ /// intentionally remain `None`; callers must never synthesize a new epoch
+ /// when settling a legacy paused child.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub parent_epoch: Option<u64>,
pub status: DurableSubagentStatus,
pub reusable: bool,
#[serde(default, with = "crate::agent::messages::history_wire::option")]
@@ -146,6 +151,8 @@ pub struct SubagentSessionUpsert {
/// retained for legacy host paths and rows written before this field was
/// introduced.
pub lifecycle_key: Option<tinyagents_orchestration::subagent::SubagentTaskKey>,
+ /// Admission generation captured for this exact current task.
+ pub parent_epoch: Option<u64>,
}
#[derive(Debug, Clone)]
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_host_impl.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_host_impl.rs
index 290d6832d1..eede28a35f 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_host_impl.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_host_impl.rs
@@ -329,6 +329,7 @@ impl SpawnAsyncSubagentTool {
parent_thread_id.clone(),
),
),
+ parent_epoch: completion_target.parent_epoch(),
},
reusable.as_ref(),
) {
@@ -502,4 +503,5 @@ impl SpawnAsyncSubagentTool {
&fleet,
))
}
-}
+ parent_epoch: None,
+ }
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
index 8186eb6882..8e550dbfa8 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
@@ -6,7 +6,8 @@ use std::collections::HashSet;
use std::path::Path;
use std::sync::Arc;
use tinyagents_harness::run_queue::RunQueue;
-use tinytools::{ToolCallOptions, ToolRunContext};
+use tinytools::{ToolCallOptions, ToolRunContext parent_epoch: None,
+ };
use tinytools_agent::dialect::ToolCallFormat;
struct ThreadContext(&'static str);
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_continuation_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_continuation_tests.rs
index 0c39f4d048..45b888e3dd 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_continuation_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_continuation_tests.rs
@@ -103,7 +103,8 @@ async fn continue_subagent_resumes_paused_job_through_shared_driver() {
completed_job.output.as_deref(),
Some("paused-child-resumed-answer")
);
-}
+ parent_epoch: None,
+ crate::agent::orchestration::subagent_sessions::}
#[tokio::test]
async fn cancelling_a_paused_driver_through_rpc_settles_once_with_its_reason() {
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_tests.rs
index 37cacb89b6..8a84df5d00 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_tests.rs
@@ -36,6 +36,7 @@ async fn spawn_subagent_tool_runs_child_agent_e2e() {
"context": "parent supplied context",
"model": "test-model",
"blocking": true
+ parent_epoch: None,
}),
None,
live.data.child(),
diff --git a/crates/openhuman-core/src/agent_runs/subagent_control.rs b/crates/openhuman-core/src/agent_runs/subagent_control.rs
index d83b30d6f3..87c4a5a935 100644
--- a/crates/openhuman-core/src/agent_runs/subagent_control.rs
+++ b/crates/openhuman-core/src/agent_runs/subagent_control.rs
@@ -231,9 +231,6 @@ async fn settle_paused_driver_cancel(
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}"))?;
@@ -242,15 +239,18 @@ async fn settle_paused_driver_cancel(
Some(reason) => format!("cancelled by user. Reason: {reason}"),
None => "cancelled by user".to_string(),
};
+ let parent_epoch = paused.parent_thread_id.as_ref().map(|_| parent_epoch.unwrap_or(0));
background_completions::record_outcome_with_parent_epoch(
ctx,
&paused.workspace_dir,
&paused.parent_session,
&paused.task_id,
&paused.agent_id,
- &error,
+ format!(
+ "[SUBAGENT_FAILED] the async sub-agent errored before producing a result: {error}"
+ ),
paused.parent_thread_id.clone(),
- Some(parent_epoch),
+ parent_epoch,
background_completions::BackgroundAgentOutcome::Failed,
)
.await;
diff --git a/crates/openhuman-core/src/agent_runs/subagent_control_tests.rs b/crates/openhuman-core/src/agent_runs/subagent_control_tests.rs
index b0da293296..4151d09bbf 100644
--- a/crates/openhuman-core/src/agent_runs/subagent_control_tests.rs
+++ b/crates/openhuman-core/src/agent_runs/subagent_control_tests.rs
@@ -11,7 +11,8 @@ fn registered_controllers_match_schemas() {
assert_eq!(schema_for("subagent_cancel").function, "cancel");
assert_eq!(schema_for("subagent_steer").namespace, "subagent");
assert_eq!(schema_for("subagent_steer").function, "steer");
-}
+ parent_epoch: None,
+ }
#[test]
fn require_str_rejects_blank_and_missing() {
@@ -184,6 +185,7 @@ async fn cancelling_a_paused_driver_queues_a_failed_delivery_with_reason() {
worker_thread_id: None,
task_id: task_id.into(),
lifecycle_key: Some(key.clone()),
+ parent_epoch: Some(1),
},
None,
)
@@ -207,6 +209,7 @@ async fn cancelling_a_paused_driver_queues_a_failed_delivery_with_reason() {
subagent_session_id: session.subagent_session_id.clone(),
workspace_dir: workspace_dir.clone(),
parent_thread_id: parent_thread.clone(),
+ parent_epoch: Some(1),
already_finished: None,
was_paused: true,
lifecycle_key: Some(key),
@@ -216,6 +219,7 @@ async fn cancelling_a_paused_driver_queues_a_failed_delivery_with_reason() {
assert!(settle_paused_driver_cancel(
&DispatchCtx::process_default(),
&observed,
+ observed.parent_epoch,
Some("user requested stop")
)
.await