Skip to content

The agent that grows with you

Report session-scoped unavailability instead of "call it directly"

chunkypiesrus55d558b45d43
diff --git a/model_tools.py b/model_tools.py
index 8566c62842c..2d46e79d1d9 100644
--- a/model_tools.py
+++ b/model_tools.py
@@ -727,7 +727,9 @@ def _dispatch_bridge_tool(function_name: str, function_args: dict[str, Any],
return ts.dispatch_tool_search(args, current_tool_defs=current_defs, call=call), None
if function_name == ts.TOOL_DESCRIBE_NAME:
return ts.dispatch_tool_describe(args, current_tool_defs=current_defs, call=call), None
- underlying_name, underlying_args, err = ts.resolve_underlying_call(args)
+ # Session-scoped names for the error path below: a core tool stripped from this
+ # session (clarify in a subagent) must not be told to "call it directly".
+ underlying_name, underlying_args, err = ts.resolve_underlying_call(args, ts.session_tool_names(current_defs))
if err or not underlying_name:
return tool_error(err or "tool_call could not be resolved"), None
if underlying_name == ts.CONNECTOR_BATCH_SENTINEL:
diff --git a/tests/tools/test_not_deferrable_session_scope.py b/tests/tools/test_not_deferrable_session_scope.py
new file mode 100644
index 00000000000..41d6e2c30f2
--- /dev/null
+++ b/tests/tools/test_not_deferrable_session_scope.py
@@ -0,0 +1,44 @@
+"""A core tool stripped from this session's toolset must not be told to 'call it directly'.
+
+`_HERMES_CORE_TOOLS` is static, so `not_deferrable_error` matched any core tool by name —
+including `clarify` inside a subagent, where it is toolset-blocked and no direct call can
+ever succeed. The static list cannot distinguish "callable, wrong door" from "not here at all",
+so the model was sent into a retry loop on a confident false instruction.
+
+Regression test for that: the fix is only proven if it goes RED when reverted.
+"""
+
+from tools.tool_search_validation import not_deferrable_error
+
+
+class TestNotDeferrableSessionScope:
+ def test_core_tool_absent_from_session_reports_unavailable(self):
+ """The subagent case: clarify is core but blocked here."""
+ msg = not_deferrable_error("clarify", session_tool_names={"read_file", "terminal"})
+ assert "not available in this execution context" in msg
+ assert "directly-listed" not in msg, (
+ "must not tell the model to call a tool this session cannot reach"
+ )
+ assert "retry" in msg.lower(), "should tell the model not to retry"
+
+ def test_core_tool_present_in_session_keeps_door_guidance(self):
+ """Top-level case unchanged: clarify IS callable directly here."""
+ msg = not_deferrable_error("clarify", session_tool_names={"clarify", "read_file"})
+ assert "directly-listed" in msg
+ assert "Call it directly" in msg
+
+ def test_no_session_context_preserves_original_behaviour(self):
+ """Callers with no session context (display layer, recorder) are unaffected."""
+ msg = not_deferrable_error("clarify")
+ assert "directly-listed" in msg
+
+ def test_unknown_tool_still_gets_discovery_hint(self):
+ """The unrelated branch must not regress: bare-suffix MCP names."""
+ msg = not_deferrable_error("definitely_not_a_real_tool", session_tool_names={"clarify"})
+ assert "not a known tool name" in msg
+
+ def test_session_names_accepts_any_iterable(self):
+ """Generators must work, not just sets — the dispatcher passes a frozenset but
+ the annotation promises Iterable and a generator is the obvious caller mistake."""
+ msg = not_deferrable_error("clarify", session_tool_names=(n for n in ["read_file"]))
+ assert "not available in this execution context" in msg
diff --git a/tools/tool_search.py b/tools/tool_search.py
index 76fe4c6a23b..3276c655004 100644
--- a/tools/tool_search.py
+++ b/tools/tool_search.py
@@ -166,6 +166,13 @@ def _tool_def_names(tool_defs: Iterable[dict[str, Any]]) -> Iterable[str]:
return (_fn(td).get("name", "") for td in tool_defs)
+def session_tool_names(tool_defs: Iterable[dict[str, Any]]) -> frozenset[str]:
+ """Every tool name in the session's ``tool_defs`` — what this session can actually
+ reach. Public wrapper over the private name extractor so callers outside this module
+ (the ``model_tools`` dispatcher) can scope error messages to the real session."""
+ return frozenset(n for n in _tool_def_names(tool_defs) if n)
+
+
def classify_tools(tool_defs: list[dict[str, Any]], defer_tools: Optional[frozenset] = None,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
"""Split a tool-defs list into (visible, deferrable); bridge tools are dropped (re-added
@@ -553,7 +560,8 @@ def out_of_scope_reason(name: str) -> Optional[str]:
return None
-def resolve_underlying_call(args: dict[str, Any]) -> tuple[Optional[str], dict[str, Any], Optional[str]]:
+def resolve_underlying_call(args: dict[str, Any], session_tool_names: Optional[Iterable[str]] = None,
+ ) -> tuple[Optional[str], dict[str, Any], Optional[str]]:
"""Parse a ``tool_call`` invocation into (underlying_name, args, error_msg).
Used by:
@@ -567,6 +575,10 @@ def resolve_underlying_call(args: dict[str, Any]) -> tuple[Optional[str], dict[s
sentinel is what planners/display layers see. A single local entry keeps
the historical single-tool contract unchanged.
+ ``session_tool_names`` is the session's own tool list, when the caller has it. Passed
+ through so a core tool this session cannot reach reports *unavailable* rather than
+ "call it directly" — the static core list cannot tell the two apart.
+
On parse error, returns ``(None, {}, error_message)``.
"""
entries, err = normalize_tool_call_entries(args)
@@ -581,7 +593,7 @@ def resolve_underlying_call(args: dict[str, Any]) -> tuple[Optional[str], dict[s
name = entries[0]["name"]
raw_args = entries[0]["arguments"]
if not is_deferrable_tool_name(name, load_config_readonly().effective_defer_tools):
- return None, {}, not_deferrable_error(name)
+ return None, {}, not_deferrable_error(name, session_tool_names)
return name, raw_args, None
diff --git a/tools/tool_search_validation.py b/tools/tool_search_validation.py
index c2e45e22234..82f56a79966 100644
--- a/tools/tool_search_validation.py
+++ b/tools/tool_search_validation.py
@@ -6,7 +6,7 @@ import copy
import json
import logging
import re
-from typing import Any, Dict, List, Optional, Tuple
+from typing import Any, Dict, Iterable, List, Optional, Tuple
from tools.registry import tool_error
from tools.tool_search_catalog import BRIDGE_TOOL_NAMES, _registry_entry
@@ -210,14 +210,25 @@ def local_batch_error(entries: list[dict[str, Any]]) -> str:
)
-def not_deferrable_error(name: str) -> str:
+def not_deferrable_error(name: str, session_tool_names: Optional[Iterable[str]] = None) -> str:
"""Rejection for a ``tool_call`` naming something that is not a deferred tool.
Two different mistakes reach here and need opposite corrections: a directly-listed
tool (call it without the bridge) vs. an unknown name — typically a deferred MCP tool
cited by its bare suffix instead of the full ``mcp__<server>__{tool}`` name. Telling
- the second group 'call it directly' is the opposite of what they must do."""
+ the second group 'call it directly' is the opposite of what they must do.
+
+ ``_HERMES_CORE_TOOLS`` is a *static* list, so a core tool stripped from this session's
+ toolset (clarify in a subagent, kanban worker) otherwise gets told "call it directly"
+ — an instruction that can never resolve, burning a turn on a confident wrong answer.
+ When the caller passes the session-scoped names, absence is reported as unavailability
+ instead. Stays None for callers with no session context; deliberately never consults the
+ global blocklist, because a top-level session legitimately has these tools.
+ """
from tools.tool_search import _core_tool_names # late: tool_search imports this module
if name in _core_tool_names() or _registry_entry(name) is not None:
+ if session_tool_names is not None and name not in set(session_tool_names):
+ return (f"'{name}' is not available in this execution context. "
+ "Don't retry it — state the assumption and proceed with the work.")
return (f"'{name}' is a directly-listed tool, not a deferred one. "
"Call it directly instead of via tool_call.")
suffix = f"__{name}"