Skip to content

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

fix(agent): handle missing tool preparation gracefully

Steven Enamakel73df46159347
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 a0ccdecbb5..ab7fc55af3 100644
--- a/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/tool_prep.rs
@@ -12,186 +12,22 @@ use crate::agent::harness::definition::{PromptSource, ToolScope};
use crate::agent::prompts::PromptContext;
use tinytools::Tool;
-// ── Heavy-schema toolkit accounting ─────────────────────────────────────
+// ── Prompt dialect selection ────────────────────────────────────────────
-/// Tight top-K ceiling for toolkits whose per-action JSON schemas are
-/// dense enough to blow through either Fireworks' 65 535-rule grammar
-/// cap (native mode) or the 196 607-token context cap (text mode) even
-/// before any tool results land in history. Determined empirically from
-/// the fixture dumps under `tests/fixtures/composio_*.json` and real
-/// staging failures — see the trace where Gmail at top-K=25 produced
-/// a 276k-token iter-1 prompt.
-const HEAVY_SCHEMA_TOOLKITS: &[&str] = &[
- "gmail",
- "notion",
- "github",
- "salesforce",
- "hubspot",
- "googledrive",
- "googlesheets",
- "googledocs",
- "microsoftteams",
-];
-
-const TOOL_FILTER_TOP_K_DEFAULT: usize = 25;
-const TOOL_FILTER_TOP_K_HEAVY: usize = 12;
-
-/// Actions a toolkit must keep on the sub-agent's surface even when the
-/// fuzzy ranker does not pick them.
-///
-/// The ranker (`tinyagents_harness::tool::select`) derives an intent verb
-/// from the prompt and then **drops** every tool whose own name-verb
-/// conflicts with it. `FETCH`/`GET` are the `Read` verb and `find`/`search`
-/// are `List`, so a plain "find the emails about X" prompt removes
-/// `GMAIL_FETCH_EMAILS` and both `GMAIL_FETCH_MESSAGE_BY_*` before scoring
-/// even starts. A flat verb-alignment bonus then lets zero-overlap tools
-/// fill the budget, so the surviving surface can be twelve `GMAIL_LIST_*`
-/// actions — ids and settings, nothing that returns a message body. The
-/// sub-agent burns its iterations and answers without the email (#6033).
-///
-/// These names are **reserved inside** the top-K budget, never appended
-/// past it: [`TOOL_FILTER_TOP_K_HEAVY`] exists because Gmail at top-K=25
-/// produced a 276k-token first-iteration prompt, so an essential displaces
-/// the lowest-ranked hit rather than growing the surface.
-///
-/// **Every entry must be read-only.** These actions are forced onto a
-/// delegation whatever it asked for, so a write action here would hand a
-/// "read my email" task an unrequested side-effecting capability — and a
-/// `ComposioActionTool` does not override `external_effect`, so the
-/// approval middleware (which gates on `external_effect_with_args`) would
-/// not stop it. Prompt-injected instructions inside a fetched email could
-/// then act on the mailbox with no human in the loop. Send and the rest of
-/// the write surface stay where they were: reachable when the prompt
-/// actually asks for them, via the ranker.
-///
-/// Only Gmail is seeded, deliberately. A wrong entry spends a slot on
-/// every prompt for that toolkit, so add one only with a fixture-backed
-/// repro — the ranker's general behaviour is the upstream fix.
-const TOOLKIT_ESSENTIAL_ACTIONS: &[(&str, &[&str])] = &[(
- "gmail",
- &[
- // The only action that returns message *content* for a search.
- "GMAIL_FETCH_EMAILS",
- // The follow-up once `GMAIL_LIST_MESSAGES` has handed back bare ids.
- "GMAIL_FETCH_MESSAGE_BY_MESSAGE_ID",
- ],
-)];
-
-/// Pick a top-K budget for the fuzzy filter based on how dense the
-/// toolkit's action schemas tend to be. Match is case-insensitive so
-/// we don't care whether the caller passed `"Gmail"` or `"gmail"`.
-pub(super) fn top_k_for_toolkit(toolkit: &str) -> usize {
- if HEAVY_SCHEMA_TOOLKITS
- .iter()
- .any(|t| t.eq_ignore_ascii_case(toolkit))
- {
- TOOL_FILTER_TOP_K_HEAVY
- } else {
- TOOL_FILTER_TOP_K_DEFAULT
- }
-}
-
-/// Actions [`TOOLKIT_ESSENTIAL_ACTIONS`] reserves for `toolkit`, or an
-/// empty slice when the toolkit has no entry. Case-insensitive, matching
-/// [`top_k_for_toolkit`].
-pub(super) fn essential_actions_for_toolkit(toolkit: &str) -> &'static [&'static str] {
- TOOLKIT_ESSENTIAL_ACTIONS
- .iter()
- .find(|(name, _)| name.eq_ignore_ascii_case(toolkit))
- .map(|(_, actions)| *actions)
- .unwrap_or(&[])
-}
-
-/// Pick the action indices the sub-agent will actually see: the toolkit's
-/// essentials first, then the ranker's hits in rank order, deduplicated and
-/// truncated to `top_k`.
-///
-/// An essential that the connected toolkit does not actually expose is
-/// skipped rather than guessed at, so a stale table entry costs nothing and
-/// can never produce an out-of-range index. A toolkit with no entry returns
-/// the ranker's hits unchanged (capped at `top_k`), which is what every
-/// call site did before the essentials existed.
-pub(super) fn select_actions_with_essentials(
- toolkit: &str,
- actions: &[crate::agent::prompts::ConnectedIntegrationTool],
- filter_hits: &[usize],
- top_k: usize,
-) -> Vec<usize> {
- let essentials = essential_actions_for_toolkit(toolkit);
- if essentials.is_empty() || top_k == 0 {
- return filter_hits.iter().take(top_k).copied().collect();
- }
-
- let mut selected: Vec<usize> = Vec::with_capacity(top_k.min(actions.len()));
- let mut reserved: Vec<&str> = Vec::new();
- for essential in essentials {
- if selected.len() >= top_k {
- break;
- }
- let found = actions
- .iter()
- .position(|a| a.name.eq_ignore_ascii_case(essential));
- if let Some(idx) = found {
- if !selected.contains(&idx) {
- selected.push(idx);
- reserved.push(essential);
- }
- }
- }
-
- let ranked_before = filter_hits.len().min(top_k);
- for &idx in filter_hits {
- if selected.len() >= top_k {
- break;
- }
- if !selected.contains(&idx) {
- selected.push(idx);
- }
- }
-
- // A reserved action the ranker had already chosen costs nothing; the
- // rest displace the lowest-ranked hits that no longer fit.
- let ranked_kept = filter_hits.iter().filter(|i| selected.contains(i)).count();
- tracing::debug!(
- toolkit = %toolkit,
- reserved = %reserved.join(","),
- kept = selected.len(),
- top_k = top_k,
- dropped_ranked = ranked_before.saturating_sub(ranked_kept),
- "[subagent_host:tools] reserved essential actions inside the top-K budget"
- );
- selected
-}
-
-// ── Text-mode dialect selection ─────────────────────────────────────────
-
-/// Select the TinyTools-owned tool-use protocol for text-mode sub-agents.
+/// The tool-call format a sub-agent's prompt is rendered for, and the
+/// dispatcher protocol block that goes with it. Both follow the parent.
///
/// Tool syntax and tool catalogue rendering live in TinyTools. Keeping only
/// dialect selection here prevents OpenHuman prompt text from drifting from
/// the parser that consumes the response.
-/// The tool-call format a sub-agent's prompt is rendered for, and the
-/// dispatcher protocol block that goes with it.
-///
-/// Normally both follow the parent. A text-mode child (the toolkit-scoped
-/// `integrations_agent`) is sent no native `tools`, so it must render its own
-/// catalogue as text whatever the parent uses. Text-mode children use the
-/// TinyTools JSON dialect, which can faithfully carry nested tool arguments.
pub(crate) fn subagent_prompt_protocol(
parent_format: crate::agent::prompts::ToolCallFormat,
- text_mode: bool,
tools: &[tinytools::ToolSpec],
) -> (crate::agent::prompts::ToolCallFormat, String) {
use crate::agent::prompts::ToolCallFormat;
use tinytools_agent::dialect::{
CodeDialect, CodeStyle, NativeDialect, PFormatDialect, ToolDialect,
};
- if text_mode {
- return (
- ToolCallFormat::Json,
- crate::agent::prompts::render_helpers::harness_json_tool_prompt(tools),
- );
- }
let instructions = match parent_format {
ToolCallFormat::PFormat => {
let registry = tinytools_agent::build_registry(
@@ -406,41 +242,3 @@ pub(super) fn load_prompt_source(
}
}
}
-
-/// Remove every sub-agent spawn/delegate tool from a child's **dynamic**
-/// (per-spawn) tool list, in place.
-///
-/// The archetype's static surface is stripped via `allowed_indices` in
-/// [`super::ops::run_typed_mode`], but dynamic tools — per-action Composio
-/// toolkit tools and `extract_from_result` — are appended afterwards and never
-/// pass through that filter. That makes this the only route by which a
-/// spawn/delegate name can reach a child's allowlist *admitted*, so the #4452
-/// invariant has to be re-asserted here (issue #6157).
-///
-/// Strip once, at the point the list is finished, rather than at each use: the
-/// same `Vec` feeds the provider-visible specs, the resolved allowlist, the
-/// prompt catalogue, harness registration and the custom-graph request. Filter
-/// one and the others disagree — a tool advertised but not admitted, or the
-/// reverse. The registration-time backstop in
-/// `tinyagents::is_subagent_spawn_or_delegate_tool` is not sufficient on its
-/// own here: it cannot resolve `delegate_name` overrides, so it matches
-/// strictly less than [`is_subagent_spawn_tool`] does.
-pub(super) fn strip_spawn_tools_from_dynamic(
- dynamic_tools: &mut Vec<Box<dyn Tool>>,
- agent_id: &str,
-) {
- dynamic_tools.retain(|tool| {
- let name = tool.name();
- // `spawn_worker_thread` is matched separately for the same reason the
- // caller-side strip does it: it spawns a run without being a delegate.
- let is_spawn = is_subagent_spawn_tool(name) || name == "spawn_worker_thread";
- if is_spawn {
- tracing::warn!(
- agent_id = %agent_id,
- tool = name,
- "[subagent_host] dropped a spawn/delegate tool from a sub-agent's dynamic tools"
- );
- }
- !is_spawn
- });
-}