Skip to content

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

feat(prompts): extract deferred-tool bridge into shared helper

Steven Enamakel06786d956bad
diff --git a/crates/openhuman-core/src/agent/prompts/types.rs b/crates/openhuman-core/src/agent/prompts/types.rs
index 63f29d36a9..b0a854b1e0 100644
--- a/crates/openhuman-core/src/agent/prompts/types.rs
+++ b/crates/openhuman-core/src/agent/prompts/types.rs
@@ -291,6 +291,47 @@ impl<'a> PromptTool<'a> {
}
}
+/// Swap a prompt catalogue's `Deferred` entries for the discovery bridge.
+///
+/// On a TEXT dialect (P-Format / code) the catalogue this prompt renders IS
+/// the model's callable surface: the harness folds it into the system prompt
+/// and clears `request.tools`. Two things follow, and both were wrong before
+/// this helper existed:
+///
+/// * **Deferred tools must leave the catalogue.** The filter each prompt site
+/// used is the policy's allow-set, which deliberately admits deferred names
+/// so a found tool stays *callable* (`reachable_names` in the session
+/// builder). Filtering the prompt by it rendered every deferred schema into
+/// the prompt — measured live at 107 connected Composio actions for 55 KB of
+/// a 71 KB prompt, the exact cost deferral exists to avoid — and told the
+/// model to search for an action whose signature it could already read.
+///
+/// * **The bridge must take their place.** The harness mints `tool_search` /
+/// `tool_call` onto `request.tools`, which a text dialect drops, and with
+/// `host_renders_tool_catalogue` it appends nothing itself. Without these
+/// entries the model reads "invoke a match with `tool_call`" in a search
+/// result and has no signature for that name; observed live as a turn that
+/// narrates the call it is about to make and then stops.
+///
+/// A native-tool-calling provider is unaffected: it reads `request.tools`,
+/// where the harness already puts exactly this pair.
+pub fn swap_deferred_for_discovery_bridge<'a>(
+ prompt_tools: &mut Vec<PromptTool<'a>>,
+ visible_tool_names: &mut std::collections::HashSet<String>,
+ deferred_tool_names: &std::collections::HashSet<String>,
+) {
+ if deferred_tool_names.is_empty() {
+ return;
+ }
+ visible_tool_names.retain(|name| !deferred_tool_names.contains(name));
+ for bridge in
+ crate::agent::tinyagents::discovery::bridge_prompt_tools(deferred_tool_names.len())
+ {
+ visible_tool_names.insert(bridge.name.to_string());
+ prompt_tools.push(bridge);
+ }
+}
+
/// How the tool catalogue should render each tool entry. Driven by the
/// dispatcher choice on the agent — JSON-schema rendering is the
/// historic format; P-Format is the new default text protocol.
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index 7f49f060a5..76e8a02e01 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -357,8 +357,13 @@ impl OpenHumanTurnPrelude {
.chain(surface.synthesized_tools.iter())
.map(|tool| tool.as_ref())
.collect::<Vec<_>>();
- let prompt_tools = PromptTool::from_tool_refs(tool_refs.iter().copied());
- let visible_tool_names = surface.tool_policy_session.visible_tool_names_for_prompt();
+ let mut prompt_tools = PromptTool::from_tool_refs(tool_refs.iter().copied());
+ let mut visible_tool_names = surface.tool_policy_session.visible_tool_names_for_prompt();
+ crate::agent::prompts::swap_deferred_for_discovery_bridge(
+ &mut prompt_tools,
+ &mut visible_tool_names,
+ &surface.deferred_tool_names,
+ );
let agents_md = if self.config.agents_md_enabled {
crate::agent::prompts::load_agents_md_layers(&self.workspace_dir, &self.action_dir)
} else {
diff --git a/crates/openhuman-core/src/agent/session_host/turn/context.rs b/crates/openhuman-core/src/agent/session_host/turn/context.rs
index 3d70b30fe3..ccdf326be7 100644
--- a/crates/openhuman-core/src/agent/session_host/turn/context.rs
+++ b/crates/openhuman-core/src/agent/session_host/turn/context.rs
@@ -218,34 +218,11 @@ impl OpenHumanSessionHost {
let mut prompt_tools = PromptTool::from_tool_refs(all_tools.iter().copied());
let mut prompt_visible_tool_names =
self.tool_policy_session.visible_tool_names_for_prompt();
- // A `Deferred` tool is off the advertised surface, and on a TEXT
- // dialect the catalogue this prompt renders IS that surface — the
- // harness clears `request.tools` and the model calls what it reads
- // here. The policy's allow-set is the wrong filter for it: the host
- // deliberately admits the deferred names there so they stay
- // *callable* (`reachable_names` in the session builder), so filtering
- // by it rendered every deferred schema into the prompt. That cost the
- // bytes deferral exists to save (measured: 107 connected Composio
- // actions rendered 55 KB of a 71 KB prompt) and, worse, contradicted
- // the prompt: the model was told to `tool_search` for an action whose
- // signature was already in its catalogue.
- //
- // The bridge takes their place, when there is one. The harness mints
- // `tool_search` / `tool_call` schemas onto `request.tools` for a run
- // with a deferred catalogue, but a text dialect drops that set, and
- // with `host_renders_tool_catalogue` the harness appends nothing of
- // its own — so without these two entries the model is told to invoke
- // matches with a `tool_call` it can never see a signature for, and
- // answers with intent instead of a call.
- if !self.deferred_tool_names.is_empty() {
- prompt_visible_tool_names.retain(|name| !self.deferred_tool_names.contains(name));
- for bridge in crate::agent::tinyagents::discovery::bridge_prompt_tools(
- self.deferred_tool_names.len(),
- ) {
- prompt_visible_tool_names.insert(bridge.name.to_string());
- prompt_tools.push(bridge);
- }
- }
+ crate::agent::prompts::swap_deferred_for_discovery_bridge(
+ &mut prompt_tools,
+ &mut prompt_visible_tool_names,
+ &self.deferred_tool_names,
+ );
// Load AGENTS.md instruction layers once per system-prompt build (never
// re-read per turn — the caller builds the prompt once at session start
// and reuses the bytes, preserving the frozen-prefix / KV-cache