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
refactor(orchestration): extract busy-turn tracking into busy_guard
Steven Enamakel8d77eb354c57
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rsindex 7cf9106d7b..6211ddc522 100644--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs@@ -45,6 +45,9 @@ use tinybus::SubscriptionHandle;use super::background_completions;use super::completion_notice::build_undelivered_notice;+use super::busy_guard::{clear_busy_for_thread, is_busy};+#[cfg(test)]+use super::busy_guard::{busy, TurnBusy};use super::completion_owners;use crate::core::runtime::tenant::{self, Tenant};use crate::core::runtime::CoreContext;@@ -56,17 +59,6 @@ const DEBOUNCE: Duration = Duration::from_secs(3);/// delivery turn, so providers and the session store are up first.const RECOVERY_DELAY: Duration = Duration::from_secs(15);-/// Sessions with a user turn in flight — delivery defers while busy. Keyed by-/// [`tenant::profile_key`]: two SaaS profiles can share a session id (desktop: bare id).-///-/// Each key holds the ids of the live [`TurnBusy`] guards on it, so a replacement-/// turn that starts before the one it interrupted has unwound stays busy when-/// the older guard drops.-fn busy() -> &'static Mutex<HashMap<String, HashSet<u64>>> {- static BUSY: OnceLock<Mutex<HashMap<String, HashSet<u64>>>> = OnceLock::new();- BUSY.get_or_init(|| Mutex::new(HashMap::new()))-}-/// Threads whose delivery turn is in flight — prevents two concurrent turns./// Keyed by [`tenant::profile_key`] of the thread id, for the same reason.fn delivering() -> &'static Mutex<HashSet<String>> {@@ -74,90 +66,6 @@ fn delivering() -> &'static Mutex<HashSet<String>> {D.get_or_init(|| Mutex::new(HashSet::new()))}-/// The calling task's profile, as the tenant its busy/slot keys carry.-fn caller() -> Tenant {- tenant::current_tenant_or_isolated("background_delivery").profile_only()-}--/// The session id of a busy key, when the key belongs to `me`.-fn session_of<'k>(key: &'k str, me: &Tenant) -> Option<&'k str> {- tenant::split_key(key)- .filter(|(owner, _)| owner == me)- .map(|(_, session)| session)-}--/// A user turn in flight on a session for the calling profile, busy until dropped.-/// The turn loop takes it in scope (the bus subscriber runs off-task and cannot-/// tell whose session it is); the key is fixed, so a cancelled turn clears it.-pub(crate) struct TurnBusy {- key: String,- id: u64,-}--impl TurnBusy {- pub(crate) fn start(session_id: &str) -> Self {- static NEXT: AtomicU64 = AtomicU64::new(0);- let key = tenant::profile_key(session_id);- let id = NEXT.fetch_add(1, Ordering::Relaxed);- busy()- .lock()- .expect("background_delivery busy poisoned")- .entry(key.clone())- .or_default()- .insert(id);- Self { key, id }- }-}--impl Drop for TurnBusy {- fn drop(&mut self) {- if let Ok(mut busy) = busy().lock() {- if let Some(guards) = busy.get_mut(&self.key) {- guards.remove(&self.id);- if guards.is_empty() {- busy.remove(&self.key);- }- }- }- }-}--/// Is any in-flight turn of the calling profile running on `thread_id`?-fn is_busy(thread_id: &str) -> bool {- let me = caller();- busy()- .lock()- .expect("background_delivery busy poisoned")- .keys()- .filter_map(|key| session_of(key, &me))- .any(|session| {- background_completions::thread_for_session(session).as_deref() == Some(thread_id)- })-}--/// Forget every in-flight turn of the calling profile on `thread_id`. A turn-/// that is cancelled cooperatively (Stop) can end without `AgentTurnCompleted`-/// or `AgentError`, and its session would otherwise stay "busy" and defer this-/// thread's deliveries until restart. Another profile's turn on a thread with-/// the same id is left alone. Returns how many sessions were cleared.-pub(crate) fn clear_busy_for_thread(thread_id: &str) -> usize {- let me = caller();- 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| {- background_completions::thread_for_session(session).as_deref() != Some(thread_id)- })- });- let cleared = before - busy.len();- if cleared > 0 {- log::debug!(- "[background_delivery] cleared {cleared} stale busy session(s) thread_id={thread_id}"- );- }- cleared-}-/// Ask for a delivery attempt on `thread_id` soon (after a Stop cleared a stale/// busy mark, or anything else that may have left a drain without its trigger).pub(crate) fn kick_delivery(thread_id: &str) {diff --git a/crates/openhuman-core/src/agent/orchestration/busy_guard.rs b/crates/openhuman-core/src/agent/orchestration/busy_guard.rsnew file mode 100644index 0000000000..b5f35bd42c--- /dev/null+++ b/crates/openhuman-core/src/agent/orchestration/busy_guard.rs@@ -0,0 +1,105 @@+//! In-flight user turns, tracked per profile so background delivery defers+//! while one runs. See [`TurnBusy`].++use std::collections::{HashMap, HashSet};+use std::sync::atomic::{AtomicU64, Ordering};+use std::sync::{Mutex, OnceLock};++use super::background_completions;+use crate::core::runtime::tenant::{self, Tenant};++/// Sessions with a user turn in flight — delivery defers while busy. Keyed by+/// [`tenant::profile_key`]: two SaaS profiles can share a session id (desktop: bare id).+///+/// Each key holds the ids of the live [`TurnBusy`] guards on it, so a replacement+/// turn that starts before the one it interrupted has unwound stays busy when+/// the older guard drops.+pub(super) fn busy() -> &'static Mutex<HashMap<String, HashSet<u64>>> {+ static BUSY: OnceLock<Mutex<HashMap<String, HashSet<u64>>>> = OnceLock::new();+ BUSY.get_or_init(|| Mutex::new(HashMap::new()))+}++/// The calling task's profile, as the tenant its busy/slot keys carry.+pub(super) fn caller() -> Tenant {+ tenant::current_tenant_or_isolated("background_delivery").profile_only()+}++/// The session id of a busy key, when the key belongs to `me`.+pub(super) fn session_of<'k>(key: &'k str, me: &Tenant) -> Option<&'k str> {+ tenant::split_key(key)+ .filter(|(owner, _)| owner == me)+ .map(|(_, session)| session)+}++/// A user turn in flight on a session for the calling profile, busy until dropped.+/// The turn loop takes it in scope (the bus subscriber runs off-task and cannot+/// tell whose session it is); the key is fixed, so a cancelled turn clears it.+pub(crate) struct TurnBusy {+ key: String,+ id: u64,+}++impl TurnBusy {+ pub(crate) fn start(session_id: &str) -> Self {+ static NEXT: AtomicU64 = AtomicU64::new(0);+ let key = tenant::profile_key(session_id);+ let id = NEXT.fetch_add(1, Ordering::Relaxed);+ busy()+ .lock()+ .expect("background_delivery busy poisoned")+ .entry(key.clone())+ .or_default()+ .insert(id);+ Self { key, id }+ }+}++impl Drop for TurnBusy {+ fn drop(&mut self) {+ if let Ok(mut busy) = busy().lock() {+ if let Some(guards) = busy.get_mut(&self.key) {+ guards.remove(&self.id);+ if guards.is_empty() {+ busy.remove(&self.key);+ }+ }+ }+ }+}++/// Is any in-flight turn of the calling profile running on `thread_id`?+pub(super) fn is_busy(thread_id: &str) -> bool {+ let me = caller();+ busy()+ .lock()+ .expect("background_delivery busy poisoned")+ .keys()+ .filter_map(|key| session_of(key, &me))+ .any(|session| {+ background_completions::thread_for_session(session).as_deref() == Some(thread_id)+ })+}++/// Forget every in-flight turn of the calling profile on `thread_id`. A turn+/// that is cancelled cooperatively (Stop) can end without `AgentTurnCompleted`+/// or `AgentError`, and its session would otherwise stay "busy" and defer this+/// thread's deliveries until restart. Another profile's turn on a thread with+/// the same id is left alone. Returns how many sessions were cleared.+pub(crate) fn clear_busy_for_thread(thread_id: &str) -> usize {+ let me = caller();+ 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| {+ background_completions::thread_for_session(session).as_deref() != Some(thread_id)+ })+ });+ let cleared = before - busy.len();+ if cleared > 0 {+ log::debug!(+ "[background_delivery] cleared {cleared} stale busy session(s) thread_id={thread_id}"+ );+ }+ cleared+}+diff --git a/crates/openhuman-core/src/agent/orchestration/mod.rs b/crates/openhuman-core/src/agent/orchestration/mod.rsindex ab6d387c47..696e667701 100644--- a/crates/openhuman-core/src/agent/orchestration/mod.rs+++ b/crates/openhuman-core/src/agent/orchestration/mod.rs@@ -18,6 +18,7 @@pub mod agent_teams;pub(crate) mod background_completions;pub(crate) mod background_delivery;+pub(crate) mod busy_guard;pub mod command_center;pub(crate) mod completion_notice;pub(crate) mod completion_owners;