Skip to content

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

test(core): scope dispatch context via CoreContext::scope in tests

Steven Enamakelb1c668aca3b4
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 49f28fb01b..d61a7a81a2 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
@@ -227,11 +227,10 @@ async fn saas_dispatch_exposes_only_the_callers_detached_worker_controls() {
);
})
.await;
- {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(pair.alice.clone());
-
- assert!(resume_ref_for_task(&scope_ctx, &pair.a.task, PARENT).is_ok());
- };
+ CoreContext::scope(pair.alice.clone(), async {
+ assert!(resume_ref_for_task(&DispatchCtx::ambient(), &pair.a.task, PARENT).is_ok());
+ })
+ .await;
for function in ["steer", "cancel"] {
assert!(dispatched_control(function, &pair.a.task).await.is_err());
}
@@ -360,26 +359,31 @@ async fn same_parent_resolution_and_wait_refuse_foreign_workers() {
return;
}
let pair = Pair::new();
- {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(pair.bob.clone());
-
+ CoreContext::scope(pair.bob.clone(), async {
assert!(matches!(
- task_id_for_session(&scope_ctx, &pair.a.session, PARENT),
+ task_id_for_session(&DispatchCtx::ambient(), &pair.a.session, PARENT),
Err(WaitError::NotOwned)
));
assert!(matches!(
- resume_ref_for_task(&scope_ctx, &pair.a.task, PARENT),
+ resume_ref_for_task(&DispatchCtx::ambient(), &pair.a.task, PARENT),
Err(WaitError::NotOwned)
));
assert!(matches!(
- wait(&scope_ctx, &pair.a.task, PARENT, Duration::ZERO).await,
+ wait(
+ &DispatchCtx::ambient(),
+ &pair.a.task,
+ PARENT,
+ Duration::ZERO
+ )
+ .await,
Err(WaitError::NotOwned)
));
assert_eq!(
- task_id_for_session(&scope_ctx, &pair.b.session, PARENT).unwrap(),
+ task_id_for_session(&DispatchCtx::ambient(), &pair.b.session, PARENT).unwrap(),
pair.b.task
);
- };
+ })
+ .await;
}
#[tokio::test]
@@ -391,12 +395,10 @@ async fn same_parent_agent_steering_refuses_foreign_worker() {
return;
}
let pair = Pair::new();
- {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(pair.bob.clone());
-
+ CoreContext::scope(pair.bob.clone(), async {
assert_eq!(
steer(
- &scope_ctx,
+ &DispatchCtx::ambient(),
&pair.a.task,
PARENT,
"foreign".into(),
@@ -407,7 +409,7 @@ async fn same_parent_agent_steering_refuses_foreign_worker() {
);
assert_eq!(pair.a.queue.status().await.steers, 0);
steer(
- &scope_ctx,
+ &DispatchCtx::ambient(),
&pair.b.task,
PARENT,
"own".into(),
@@ -416,7 +418,8 @@ async fn same_parent_agent_steering_refuses_foreign_worker() {
.await
.unwrap();
assert_eq!(pair.b.queue.status().await.steers, 1);
- };
+ })
+ .await;
}
#[tokio::test]
@@ -449,27 +452,25 @@ async fn public_cancellation_refuses_another_profiles_task() {
return;
}
let pair = Pair::new();
- {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(pair.bob.clone());
-
+ CoreContext::scope(pair.bob.clone(), async {
assert_eq!(
field(&control("cancel", &pair.a.task).await, "cancelled"),
Some(false)
);
- assert!(resume_ref_for_task(&scope_ctx, &pair.b.task, PARENT).is_ok());
+ assert!(resume_ref_for_task(&DispatchCtx::ambient(), &pair.b.task, PARENT).is_ok());
assert_eq!(
field(&control("cancel", &pair.b.task).await, "cancelled"),
Some(true)
);
- };
- {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(pair.alice.clone());
-
+ })
+ .await;
+ CoreContext::scope(pair.alice.clone(), async {
assert!(
- resume_ref_for_task(&scope_ctx, &pair.a.task, PARENT).is_ok(),
+ resume_ref_for_task(&DispatchCtx::ambient(), &pair.a.task, PARENT).is_ok(),
"foreign cancel leaves original worker live"
);
- };
+ })
+ .await;
}
#[tokio::test]
@@ -511,13 +512,10 @@ async fn missing_saas_scope_cannot_observe_or_control_workers() {
assert!(resume_ref_for_task(&DispatchCtx::ambient(), &pair.a.task, PARENT).is_ok())
});
// A scoped kernel context without an admitted profile is not authority.
- {
- let scope_ctx =
- crate::core::runtime::DispatchCtx::new(CoreContext::for_test(DomainSet::full(), None));
-
- assert!(snapshot_for_parent(&scope_ctx, PARENT).is_empty());
+ CoreContext::scope(CoreContext::for_test(DomainSet::full(), None), async {
+ assert!(snapshot_for_parent(&DispatchCtx::ambient(), PARENT).is_empty());
assert!(matches!(
- resume_ref_for_task(&scope_ctx, &pair.a.task, PARENT),
+ resume_ref_for_task(&DispatchCtx::ambient(), &pair.a.task, PARENT),
Err(WaitError::NotOwned)
));
assert_eq!(
@@ -528,7 +526,8 @@ async fn missing_saas_scope_cannot_observe_or_control_workers() {
field(&control("cancel", &pair.a.task).await, "cancelled"),
Some(false)
);
- };
+ })
+ .await;
}
#[tokio::test]
diff --git a/crates/openhuman-core/src/channels/routes_tests.rs b/crates/openhuman-core/src/channels/routes_tests.rs
index 6dcff26406..ca25fd4b39 100644
--- a/crates/openhuman-core/src/channels/routes_tests.rs
+++ b/crates/openhuman-core/src/channels/routes_tests.rs
@@ -244,10 +244,11 @@ async fn sign_out_cancels_remote_invalidation_while_it_waits_for_the_session_tab
let core_context =
crate::core::runtime::CoreContext::for_test(crate::core::runtime::DomainSet::full(), None);
let expected_core_context = Arc::clone(&core_context);
- {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(core_context);
-
- let guard = crate::web_chat::lock_thread_sessions_for_test(&scope_ctx).await;
+ crate::core::runtime::CoreContext::scope(core_context, async {
+ let guard = crate::web_chat::lock_thread_sessions_for_test(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ )
+ .await;
let cancellation = tokio_util::sync::CancellationToken::new();
let callback = RouteCallbacks::new(
runtime_context(PathBuf::from("/tmp/channel-callback")),
@@ -282,7 +283,8 @@ async fn sign_out_cancels_remote_invalidation_while_it_waits_for_the_session_tab
);
drop(guard);
- };
+ })
+ .await;
}
#[cfg(feature = "modules")]
diff --git a/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs b/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs
index ce04a36f59..d3127bfc54 100644
--- a/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs
@@ -368,12 +368,10 @@ async fn queued_work_keeps_the_callers_core_context() {
crate::core::runtime::DomainSet::full(),
Some(config.workspace_dir.clone()),
);
- {
- let scope_ctx = crate::core::runtime::DispatchCtx::new(ctx.clone());
-
+ crate::core::runtime::CoreContext::scope(ctx.clone(), async {
let identity = MemoryIdentity::agent("scoped-agent").resolve(&config);
let key = Key::new(&config, &identity, "t", false);
- let cache = cache(&scope_ctx);
+ let cache = cache(&crate::core::runtime::DispatchCtx::ambient());
let expected = ctx.clone();
enqueue(
cache.clone(),
@@ -388,7 +386,8 @@ async fn queued_work_keeps_the_callers_core_context() {
);
drain(&cache, &key).await;
assert_eq!(completed(&cache, &key).unwrap().markdown, "scoped");
- };
+ })
+ .await;
}
#[test]