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
test(orchestration): add background delivery slot tests
Steven Enamakelc1b1b501296c
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery_slots_tests.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery_slots_tests.rsnew file mode 100644index 0000000000..2cdeb44cbb--- /dev/null+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery_slots_tests.rs@@ -0,0 +1,189 @@+//! Busy state and delivery slots are per profile. In SaaS two profiles can use+//! the same thread id and the same web-chat session id (`{client_id,+//! thread_id}`); one profile's in-flight turn or delivery must neither hold+//! back nor stand in for the other's. Desktop keys stay the bare ids.++use super::*;+use crate::agent::orchestration::background_completions::{+ pending_for, record_completion, router_for_workspace, TestWorkspace,+};+use crate::core::runtime::{ContextOverlay, DomainSet};+use crate::tools::toolpacks::ToolGroups;++fn profile(name: &str, workspace: &Path) -> Arc<CoreContext> {+ let config = crate::config::Config {+ workspace_dir: workspace.to_path_buf(),+ ..crate::config::Config::default()+ };+ CoreContext::for_test(DomainSet::full(), None).derive_with(+ ContextOverlay::new(config, DomainSet::kernel(), ToolGroups::none())+ .profile(name)+ .session_agent(name),+ )+}++fn unique(prefix: &str) -> String {+ format!("{prefix}-{}", uuid::Uuid::new_v4().simple())+}++fn web_session(thread: &str) -> String {+ format!(r#"{{"client_id":"c","thread_id":"{thread}"}}"#)+}++/// Two profiles, each with one pending result on the same thread id.+struct Pair {+ alice: Arc<CoreContext>,+ bob: Arc<CoreContext>,+ ws_a: TestWorkspace,+ ws_b: TestWorkspace,+ thread: String,+ session: String,+}++async fn pair() -> Pair {+ let (ws_a, ws_b) = (TestWorkspace::new(), TestWorkspace::new());+ let alice = profile(&unique("u-alice"), ws_a.path());+ let bob = profile(&unique("u-bob"), ws_b.path());+ let thread = unique("t");+ let session = web_session(&thread);+ for (ctx, ws, task) in [(&alice, &ws_a, "sub-a"), (&bob, &ws_b, "sub-b")] {+ CoreContext::scope(+ Arc::clone(ctx),+ record_completion(+ ws.path(),+ &session,+ task,+ "researcher",+ "done",+ Some(thread.clone()),+ ),+ )+ .await;+ }+ Pair {+ alice,+ bob,+ ws_a,+ ws_b,+ thread,+ session,+ }+}++fn in_scope<R>(ctx: &Arc<CoreContext>, f: impl FnOnce() -> R) -> R {+ CoreContext::sync_scope(Arc::clone(ctx), f)+}++#[tokio::test]+async fn one_profiles_busy_turn_does_not_block_anothers_thread() {+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;+ let p = pair().await;+ let turn = in_scope(&p.alice, || TurnBusy::start(&p.session));++ assert!(in_scope(&p.alice, || is_busy(&p.thread)));+ assert!(!in_scope(&p.bob, || is_busy(&p.thread)));+ let router_b = router_for_workspace(p.ws_b.path());+ let batch = in_scope(&p.bob, || claim_ready(&router_b, &p.thread))+ .expect("bob's thread is idle while alice's turn runs");+ assert_eq!(batch[0].task_id, "sub-b");+ router_b.release(&["sub-b".to_string()]);++ // Bob's Stop clears only Bob's turns; Alice stays busy.+ assert_eq!(in_scope(&p.bob, || clear_busy_for_thread(&p.thread)), 0);+ assert!(in_scope(&p.alice, || is_busy(&p.thread)));++ drop(turn);+ assert!(!in_scope(&p.alice, || is_busy(&p.thread)));+}++#[tokio::test]+async fn a_delivery_in_flight_for_one_profile_does_not_take_anothers_slot() {+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;+ let p = pair().await;+ let (router_a, router_b) = (+ router_for_workspace(p.ws_a.path()),+ router_for_workspace(p.ws_b.path()),+ );++ let held = in_scope(&p.alice, || DeliverySlot::claim(&p.thread, router_a.clone()))+ .expect("alice claims her slot");+ assert!(+ in_scope(&p.alice, || DeliverySlot::claim(&p.thread, router_a.clone())).is_none(),+ "one delivery per thread within a profile"+ );+ let bob_slot = in_scope(&p.bob, || DeliverySlot::claim(&p.thread, router_b.clone()));+ assert!(bob_slot.is_some(), "alice's delivery does not hold bob's slot");+ drop((held, bob_slot));++ // End to end: while Alice's delivery turn is in flight, Bob's runs and+ // lands in Bob's thread; then Alice's lands in hers.+ let delivered: Arc<Mutex<Vec<(String, String)>>> = Arc::default();+ let log = |who: &'static str| {+ let delivered = Arc::clone(&delivered);+ move |thread: String, notice: String| {+ delivered+ .lock()+ .unwrap()+ .push((format!("{who}:{thread}"), notice));+ async { Ok::<_, String>("ok".to_string()) }+ }+ };+ let bob = Arc::clone(&p.bob);+ let (thread, router_bob, bob_log) = (p.thread.clone(), router_b.clone(), log("bob"));+ let alice_deliver = move |thread_a: String, notice: String| {+ let bob = Arc::clone(&bob);+ let (thread, router_bob, bob_log) = (thread.clone(), router_bob.clone(), bob_log.clone());+ let mut alice_log = log("alice");+ async move {+ let retry = CoreContext::scope(+ bob,+ try_deliver_with(thread, router_bob, bob_log, |_t, _n| async {+ unreachable!("bob's delivery succeeds")+ }),+ )+ .await;+ assert_eq!(retry, None);+ alice_log(thread_a, notice).await+ }+ };+ let retry = CoreContext::scope(+ Arc::clone(&p.alice),+ try_deliver_with(p.thread.clone(), router_a, alice_deliver, |_t, _n| async {+ unreachable!("alice's delivery succeeds")+ }),+ )+ .await;+ assert_eq!(retry, None);++ let delivered = delivered.lock().unwrap().clone();+ assert_eq!(delivered.len(), 2, "both profiles got their result");+ assert_eq!(delivered[0].0, format!("bob:{}", p.thread));+ assert!(delivered[0].1.contains("sub-b") && !delivered[0].1.contains("sub-a"));+ assert_eq!(delivered[1].0, format!("alice:{}", p.thread));+ assert!(delivered[1].1.contains("sub-a") && !delivered[1].1.contains("sub-b"));+ assert!(pending_for(p.ws_a.path(), &p.thread).is_empty());+ assert!(pending_for(p.ws_b.path(), &p.thread).is_empty());+}++#[tokio::test]+async fn desktop_keys_are_the_bare_ids() {+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;+ let desktop = CoreContext::for_test(DomainSet::full(), None);+ let ws = TestWorkspace::new();+ let thread = unique("t-desk");+ let session = web_session(&thread);++ let turn = in_scope(&desktop, || TurnBusy::start(&session));+ assert_eq!(turn.key, session);+ assert!(busy().lock().unwrap().contains(&session));+ assert!(in_scope(&desktop, || is_busy(&thread)));+ drop(turn);+ assert!(!busy().lock().unwrap().contains(&session));++ let router = router_for_workspace(ws.path());+ let slot = in_scope(&desktop, || DeliverySlot::claim(&thread, router)).expect("free");+ assert_eq!(slot.key, thread);+ assert!(delivering().lock().unwrap().contains(&thread));+ drop(slot);+ assert!(!delivering().lock().unwrap().contains(&thread));+}