Skip to content

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

fix(threads): fence sub-agent owner registration during thread deletion

Steven Enamakeldd8885801a85
diff --git a/crates/openhuman-core/src/agent/orchestration/completion_owners.rs b/crates/openhuman-core/src/agent/orchestration/completion_owners.rs
index 44814ad949..8e09ee485c 100644
--- a/crates/openhuman-core/src/agent/orchestration/completion_owners.rs
+++ b/crates/openhuman-core/src/agent/orchestration/completion_owners.rs
@@ -110,6 +110,16 @@ fn remember_active_driver_task(
owner: DriverTaskOwner,
capacity: usize,
) -> Result<(), String> {
+ if let Some(thread_id) = owner.parent_thread_id.as_deref() {
+ let scope = driver_thread_scope(
+ owner.profile.clone(),
+ owner.workspace_dir.clone(),
+ thread_id.to_owned(),
+ );
+ if st.deleting_threads.contains(&scope) {
+ return Err("parent thread deletion is in progress".into());
+ }
+ }
if !st.driver_tasks.contains_key(&key) && st.driver_tasks.len() >= capacity {
return Err(format!(
"active sub-agent owner capacity ({capacity}) is full"
@@ -134,6 +144,38 @@ fn driver_thread_scope(
}
}
+/// Close owner registration before thread deletion starts its one-time child sweep.
+pub(crate) fn begin_thread_deletion(
+ ctx: &crate::core::runtime::DispatchCtx,
+ workspace_dir: &std::path::Path,
+ thread_id: &str,
+) -> Result<(), String> {
+ let profile = ctx
+ .tenant()
+ .map_err(|error| format!("cannot resolve profile for thread deletion: {error}"))?
+ .profile;
+ owners().deleting_threads.insert(driver_thread_scope(
+ profile,
+ workspace_dir.to_path_buf(),
+ thread_id.to_owned(),
+ ));
+ Ok(())
+}
+
+/// Release a deletion fence after either delete failure or durable parent closure.
+pub(crate) fn release_thread_deletion(
+ ctx: &crate::core::runtime::DispatchCtx,
+ workspace_dir: &std::path::Path,
+ thread_id: &str,
+) {
+ let profile = ctx.tenant().ok().and_then(|tenant| tenant.profile);
+ owners().deleting_threads.remove(&driver_thread_scope(
+ profile,
+ workspace_dir.to_path_buf(),
+ thread_id.to_owned(),
+ ));
+}
+
/// Move a settled task out of the non-evictable live-owner set while retaining
/// its scope for the bounded late cancel-by-task compatibility path.
pub(crate) fn finish_driver_task(ctx: &crate::core::runtime::DispatchCtx, task_id: &str) {
diff --git a/crates/openhuman-core/src/threads/ops/crud.rs b/crates/openhuman-core/src/threads/ops/crud.rs
index 12286d9935..f25b2b1f49 100644
--- a/crates/openhuman-core/src/threads/ops/crud.rs
+++ b/crates/openhuman-core/src/threads/ops/crud.rs
@@ -471,13 +471,29 @@ async fn thread_delete_inner(
request: DeleteConversationThreadRequest,
) -> Result<Outcome<ApiEnvelope<DeleteConversationThreadResponse>>, String> {
let dir: PathBuf = config.workspace_dir.clone();
- let deleted = conversations::blocking::delete_thread(
+ crate::agent::orchestration::completion_owners::begin_thread_deletion(
+ &ctx,
+ &dir,
+ &request.thread_id,
+ )?;
+ let deleted = match conversations::blocking::delete_thread(
ctx.clone(),
dir.clone(),
request.thread_id.clone(),
request.deleted_at.clone(),
)
- .await?;
+ .await
+ {
+ Ok(deleted) => deleted,
+ Err(error) => {
+ crate::agent::orchestration::completion_owners::release_thread_deletion(
+ &ctx,
+ &dir,
+ &request.thread_id,
+ );
+ return Err(error);
+ }
+ };
// Invalidate the in-process web-channel session BEFORE the
// turn-state cleanup. The snapshot deletion is fallible and
// returns early on error; if invalidation ran after, an active
@@ -509,6 +525,11 @@ async fn thread_delete_inner(
&dir,
&request.thread_id,
);
+ crate::agent::orchestration::completion_owners::release_thread_deletion(
+ &ctx,
+ &dir,
+ &request.thread_id,
+ );
log::debug!(
"[threads] thread_delete thread_id={} cancelled_subagents={} discarded_completions={}",
request.thread_id,