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(agent): resolve background delivery drains per owning profile
Steven Enamakel757cf4fbc41c
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rsindex 95d85305d6..763b2bd8b2 100644--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs@@ -44,6 +44,8 @@ use tinybus::SubscriptionHandle;use super::background_completions;use super::completion_notice::build_undelivered_notice;+use super::completion_owners;+use crate::core::runtime::CoreContext;/// Coalesce completions landing within this window into one delivery turn.const DEBOUNCE: Duration = Duration::from_secs(3);@@ -124,20 +126,54 @@ impl EventHandler<DomainEvent> for BackgroundDeliveryHandler {}_ => {}}- if let Some((thread_id, delay)) = drain_schedule(event) {- schedule_delivery(thread_id, delay);+ let saas = crate::core::runtime::is_saas();+ for drain in drain_schedule_in(saas, event, completion_owners::context_for_profile) {+ match drain.owner {+ // Re-enter the owner's scope so the scheduled task inherits it.+ Some(ctx) => {+ CoreContext::sync_scope(ctx, || schedule_delivery(drain.thread_id, drain.delay))+ }+ None => schedule_delivery(drain.thread_id, drain.delay),+ }}}}-/// Which thread to drain, and after how long, for an event. A session that maps-/// to no thread (cron, voice, skills) has nothing to deliver into.+/// One drain an event asks for: the thread, how soon, and the context of the+/// profile that owns it (`None` on the desktop, where nothing is per profile).+struct Drain {+ owner: Option<Arc<CoreContext>>,+ thread_id: String,+ delay: Duration,+}++/// Which thread to drain, and after how long, for an event, outside any+/// profile. A session that maps to no thread (cron, voice, skills) has nothing+/// to deliver into.+#[cfg(test)]fn drain_schedule(event: &DomainEvent) -> Option<(String, Duration)> {- let (session, delay) = match event {+ let drain = drain_schedule_in(false, event, |_| None).into_iter().next()?;+ Some((drain.thread_id, drain.delay))+}++/// The drains for `event`. This subscriber runs off-task, with no tenant scope,+/// but the thread tables are keyed per profile: the profiles that recorded the+/// completion (by task id) or ran the session are looked up in+/// [`completion_owners`] and each is resolved through `resolve` and drained in+/// its own scope, where it reaches only its own tables. In SaaS (`saas`) an id+/// with no owner is dropped; elsewhere it drains unscoped, as on the desktop.+fn drain_schedule_in(+ saas: bool,+ event: &DomainEvent,+ resolve: impl Fn(&str) -> Option<Arc<CoreContext>>,+) -> Vec<Drain> {+ let (session, task, delay) = match event {// A user turn just ended (or failed) — drain anything that finished while// it ran.DomainEvent::AgentTurnCompleted { session_id, .. }- | DomainEvent::AgentError { session_id, .. } => (session_id, Duration::from_millis(300)),+ | DomainEvent::AgentError { session_id, .. } => {+ (session_id, None, Duration::from_millis(300))+ }// Any subagent terminal state — completed, failed, or awaiting-user — can// arrive after the parent turn already went idle. Schedule a debounced// drain for all three so the pending result is delivered promptly instead@@ -146,18 +182,61 @@ fn drain_schedule(event: &DomainEvent) -> Option<(String, Duration)> {// the parent turn went idle left the chat stuck on the original// "Accepted" response (#4896). Debounce so a burst batches into a single// turn.- DomainEvent::SubagentCompleted { parent_session, .. }- | DomainEvent::SubagentFailed { parent_session, .. }- | DomainEvent::SubagentAwaitingUser { parent_session, .. } => (parent_session, DEBOUNCE),- _ => return None,+ DomainEvent::SubagentCompleted {+ parent_session,+ task_id,+ ..+ }+ | DomainEvent::SubagentFailed {+ parent_session,+ task_id,+ ..+ }+ | DomainEvent::SubagentAwaitingUser {+ parent_session,+ task_id,+ ..+ } => (parent_session, Some(task_id), DEBOUNCE),+ _ => return Vec::new(),};- match background_completions::thread_for_session(session) {- Some(thread_id) => Some((thread_id, delay)),- None => {+ // A task id is core-minted and unique, so it names its one owner; a+ // session id can be shared by profiles, so it may name several.+ let mut profiles = task.map(|t| completion_owners::profiles_of(t)).unwrap_or_default();+ if profiles.is_empty() {+ profiles = completion_owners::profiles_of(session);+ }+ let thread_in = |owner: Option<Arc<CoreContext>>| {+ let thread_id = match &owner {+ Some(ctx) => CoreContext::sync_scope(Arc::clone(ctx), || {+ background_completions::thread_for_session(session)+ }),+ None => background_completions::thread_for_session(session),+ };+ if thread_id.is_none() {log::trace!("[background_delivery] session has no delivery thread; not scheduling");- None}+ thread_id.map(|thread_id| Drain { owner, thread_id, delay })+ };+ if profiles.is_empty() {+ if saas {+ log::debug!(+ "[background_delivery] no owning profile for the session or task; dropping \+ the drain (fails closed in SaaS)"+ );+ return Vec::new();+ }+ return thread_in(None).into_iter().collect();}+ profiles+ .iter()+ .filter_map(|profile| match resolve(profile) {+ Some(ctx) => thread_in(Some(ctx)),+ None => {+ log::debug!("[background_delivery] owning profile has no live context; dropping");+ None+ }+ })+ .collect()}/// Schedule a debounced delivery attempt for a thread.