Skip to content

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

feat(agent): carry the agent context across spawned turn work (#7032)

oxoxDeve2c463842966
diff --git a/crates/openhuman-core/src/agent/orchestration/agent_teams/runtime.rs b/crates/openhuman-core/src/agent/orchestration/agent_teams/runtime.rs
index 141049422b..f22182cc0a 100644
--- a/crates/openhuman-core/src/agent/orchestration/agent_teams/runtime.rs
+++ b/crates/openhuman-core/src/agent/orchestration/agent_teams/runtime.rs
@@ -179,7 +179,7 @@ pub async fn start_member_run(
// worker keeps the label the approval gate needs. Inherit-only: no origin
// in scope means the worker stays unlabelled and fails closed as before.
let inherited_origin = crate::agent::turn_origin::capture();
- tokio::spawn(async move {
+ crate::core::runtime::spawn_scoped(async move {
crate::agent::turn_origin::with_inherited_origin(
inherited_origin,
run_member_loop(
diff --git a/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/lifecycle.rs b/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/lifecycle.rs
index 4ce684d3ba..143dcdc640 100644
--- a/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/lifecycle.rs
+++ b/crates/openhuman-core/src/agent/orchestration/workflow_runs/engine/lifecycle.rs
@@ -68,7 +68,7 @@ pub async fn start_workflow_run(
// sub-agents whose tool calls the approval gate judges by that label.
// Inherit-only: `None` leaves the loop unlabelled and failing closed.
let inherited_origin = crate::agent::turn_origin::capture();
- tokio::spawn(async move {
+ crate::core::runtime::spawn_scoped(async move {
match Config::load_or_init().await {
Ok(task_config) => {
crate::agent::turn_origin::with_inherited_origin(
@@ -208,7 +208,7 @@ pub async fn resume_workflow_run(config: &Config, id: &str) -> Result<WorkflowRu
// Same inherit-only origin propagation as `start_workflow_run`: the resumed
// loop runs on a fresh task, which would otherwise drop the caller's label.
let inherited_origin = crate::agent::turn_origin::capture();
- tokio::spawn(async move {
+ crate::core::runtime::spawn_scoped(async move {
match Config::load_or_init().await {
Ok(task_config) => {
crate::agent::turn_origin::with_inherited_origin(
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs b/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
index 2d8e4e6309..6529ef8d94 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
@@ -170,7 +170,7 @@ impl OpenHumanTurnPrelude {
return;
};
let prelude = self.clone();
- tokio::spawn(async move {
+ crate::core::runtime::spawn_scoped(async move {
let Some(config) = prelude.memory_config().await else {
return;
};
diff --git a/crates/openhuman-core/src/agent/turn_origin.rs b/crates/openhuman-core/src/agent/turn_origin.rs
index db590ccfc2..d8648d6a95 100644
--- a/crates/openhuman-core/src/agent/turn_origin.rs
+++ b/crates/openhuman-core/src/agent/turn_origin.rs
@@ -277,8 +277,8 @@ pub async fn with_inherited_origin<F: std::future::Future>(
}
}
-/// Carry the origin scoped **right now** into a future that will run on
-/// another task.
+/// Carry the origin and the [`CoreContext`](crate::core::runtime::CoreContext)
+/// scoped **right now** into a future that will run on another task.
///
/// A `tokio::task_local` does not cross `tokio::spawn`: a detached sub-agent
/// (`spawn_async_subagent`, the orchestration `spawn_agent` task) starts on a
@@ -305,10 +305,17 @@ pub async fn with_inherited_origin<F: std::future::Future>(
/// cannot manufacture trust that did not exist on the spawning task.
pub fn propagate<F: std::future::Future>(fut: F) -> impl std::future::Future<Output = F::Output> {
let captured = current();
+ let context = crate::core::runtime::CoreContext::scoped();
async move {
- match captured {
- Some(origin) => with_origin(origin, fut).await,
- None => fut.await,
+ let run = async move {
+ match captured {
+ Some(origin) => with_origin(origin, fut).await,
+ None => fut.await,
+ }
+ };
+ match context {
+ Some(context) => crate::core::runtime::CoreContext::scope(context, run).await,
+ None => run.await,
}
}
}
diff --git a/crates/openhuman-core/src/core/runtime/agent_scope.rs b/crates/openhuman-core/src/core/runtime/agent_scope.rs
index f9b8b1eb90..fa036488a6 100644
--- a/crates/openhuman-core/src/core/runtime/agent_scope.rs
+++ b/crates/openhuman-core/src/core/runtime/agent_scope.rs
@@ -151,6 +151,17 @@ pub fn agent_scope_dir(config: &crate::config::Config) -> PathBuf {
}
}
+/// `tokio::spawn` carrying the ambient [`CoreContext`] and turn origin onto
+/// the new task. A bare `tokio::spawn` runs its future under the process
+/// default context, so an agent's work would read the runtime's policy.
+pub fn spawn_scoped<F>(fut: F) -> tokio::task::JoinHandle<F::Output>
+where
+ F: std::future::Future + Send + 'static,
+ F::Output: Send + 'static,
+{
+ crate::agent::turn_origin::spawn(fut)
+}
+
/// The agent id of the ambient context, when it was derived for one.
pub fn current_agent_id() -> Option<String> {
CoreContext::current().and_then(|ctx| ctx.session_agent().map(str::to_owned))
diff --git a/crates/openhuman-core/src/core/runtime/mod.rs b/crates/openhuman-core/src/core/runtime/mod.rs
index 81d084020f..fa82338137 100644
--- a/crates/openhuman-core/src/core/runtime/mod.rs
+++ b/crates/openhuman-core/src/core/runtime/mod.rs
@@ -50,6 +50,8 @@ pub mod context;
pub mod services;
pub(crate) mod subscribers;
-pub use agent_scope::{agent_scope_dir, current_slot, AgentContextRegistry, AgentScopedState};
+pub use agent_scope::{
+ agent_scope_dir, current_slot, spawn_scoped, AgentContextRegistry, AgentScopedState,
+};
pub use builder::{CoreBuilder, CoreRuntime, DomainSet, ServiceSet, TokenSource};
pub use context::{ContextOverlay, CoreContext};