Skip to content

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

refactor(orchestration): update background completion tracking with epoch

Steven Enamakelf36ad0187cac
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 00d4d354db..e9983af78e 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -519,7 +519,7 @@ fn mark_collected_in_entry(entry: Arc<Entry>, task_id: &str) -> bool {
}
/// Drop every queued completion for `thread_id` and everything that finishes
-/// for it later. Called when the thread is deleted: the router's cancelled-parent
+/// for it later. Called when the thread is deleted: the router's deleted-parent
/// marker is durable, and the thread is also remembered as deleted in memory, so
/// a straggler that wins the cooperative-abort race, a child that registers after
/// the delete, or a result that finishes after a restart is dropped rather than
@@ -565,29 +565,15 @@ fn is_marked_deleted(entry: &Entry, thread_id: &str) -> bool {
entry.store.get(&deleted_marker_id(thread_id)).is_some()
}
-/// Cancel `thread_id` for good in `entry`'s router and write the deleted marker.
-/// Failures are logged; the in-memory deleted set still gates this process.
+/// Permanently delete `thread_id` in the router. Failures are logged; the
+/// in-memory deleted set still gates this process.
fn cancel_deleted_parent(entry: &Entry, thread_id: &str) -> usize {
- let marker = CompletionRecord::new(
- deleted_marker_id(thread_id),
- "",
- "",
- CompletionStatus::Incomplete,
- CompletionResult::default(),
- )
- .with_notify_mode(NotifyMode::Off);
- if let Err(error) = entry.store.put(&marker) {
- log::error!(
- "[background_completions] could not persist the deleted marker thread_id={thread_id} \
- error={error}"
- );
- }
entry
.router
- .cancel_parent(thread_id)
+ .delete_parent(thread_id)
.unwrap_or_else(|error| {
log::error!(
- "[background_completions] cancel_parent failed thread_id={thread_id} error={error}"
+ "[background_completions] delete_parent failed thread_id={thread_id} error={error}"
);
0
})
@@ -680,11 +666,26 @@ pub(crate) fn mark_stopped_task_if_thread_stopped(
st.stopped_threads.contains(&thread_key) || st.deleted_threads.contains(&thread_key);
(first_sight, stopped)
};
- // A delete outlives a restart: the durable marker, not just this process's
- // memory, decides whether a late child belongs to a dead thread.
- if first_sight && !stopped && is_marked_deleted(&entry, thread_id) {
- state().deleted_threads.insert(thread_key);
- stopped = true;
+ // Existing host delete markers predate the router's distinct permanent
+ // deletion state. Preserve them, then consult the owner's lifecycle marker
+ // for threads deleted by current versions.
+ if first_sight && !stopped {
+ if is_marked_deleted(&entry, thread_id) {
+ state().deleted_threads.insert(thread_key.clone());
+ stopped = true;
+ } else {
+ // A Stop is explicitly lifted by a new accepted child, but a
+ // deleted parent cannot be resumed. Re-admit after attempting the
+ // resume so deletion and storage errors fail closed.
+ entry.router.resume_parent(thread_id);
+ match entry.router.admit_child_generation(thread_id) {
+ Ok(Some(_)) => {}
+ Ok(None) | Err(_) => {
+ state().deleted_threads.insert(thread_key.clone());
+ stopped = true;
+ }
+ }
+ }
}
if stopped {
if let Err(error) = entry.router.tombstone(task_id) {
@@ -692,9 +693,6 @@ pub(crate) fn mark_stopped_task_if_thread_stopped(
}
return true;
}
- if first_sight {
- entry.router.resume_parent(thread_id);
- }
false
}
diff --git a/crates/openhuman-core/src/agent/orchestration/completion_target.rs b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
index 2bb91ed6b5..6af4b53c85 100644
--- a/crates/openhuman-core/src/agent/orchestration/completion_target.rs
+++ b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
@@ -9,7 +9,7 @@ use std::path::PathBuf;
use super::background_completions::state;
-use super::background_completions::{record_awaiting_input, record_completion, record_failure};
+use super::background_completions::{record_outcome_with_parent_epoch, BackgroundAgentOutcome};
#[cfg(test)]
#[path = "completion_target_tests.rs"]
@@ -49,6 +49,7 @@ pub(crate) struct CompletionTarget {
workspace_dir: PathBuf,
parent_session: String,
parent_thread_id: Option<String>,
+ parent_epoch: Option<u64>,
}
impl CompletionTarget {
@@ -57,18 +58,32 @@ impl CompletionTarget {
workspace_dir: PathBuf,
parent_session: String,
parent_thread_id: Option<String>,
- ) -> Self {
- Self {
+ ) -> anyhow::Result<Self> {
+ let parent_epoch = if let Some(thread_id) = parent_thread_id.as_deref() {
+ let router = super::background_completions::router_for_workspace_ctx(
+ &ctx,
+ &workspace_dir,
+ );
+ Some(
+ router
+ .admit_child_generation(thread_id)?
+ .ok_or_else(|| anyhow::anyhow!("parent thread is stopped or deleted"))?,
+ )
+ } else {
+ None
+ };
+ Ok(Self {
ctx,
workspace_dir,
parent_session,
parent_thread_id,
- }
+ parent_epoch,
+ })
}
/// Queue a finished result. See [`record_completion`].
pub(crate) async fn completed(&self, task_id: &str, agent_id: &str, summary: String) {
- record_completion(
+ record_outcome_with_parent_epoch(
&self.ctx,
&self.workspace_dir,
&self.parent_session,
@@ -76,20 +91,24 @@ impl CompletionTarget {
agent_id,
summary,
self.parent_thread_id.clone(),
+ self.parent_epoch,
+ BackgroundAgentOutcome::Completed,
)
.await;
}
/// Queue a failure. See [`record_failure`].
pub(crate) async fn failed(&self, task_id: &str, agent_id: &str, error: &str) {
- record_failure(
+ record_outcome_with_parent_epoch(
&self.ctx,
&self.workspace_dir,
&self.parent_session,
task_id,
agent_id,
- error,
+ format!("[SUBAGENT_FAILED] the async sub-agent errored before producing a result: {error}"),
self.parent_thread_id.clone(),
+ self.parent_epoch,
+ BackgroundAgentOutcome::Failed,
)
.await;
}
@@ -102,15 +121,24 @@ impl CompletionTarget {
question: &str,
checkpointed: bool,
) {
- record_awaiting_input(
- &self.ctx,
- &self.workspace_dir,
- &self.parent_session,
+ let record_id = awaiting_input_record_id(task_id);
+ let summary = crate::agent::orchestration::tools::awaiting_user::awaiting_user_envelope(
task_id,
agent_id,
+ None,
question,
checkpointed,
+ );
+ record_outcome_with_parent_epoch(
+ &self.ctx,
+ &self.workspace_dir,
+ &self.parent_session,
+ record_id,
+ agent_id,
+ summary,
self.parent_thread_id.clone(),
+ self.parent_epoch,
+ BackgroundAgentOutcome::AwaitingInput,
)
.await;
}
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 f27e3f4f60..30eee13402 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
@@ -384,7 +384,7 @@ impl SpawnAsyncSubagentTool {
parent.workspace_dir.clone(),
parent_session.clone(),
parent_thread_id.clone(),
- );
+ )?;
let background_worker_thread_id = worker_thread_id.clone();
let background_store = store.clone();
let background_subagent_session_id = durable_session.subagent_session_id.clone();