Skip to content

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

refactor(tinyagents): route subagent delegation through spawn_subagent

Steven Enamakel6d5b2b10508b
diff --git a/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs b/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
index 07b28d26d8..9f65d9c466 100644
--- a/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
@@ -43,7 +43,7 @@ pub(crate) fn subagent_prompt_protocol(
/// The shared static names stay aligned with TinyAgents; registry lookup is
/// needed only for host-defined, unprefixed `delegate_name` overrides.
pub(super) fn is_subagent_spawn_tool(name: &str) -> bool {
- if crate::agent::tinyagents::turn_policy::is_subagent_spawn_or_delegate_tool(name) {
+ if crate::agent::tinyagents::turn_policy::is_subagent_spawn_tool(name) {
return true;
}
let registry = crate::agent_registry::host_definition::AgentDefinitionRegistry::current();
diff --git a/crates/openhuman-core/src/agent/subagent_host/tool_prep_tests.rs b/crates/openhuman-core/src/agent/subagent_host/tool_prep_tests.rs
index c77a5b1245..3bf324d5e9 100644
--- a/crates/openhuman-core/src/agent/subagent_host/tool_prep_tests.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/tool_prep_tests.rs
@@ -16,7 +16,7 @@ fn child_tool_surface_never_contains_any_spawn_or_continuation_tool() {
.collect::<Vec<_>>();
for name in &spawn_names {
assert!(
- crate::agent::tinyagents::is_subagent_spawn_or_delegate_tool(name),
+ crate::agent::tinyagents::is_subagent_spawn_tool(name),
"shared spawn predicate missed {name}"
);
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_policy.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_policy.rs
index 8c3c350a69..4b9e801968 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_policy.rs
@@ -166,55 +166,31 @@ impl ToolPolicyHostState {
.find(|t| t.name() == name)
}
- /// The delegation tools this session can actually call that reach one of
- /// `owners`, as tool names.
- ///
- /// Derived from the session's own tool set — every synthesised `delegate_*`
- /// tool publishes its target agent on the erased host-extension slot
- /// (`traits::delegation_target`). That is deliberately the only source: a
- /// static owner-to-tool table would duplicate each agent's `delegate_name`
- /// and could name a tool this session was never built with, sending the
- /// model from one dead end into another. Asking the tool set cannot.
+ /// Return the single spawn route when it can dispatch any owner in this
+ /// pack. The registered schema carries the actual allowed agent ids.
fn callable_delegates_for(&self, owners: &[&str]) -> Vec<String> {
- let mut found: Vec<String> = Vec::new();
- for tool in self.tool_sets.iter().flat_map(|set| set.iter()) {
- let Some(target) = crate::tools::host_extensions::delegation_target(tool.as_ref())
- else {
- continue;
- };
- if !owners.contains(&target) {
- continue;
- }
- let name = tool.name().to_string();
- // Uses `is_denied()`, and that is deliberate — it is the same
- // predicate as the gate this hint points at.
- //
- // Spelled out, because two predicates live in this file and a
- // sentence that does not name one has been misread three times:
- //
- // * This hint names a tool for the model to call DIRECTLY.
- // * The direct-call gate is `channel_permission_block`'s first
- // check, `if decision.is_denied()` (this file, top of the fn).
- // * `is_denied()` is `!matches!(action, Allow)`, so it is TRUE for
- // `HideFromPrompt` — that check is what refuses a prompt-hidden
- // tool called by name.
- // * Therefore a prompt-hidden delegate is not a route, and
- // `is_denied()` here is exactly what keeps it out.
- //
- // `blocks_execution()` would be wrong here: it deliberately admits
- // `HideFromPrompt` for the `use_skill` path below, where hiding is
- // the disclosure mechanism rather than a refusal. Same tool, two
- // call paths, two answers. A hint must use the predicate of the gate
- // it points at — the hint and the gate disagreeing is how this whole
- // class of bug started.
- //
- // Pinned by `a_prompt_hidden_delegate_is_not_offered_as_a_direct_route`.
- if self.session.decision_for(&name).is_denied() || found.contains(&name) {
- continue;
- }
- found.push(name);
+ let Some(spawn) = self.resolve_tool("spawn_subagent") else {
+ return Vec::new();
+ };
+ if self.session.decision_for("spawn_subagent").is_denied() {
+ return Vec::new();
+ }
+ let allowed = spawn
+ .parameters_schema()
+ .pointer("/properties/agent_id/enum")
+ .and_then(serde_json::Value::as_array);
+ let can_reach_owner = allowed
+ .map(|ids| {
+ owners
+ .iter()
+ .any(|owner| ids.iter().any(|id| id.as_str() == Some(owner)))
+ })
+ .unwrap_or(true);
+ if can_reach_owner {
+ vec!["spawn_subagent".to_string()]
+ } else {
+ Vec::new()
}
- found
}
/// The answer to a `use_skill` call naming a tool `pack` does not contain:
diff --git a/crates/openhuman-core/src/agent/tinyagents/mod.rs b/crates/openhuman-core/src/agent/tinyagents/mod.rs
index e90dc88697..a17857b7d2 100644
--- a/crates/openhuman-core/src/agent/tinyagents/mod.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/mod.rs
@@ -75,7 +75,7 @@ pub(crate) use turn_exec_native::NativeSubagentRunContext;
pub(crate) use turn_outcome::DeferredTurnTerminal;
pub(crate) use turn_outcome::TinyagentsTurnOutcome;
#[cfg(test)]
-pub(crate) use turn_policy::is_subagent_spawn_or_delegate_tool;
+pub(crate) use turn_policy::is_subagent_spawn_tool;
pub(crate) use turn_policy::{
agent_turn_wall_clock_ms_for, chat_provider_is_local, local_web_turn_backstop_secs,
ToolPolicyEnforcement,
diff --git a/crates/openhuman-core/src/agent/tinyagents/tool_registration.rs b/crates/openhuman-core/src/agent/tinyagents/tool_registration.rs
index fe80091aac..485a4c30a0 100644
--- a/crates/openhuman-core/src/agent/tinyagents/tool_registration.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/tool_registration.rs
@@ -21,7 +21,7 @@ use tinytools::{Tool, ToolCallOptions, ToolResult, ToolRunContext};
use crate::agent::orchestration::tools::SpawnSubagentDispatch;
use crate::agent::tinyagents::host::run_context::HostOperationContext;
use crate::agent::tinyagents::host::OpenHumanRunContext;
-use crate::agent::tinyagents::turn_policy::is_subagent_spawn_or_delegate_tool;
+use crate::agent::tinyagents::turn_policy::is_subagent_spawn_tool;
use crate::agent::tools::TodoToolDispatch;
use crate::core::runtime::CoreContext;
@@ -121,13 +121,13 @@ fn live_dispatch_selector(tool_sets: Vec<SharedToolSet>) -> DispatchSelector {
dispatch_selector(tool_sets)
}
-/// Names among the candidate tools that spawn or delegate to a sub-agent.
+/// Names among the candidate tools that spawn a sub-agent.
fn candidate_spawn_tools(tool_sets: &[SharedToolSet]) -> HashSet<String> {
tool_sets
.iter()
.flat_map(|set| set.iter())
.map(|tool| tool.name().to_string())
- .filter(|name| is_subagent_spawn_or_delegate_tool(name))
+ .filter(|name| is_subagent_spawn_tool(name))
.collect()
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
index 2f20cbc3ee..f522bd648e 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
@@ -156,7 +156,7 @@ pub(crate) fn tool_call_limit(max_iterations: usize, cap: Option<usize>) -> usiz
/// defense-in-depth so a misconfigured allowlist cannot reintroduce sub-agent
/// spawning into a nested run. It shares TinyAgents' canonical spawn names and
/// the host's single spawn surface.
-pub(crate) fn is_subagent_spawn_or_delegate_tool(name: &str) -> bool {
+pub(crate) fn is_subagent_spawn_tool(name: &str) -> bool {
name == "spawn_subagent" || tinyagents_definition::SPAWN_TOOL_NAMES.contains(&name)
}
diff --git a/crates/openhuman-core/src/tools/host_extensions.rs b/crates/openhuman-core/src/tools/host_extensions.rs
index 13e74ba4a7..38a9e179b6 100644
--- a/crates/openhuman-core/src/tools/host_extensions.rs
+++ b/crates/openhuman-core/src/tools/host_extensions.rs
@@ -1,10 +1,9 @@
//! OpenHuman-owned metadata recovered from TinyTools' erased extension seams.
//!
//! These helpers are intentionally separate from the `tinytools` vocabulary:
-//! pack registries and delegation targets are
-//! host implementation details, not portable tool-trait APIs.
+//! pack-registry handles are host implementation details, not portable
+//! tool-trait APIs.
-use crate::agent::orchestration::tools::DelegationTarget;
use tinyagents_harness_tools::skills::PackRegistryHandle;
#[cfg(test)]
use tinytools::{PermissionLevel, ToolCategory, ToolResult, ToolScope};
@@ -15,13 +14,6 @@ pub fn pack_registry_handle(tool: &dyn Tool) -> Option<PackRegistryHandle> {
tinyagents_harness_tools::skills::pack_registry_handle(tool)
}
-/// Reads the target agent a synthesized `delegate_*` tool routes to.
-pub fn delegation_target(tool: &dyn Tool) -> Option<&str> {
- tool.host_extension()
- .and_then(|any| any.downcast_ref::<DelegationTarget>())
- .map(|target| target.0.as_str())
-}
-
/// Reads the provider-assigned tool-call id from a canonical tool's run
/// context, when the run is driven through the tinyagents harness.
///
diff --git a/crates/openhuman-core/src/tools/toolpacks/ops.rs b/crates/openhuman-core/src/tools/toolpacks/ops.rs
index 0d31b5a588..6b91270ac8 100644
--- a/crates/openhuman-core/src/tools/toolpacks/ops.rs
+++ b/crates/openhuman-core/src/tools/toolpacks/ops.rs
@@ -143,43 +143,24 @@ pub fn is_withheld_from(agent_id: &str, tool: &str) -> bool {
/// guessed at tool names, and never handed off. Closing the raw tools leaves the
/// hand-off as the route, and the pack listing then names it (`route_sentence`).
///
-/// A pack closes only when all of this holds:
-/// * it is withheld from `agent_id` (a pack's owner keeps its own belt),
-/// * one of the pack's owners is the [`delegation_target`] of a hand-off this
-/// agent carries **unpacked** — a delegate no pack withholds, and therefore
-/// one it advertises by construction.
-///
-/// [`delegation_target`]: crate::tools::host_extensions::delegation_target
-///
-/// Inside a closed pack, a tool that is itself a hand-off (e.g. `create_skill`)
-/// stays reachable: it is a route, not a raw tool. A tool whose group an embedder
-/// advertised is on the wire, not withheld, and is left alone too.
-///
-/// **Deliberately not keyed on the visible set.** It used to be, and that made
-/// the rule true in tests and false in production. An agent with
-/// `ToolScope::Named` is built with `visible` = its named list, which cannot
-/// contain a *synthesised* delegate like `setup_skills`; those names arrive
-/// later, when `refresh_delegation_tools` inserts them. So at build time no
-/// hand-off looked reachable and nothing closed, while a harness agent — whose
-/// empty visible set is seeded from every tool, synthesised ones included —
-/// closed correctly. The live orchestrator kept its raw `skill_registry_*` /
-/// `mcp_registry_*` route the whole time (#6302). Pack membership is knowable
-/// the moment the tools exist, so the answer no longer depends on when the
-/// visible set is filled in.
+/// Close tools owned by specialists that this agent can reach through its
+/// `spawn_subagent` schema. The schema's agent-id enum is the same allowlist
+/// enforced during execution, so this keeps a single route to each capability.
pub fn closed_by_direct_handoff(agent_id: &str, tools: &[&dyn Tool]) -> Vec<&'static str> {
let reachable_owners: HashSet<&str> = tools
.iter()
- .filter(|tool| registry::pack_for_tool(tool.name()).is_none())
- .filter_map(|tool| crate::tools::host_extensions::delegation_target(*tool))
+ .filter(|tool| tool.name() == "spawn_subagent")
+ .filter_map(|tool| {
+ tool.parameters_schema()
+ .pointer("/properties/agent_id/enum")
+ .and_then(serde_json::Value::as_array)
+ })
+ .flatten()
+ .filter_map(serde_json::Value::as_str)
.collect();
if reachable_owners.is_empty() {
return Vec::new();
}
- let handoffs: HashSet<&str> = tools
- .iter()
- .filter(|tool| crate::tools::host_extensions::delegation_target(**tool).is_some())
- .map(|tool| tool.name())
- .collect();
let groups = super::groups::current();
registry::PACKS
.iter()
@@ -190,7 +171,6 @@ pub fn closed_by_direct_handoff(agent_id: &str, tools: &[&dyn Tool]) -> Vec<&'st
.any(|owner| reachable_owners.contains(owner))
})
.flat_map(|pack| pack.tools.iter().copied())
- .filter(|name| !handoffs.contains(name))
.filter(|name| groups.mode_for_tool(name) == super::groups::GroupMode::Withheld)
.collect()
}