Skip to content

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

feat(threads): guard against purging threads with active agents

Steven Enamakele7947ad5c974
diff --git a/crates/openhuman-core/src/agent/orchestration/busy_guard.rs b/crates/openhuman-core/src/agent/orchestration/busy_guard.rs
index 5f31e0a976..a2fec43d94 100644
--- a/crates/openhuman-core/src/agent/orchestration/busy_guard.rs
+++ b/crates/openhuman-core/src/agent/orchestration/busy_guard.rs
@@ -67,10 +67,10 @@ impl TurnBusy {
const THREAD_MARK: char = '\u{2}';
/// The thread a busy session id is a turn on.
-fn thread_of(session: &str) -> Option<String> {
+fn thread_of(ctx: &crate::core::runtime::DispatchCtx, session: &str) -> Option<String> {
match session.strip_prefix(THREAD_MARK) {
Some(thread) => Some(thread.to_string()),
- None => background_completions::thread_for_session(session),
+ None => background_completions::thread_for_session(ctx, session),
}
}
@@ -95,7 +95,7 @@ pub(crate) fn is_busy(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str)
.expect("background_delivery busy poisoned")
.keys()
.filter_map(|key| session_of(key, &me))
- .any(|session| thread_of(session).as_deref() == Some(thread_id))
+ .any(|session| thread_of(ctx, session).as_deref() == Some(thread_id))
}
/// Forget every in-flight turn of the calling profile on `thread_id`. A turn
@@ -108,7 +108,7 @@ pub(crate) fn clear_busy_for_thread(ctx: &crate::core::runtime::DispatchCtx, thr
let mut busy = busy().lock().expect("background_delivery busy poisoned");
let before = busy.len();
busy.retain(|key, _| {
- session_of(key, &me).is_none_or(|session| thread_of(session).as_deref() != Some(thread_id))
+ session_of(key, &me).is_none_or(|session| thread_of(ctx, session).as_deref() != Some(thread_id))
});
let cleared = before - busy.len();
if cleared > 0 {
diff --git a/crates/openhuman-core/src/threads/ops/crud.rs b/crates/openhuman-core/src/threads/ops/crud.rs
index bffb8bfef5..e4f73c83f9 100644
--- a/crates/openhuman-core/src/threads/ops/crud.rs
+++ b/crates/openhuman-core/src/threads/ops/crud.rs
@@ -468,6 +468,7 @@ async fn thread_delete_inner(
let cancelled =
crate::agent::orchestration::running_subagents::cancel_for_thread(&ctx, &request.thread_id);
let discarded = crate::agent::orchestration::background_completions::discard_for_thread(
+ &ctx,
&dir,
&request.thread_id,
);
diff --git a/crates/openhuman-core/src/threads/ops/purge.rs b/crates/openhuman-core/src/threads/ops/purge.rs
index 0b7a1f0db3..4e3f4d4a6c 100644
--- a/crates/openhuman-core/src/threads/ops/purge.rs
+++ b/crates/openhuman-core/src/threads/ops/purge.rs
@@ -35,9 +35,9 @@ async fn threads_purge_inner(
let cancelled_threads = running_subagents::cancel_all(&ctx);
let mut discarded = 0;
for thread_id in &cancelled_threads {
- discarded += background_completions::discard_for_thread(&dir, thread_id);
+ discarded += background_completions::discard_for_thread(&ctx, &dir, thread_id);
}
- discarded += background_completions::clear_all(&dir);
+ discarded += background_completions::clear_all(&ctx, &dir);
log::debug!(
"[threads] threads_purge cancelled_threads={} discarded_completions={}",
cancelled_threads.len(),
diff --git a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
index ff16f2c778..4eca5bc1ea 100644
--- a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
+++ b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
@@ -161,11 +161,12 @@ async fn cancel_chat_inner(
// the gap between abort and the queue sweep.
let discarded =
crate::agent::orchestration::background_completions::discard_pending_for_thread(
+ ctx,
thread_id,
);
let stopped = crate::agent::orchestration::running_subagents::stop_for_thread(ctx, thread_id);
crate::agent::orchestration::background_completions::finish_stop_for_thread(
- thread_id, &stopped,
+ ctx, thread_id, &stopped,
);
log::info!(
"[web-channel] stop thread_id={} turn={:?} parallel={} subagents_cancelled={} completions_discarded={}",
diff --git a/crates/openhuman-core/src/web_chat/ops/start_chat.rs b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
index 7d767110d6..4e052604ad 100644
--- a/crates/openhuman-core/src/web_chat/ops/start_chat.rs
+++ b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
@@ -348,7 +348,7 @@ async fn start_chat_inner(
// A fresh accepted user request is the explicit boundary after Stop. Keep
// the gate installed through validation and registry cancellation so a
// child registering late cannot deliver into the stopped generation.
- crate::agent::orchestration::background_completions::resume_stopped_thread(&thread_id);
+ crate::agent::orchestration::background_completions::resume_stopped_thread(ctx, &thread_id);
let map_key = key_for(ctx, &thread_id);