Skip to content

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

feat(orchestration): add parent epoch tracking to subagent registration

Steven Enamakeled875062e6ca
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
index ece645cb93..b1f4fef198 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
@@ -184,6 +184,7 @@ pub(crate) fn register(
subagent_session_id: Option<String>,
workspace_dir: PathBuf,
parent_thread_id: Option<String>,
+ parent_epoch: Option<u64>,
run_queue: Arc<RunQueue<crate::agent::queued_turn::QueuedTurn>>,
abort: AbortHandle,
status: watch::Receiver<DetachedSubagentStatus>,
@@ -232,23 +233,38 @@ pub(crate) fn register(
crate::agent::orchestration::background_delivery::recover_on_boot(ctx, &workspace_dir);
}
- let parent_epoch = match parent_thread_id.as_deref() {
- Some(thread_id) => {
- match crate::agent::orchestration::background_completions::CompletionTarget::new(
- ctx.clone(),
- workspace_dir.clone(),
- parent_session.clone(),
- Some(thread_id.to_owned()),
- ) {
- Ok(target) => target.parent_epoch(),
- Err(error) => {
- abort.abort();
- return Err(RegistrationError::ParentLifecycle(error));
- }
+ if let Some(thread_id) = parent_thread_id.as_deref() {
+ let Some(parent_epoch) = parent_epoch else {
+ abort.abort();
+ return Err(RegistrationError::ParentLifecycle(anyhow::anyhow!(
+ "parent admission generation was not captured at spawn"
+ )));
+ };
+ let router = crate::agent::orchestration::background_completions::router_for_workspace_ctx(
+ ctx,
+ &workspace_dir,
+ );
+ match router.parent_generation_admitted(thread_id, parent_epoch) {
+ Ok(true) => {}
+ Ok(false) => {
+ let _ = router.tombstone(&task_id);
+ abort.abort();
+ return Err(RegistrationError::ParentLifecycle(anyhow::anyhow!(
+ "parent admission generation is no longer current"
+ )));
+ }
+ Err(error) => {
+ let _ = router.tombstone(&task_id);
+ abort.abort();
+ return Err(RegistrationError::ParentLifecycle(error.into()));
}
}
- None => None,
- };
+ } else if parent_epoch.is_some() {
+ abort.abort();
+ return Err(RegistrationError::ParentLifecycle(anyhow::anyhow!(
+ "threadless child cannot carry a parent admission generation"
+ )));
+ }
let metadata = RunningSubagentMetadata {
profile,
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 186da98776..5fd11e2e61 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs
@@ -101,15 +101,29 @@ fn register_test_with_thread(
rq: Arc<RunQueue<QueuedTurn>>,
) -> watch::Sender<DetachedSubagentStatus> {
let (tx, rx) = status_channel();
+ let ctx = cx();
+ let workspace = test_workspace();
+ let parent_epoch = parent_thread_id.map(|thread_id| {
+ crate::agent::orchestration::background_completions::CompletionTarget::new(
+ ctx.clone(),
+ workspace.clone(),
+ parent_session.to_owned(),
+ Some(thread_id.to_owned()),
+ )
+ .expect("capture parent generation at spawn")
+ .parent_epoch()
+ .expect("threaded spawn has a parent generation")
+ });
register(
- &cx(),
+ &ctx,
task_id.into(),
"researcher".into(),
parent_session.into(),
None,
None,
- test_workspace(),
+ workspace,
parent_thread_id.map(Into::into),
+ parent_epoch,
rq,
dummy_abort(),
rx,
@@ -118,6 +132,54 @@ fn register_test_with_thread(
tx
}
+#[tokio::test]
+async fn late_registration_rejects_a_parent_generation_reopened_after_spawn() {
+ let _guard = test_guard_async().await;
+ let ctx = cx();
+ let workspace = tempfile::tempdir().expect("workspace");
+ let thread_id = "late-register-old-generation";
+ let old_epoch = crate::agent::orchestration::background_completions::CompletionTarget::new(
+ ctx.clone(),
+ workspace.path().to_path_buf(),
+ "late-register-parent".into(),
+ Some(thread_id.into()),
+ )
+ .expect("initial spawn admitted")
+ .parent_epoch()
+ .unwrap();
+ let router = crate::agent::orchestration::background_completions::router_for_workspace_ctx(
+ &ctx,
+ workspace.path(),
+ );
+ router
+ .cancel_parent(thread_id)
+ .expect("stop old generation");
+ router.resume_parent(thread_id);
+ assert!(!router
+ .parent_generation_admitted(thread_id, old_epoch)
+ .unwrap());
+
+ let child = tokio::spawn(std::future::pending::<()>());
+ let (_status_tx, status_rx) = status_channel();
+ let result = register(
+ &ctx,
+ "late-register-old-task".into(),
+ "researcher".into(),
+ "late-register-parent".into(),
+ None,
+ None,
+ workspace.path().to_path_buf(),
+ Some(thread_id.into()),
+ Some(old_epoch),
+ run_queue(),
+ child.abort_handle(),
+ status_rx,
+ );
+ assert!(matches!(result, Err(RegistrationError::ParentLifecycle(_))));
+ assert!(child.await.unwrap_err().is_cancelled());
+ assert!(cancel_by_task(&ctx, "late-register-old-task").is_none());
+}
+
#[tokio::test]
async fn task_store_records_spawn_complete_and_cancel() {
let _guard = test_guard_async().await;
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 210272175f..ff2cbe4ee6 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
@@ -153,6 +153,85 @@ fn stale_completion_cannot_overwrite_reused_or_closed_session() {
assert!(closed.latest_history.is_none());
}
+#[test]
+fn reused_session_replaces_parent_epoch_for_each_current_task() {
+ let dir = tempfile::tempdir().unwrap();
+ let store = SubagentSessionStore::new(dir.path().to_path_buf());
+ let first = upsert_running(
+ &store,
+ SubagentSessionUpsert {
+ selector: selector("epoch-reuse"),
+ display_name: None,
+ task_title: "First run".into(),
+ worker_thread_id: None,
+ task_id: "epoch-task-1".into(),
+ lifecycle_key: None,
+ parent_epoch: Some(7),
+ },
+ None,
+ )
+ .unwrap();
+ assert_eq!(first.parent_epoch, Some(7));
+ mark_finished(
+ &store,
+ &first.subagent_session_id,
+ "epoch-task-1",
+ &SubagentRunStatus::Completed,
+ Vec::new(),
+ )
+ .unwrap();
+
+ let second = upsert_running(
+ &store,
+ SubagentSessionUpsert {
+ selector: selector("epoch-reuse"),
+ display_name: None,
+ task_title: "Second run".into(),
+ worker_thread_id: None,
+ task_id: "epoch-task-2".into(),
+ lifecycle_key: None,
+ parent_epoch: Some(8),
+ },
+ Some(&first),
+ )
+ .unwrap();
+ assert_eq!(second.subagent_session_id, first.subagent_session_id);
+ assert_eq!(second.parent_epoch, Some(8));
+ mark_finished(
+ &store,
+ &second.subagent_session_id,
+ "epoch-task-2",
+ &SubagentRunStatus::Completed,
+ Vec::new(),
+ )
+ .unwrap();
+
+ let legacy = upsert_running(
+ &store,
+ SubagentSessionUpsert {
+ selector: selector("epoch-reuse"),
+ display_name: None,
+ task_title: "Legacy run".into(),
+ worker_thread_id: None,
+ task_id: "epoch-task-3".into(),
+ lifecycle_key: None,
+ parent_epoch: None,
+ },
+ Some(&second),
+ )
+ .unwrap();
+ assert_eq!(legacy.subagent_session_id, first.subagent_session_id);
+ assert_eq!(legacy.parent_epoch, None);
+ let reopened = store
+ .load()
+ .unwrap()
+ .into_iter()
+ .find(|session| session.subagent_session_id == first.subagent_session_id)
+ .unwrap();
+ assert_eq!(reopened.current_task_id.as_deref(), Some("epoch-task-3"));
+ assert_eq!(reopened.parent_epoch, None);
+}
+
#[test]
fn driver_task_cancel_reports_legacy_failure_and_refuses_ambiguous_ids() {
let dir = tempfile::tempdir().unwrap();
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 e08c1efbc1..0b9f70c39a 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
@@ -88,6 +88,7 @@ async fn late_registration_refusal_aborts_the_worker_and_settles_its_durable_ses
Some(session.subagent_session_id.clone()),
workspace.path().to_path_buf(),
None,
+ None,
Arc::new(RunQueue::new()),
join.abort_handle(),
rx,
diff --git a/crates/openhuman-core/src/web_chat/web_tests_session_and_concurrency_tests.rs b/crates/openhuman-core/src/web_chat/web_tests_session_and_concurrency_tests.rs
index 6229bc1e27..c2d16a991a 100644
--- a/crates/openhuman-core/src/web_chat/web_tests_session_and_concurrency_tests.rs
+++ b/crates/openhuman-core/src/web_chat/web_tests_session_and_concurrency_tests.rs
@@ -619,8 +619,17 @@ async fn unscoped_cancel_stops_the_threads_detached_subagents() {
let workspace = tempfile::tempdir().expect("workspace");
let child = tokio::spawn(std::future::pending::<()>());
let (_status_tx, status_rx) = running_subagents::status_channel();
+ let ctx = crate::core::runtime::DispatchCtx::ambient();
+ let parent_epoch = crate::agent::orchestration::background_completions::CompletionTarget::new(
+ ctx.clone(),
+ workspace.path().to_path_buf(),
+ "session-web-stop".into(),
+ Some(thread_id.into()),
+ )
+ .expect("capture generation at spawn")
+ .parent_epoch();
running_subagents::register(
- &crate::core::runtime::DispatchCtx::ambient(),
+ &ctx,
"task-web-stop-1".into(),
"researcher".into(),
"session-web-stop".into(),
@@ -628,6 +637,7 @@ async fn unscoped_cancel_stops_the_threads_detached_subagents() {
None,
workspace.path().to_path_buf(),
Some(thread_id.into()),
+ parent_epoch,
std::sync::Arc::new(tinyagents_harness::run_queue::RunQueue::new()),
child.abort_handle(),
status_rx,