Skip to content

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

feat(runtime): add spawn helper for runtime processes

Steven Enamakel9cc6cbc4efdd
diff --git a/crates/openhuman-core/src/core/runtime/spawn.rs b/crates/openhuman-core/src/core/runtime/spawn.rs
index ca6002e0e5..b43e100570 100644
--- a/crates/openhuman-core/src/core/runtime/spawn.rs
+++ b/crates/openhuman-core/src/core/runtime/spawn.rs
@@ -1,14 +1,16 @@
//! Spawning tasks without dropping who they work for.
//!
-//! The ambient [`CoreContext`] and the memory identity are tokio task-locals,
-//! so a bare `tokio::spawn` starts its task with neither: the child falls back
-//! to the process default context and the root memory identity. In a
-//! single-user process that default is the right user by accident. In a SaaS
+//! The ambient [`CoreContext`] is a tokio task-local, so a bare `tokio::spawn`
+//! starts its task without it: the child falls back to the process default
+//! context. In a single-user process that default is the right user by
+//! accident. In a SaaS
//! process it is the operator, so a dropped scope writes one user's work
//! somewhere it does not belong — or, with the SaaS config redirect, fails.
//!
-//! [`spawn_scoped`] captures both at the call site and re-enters them in the
-//! child. New code in the core spawns through it; `scripts/ci/check-saas-ambient.mjs`
+//! [`spawn_scoped`] captures it at the call site and re-enters it in the
+//! child. The memory identity is not ambient any more: spawned work that acts
+//! on memory is handed its `MemoryIdentity` explicitly.
+//! New code in the core spawns through it; `scripts/ci/check-saas-ambient.mjs`
//! ratchets the bare spawns that remain.
use std::future::Future;
@@ -17,8 +19,7 @@ use tokio::task::JoinHandle;
use super::CoreContext;
-/// Like `tokio::spawn`, but the task keeps the caller's [`CoreContext`] and
-/// memory identity.
+/// Like `tokio::spawn`, but the task keeps the caller's [`CoreContext`].
pub fn spawn_scoped<F>(fut: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
diff --git a/crates/openhuman-core/src/core/runtime/spawn_tests.rs b/crates/openhuman-core/src/core/runtime/spawn_tests.rs
index 19b949343d..ecc412d92b 100644
--- a/crates/openhuman-core/src/core/runtime/spawn_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/spawn_tests.rs
@@ -1,6 +1,5 @@
use super::*;
use crate::core::runtime::{ContextOverlay, DomainSet};
-use crate::memory::scope::MemoryIdentity;
fn agent_context(agent: &str) -> std::sync::Arc<CoreContext> {
CoreContext::for_test(DomainSet::full(), None).derive_with(
@@ -13,31 +12,23 @@ fn agent_context(agent: &str) -> std::sync::Arc<CoreContext> {
)
}
-fn seen() -> (Option<String>, Option<String>) {
- (
- CoreContext::current().and_then(|c| c.session_agent().map(str::to_owned)),
- crate::memory::scope::current().and_then(|i| i.agent_id),
- )
+fn seen() -> Option<String> {
+ CoreContext::current().and_then(|c| c.session_agent().map(str::to_owned))
}
#[tokio::test]
async fn a_bare_spawn_loses_the_scope_and_spawn_scoped_keeps_it() {
let ctx = agent_context("u-asha");
CoreContext::scope(ctx, async {
- crate::memory::scope::within(MemoryIdentity::agent("orchestrator"), async {
- let bare = tokio::spawn(async { seen() }).await.unwrap();
- assert_ne!(
- bare.0.as_deref(),
- Some("u-asha"),
- "a bare spawn falls back to the default context"
- );
- assert_eq!(bare.1, None, "and to no memory identity");
+ let bare = tokio::spawn(async { seen() }).await.unwrap();
+ assert_ne!(
+ bare.as_deref(),
+ Some("u-asha"),
+ "a bare spawn falls back to the default context"
+ );
- let scoped = spawn_scoped(async { seen() }).await.unwrap();
- assert_eq!(scoped.0.as_deref(), Some("u-asha"));
- assert_eq!(scoped.1.as_deref(), Some("orchestrator"));
- })
- .await;
+ let scoped = spawn_scoped(async { seen() }).await.unwrap();
+ assert_eq!(scoped.as_deref(), Some("u-asha"));
})
.await;
}
@@ -56,26 +47,6 @@ async fn spawn_blocking_scoped_keeps_the_context() {
assert_eq!(agent.as_deref(), Some("u-ravi"));
}
-#[tokio::test]
-async fn outside_any_scope_spawn_scoped_is_a_plain_spawn() {
- let (_, identity) = spawn_scoped(async { seen() }).await.unwrap();
- assert_eq!(identity, None);
-}
-
-#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
-async fn spawn_blocking_scoped_keeps_the_memory_identity() {
- let ctx = agent_context("u-ravi");
- let seen = CoreContext::scope(ctx, async {
- crate::memory::scope::within(MemoryIdentity::agent("planner"), async {
- spawn_blocking_scoped(seen).await.unwrap()
- })
- .await
- })
- .await;
- assert_eq!(seen.0.as_deref(), Some("u-ravi"));
- assert_eq!(seen.1.as_deref(), Some("planner"));
-}
-
#[test]
fn scoping_a_large_task_keeps_its_wrapper_bounded() {
// Background pollers capture large async state. Carrying that state