Skip to content

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

refactor: dispatch async subagents with parent context

Steven Enamakel0076eaaaee65
diff --git a/crates/openhuman-core/src/agent/harness/subagent_runner/ops/graph_tests.rs b/crates/openhuman-core/src/agent/harness/subagent_runner/ops/graph_tests.rs
index 654176bbff..3c9b569a64 100644
--- a/crates/openhuman-core/src/agent/harness/subagent_runner/ops/graph_tests.rs
+++ b/crates/openhuman-core/src/agent/harness/subagent_runner/ops/graph_tests.rs
@@ -107,6 +107,7 @@ async fn subagent_runs_through_the_graph_engine_with_real_tools() {
"task-1",
false,
None,
+ crate::agent::tinyagents::host::OpenHumanRunContext::new(),
std::env::temp_dir(),
None,
1024,
@@ -192,6 +193,7 @@ async fn child_text_and_thinking_deltas_are_scoped_to_the_subagent() {
"task-7",
false,
None,
+ crate::agent::tinyagents::host::OpenHumanRunContext::new(),
std::env::temp_dir(),
None,
1024,
@@ -327,6 +329,7 @@ async fn ask_user_clarification_pauses_and_surfaces_the_question() {
"task-9",
false,
None,
+ crate::agent::tinyagents::host::OpenHumanRunContext::new(),
std::env::temp_dir(),
None,
1024,
@@ -418,6 +421,7 @@ async fn cap_hit_summarizes_a_resumable_checkpoint() {
"task-cap",
false,
None,
+ crate::agent::tinyagents::host::OpenHumanRunContext::new(),
std::env::temp_dir(),
None,
1024,
@@ -583,6 +587,7 @@ async fn run_with_spawn_tool_in_parent_surface(allowed: HashSet<String>) -> (boo
"task-6157",
false,
None,
+ crate::agent::tinyagents::host::OpenHumanRunContext::new(),
std::env::temp_dir(),
None,
1024,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools.rs b/crates/openhuman-core/src/agent/orchestration/tools.rs
index d74cf81882..c90d3ea7cb 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools.rs
@@ -73,6 +73,7 @@ pub use continue_subagent::ContinueSubagentTool;
pub use delegate_graph::DelegateGraphTool;
pub use list_subagents::ListSubagentsTool;
pub use skill_delegation::{SkillDelegationTool, INTEGRATIONS_DELEGATE_TOOL_NAME};
+pub(crate) use spawn_async_subagent::SpawnAsyncSubagentDispatch;
pub use spawn_async_subagent::SpawnAsyncSubagentTool;
pub(crate) use spawn_parallel_agents::SpawnParallelAgentsDispatch;
pub use spawn_parallel_agents::SpawnParallelAgentsTool;
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
index d5da9f5ec3..06b49437a9 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
@@ -18,11 +18,48 @@ use crate::agent::progress::AgentProgress;
use crate::memory::conversations::{self as conversations, ConversationMessage};
use async_trait::async_trait;
use serde_json::json;
+use std::sync::Arc;
+use tinyagents_harness::context::RunContext;
+use tinyagents_harness::tool::{ToolDispatch, ToolExecutionContext};
use tinytools::ToolRunContext;
use tinytools::{PermissionLevel, Tool, ToolCallOptions, ToolResult};
pub struct SpawnAsyncSubagentTool;
+/// Harness dispatch for the detached child path. It owns the typed parent run
+/// so the spawned child receives the caller's carrier before `tokio::spawn`.
+pub(crate) struct SpawnAsyncSubagentDispatch {
+ tool: Arc<dyn Tool>,
+}
+
+impl SpawnAsyncSubagentDispatch {
+ pub(crate) fn new(tool: Arc<dyn Tool>) -> Self {
+ Self { tool }
+ }
+}
+
+#[async_trait]
+impl ToolDispatch<(), crate::agent::tinyagents::host::OpenHumanRunContext>
+ for SpawnAsyncSubagentDispatch
+{
+ fn tool(&self) -> Arc<dyn Tool> {
+ self.tool.clone()
+ }
+
+ async fn execute(
+ &self,
+ _state: &(),
+ arguments: serde_json::Value,
+ _options: ToolCallOptions,
+ parent: &RunContext<crate::agent::tinyagents::host::OpenHumanRunContext>,
+ ) -> anyhow::Result<ToolResult> {
+ let context = ToolExecutionContext::from_run_context(parent);
+ SpawnAsyncSubagentTool::new()
+ .execute_with_parent_context(arguments, Some(&context), parent.data.child())
+ .await
+ }
+}
+
impl SpawnAsyncSubagentTool {
pub fn new() -> Self {
Self
@@ -115,7 +152,24 @@ impl Tool for SpawnAsyncSubagentTool {
options: ToolCallOptions,
tool_context: Option<&dyn ToolRunContext>,
) -> anyhow::Result<ToolResult> {
- self.execute_with_context_inner(args, options, tool_context)
+ self.execute_with_context_inner(
+ args,
+ options,
+ tool_context,
+ crate::agent::tinyagents::host::OpenHumanRunContext::new(),
+ )
+ .await
+ }
+}
+
+impl SpawnAsyncSubagentTool {
+ pub(crate) async fn execute_with_parent_context(
+ &self,
+ args: serde_json::Value,
+ tool_context: Option<&dyn ToolRunContext>,
+ run_context: crate::agent::tinyagents::host::OpenHumanRunContext,
+ ) -> anyhow::Result<ToolResult> {
+ self.execute_with_context_inner(args, ToolCallOptions::default(), tool_context, run_context)
.await
}
}
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
index 5c37fef3e0..4dc22dec0a 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
@@ -4,6 +4,7 @@ impl SpawnAsyncSubagentTool {
args: serde_json::Value,
_options: ToolCallOptions,
tool_context: Option<&dyn ToolRunContext>,
+ run_context: crate::agent::tinyagents::host::OpenHumanRunContext,
) -> anyhow::Result<ToolResult> {
let agent_id = args
.get("agent_id")
@@ -430,7 +431,7 @@ impl SpawnAsyncSubagentTool {
model_override,
task_id: Some(background_task_id.clone()),
thread_id: Some(background_thread_id),
- run_context: Default::default(),
+ run_context,
worker_thread_id: background_worker_thread_id.clone(),
initial_history: background_initial_history,
checkpoint_dir: None,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
index a6e3944875..309c00f522 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
@@ -7,6 +7,15 @@ use crate::memory::{Memory, MemoryCategory, MemoryEntry, NamespaceSummary, Recal
use std::collections::HashSet;
use std::path::Path;
use std::sync::Arc;
+use tinytools::{ToolCallOptions, ToolRunContext};
+
+struct ThreadContext(&'static str);
+
+impl ToolRunContext for ThreadContext {
+ fn thread_id(&self) -> Option<&str> {
+ Some(self.0)
+ }
+}
#[test]
fn parameters_schema_advertises_fire_and_forget_fields() {
@@ -293,16 +302,18 @@ async fn guard_does_not_fire_when_parent_thread_is_bound() {
let _ = AgentDefinitionRegistry::init_global_builtins();
let workspace = tempfile::TempDir::new().expect("workspace");
+ let thread = ThreadContext("t-parent");
let result = with_parent_context(parent_context(workspace.path()), async {
- crate::agent::tinyagents::thread_context::with_thread_id("t-parent", async {
- SpawnAsyncSubagentTool::new()
- .execute(json!({
+ SpawnAsyncSubagentTool::new()
+ .execute_with_context(
+ json!({
"agent_id": "researcher",
"prompt": "investigate x",
- }))
- .await
- })
- .await
+ }),
+ ToolCallOptions::default(),
+ Some(&thread),
+ )
+ .await
})
.await
.unwrap();
diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_tool_registration.rs b/crates/openhuman-core/src/agent/tinyagents/harness_tool_registration.rs
index a4e0889dda..495c84ffd0 100644
--- a/crates/openhuman-core/src/agent/tinyagents/harness_tool_registration.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_tool_registration.rs
@@ -11,7 +11,7 @@ use tinyagents_registry::{
CapabilityRegistry, ComponentKind, RegistryDiagnostic, RegistrySnapshot,
};
-use crate::agent::orchestration::tools::SpawnParallelAgentsDispatch;
+use crate::agent::orchestration::tools::{SpawnAsyncSubagentDispatch, SpawnParallelAgentsDispatch};
use crate::agent::tinyagents::host::OpenHumanRunContext;
use crate::agent::tinyagents::tools::{CanonicalSharedToolAdapter, EarlyExitHook};
use crate::agent::tinyagents::turn_policy::is_subagent_spawn_or_delegate_tool;
@@ -96,6 +96,9 @@ pub(super) fn register_turn_tools_and_agents(
harness.register_tool_dispatch(Arc::new(SpawnParallelAgentsDispatch::new(
adapter,
)));
+ } else if name == "spawn_async_subagent" {
+ harness
+ .register_tool_dispatch(Arc::new(SpawnAsyncSubagentDispatch::new(adapter)));
} else {
harness.register_tool(adapter);
}