Skip to content

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

refactor(runtime): make host_default's saas rule testable

Steven Enamakelfde06a0df9c6
diff --git a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
index ed9e63afb4..f3b137b5c8 100644
--- a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
+++ b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
@@ -73,7 +73,13 @@ impl DispatchCtx {
/// the calling task's scope; an event that names its tenant uses
/// [`for_event`](Self::for_event), a caller holding a context passes it.
pub fn host_default() -> Self {
- if super::is_saas() {
+ Self::host_default_in(super::is_saas())
+ }
+
+ /// [`host_default`](Self::host_default) as a pure function of the mode, so
+ /// the SaaS rule is testable without locking the test process to SaaS.
+ pub(crate) fn host_default_in(saas: bool) -> Self {
+ if saas {
Self::unbound()
} else {
Self::process_default()
diff --git a/crates/openhuman-core/src/core/runtime/dispatch_ctx_tests.rs b/crates/openhuman-core/src/core/runtime/dispatch_ctx_tests.rs
index 3b67884db4..213df56aa9 100644
--- a/crates/openhuman-core/src/core/runtime/dispatch_ctx_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/dispatch_ctx_tests.rs
@@ -104,3 +104,20 @@ async fn host_default_is_the_process_default_whatever_the_task_scope_says() {
assert!(same(&host));
assert!(host.core().is_none_or(|got| !Arc::ptr_eq(got, &scoped)));
}
+
+#[tokio::test]
+async fn host_default_in_saas_is_unbound_even_inside_a_tenants_scope() {
+ let tenant = CoreContext::for_test(DomainSet::full(), None);
+ CoreContext::scope(Arc::clone(&tenant), async {
+ tokio::task::yield_now().await;
+ // The task's scope is a tenant's; work that arrives without its own
+ // tenant must not inherit it, and must not become the operator.
+ assert!(DispatchCtx::host_default_in(true).core().is_none());
+ assert!(DispatchCtx::host_default_in(true).resolved().is_none() || !crate::core::runtime::is_saas());
+ let spawned = tokio::spawn(async { DispatchCtx::host_default_in(true) })
+ .await
+ .unwrap();
+ assert!(spawned.core().is_none());
+ })
+ .await;
+}