Skip to content

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

refactor(agent): adjust background completion logic and tests

Steven Enamakel181f7034ad9e
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 3656f06def..21941d7219 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -712,20 +712,27 @@ pub(crate) fn mark_stopped_task_if_thread_stopped(
entry.router.resume_parent(thread_id);
match entry.router.admit_child_generation(thread_id) {
Ok(Some(_)) => {}
- Ok(None) => {
- state().deleted_threads.insert(thread_key.clone());
- stopped = true;
- }
- Err(error) => {
- // Refuse this admission, but do not turn a transient store
- // failure into process-lifetime deletion. A later spawn can
- // retry once the owner store is healthy.
- log::warn!(
- "[background_completions] parent admission failed thread_id={thread_id} error={error}"
- );
+ admission => {
+ // Neither a closed lifecycle nor a transient store error
+ // proves permanent deletion: resume may have failed while
+ // the subsequent admission successfully read the old Stop
+ // marker. Refuse this child and let the next one retry.
+ if let Err(error) = admission {
+ log::warn!(
+ "[background_completions] parent admission failed thread_id={thread_id} error={error}"
+ );
+ }
if let Err(tombstone_error) = entry.router.tombstone(task_id) {
log::warn!("[background_completions] tombstone failed task_id={task_id} error={tombstone_error}");
}
+ let mut st = state();
+ if st
+ .thread_workspaces
+ .get(&thread_key)
+ .is_some_and(|path| path == workspace_dir)
+ {
+ st.thread_workspaces.remove(&thread_key);
+ }
return true;
}
}
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions_migration_tests.rs b/crates/openhuman-core/src/agent/orchestration/background_completions_migration_tests.rs
index d5fa5b42fc..5d10625584 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions_migration_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions_migration_tests.rs
@@ -112,6 +112,14 @@ impl CompletionStore for FullDisk {
struct ToggleStore {
inner: InMemoryCompletionStore,
fail_writes: AtomicBool,
+ fail_next_write: AtomicBool,
+}
+
+impl ToggleStore {
+ fn should_fail_write(&self) -> bool {
+ self.fail_writes.load(Ordering::SeqCst)
+ || self.fail_next_write.swap(false, Ordering::SeqCst)
+ }
}
impl CompletionStore for ToggleStore {
@@ -164,7 +172,7 @@ impl CompletionStore for ToggleStore {
}
fn put(&self, record: &CompletionRecord) -> tinyagents_harness::error::Result<()> {
- if self.fail_writes.load(Ordering::SeqCst) {
+ if self.should_fail_write() {
return Err(tinyagents_harness::error::TinyAgentsError::Graph(
"disk full".into(),
));
@@ -178,7 +186,7 @@ impl CompletionStore for ToggleStore {
parent_key: &str,
gate: &tinyagents_tasks::ParentGateClaim,
) -> tinyagents_harness::error::Result<()> {
- if self.fail_writes.load(Ordering::SeqCst) {
+ if self.should_fail_write() {
return Err(tinyagents_harness::error::TinyAgentsError::Graph(
"disk full".into(),
));
@@ -191,7 +199,7 @@ impl CompletionStore for ToggleStore {
record: &CompletionRecord,
gate: &tinyagents_tasks::ParentGateClaim,
) -> tinyagents_harness::error::Result<bool> {
- if self.fail_writes.load(Ordering::SeqCst) {
+ if self.should_fail_write() {
return Err(tinyagents_harness::error::TinyAgentsError::Graph(
"disk full".into(),
));
@@ -387,3 +395,37 @@ async fn transient_parent_admission_failure_does_not_permanently_delete_thread()
)
.is_ok());
}
+
+#[tokio::test]
+async fn failed_stop_resume_does_not_cache_permanent_deletion() {
+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;
+ let ws = workspace();
+ let w = ws.path();
+ let ctx = crate::core::runtime::DispatchCtx::ambient();
+ let store = Arc::new(ToggleStore::default());
+ install_store_for_test(w, store.clone());
+ router_for_workspace(w)
+ .cancel_parent("retry-after-resume-error")
+ .expect("the stop marker is durable before simulating the resume error");
+ store.fail_next_write.store(true, Ordering::SeqCst);
+
+ assert!(mark_stopped_task_if_thread_stopped(
+ &ctx,
+ w,
+ "retry-after-resume-error",
+ "rejected-child",
+ ));
+ assert!(!mark_stopped_task_if_thread_stopped(
+ &ctx,
+ w,
+ "retry-after-resume-error",
+ "accepted-child",
+ ));
+ assert!(CompletionTarget::new(
+ ctx,
+ w.to_path_buf(),
+ "retry-after-resume-session".into(),
+ Some("retry-after-resume-error".into()),
+ )
+ .is_ok());
+}