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 for subagents

Steven Enamakelb79ade4df82e
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 1666dcc957..3656f06def 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -191,7 +191,7 @@ pub(crate) async fn record_outcome(
.await;
}
-pub(super) async fn record_outcome_with_parent_epoch(
+pub(crate) async fn record_outcome_with_parent_epoch(
ctx: &DispatchCtx,
workspace_dir: &Path,
parent_session: &str,
@@ -304,6 +304,26 @@ pub(super) 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,
workspace_dir: &Path,
diff --git a/crates/openhuman-core/src/agent/orchestration/completion_target.rs b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
index 9676c2bdab..44f46d6b11 100644
--- a/crates/openhuman-core/src/agent/orchestration/completion_target.rs
+++ b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
@@ -79,6 +79,10 @@ impl CompletionTarget {
})
}
+ pub(crate) fn parent_epoch(&self) -> Option<u64> {
+ self.parent_epoch
+ }
+
/// Queue a finished result. See [`record_completion`].
pub(crate) async fn completed(&self, task_id: &str, agent_id: &str, summary: String) {
record_outcome_with_parent_epoch(
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 15a3eaa441..e37d705ee2 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
@@ -52,6 +52,9 @@ pub(crate) struct RunningSubagentMetadata {
/// `None` for a headless spawn with no originating thread. Used to abort the
/// sub-agent when its parent thread is deleted (see [`super::cancel::cancel_for_thread`]).
pub(crate) parent_thread_id: Option<String>,
+ /// Parent lifecycle generation admitted when this child was registered.
+ /// Its eventual cancellation record must not be attached to a later turn.
+ pub(crate) parent_epoch: Option<u64>,
pub(crate) run_queue: Arc<RunQueue<crate::agent::queued_turn::QueuedTurn>>,
}
@@ -71,6 +74,8 @@ pub(crate) enum RegistrationError {
Scope,
#[error("detached task registration failed: {0}")]
Registry(#[source] tinyagents_harness::error::TinyAgentsError),
+ #[error("parent lifecycle admission failed: {0}")]
+ ParentLifecycle(#[source] anyhow::Error),
}
/// Check the actual caller before any reusable-session or worker-thread write.
@@ -218,12 +223,31 @@ pub(crate) fn register(
task_id,
thread_id
);
+ return Err(RegistrationError::ParentLifecycle(anyhow::anyhow!(
+ "parent thread is stopped or deleted"
+ )));
}
// First spawn in a workspace this process has not scanned: redeliver any
// completions a previous process finished there but never delivered.
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));
+ }
+ },
+ None => None,
+ };
+
let metadata = RunningSubagentMetadata {
profile,
parent_session: parent_session.clone(),
@@ -231,6 +255,7 @@ pub(crate) fn register(
subagent_session_id: subagent_session_id.clone(),
workspace_dir: workspace_dir.clone(),
parent_thread_id: parent_thread_id.clone(),
+ parent_epoch,
run_queue,
};
registry()