Skip to content

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

refactor(agent): extract delivery slot into its own module

Steven Enamakel535cf275241b
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index 66912ac495..9fa49c53b3 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -43,6 +43,11 @@ use crate::core::events::DomainEvent;
use tinybus::EventHandler;
use tinybus::SubscriptionHandle;
+mod slot;
+use slot::DeliverySlot;
+#[cfg(test)]
+use slot::delivering;
+
use super::background_completions;
use super::busy_guard::is_busy;
#[cfg(test)]
@@ -60,13 +65,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);
-/// 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>> {
- static D: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
- D.get_or_init(|| Mutex::new(HashSet::new()))
-}
-
/// 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) {
@@ -596,47 +594,6 @@ where
retry_after
}
-/// The per-thread (per profile) delivery slot, plus the batch lease.
-/// Dropping it frees both: `release` on an already-settled record is a no-op,
-/// so a settled batch is untouched and an abandoned one is claimable again.
-struct DeliverySlot {
- /// [`tenant::profile_key`] of the thread id.
- key: String,
- router: Arc<CompletionRouter>,
- held: Vec<String>,
-}
-
-impl DeliverySlot {
- /// `None` when a delivery is already in flight for the thread.
- fn claim(ctx: &DispatchCtx, thread_id: &str, router: Arc<CompletionRouter>) -> Option<Self> {
- let key = tenant::profile_key_in(&super::busy_guard::caller(ctx), thread_id);
- let mut d = delivering().lock().expect("delivering poisoned");
- if !d.insert(key.clone()) {
- return None;
- }
- Some(Self {
- key,
- router,
- held: Vec::new(),
- })
- }
-
- fn hold(&mut self, task_ids: &[String]) {
- self.held = task_ids.to_vec();
- }
-}
-
-impl Drop for DeliverySlot {
- fn drop(&mut self) {
- if !self.held.is_empty() {
- self.router.release(&self.held);
- }
- if let Ok(mut d) = delivering().lock() {
- d.remove(&self.key);
- }
- }
-}
-
/// Run one system-authored delivery turn on an existing conversation thread.
/// It only delivers a detached sub-agent result already produced by
/// the completion router.
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery/slot.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery/slot.rs
new file mode 100644
index 0000000000..d6c68ac059
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery/slot.rs
@@ -0,0 +1,58 @@
+//! The per-thread (per profile) delivery slot: at most one delivery turn is in
+//! flight for a thread, and the batch it claimed is leased until it settles.
+
+use std::collections::HashSet;
+use std::sync::{Arc, Mutex, OnceLock};
+
+use tinyagents_tasks::CompletionRouter;
+
+use crate::core::runtime::tenant;
+use crate::core::runtime::DispatchCtx;
+
+/// Threads whose delivery turn is in flight — prevents two concurrent turns.
+/// Keyed by [`tenant::profile_key`] of the thread id, for the same reason.
+pub(super) fn delivering() -> &'static Mutex<HashSet<String>> {
+ static D: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
+ D.get_or_init(|| Mutex::new(HashSet::new()))
+}
+
+/// The per-thread (per profile) delivery slot, plus the batch lease.
+/// Dropping it frees both: `release` on an already-settled record is a no-op,
+/// so a settled batch is untouched and an abandoned one is claimable again.
+pub(super) struct DeliverySlot {
+ /// [`tenant::profile_key`] of the thread id.
+ key: String,
+ router: Arc<CompletionRouter>,
+ held: Vec<String>,
+}
+
+impl DeliverySlot {
+ /// `None` when a delivery is already in flight for the thread.
+ pub(super) fn claim(ctx: &DispatchCtx, thread_id: &str, router: Arc<CompletionRouter>) -> Option<Self> {
+ let key = tenant::profile_key_in(&super::super::busy_guard::caller(ctx), thread_id);
+ let mut d = delivering().lock().expect("delivering poisoned");
+ if !d.insert(key.clone()) {
+ return None;
+ }
+ Some(Self {
+ key,
+ router,
+ held: Vec::new(),
+ })
+ }
+
+ pub(super) fn hold(&mut self, task_ids: &[String]) {
+ self.held = task_ids.to_vec();
+ }
+}
+
+impl Drop for DeliverySlot {
+ fn drop(&mut self) {
+ if !self.held.is_empty() {
+ self.router.release(&self.held);
+ }
+ if let Ok(mut d) = delivering().lock() {
+ d.remove(&self.key);
+ }
+ }
+}