Skip to content

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

fix(runtime): restore agent spawn after runtime refactor

Steven Enamakel21d1e4d32565
diff --git a/crates/openhuman-core/src/core/runtime/mod.rs b/crates/openhuman-core/src/core/runtime/mod.rs
index 7174bf6832..8b7b4b9300 100644
--- a/crates/openhuman-core/src/core/runtime/mod.rs
+++ b/crates/openhuman-core/src/core/runtime/mod.rs
@@ -68,7 +68,6 @@ pub use context::{ContextOverlay, CoreContext};
pub use dispatch_ctx::DispatchCtx;
pub use mode::{current_mode, is_saas, Mode};
pub use saas::SaasConfig;
-pub use spawn::{spawn_blocking_scoped, spawn_scoped};
pub use tenant::{current_tenant, session_key, session_key_prefix, tenant_key, NoTenant, Tenant};
/// Operator-owned direct inference settings.
diff --git a/crates/openhuman-core/src/core/runtime/spawn.rs b/crates/openhuman-core/src/core/runtime/spawn.rs
index b43e100570..1e685a8b18 100644
--- a/crates/openhuman-core/src/core/runtime/spawn.rs
+++ b/crates/openhuman-core/src/core/runtime/spawn.rs
@@ -1,34 +1,70 @@
//! Spawning tasks without dropping who they work for.
//!
-//! 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.
+//! A tokio task starts with no ambient [`CoreContext`] (it is a task-local), so
+//! a bare `tokio::spawn` falls back to the process default context. In a
+//! single-user process that is the right user by accident; in SaaS it is the
+//! operator, so a dropped scope writes one user's work somewhere it does not
+//! belong, or fails.
//!
-//! [`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.
+//! The context travels as a value instead: a spawner holds a [`DispatchCtx`]
+//! and spawns through [`DispatchCtx::spawn`] / [`DispatchCtx::spawn_blocking`],
+//! which hand it to the task. A task whose body acts on no tenant state
+//! (a signal relay, a forwarder, a process-wide sweeper) is a plain
+//! `tokio::spawn`.
use std::future::Future;
use tokio::task::JoinHandle;
-use super::CoreContext;
+use super::{CoreContext, DispatchCtx};
-/// 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,
- F::Output: Send + 'static,
-{
- tokio::spawn(scoped(fut))
+impl DispatchCtx {
+ /// `fut`, wrapped to run under this context wherever it is polled. An
+ /// unbound context runs it bare, which in SaaS fails closed.
+ pub fn enter<F>(&self, fut: F) -> impl Future<Output = F::Output> + Send
+ where
+ F: Future + Send,
+ F::Output: Send,
+ {
+ let ctx = self.core().cloned();
+ // Box before the task-local wrapper. Its branching async state otherwise
+ // multiplies a large task's inline size before Tokio can box it,
+ // overflowing the spawning thread (including the CLI's main stack).
+ let fut = Box::pin(fut);
+ async move {
+ match ctx {
+ Some(ctx) => CoreContext::scope(ctx, fut).await,
+ None => fut.await,
+ }
+ }
+ }
+
+ /// Like `tokio::spawn`, but the task acts under this context.
+ pub fn spawn<F>(&self, fut: F) -> JoinHandle<F::Output>
+ where
+ F: Future + Send + 'static,
+ F::Output: Send + 'static,
+ {
+ tokio::spawn(self.enter(fut))
+ }
+
+ /// Like `tokio::task::spawn_blocking`, but the closure runs under this
+ /// context.
+ pub fn spawn_blocking<F, R>(&self, f: F) -> JoinHandle<R>
+ where
+ F: FnOnce() -> R + Send + 'static,
+ R: Send + 'static,
+ {
+ let ctx = self.core().cloned();
+ let handle = tokio::runtime::Handle::current();
+ tokio::task::spawn_blocking(move || match ctx {
+ Some(ctx) => handle.block_on(CoreContext::scope(ctx, async move { f() })),
+ None => f(),
+ })
+ }
}
-/// The context a spawned task inherits. In SaaS only the caller's own task
+/// The context a carried future inherits. In SaaS only the caller's own task
/// scope is carried: an unscoped caller's task gets no context (and so fails
/// closed) rather than the operator's default one.
fn captured() -> Option<std::sync::Arc<CoreContext>> {
@@ -39,27 +75,6 @@ fn captured() -> Option<std::sync::Arc<CoreContext>> {
}
}
-/// `fut`, wrapped to run under the caller's context wherever it is eventually
-/// polled. The memory identity is not ambient and so is not carried: work that
-/// acts on memory takes it explicitly (the run context, `MemoryIdentity`).
-pub fn scoped<F>(fut: F) -> impl Future<Output = F::Output> + Send
-where
- F: Future + Send,
- F::Output: Send,
-{
- let ctx = captured();
- // Box before the task-local wrapper. Its branching async state otherwise
- // multiplies a large task's inline size before Tokio can box it,
- // overflowing the spawning thread (including the CLI's main stack).
- let fut = Box::pin(fut);
- async move {
- match ctx {
- Some(ctx) => CoreContext::scope(ctx, fut).await,
- None => fut.await,
- }
- }
-}
-
/// What [`CoreContext::propagate`] wraps a future in: the caller's context
/// captured now and re-entered wherever the future is polled.
///
@@ -77,21 +92,6 @@ pub(crate) fn carry<F: Future>(fut: F) -> impl Future<Output = F::Output> {
}
}
-/// Like `tokio::task::spawn_blocking`, but the closure runs under the caller's
-/// [`CoreContext`].
-pub fn spawn_blocking_scoped<F, R>(f: F) -> JoinHandle<R>
-where
- F: FnOnce() -> R + Send + 'static,
- R: Send + 'static,
-{
- let ctx = captured();
- let handle = tokio::runtime::Handle::current();
- tokio::task::spawn_blocking(move || match ctx {
- Some(ctx) => handle.block_on(CoreContext::scope(ctx, async move { f() })),
- None => f(),
- })
-}
-
#[cfg(test)]
#[path = "spawn_tests.rs"]
mod tests;