Skip to content

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

fix(agent): prevent background completion from dropping queued results

Steven Enamakel213840aacd0b
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index a32c99c6fd..e04a0288d3 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -23,7 +23,11 @@
use std::collections::{HashMap, HashSet, VecDeque};
// Keys of `HostState`'s thread and session maps: per profile (see `profile_key`).
-use crate::core::runtime::tenant::profile_key as key;
+/// The key of a thread or session id in `HostState`'s maps: per profile of `ctx`'s tenant.
+fn key(ctx: &crate::core::runtime::DispatchCtx, id: &str) -> String {
+ use crate::core::runtime::tenant;
+ tenant::profile_key_in(&tenant::isolated_unless_tenant(ctx.tenant(), "profile_key"), id)
+}
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;
@@ -192,27 +196,27 @@ pub(crate) fn router_for_workspace(workspace_dir: &Path) -> Arc<CompletionRouter
}
/// Remember which workspace holds `thread_id`'s completions.
-fn note_thread_workspace(thread_id: &str, workspace_dir: &Path) {
+fn note_thread_workspace(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str, workspace_dir: &Path) {
state()
.thread_workspaces
- .insert(key(thread_id), workspace_dir.to_path_buf());
+ .insert(key(ctx, thread_id), workspace_dir.to_path_buf());
}
/// The router that holds `thread_id`'s completions, if any child of that thread
/// was spawned or recorded in this process.
-pub(crate) fn router_for_thread(thread_id: &str) -> Option<Arc<CompletionRouter>> {
- let workspace = state().thread_workspaces.get(&key(thread_id)).cloned()?;
+pub(crate) fn router_for_thread(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str) -> Option<Arc<CompletionRouter>> {
+ let workspace = state().thread_workspaces.get(&key(ctx, thread_id)).cloned()?;
Some(router_for_workspace(&workspace))
}
/// The workspace holding `thread_id`'s completions, if known to this process.
-pub(crate) fn workspace_for_thread(thread_id: &str) -> Option<PathBuf> {
- state().thread_workspaces.get(&key(thread_id)).cloned()
+pub(crate) fn workspace_for_thread(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str) -> Option<PathBuf> {
+ state().thread_workspaces.get(&key(ctx, thread_id)).cloned()
}
/// Remember that `session_id` is a turn on `thread_id`.
-pub(crate) fn note_session_thread(session_id: &str, thread_id: &str) {
- let session_key = key(session_id);
+pub(crate) fn note_session_thread(ctx: &crate::core::runtime::DispatchCtx, session_id: &str, thread_id: &str) {
+ let session_key = key(ctx, session_id);
let mut st = state();
if st
.session_threads
@@ -230,8 +234,8 @@ pub(crate) fn note_session_thread(session_id: &str, thread_id: &str) {
/// The chat thread a session id belongs to: the cached mapping, else the
/// `thread_id` a web-channel session id carries in its JSON body.
-pub(crate) fn thread_for_session(session_id: &str) -> Option<String> {
- if let Some(thread) = state().session_threads.get(&key(session_id)) {
+pub(crate) fn thread_for_session(ctx: &crate::core::runtime::DispatchCtx, session_id: &str) -> Option<String> {
+ if let Some(thread) = state().session_threads.get(&key(ctx, session_id)) {
return Some(thread.clone());
}
if !session_id.starts_with('{') {
@@ -304,7 +308,7 @@ pub(crate) async fn record_outcome(
{
// The in-memory gates back up the router's durable cancelled-parent
// marker, so a failed `cancel_parent` write cannot let a late result in.
- let thread_key = key(&thread_id);
+ let thread_key = key(ctx, &thread_id);
let st = state();
if st.deleted_threads.contains(&thread_key) || st.stopped_threads.contains(&thread_key) {
log::debug!(
@@ -321,8 +325,8 @@ pub(crate) async fn record_outcome(
);
return;
}
- note_thread_workspace(&thread_id, workspace_dir);
- note_session_thread(parent_session, &thread_id);
+ note_thread_workspace(ctx, &thread_id, workspace_dir);
+ note_session_thread(ctx, parent_session, &thread_id);
super::completion_owners::note(ctx, &[&task_id, parent_session]); // owner, for off-task delivery
let record = CompletionRecord::new(
@@ -518,9 +522,9 @@ pub(crate) fn mark_collected(workspace_dir: &Path, task_id: &str) -> bool {
/// delivered into a thread that no longer exists. Takes the caller's workspace so
/// the marker is written even when no child of the thread was seen by this
/// process. Returns the number of queued completions removed.
-pub(crate) fn discard_for_thread(workspace_dir: &Path, thread_id: &str) -> usize {
+pub(crate) fn discard_for_thread(ctx: &crate::core::runtime::DispatchCtx, workspace_dir: &Path, thread_id: &str) -> usize {
let seen = {
- let thread_key = key(thread_id);
+ let thread_key = key(ctx, thread_id);
let mut st = state();
st.deleted_threads.insert(thread_key.clone());
st.stopped_threads.remove(&thread_key);
@@ -590,9 +594,9 @@ fn cancel_deleted_parent(entry: &Entry, thread_id: &str) -> usize {
/// gate lifts at [`resume_stopped_thread`] (the next accepted user turn), while
/// the stopped generation's task ids stay tombstoned
/// ([`finish_stop_for_thread`]). Returns the number of queued completions removed.
-pub(crate) fn discard_pending_for_thread(thread_id: &str) -> usize {
- state().stopped_threads.insert(key(thread_id));
- let Some(router) = router_for_thread(thread_id) else {
+pub(crate) fn discard_pending_for_thread(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str) -> usize {
+ state().stopped_threads.insert(key(ctx, thread_id));
+ let Some(router) = router_for_thread(ctx, thread_id) else {
return 0;
};
let removed = router.cancel_parent(thread_id).unwrap_or_else(|error| {
@@ -610,8 +614,8 @@ pub(crate) fn discard_pending_for_thread(thread_id: &str) -> usize {
/// Complete a Stop after its registered children were aborted: tombstone their
/// task ids so a straggler from the stopped generation cannot record once a
/// later user turn reopens the thread.
-pub(crate) fn finish_stop_for_thread(thread_id: &str, task_ids: &[String]) {
- let Some(router) = router_for_thread(thread_id) else {
+pub(crate) fn finish_stop_for_thread(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str, task_ids: &[String]) {
+ let Some(router) = router_for_thread(ctx, thread_id) else {
return;
};
for task_id in task_ids {
@@ -628,12 +632,12 @@ pub(crate) fn finish_stop_for_thread(thread_id: &str, task_ids: &[String]) {
/// Stop is pressed. New task ids remain distinct from the stopped generation.
/// Only a thread this process stopped or cancelled pays for the durable resume
/// marker, so an ordinary chat message writes nothing.
-pub(crate) fn resume_stopped_thread(thread_id: &str) {
- let was_stopped = state().stopped_threads.remove(&key(thread_id));
+pub(crate) fn resume_stopped_thread(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str) {
+ let was_stopped = state().stopped_threads.remove(&key(ctx, thread_id));
if !was_stopped {
return;
}
- if let Some(router) = router_for_thread(thread_id) {
+ if let Some(router) = router_for_thread(ctx, thread_id) {
router.resume_parent(thread_id);
log::debug!("[background_completions] resumed thread_id={thread_id}");
}
@@ -651,12 +655,13 @@ pub(crate) fn resume_stopped_thread(thread_id: &str) {
/// restart): a live spawn proves the user re-engaged the thread. Returns whether
/// the child must be aborted.
pub(crate) fn mark_stopped_task_if_thread_stopped(
+ ctx: &crate::core::runtime::DispatchCtx,
workspace_dir: &Path,
thread_id: &str,
task_id: &str,
) -> bool {
let entry = entry_for(workspace_dir);
- let thread_key = key(thread_id);
+ let thread_key = key(ctx, thread_id);
let (first_sight, mut stopped) = {
let mut st = state();
let first_sight = st
@@ -689,7 +694,7 @@ pub(crate) fn mark_stopped_task_if_thread_stopped(
/// purge of that workspace; each thread with undelivered results is cancelled
/// durably (and remembered as deleted), so stragglers are still dropped. Other
/// workspaces are untouched. Returns the number of completions removed.
-pub(crate) fn clear_all(workspace_dir: &Path) -> usize {
+pub(crate) fn clear_all(ctx: &crate::core::runtime::DispatchCtx, workspace_dir: &Path) -> usize {
let entry = entry_for(workspace_dir);
let parents: HashSet<String> = entry
.store
@@ -700,7 +705,7 @@ pub(crate) fn clear_all(workspace_dir: &Path) -> usize {
.collect();
let mut removed = 0;
for parent in parents {
- state().deleted_threads.insert(key(&parent));
+ state().deleted_threads.insert(key(ctx, &parent));
removed += cancel_deleted_parent(&entry, &parent);
}
log::debug!("[background_completions] clear_all removed={removed}");
@@ -711,7 +716,7 @@ pub(crate) fn clear_all(workspace_dir: &Path) -> usize {
/// return the threads that still hold undelivered completions (a previous
/// process finished the work but never delivered it). Remembers each thread's
/// workspace so the delivery loop can claim them.
-pub(crate) fn recover_pending_threads(workspace_dir: &Path) -> Vec<String> {
+pub(crate) fn recover_pending_threads(ctx: &crate::core::runtime::DispatchCtx, workspace_dir: &Path) -> Vec<String> {
let entry = entry_for(workspace_dir);
if let Err(error) = entry.router.compact(SETTLED_RETENTION) {
log::warn!("[background_completions] compact failed error={error}");
@@ -735,7 +740,7 @@ pub(crate) fn recover_pending_threads(workspace_dir: &Path) -> Vec<String> {
.collect();
threads.sort();
for thread in &threads {
- note_thread_workspace(thread, workspace_dir);
+ note_thread_workspace(ctx, thread, workspace_dir);
}
log::info!(
"[background_completions] boot recovery found {} thread(s) with undelivered completions \