Skip to content

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

test(orchestration): add tenant isolation tests for background completions and subagents

Steven Enamakel44d9c6fb76f0
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions_tenant_tests.rs b/crates/openhuman-core/src/agent/orchestration/background_completions_tenant_tests.rs
index 0b880852e7..a845acc79f 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions_tenant_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions_tenant_tests.rs
@@ -118,3 +118,64 @@ fn a_saas_cancel_reaches_only_the_callers_workspace() {
_ => panic!("a scoped SaaS caller reaches its own workspace"),
}
}
+
+/// The explicit context decides, with no ambient scope anywhere: two profiles
+/// recording and resolving the same thread id concurrently, across awaits and
+/// from spawned tasks, never see each other's state.
+#[tokio::test]
+async fn the_explicit_context_keys_completions_without_any_ambient_scope() {
+ let _guard = crate::config::TEST_ENV_LOCK.lock().await;
+ let (ws_a, ws_b) = (TestWorkspace::new(), TestWorkspace::new());
+ let alice = DispatchCtx::new(profile("x-alice", ws_a.path()));
+ let bob = DispatchCtx::new(profile("x-bob", ws_b.path()));
+
+ let record_for = |ctx: DispatchCtx, ws: std::path::PathBuf, task: &'static str| async move {
+ // Yield between the steps: nothing may lean on a task-local scope.
+ tokio::task::yield_now().await;
+ record_completion(
+ &ctx,
+ &ws,
+ "sess-x",
+ task,
+ "researcher",
+ "done",
+ Some("t-x".into()),
+ )
+ .await;
+ tokio::task::yield_now().await;
+ workspace_for_thread(&ctx, "t-x")
+ };
+ let spawned = tokio::spawn(record_for(
+ alice.clone(),
+ ws_a.path().to_path_buf(),
+ "x-alice-1",
+ ));
+ let (bobs, alices) = (
+ record_for(bob.clone(), ws_b.path().to_path_buf(), "x-bob-1").await,
+ spawned.await.expect("alice's task ran"),
+ );
+
+ assert_eq!(alices.as_deref(), Some(ws_a.path()));
+ assert_eq!(bobs.as_deref(), Some(ws_b.path()));
+ assert_eq!(
+ thread_for_session(&alice, "sess-x").as_deref(),
+ Some("t-x"),
+ "the session index is alice's own"
+ );
+ // A stop through one context gates only that profile's `t-x`.
+ discard_pending_for_thread(&alice, "t-x");
+ assert!(
+ pending_for(ws_a.path(), "t-x").is_empty(),
+ "alice's stop withdraws her queue"
+ );
+ assert_eq!(
+ pending_for(ws_b.path(), "t-x")
+ .into_iter()
+ .map(|r| r.task_id)
+ .collect::<Vec<_>>(),
+ ["x-bob-1"],
+ "bob's thread of the same id is untouched"
+ );
+ resume_stopped_thread(&alice, "t-x");
+ discard_for_thread(&bob, ws_b.path(), "t-x");
+}
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents_tenant_tests.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents_tenant_tests.rs
index 1383e4c11e..973f457217 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents_tenant_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents_tenant_tests.rs
@@ -597,3 +597,58 @@ async fn desktop_public_controls_remain_available_without_parent_scope() {
Some(true)
);
}
+
+/// The explicit context decides, with no task scope anywhere: a worker is
+/// reachable through its own profile's context and refused through another's,
+/// across awaits, from spawned tasks and concurrently.
+#[tokio::test]
+async fn saas_controls_follow_the_explicit_context_not_the_tasks_scope() {
+ if !child(
+ "saas_controls_follow_the_explicit_context_not_the_tasks_scope",
+ Mode::Saas,
+ ) {
+ return;
+ }
+ let pair = Pair::new();
+ let alice = DispatchCtx::new(pair.alice.clone());
+ let bob = DispatchCtx::new(pair.bob.clone());
+ let (a_task, b_task) = (pair.a.task.clone(), pair.b.task.clone());
+
+ // Not a single `CoreContext::scope` here: with the ambient reader the
+ // task would be unscoped, which fails closed in SaaS.
+ assert!(resume_ref_for_task(&alice, &a_task, PARENT).is_ok());
+ assert!(matches!(
+ resume_ref_for_task(&bob, &a_task, PARENT),
+ Err(WaitError::NotOwned)
+ ));
+ assert!(cancel_by_task(&bob, &a_task).is_none());
+ assert_eq!(
+ steer_control(&bob, &a_task, "x".into(), QueueLane::Steer).await,
+ Err(SteerError::NotOwned)
+ );
+ assert_eq!(pair.a.queue.status().await.steers, 0);
+ assert!(snapshot_for_parent(&bob, PARENT)
+ .iter()
+ .all(|worker| worker.task_id != a_task));
+
+ // Concurrent, across awaits, and from spawned tasks: each carries its own.
+ let spawned = |ctx: DispatchCtx, task: String| {
+ tokio::spawn(async move {
+ tokio::task::yield_now().await;
+ let own = resume_ref_for_task(&ctx, &task, PARENT).is_ok();
+ tokio::task::yield_now().await;
+ let foreign = cancel_by_task(&ctx, &task).is_some();
+ (own, foreign)
+ })
+ };
+ let (a_side, b_side) = tokio::join!(
+ spawned(alice.clone(), a_task.clone()),
+ spawned(bob.clone(), b_task.clone())
+ );
+ assert_eq!(a_side.unwrap(), (true, true), "alice reaches her own worker");
+ assert_eq!(b_side.unwrap(), (true, true), "bob reaches his own worker");
+ assert!(
+ cancel_by_task(&alice, &b_task).is_none(),
+ "and each cancel removed only that profile's worker"
+ );
+}