Skip to content

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

test(channels): cover relay ops, runtime dispatch, and agent scope

Steven Enamakelde04a95b63f7
diff --git a/crates/openhuman-core/src/channels/providers/relay/ops_tests.rs b/crates/openhuman-core/src/channels/providers/relay/ops_tests.rs
index 2412af1b08..e7693174a4 100644
--- a/crates/openhuman-core/src/channels/providers/relay/ops_tests.rs
+++ b/crates/openhuman-core/src/channels/providers/relay/ops_tests.rs
@@ -29,7 +29,7 @@ async fn relayed_thread_and_external_authority_survive_scoped_spawn() {
async {
crate::agent::turn_origin::with_origin(origin, async {
let (child_thread, child_agent, child_origin) =
- crate::core::runtime::spawn_scoped(async {
+ crate::core::runtime::DispatchCtx::ambient().spawn(async {
(
CoreContext::current_channel_thread(),
CoreContext::scoped()
diff --git a/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs b/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs
index cf8caedb59..b0cc7cb100 100644
--- a/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs
+++ b/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs
@@ -363,7 +363,7 @@ fn watch_for_channel_processed(message_id: &str) -> tokio::task::JoinHandle<Doma
.expect("event bus should be initialized")
.receiver();
let message_id = message_id.to_string();
- crate::core::runtime::spawn_scoped(async move {
+ tokio::spawn(async move {
let deadline = tokio::time::Instant::now() + Duration::from_secs(30);
loop {
let event = tokio::time::timeout_at(deadline, events.recv())
diff --git a/crates/openhuman-core/src/core/runtime/agent_scope_tests.rs b/crates/openhuman-core/src/core/runtime/agent_scope_tests.rs
index ea57d836b3..d297fe4c58 100644
--- a/crates/openhuman-core/src/core/runtime/agent_scope_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/agent_scope_tests.rs
@@ -1,5 +1,5 @@
use super::*;
-use crate::core::runtime::{spawn_scoped, ContextOverlay, DispatchCtx, DomainSet};
+use crate::core::runtime::{ContextOverlay, DispatchCtx, DomainSet};
use crate::security::{AutonomyLevel, SecurityPolicy};
use crate::tools::toolpacks::ToolGroups;
@@ -143,7 +143,7 @@ async fn scope_dir_and_agent_id_follow_the_ambient_context() {
}
#[tokio::test]
-async fn spawn_scoped_keeps_the_agent_context_and_tokio_spawn_does_not() {
+async fn a_context_spawn_keeps_the_agent_context_and_tokio_spawn_does_not() {
let parent = root("/tmp/agent-scope-spawn");
let policy = Arc::new(SecurityPolicy {
autonomy: AutonomyLevel::ReadOnly,
@@ -156,7 +156,7 @@ async fn spawn_scoped_keeps_the_agent_context_and_tokio_spawn_does_not() {
);
let (scoped, bare) = CoreContext::scope(alpha, async {
- let scoped = spawn_scoped(async {
+ let scoped = DispatchCtx::ambient().spawn(async {
(
current_agent_id(),
DispatchCtx::ambient().agent_policy().map(|p| p.autonomy),
diff --git a/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs b/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
index b4647b5c67..916dd94cfd 100644
--- a/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
@@ -114,7 +114,7 @@ async fn bound_external_authority_wins_over_legacy_trusted_automation() {
if sender.as_deref() == Some("external-sender")
));
let child =
- crate::core::runtime::spawn_scoped(async { turn_origin::current() })
+ crate::core::runtime::DispatchCtx::ambient().spawn(async { turn_origin::current() })
.await
.unwrap();
assert!(matches!(
@@ -174,7 +174,7 @@ async fn concurrent_channel_turns_keep_tenant_agent_thread_and_policy_isolated()
};
let turn = context.derive_channel_turn(Some(thread.clone()), Some(origin));
CoreContext::scope(turn, async move {
- crate::core::runtime::spawn_scoped(async move {
+ crate::core::runtime::DispatchCtx::ambient().spawn(async move {
barrier.wait().await;
let child = CoreContext::scoped().unwrap();
assert_eq!(child.profile(), Some(format!("profile-{label}").as_str()));
@@ -209,7 +209,7 @@ async fn concurrent_channel_turns_keep_tenant_agent_thread_and_policy_isolated()
async fn scoped_spawn_does_not_invent_missing_turn_authority() {
CoreContext::scope(ctx("/tmp/origin-missing"), async {
let child =
- crate::core::runtime::spawn_scoped(async { crate::agent::turn_origin::current() })
+ crate::core::runtime::DispatchCtx::ambient().spawn(async { crate::agent::turn_origin::current() })
.await
.unwrap();
assert!(child.is_none());
diff --git a/crates/openhuman-core/src/core/runtime/spawn_tests.rs b/crates/openhuman-core/src/core/runtime/spawn_tests.rs
index ecc412d92b..6dbd654c91 100644
--- a/crates/openhuman-core/src/core/runtime/spawn_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/spawn_tests.rs
@@ -1,5 +1,5 @@
use super::*;
-use crate::core::runtime::{ContextOverlay, DomainSet};
+use crate::core::runtime::{ContextOverlay, DispatchCtx, DomainSet};
fn agent_context(agent: &str) -> std::sync::Arc<CoreContext> {
CoreContext::for_test(DomainSet::full(), None).derive_with(
@@ -17,8 +17,14 @@ fn seen() -> Option<String> {
}
#[tokio::test]
-async fn a_bare_spawn_loses_the_scope_and_spawn_scoped_keeps_it() {
+async fn a_bare_spawn_loses_the_scope_and_a_context_spawn_keeps_it() {
let ctx = agent_context("u-asha");
+ let dispatch = DispatchCtx::new(ctx.clone());
+ // Spawned from outside any scope: the task acts for the context it was
+ // handed, not for whatever its spawner happened to run in.
+ let handed = dispatch.spawn(async { seen() }).await.unwrap();
+ assert_eq!(handed.as_deref(), Some("u-asha"));
+
CoreContext::scope(ctx, async {
let bare = tokio::spawn(async { seen() }).await.unwrap();
assert_ne!(
@@ -26,29 +32,56 @@ async fn a_bare_spawn_loses_the_scope_and_spawn_scoped_keeps_it() {
Some("u-asha"),
"a bare spawn falls back to the default context"
);
-
- let scoped = spawn_scoped(async { seen() }).await.unwrap();
- assert_eq!(scoped.as_deref(), Some("u-asha"));
})
.await;
}
+#[tokio::test]
+async fn two_contexts_spawn_tasks_that_do_not_cross() {
+ let a = DispatchCtx::new(agent_context("u-asha"));
+ let b = DispatchCtx::new(agent_context("u-ravi"));
+ let (ra, rb) = tokio::join!(
+ a.spawn(async {
+ tokio::task::yield_now().await;
+ seen()
+ }),
+ b.spawn(async {
+ tokio::task::yield_now().await;
+ seen()
+ })
+ );
+ assert_eq!(ra.unwrap().as_deref(), Some("u-asha"));
+ assert_eq!(rb.unwrap().as_deref(), Some("u-ravi"));
+}
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
-async fn spawn_blocking_scoped_keeps_the_context() {
- let ctx = agent_context("u-ravi");
- let agent = CoreContext::scope(ctx, async {
- spawn_blocking_scoped(|| {
+async fn spawn_blocking_keeps_the_handed_context() {
+ let dispatch = DispatchCtx::new(agent_context("u-ravi"));
+ let agent = dispatch
+ .spawn_blocking(|| {
CoreContext::current().and_then(|c| c.session_agent().map(str::to_owned))
})
.await
- .unwrap()
- })
- .await;
+ .unwrap();
assert_eq!(agent.as_deref(), Some("u-ravi"));
}
+#[tokio::test]
+async fn a_task_spawned_from_a_context_keeps_it_across_awaits_and_nested_spawns() {
+ let dispatch = DispatchCtx::new(agent_context("u-asha"));
+ let seen_in_child = dispatch
+ .spawn(async {
+ tokio::task::yield_now().await;
+ // A nested spawn from inside re-hands the ambient context.
+ DispatchCtx::ambient().spawn(async { seen() }).await.unwrap()
+ })
+ .await
+ .unwrap();
+ assert_eq!(seen_in_child.as_deref(), Some("u-asha"));
+}
+
#[test]
-fn scoping_a_large_task_keeps_its_wrapper_bounded() {
+fn entering_a_large_task_keeps_its_wrapper_bounded() {
// Background pollers capture large async state. Carrying that state
// inline through both task-local wrappers multiplies the moves on the
// spawning thread, including the CLI's ordinary process-main stack.
@@ -57,7 +90,7 @@ fn scoping_a_large_task_keeps_its_wrapper_bounded() {
tokio::task::yield_now().await;
std::hint::black_box(payload)[0]
};
- let wrapped = scoped(task);
+ let wrapped = DispatchCtx::new(agent_context("u-asha")).enter(task);
assert!(
std::mem::size_of_val(&wrapped) < 64 * 1024,
"context propagation must not duplicate a large task inline ({} bytes)",