Skip to content

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

refactor(subagent_host): delegate tool filtering to tinyagents

Steven Enamakel6577b18423e9
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 dc2f77e5ee..0b4bddf4e7 100644
--- a/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
@@ -8,6 +8,8 @@ use super::types::SubagentRunError;
use crate::agent::prompts::PromptContext;
use crate::agent_registry::host_definition::AgentDefinition;
use tinyagents_definition::{PromptSource, ToolScope};
+use tinyagents_orchestration::subagent::{restrict_tools_with_scope, SubagentRole};
+use tinyagents_runtime::ToolSnapshot;
use tinytools::Tool;
// ── Prompt dialect selection ────────────────────────────────────────────
@@ -89,10 +91,8 @@ pub(super) fn is_subagent_spawn_tool(name: &str) -> bool {
/// invoke. Index-based filtering avoids cloning `Box<dyn Tool>` (which
/// isn't Clone) and lets us reuse the parent's existing instances.
///
-/// Filters are applied in this order (shorter-circuit first):
-/// 1. `disallowed` — explicit deny list.
-/// 2. `skill_filter` — restrict to tools named `{skill}__*`.
-/// 3. `scope` — `Wildcard` (everything remaining) or `Named` allowlist.
+/// OpenHuman applies its explicit deny globs and the CCR recovery exception;
+/// TinyAgents applies the portable role, skill, scope, and delegation rules.
///
pub(super) fn filter_tool_indices(
parent_tools: &[Box<dyn Tool>],
@@ -100,36 +100,61 @@ pub(super) fn filter_tool_indices(
disallowed: &[String],
skill_filter: Option<&str>,
) -> Vec<usize> {
- let skill_prefix = skill_filter.map(|s| format!("{s}__"));
+ let mut first_specs = std::collections::HashMap::new();
+ let mut specs = Vec::new();
+ let mut disallowed_recovery = std::collections::HashSet::new();
+ let mut delegation_tools = vec!["spawn_worker_thread".to_string()];
+ for tool in parent_tools {
+ let name = tool.name();
+ if is_subagent_spawn_tool(name) || name == "spawn_worker_thread" {
+ delegation_tools.push(name.to_string());
+ }
+ if crate::tools::rules::glob_list_matches(disallowed, name) {
+ if crate::inference::tokenjuice::is_recovery_tool(name) {
+ disallowed_recovery.insert(name.to_string());
+ }
+ continue;
+ }
+ if first_specs.insert(name.to_string(), ()).is_none() {
+ specs.push(tool.spec());
+ }
+ }
+ let snapshot = match ToolSnapshot::new(specs) {
+ Ok(snapshot) => snapshot,
+ Err(_) => return Vec::new(),
+ };
+ let restricted = restrict_tools_with_scope(
+ &snapshot,
+ SubagentRole::Leaf,
+ None,
+ &delegation_tools,
+ Some(scope),
+ skill_filter,
+ );
+ let mut retained: std::collections::HashSet<String> = restricted
+ .specs()
+ .iter()
+ .map(|spec| spec.name.clone())
+ .collect();
+
+ // CCR recovery remains a host exception: it bypasses scope and skill
+ // filters, while the deny glob, leaf role, and explicit empty scope win.
+ if !matches!(scope, ToolScope::Named(allowed) if allowed.is_empty()) {
+ for tool in parent_tools {
+ let name = tool.name();
+ if crate::inference::tokenjuice::is_recovery_tool(name)
+ && !disallowed_recovery.contains(name)
+ && !delegation_tools.iter().any(|tool| tool == name)
+ {
+ retained.insert(name.to_string());
+ }
+ }
+ }
parent_tools
.iter()
.enumerate()
- .filter(|(_, tool)| {
- let name = tool.name();
- if crate::tools::rules::glob_list_matches(disallowed, name) {
- return false;
- }
- // The CCR recovery tool is advertised to any agent that has a tool
- // surface — compaction applies to its tool output, so the retrieve
- // footer must be actionable regardless of scope/skill filters (an
- // explicit `disallow` above still wins). A deliberately tool-less
- // agent (`Named([])`, e.g. the payload summarizer) runs no tools,
- // produces no compacted output, and so stays tool-less.
- if crate::inference::tokenjuice::is_recovery_tool(name) {
- return !matches!(scope, ToolScope::Named(allowed) if allowed.is_empty());
- }
- if let Some(prefix) = skill_prefix.as_deref() {
- if !name.starts_with(prefix) {
- return false;
- }
- }
- match scope {
- ToolScope::Wildcard => true,
- ToolScope::Named(allowed) => allowed.iter().any(|n| n == name),
- }
- })
- .map(|(i, _)| i)
+ .filter_map(|(index, tool)| retained.contains(tool.name()).then_some(index))
.collect()
}
@@ -144,7 +169,39 @@ pub(super) fn retain_parent_visible_tool_indices(
if parent_visible.is_empty() {
return;
}
- indices.retain(|&index| parent_visible.contains(parent_tools[index].name()));
+ let mut seen = std::collections::HashSet::new();
+ let specs = parent_tools
+ .iter()
+ .filter(|tool| seen.insert(tool.name().to_string()))
+ .map(|tool| tool.spec())
+ .collect();
+ let Ok(snapshot) = ToolSnapshot::new(specs) else {
+ indices.clear();
+ return;
+ };
+ let mut seen_ceiling = std::collections::HashSet::new();
+ let ceiling_specs = parent_tools
+ .iter()
+ .filter(|tool| {
+ parent_visible.contains(tool.name()) && seen_ceiling.insert(tool.name().to_string())
+ })
+ .map(|tool| tool.spec())
+ .collect();
+ let Ok(ceiling) = ToolSnapshot::new(ceiling_specs) else {
+ indices.clear();
+ return;
+ };
+ let narrowed = restrict_tools_with_scope(
+ &snapshot,
+ SubagentRole::Leaf,
+ Some(&ceiling),
+ &[],
+ None,
+ None,
+ );
+ let names: std::collections::HashSet<_> =
+ narrowed.specs().iter().map(|spec| spec.name.as_str()).collect();
+ indices.retain(|&index| names.contains(parent_tools[index].name()));
}
#[cfg(test)]