Skip to content

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

refactor(orchestration): delegate busy tracking to upstream parent gate

Steven Enamakelebbdf2daaae2
diff --git a/crates/openhuman-core/src/agent/orchestration/busy_guard.rs b/crates/openhuman-core/src/agent/orchestration/busy_guard.rs
index 5981664c6f..848aff8b94 100644
--- a/crates/openhuman-core/src/agent/orchestration/busy_guard.rs
+++ b/crates/openhuman-core/src/agent/orchestration/busy_guard.rs
@@ -1,127 +1,83 @@
-//! In-flight user turns, tracked per profile so background delivery defers
-//! while one runs. See [`TurnBusy`].
+//! Host-owned keys for TinyAgents' parent-turn busy gate.
-use std::collections::{HashMap, HashSet};
-use std::sync::atomic::{AtomicU64, Ordering};
+use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
+use tinyagents_tasks::{ParentBusyGate, ParentBusyLease, TenantThreadKey};
+
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_in`]: 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.
+/// The calling task's profile, as the tenant its busy keys carry.
pub(super) fn caller(ctx: &crate::core::runtime::DispatchCtx) -> Tenant {
tenant::isolated_unless_tenant(ctx.tenant(), "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)
+/// One upstream gate per profile. The gate owns leases and session aliases;
+/// OpenHuman only supplies the tenant boundary and the session-to-thread map.
+fn gates() -> &'static Mutex<HashMap<String, ParentBusyGate>> {
+ static GATES: OnceLock<Mutex<HashMap<String, ParentBusyGate>>> = OnceLock::new();
+ GATES.get_or_init(|| Mutex::new(HashMap::new()))
+}
+
+fn gate_for(tenant_key: &str) -> ParentBusyGate {
+ gates()
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .entry(tenant_key.to_owned())
+ .or_default()
+ .clone()
+}
+
+fn tenant_key(ctx: &crate::core::runtime::DispatchCtx) -> String {
+ tenant::profile_key_in(&caller(ctx), "")
}
-/// 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.
+/// A user turn in flight on a session for the calling profile, busy until
+/// dropped. The upstream lease is the sole busy-state authority.
pub(crate) struct TurnBusy {
- pub(super) key: String,
- id: u64,
+ _lease: ParentBusyLease,
}
impl TurnBusy {
pub(crate) fn start(ctx: &crate::core::runtime::DispatchCtx, session_id: &str) -> Self {
- static NEXT: AtomicU64 = AtomicU64::new(0);
- let key = tenant::profile_key_in(&caller(ctx), 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 }
+ let tenant_key = tenant_key(ctx);
+ let gate = gate_for(&tenant_key);
+ let session = TenantThreadKey::new(&tenant_key, session_id);
+ if let Some(thread_id) = background_completions::thread_for_session(ctx, session_id) {
+ let thread = TenantThreadKey::new(&tenant_key, thread_id);
+ let _ = gate.alias_session_to_thread(session.clone(), thread);
+ }
+ let lease = gate
+ .enter(session)
+ .expect("background_delivery parent busy gate poisoned");
+ Self { _lease: lease }
}
- /// A turn in flight on `thread_id` itself, for a turn whose session id does
- /// not map to its thread: a relayed channel turn runs under the channel
- /// pipeline's own session id, which [`background_completions`] never
- /// learns, so marking that session would leave the thread looking idle.
+ /// A turn running on a channel thread whose session id has no thread map.
pub(crate) fn start_on_thread(
ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
) -> Self {
- Self::start(ctx, &format!("{THREAD_MARK}{thread_id}"))
- }
-}
-
-/// Prefix of a busy session id that names its thread directly
-/// ([`TurnBusy::start_on_thread`]). Not a character a session id carries.
-const THREAD_MARK: char = '\u{2}';
-
-/// The thread a busy session id is a turn on.
-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(ctx, session),
- }
-}
-
-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);
- }
- }
- }
+ let tenant_key = tenant_key(ctx);
+ let lease = gate_for(&tenant_key)
+ .enter(TenantThreadKey::new(tenant_key, thread_id))
+ .expect("background_delivery parent busy gate poisoned");
+ Self { _lease: lease }
}
}
/// Is any in-flight turn of the calling profile running on `thread_id`?
pub(crate) fn is_busy(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str) -> bool {
- let me = caller(ctx);
- busy()
- .lock()
- .expect("background_delivery busy poisoned")
- .keys()
- .filter_map(|key| session_of(key, &me))
- .any(|session| thread_of(ctx, session).as_deref() == Some(thread_id))
+ let tenant_key = tenant_key(ctx);
+ gate_for(&tenant_key).is_busy_scoped(&TenantThreadKey::new(tenant_key, 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.
+/// Forget every in-flight turn of the calling profile on `thread_id`.
pub(crate) fn clear_busy_for_thread(
ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
) -> usize {
- let me = caller(ctx);
- 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(ctx, 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
+ let tenant_key = tenant_key(ctx);
+ gate_for(&tenant_key).clear(&TenantThreadKey::new(tenant_key, thread_id))
}