The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(orchestration): extract parent context builder
Steven Enamakel7ced82bc886d
diff --git a/crates/openhuman-core/src/agent/orchestration/ops.rs b/crates/openhuman-core/src/agent/orchestration/ops.rsindex a6d0d5694d..8fa3597d0d 100644--- a/crates/openhuman-core/src/agent/orchestration/ops.rs+++ b/crates/openhuman-core/src/agent/orchestration/ops.rs@@ -26,7 +26,7 @@ use super::types::{AgentSnapshot, SpawnAgentRequest, SpawnAgentResponse, WaitAgentOptions, WaitAgentResponse,};use crate::agent::harness::fork_context::{- current_parent, with_parent_context, ParentExecutionContext,+ ParentExecutionContext,};use crate::agent::harness::host_definition::{AgentDefinition, AgentDefinitionRegistry};use crate::agent::progress::AgentProgress;@@ -146,7 +146,10 @@ impl AgentOrchestrationSession {&self.session_id}- /// Spawn a child agent from the active parent agent turn.+ /// Spawn a child agent under `parent`, the explicit execution context of the+ /// agent turn (or controller-built root, see+ /// [`with_root_parent`](crate::agent::orchestration::parent_context::with_root_parent))+ /// that is starting it.////// `request` must provide a non-empty `agent_id` and `prompt`; optional/// context, model, parent id, and metadata are carried into the@@ -154,8 +157,7 @@ impl AgentOrchestrationSession {/// accepted child id and initial status while a background task executes the/// child through [`run_subagent`].///- /// Returns [`OrchestrationError::NoParentContext`] when called outside an- /// agent turn, [`OrchestrationError::InvalidSpawnRequest`] for an empty+ /// Returns [`OrchestrationError::InvalidSpawnRequest`] for an empty/// agent id or prompt, [`OrchestrationError::RegistryUnavailable`] when the/// agent definition registry is not initialized, or/// [`OrchestrationError::DefinitionNotFound`] for an unknown agent id. Side@@ -164,9 +166,9 @@ impl AgentOrchestrationSession {/// emitting parent progress when available.pub async fn spawn_agent(&self,+ parent: ParentExecutionContext,request: SpawnAgentRequest,) -> Result<SpawnAgentResponse, OrchestrationError> {- let parent = current_parent().ok_or(OrchestrationError::NoParentContext)?;let definition = resolve_definition(&request)?;self.spawn_agent_with_definition(parent, definition, request).await@@ -415,10 +417,8 @@ impl AgentOrchestrationSession {// tools reach the approval gate unlabelled and are refused.let handle = tokio::spawn(crate::agent::turn_origin::propagate(async move {mark_running(&task_status_tx);- let result = with_parent_context(parent, async move {- run_subagent(&definition, &prompt, options).await- })- .await;+ // The parent rides `options.run_context.parent`, built above.+ let result = run_subagent(&definition, &prompt, options).await;session.finish_agent(&task_id,diff --git a/crates/openhuman-core/src/agent/orchestration/parent_context/builder.rs b/crates/openhuman-core/src/agent/orchestration/parent_context/builder.rsindex b321c917d0..23a5272fab 100644--- a/crates/openhuman-core/src/agent/orchestration/parent_context/builder.rs+++ b/crates/openhuman-core/src/agent/orchestration/parent_context/builder.rs@@ -3,20 +3,20 @@//!//! The workflow-run engine ([`workflow_runs::engine`]) and the agent-team//! runtime ([`agent_teams::runtime`]) both need to spawn real sub-agents from a-//! background task that has **no** enclosing agent turn on the stack. Those-//! spawns read their parent execution context from a task-local-//! ([`current_parent`]) that is only set inside an agent turn — so a naive spawn+//! background task that has **no** enclosing agent turn on the stack, so there is+//! no live parent execution context for the spawn to inherit — a naive spawn//! fails with `NoParentContext` (the TAURI-RUST-HMW regression, #4337).//!//! The fix (proven in `triage::escalation::dispatch_target_agent`) is to build a-//! *root* [`ParentExecutionContext`] from a config-built [`Agent`] and run the-//! whole loop inside [`with_parent_context`]. Every nested `spawn_agent` then-//! resolves `current_parent()` to this root, inheriting a real provider, tool-//! registry, memory, and model — the same construction path `agent_chat` uses.+//! *root* [`ParentExecutionContext`] from a config-built [`Agent`] and hand it,+//! explicitly, to the loop that spawns. Every `spawn_agent` call then names this+//! root, inheriting a real provider, tool registry, memory, and model — the same+//! construction path `agent_chat` uses.//!-//! [`with_root_parent`] is the single blessed entry point that folds the build +-//! install into one call, so a surface can neither hand-roll the parent nor-//! forget to install it. Every background orchestration surface goes through it.+//! [`with_root_parent`] is the single blessed entry point that builds the root+//! and passes it to the surface's closure, so a surface can neither hand-roll the+//! parent nor forget to supply one. Every background orchestration surface goes+//! through it.//!//! This was originally inlined in the workflow engine; #3374 PR4 lifted it here//! so each surface reuses the exact same construction (and the single@@ -28,9 +28,7 @@ use std::sync::Arc;use anyhow::{Context, Result};-use crate::agent::harness::fork_context::{- current_parent, with_parent_context, ParentExecutionContext,-};+use crate::agent::harness::fork_context::ParentExecutionContext;use crate::agent::OpenHumanSessionHost;use crate::config::Config;@@ -123,53 +121,43 @@ pub(crate) async fn build_root_parent(})}-/// Ensure a parent execution context is installed for `fut`, then run it — the-/// single blessed entry point for **controller-spawned background orchestration-/// surfaces** that have no enclosing agent turn (the workflow-run engine, the-/// agent-team runtime, cron jobs).+/// Build (or accept) the parent execution context for a **controller-spawned+/// background orchestration surface** that has no enclosing agent turn (the+/// workflow-run engine, the agent-team runtime, cron jobs), then run+/// `run(parent)` with it.///-/// Folds [`build_root_parent`] + [`with_parent_context`] into one call so a-/// surface cannot install a hand-rolled parent, and — the TAURI-RUST-HMW-/// failure mode — cannot *forget* to install one at all and have every nested-/// `spawn_subagent` die at runtime with+/// Folds [`build_root_parent`] and the hand-off into one call so a surface+/// cannot supply a hand-rolled parent, and cannot *forget* to supply one and+/// have every nested `spawn_subagent` die at runtime with/// [`SubagentRunError::NoParentContext`](crate::agent::subagent_host::SubagentRunError::NoParentContext).-/// Running a background surface and establishing its root context become the-/// same act.+/// The parent is a value the closure receives and passes to the spawns it makes+/// (`AgentOrchestrationSession::spawn_agent`); nothing reads it ambiently.///-/// When an ambient [`current_parent`] is already installed, `fut` runs under it-/// unchanged rather than building a second root. In production these surfaces-/// run on freshly-spawned tasks where task-locals never cross the `tokio::spawn`-/// boundary, so the ambient parent is always absent and a root is built from-/// `config` — but reusing an installed parent keeps the helper correct if it is-/// ever nested inside a turn, and lets tests drive a surface under a mock-/// parent (and thus a mock provider) hermetically.+/// `parent_override` is the explicit injection point for a parent the caller+/// already has (a test driving a surface under a mock parent, and thus a mock+/// provider, hermetically): when `Some`, it is used unchanged and no root is+/// built. Production surfaces pass `None` and a root is built from `config`.///-/// Returns the future's output on success, or the [`build_root_parent`] error+/// Returns the closure's output on success, or the [`build_root_parent`] error/// when the root context can't be constructed — the caller decides how to/// degrade (fail the run, or fall back to an un-grounded path).-///-/// Only for surfaces that build their parent *from `Config`* because there is-/// no ambient parent. Spawn sites that re-install an **inherited**-/// `current_parent()` across a `tokio::spawn` boundary (e.g.-/// `spawn_async_subagent`, `spawn_worker_thread`, the orchestration `ops` task)-/// are a different pattern and call [`with_parent_context`] directly.-pub(crate) async fn with_root_parent<F>(+pub(crate) async fn with_root_parent<F, Fut>(+ parent_override: Option<ParentExecutionContext>,config: &Config,agent_definition_id: &str,channel: &str,session_prefix: &str,- fut: F,-) -> Result<F::Output>+ run: F,+) -> Result<Fut::Output>where- F: std::future::Future,+ F: FnOnce(ParentExecutionContext) -> Fut,+ Fut: std::future::Future,{- // Already inside a turn (nested call, or a test harness installed a mock- // parent): reuse it rather than building a second root.- if current_parent().is_some() {- return Ok(fut.await);- }- let parent = build_root_parent(config, agent_definition_id, channel, session_prefix).await?;- Ok(with_parent_context(parent, fut).await)+ let parent = match parent_override {+ Some(parent) => parent,+ None => build_root_parent(config, agent_definition_id, channel, session_prefix).await?,+ };+ Ok(run(parent).await)}#[cfg(test)]