The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(orchestration): track background completion routers by scope
Steven Enamakel378da7f7a2eb
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rsindex b282e2cfae..1a6c29bc0f 100644--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs@@ -146,6 +146,14 @@ fn router_pool() -> &'static CompletionRouterPool<RouterScope> {static POOL: OnceLock<CompletionRouterPool<RouterScope>> = OnceLock::new();POOL.get_or_init(|| {CompletionRouterPool::new(|scope| {+ #[cfg(test)]+ if let Some(store) = test_store_overrides()+ .lock()+ .unwrap_or_else(|poisoned| poisoned.into_inner())+ .remove(scope)+ {+ return Arc::new(new_router(store));+ }let store = match open_driver_store(&scope.workspace_dir) {Ok(store) => store,Err(error) => {@@ -162,6 +170,13 @@ fn router_pool() -> &'static CompletionRouterPool<RouterScope> {})}+#[cfg(test)]+fn test_store_overrides() -> &'static Mutex<HashMap<RouterScope, Arc<dyn CompletionStore>>> {+ static STORES: OnceLock<Mutex<HashMap<RouterScope, Arc<dyn CompletionStore>>>> =+ OnceLock::new();+ STORES.get_or_init(|| Mutex::new(HashMap::new()))+}+fn entry_for(workspace_dir: &Path) -> Arc<Entry> {let scope = router_scope(workspace_dir);let router = match router_pool().get_or_open(&scope) {@@ -469,15 +484,24 @@ pub(crate) async fn release_all() -> usize {let deadline = tokio::time::Instant::now() + RELEASE_DRAIN;let mut released = 0;loop {- let retained = {- let mut st = state();- let before = st.routers.len();- st.routers- .retain(|_, e| Arc::strong_count(e) > 1 || Arc::strong_count(&e.router) > 1);- released += before - st.routers.len();- st.routers.len()- };+ let scopes: Vec<_> = state().stores.keys().cloned().collect();+ let retained = scopes+ .iter()+ .filter(|scope| {+ router_pool()+ .get(scope)+ .ok()+ .flatten()+ .is_some_and(|router| Arc::strong_count(&router) > 2)+ })+ .count();if retained == 0 {+ let mut st = state();+ released += st.stores.len();+ st.stores.clear();+ if let Err(error) = router_pool().clear() {+ log::warn!("[background_completions] could not clear router pool: {error}");+ }break;}if tokio::time::Instant::now() >= deadline {@@ -485,7 +509,12 @@ pub(crate) async fn release_all() -> usize {"[background_completions] {retained} router(s) still in use at shutdown; \dropping them anyway");- released += retained;+ let mut st = state();+ released += st.stores.len();+ st.stores.clear();+ if let Err(error) = router_pool().clear() {+ log::warn!("[background_completions] could not clear router pool: {error}");+ }break;}tokio::time::sleep(Duration::from_millis(50)).await;diff --git a/crates/openhuman-core/src/agent/orchestration/completion_target.rs b/crates/openhuman-core/src/agent/orchestration/completion_target.rsindex 7503098a6c..479795d814 100644--- a/crates/openhuman-core/src/agent/orchestration/completion_target.rs+++ b/crates/openhuman-core/src/agent/orchestration/completion_target.rs@@ -123,7 +123,7 @@ impl CompletionTarget {pub(crate) fn claim_recovery(workspace_dir: &Path) -> bool {state().recovered_workspaces- .insert(workspace_dir.to_path_buf())+ .insert(super::background_completions::router_scope(workspace_dir))}/// Let `workspace_dir` be recovered again (a profile re-leased after release),@@ -131,13 +131,27 @@ pub(crate) fn claim_recovery(workspace_dir: &Path) -> bool {/// the log another holder may have appended to. A router still in use stays,/// so a log never has two writers.pub(crate) fn forget_recovery(workspace_dir: &Path) {- let mut st = state();- st.recovered_workspaces.remove(workspace_dir);- let idle = st- .routers- .get(workspace_dir)- .is_some_and(|e| Arc::strong_count(e) == 1 && Arc::strong_count(&e.router) == 1);- if idle {- st.routers.remove(workspace_dir);+ let scope = super::background_completions::router_scope(workspace_dir);+ state().recovered_workspaces.remove(&scope);+ let Ok(Some(router)) = super::background_completions::router_pool().get(&scope) else {+ return;+ };+ // The pool plus this lookup own two references; any additional reference+ // means a delivery or a record still has the router open.+ if Arc::strong_count(&router) != 2 {+ return;+ }+ let Ok(store) = super::background_completions::open_driver_store(workspace_dir) else {+ return;+ };+ let replacement = Arc::new(super::background_completions::new_router(store));+ if let Ok(tinyagents_tasks::CompletionRouterReplaceOutcome::Replaced(router)) =+ super::background_completions::router_pool().replace_if_current(+ &scope,+ &router,+ replacement,+ )+ {+ state().stores.insert(scope, router.store());}}