Skip to content

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

refactor(agent): extract fork context into its own module

Steven Enamakeldf7bd6bab4b5
diff --git a/crates/openhuman-core/src/agent/harness/fork_context.rs b/crates/openhuman-core/src/agent/harness/fork_context.rs
index a6eb750f34..33c81818c6 100644
--- a/crates/openhuman-core/src/agent/harness/fork_context.rs
+++ b/crates/openhuman-core/src/agent/harness/fork_context.rs
@@ -1,11 +1,14 @@
-//! Task-local plumbing that lets `SpawnSubagentTool` reach the parent
-//! agent's runtime context (provider, tools, model, …) without widening
-//! the [`tinytools::Tool`] trait.
+//! The parent agent's runtime snapshot that `spawn_subagent` and the other
+//! orchestration tools hand to the sub-agents they start (provider, tools,
+//! model, …) without widening the [`tinytools::Tool`] trait.
//!
-//! [`PARENT_CONTEXT`] is set by the parent
-//! [`crate::agent::OpenHumanSessionHost`] around its `turn` so that any tool
-//! executing inside that turn (in particular `spawn_subagent`) can read
-//! the parent's provider, tool list, and model information.
+//! A [`ParentExecutionContext`] travels explicitly: the parent
+//! [`crate::agent::OpenHumanSessionHost`] attaches it to its turn's
+//! [`OpenHumanRunContext`](crate::agent::tinyagents::host::OpenHumanRunContext)
+//! (`parent`), tools read it from there, and controller-spawned background
+//! surfaces (the workflow engine, agent teams) build a root one with
+//! [`crate::agent::orchestration::parent_context::with_root_parent`] and pass it
+//! to the spawns they make. There is no ambient copy.
//!
//! Stashed in `Arc`s so cloning into a child costs a refcount bump
//! rather than a full copy.
@@ -25,8 +28,8 @@ use tinytools::{Tool, ToolSpec};
// ─────────────────────────────────────────────────────────────────────────────
/// Snapshot of the parent agent's runtime, made available to any tool
-/// running inside [`crate::agent::OpenHumanSessionHost::turn`] via the
-/// [`PARENT_CONTEXT`] task-local.
+/// running inside [`crate::agent::OpenHumanSessionHost::turn`] through the
+/// turn's run context.
///
/// All heavy fields are `Arc`-shared so cloning the context for sub-agents
/// is essentially free.
@@ -166,29 +169,3 @@ pub struct AgentContextPreparedSource {
pub source: String,
pub has_enough_context: Option<bool>,
}
-
-tokio::task_local! {
- /// Parent execution context, scoped per agent turn. `None` for any
- /// tool invocation that happens outside an agent turn (e.g. CLI/RPC
- /// direct tool calls); `spawn_subagent` rejects in that case.
- pub static PARENT_CONTEXT: ParentExecutionContext;
-}
-
-/// Returns a clone of the current parent execution context, if one is set.
-///
-/// Returns `None` when called from outside [`crate::agent::OpenHumanSessionHost::turn`]
-/// (e.g. CLI tool invocation).
-pub fn current_parent() -> Option<ParentExecutionContext> {
- PARENT_CONTEXT.try_with(|ctx| ctx.clone()).ok()
-}
-
-/// Run `future` with `ctx` installed as the active parent context.
-pub async fn with_parent_context<F, R>(ctx: ParentExecutionContext, future: F) -> R
-where
- F: std::future::Future<Output = R>,
-{
- // Box before `scope` so only a pointer moves into the task-local frame
- // rather than the whole nested turn generator — see the measurements on
- // the explicit usage ledger on `OpenHumanRunContext`.
- PARENT_CONTEXT.scope(ctx, Box::pin(future)).await
-}
diff --git a/crates/openhuman-core/src/agent/harness/mod.rs b/crates/openhuman-core/src/agent/harness/mod.rs
index 0c8b1c4f77..9e13b0a67d 100644
--- a/crates/openhuman-core/src/agent/harness/mod.rs
+++ b/crates/openhuman-core/src/agent/harness/mod.rs
@@ -15,7 +15,7 @@
//!
//! ## Key Sub-modules
//! - **[`host_definition`]**: OpenHuman's extension slot and registry over `tinyagents-definition`.
-//! - **[`fork_context`]**: Task-local storage for parent context sharing.
+//! - **[`fork_context`]**: The parent execution snapshot handed to sub-agents.
//!
//! Cancellation is handled by the tinyagents steering channel (see
//! `crate::agent::tinyagents`); there is no in-house interrupt fence.
@@ -30,8 +30,6 @@ pub(crate) mod tool_result_artifacts;
// NOTE: deliberately no flat re-export here. `artifact_offload::ArtifactKind`
// would shadow the unrelated `openhuman::agent::artifacts::ArtifactKind` for anyone
// glob-importing this module; callers import the owning library directly.
-pub use fork_context::{
- current_parent, with_parent_context, AgentContextPreparedSource, ParentExecutionContext,
-};
+pub use fork_context::{AgentContextPreparedSource, ParentExecutionContext};
pub(crate) use graph::run_channel_turn_via_graph;