The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): tool_call scope gate names the missing GUI surface for preview tools
finn763c8d61bbc6031
diff --git a/agent/tool_executor.py b/agent/tool_executor.pyindex 40905959b78..50a9e625999 100644--- a/agent/tool_executor.py+++ b/agent/tool_executor.py@@ -416,8 +416,11 @@ def _unwrap_tool_search_call(# in the batch dispatcher, not against a synthetic registry name.return function_name, function_args, Noneif underlying not in _tool_search_scoped_names(agent):+ # Session-gated GUI tools name their missing surface (#120413);+ # anything else keeps the generic block.return function_name, function_args, (- f"'{underlying}' is not available in this session. Use tool_search to find tools you can call."+ _ts.out_of_scope_reason(underlying)+ or f"'{underlying}' is not available in this session. Use tool_search to find tools you can call.")# Validate before unwrapping: the generic bridge hides the concrete# parameter schema from provider-native tool-call validation.diff --git a/model_tools.py b/model_tools.pyindex 55f8de5bfdd..f6b37b5a98d 100644--- a/model_tools.py+++ b/model_tools.py@@ -739,8 +739,11 @@ def _dispatch_bridge_tool(function_name: str, function_args: Dict[str, Any],return None, (underlying_name, underlying_args)# Defense in depth: resolve_underlying_call only checks the global# registry; also require membership in the session-scoped catalog.+ # Session-gated GUI tools fail fast with their real reason (#120413):+ # tool_search can never surface them in this session.if underlying_name not in ts.scoped_deferrable_names(current_defs):- return tool_error(f"'{underlying_name}' is not available in this session. "+ return tool_error(ts.out_of_scope_reason(underlying_name)+ or f"'{underlying_name}' is not available in this session. ""Use tool_search to find tools you can call."), None# Validate against the deferred tool's concrete schema — the generic# ``arguments: object`` bridge schema can't enforce it.diff --git a/tests/tools/test_preview_scope_gate.py b/tests/tools/test_preview_scope_gate.pynew file mode 100644index 00000000000..378dce603f0--- /dev/null+++ b/tests/tools/test_preview_scope_gate.py@@ -0,0 +1,68 @@+"""tool_call scope gate for session-gated GUI tools (#120413).++drive_preview / desktop_preview live in the ``desktop_ui`` toolset: a session+without that surface (TUI, CLI, webhook, ...) can never reach them, not even+via tool_search. The bridge must fail fast with the real reason instead of+the generic "not available ... Use tool_search", which sends the model on a+round-trip tool_search can never satisfy. Desktop-scoped sessions keep+working (the call reaches the tool).+"""++import json++import model_tools+from agent.tool_executor import _unwrap_tool_search_call++TUI_SCOPE = ["coding", "project"]+DESKTOP_SCOPE = ["coding", "desktop_ui", "project"]+++class _Agent:+ def __init__(self, enabled):+ self.enabled_toolsets = enabled+ self.disabled_toolsets = None+++def _bridge(name, scope):+ return model_tools.handle_function_call(+ "tool_call", {"name": name, "arguments": {"action": "elements"}},+ enabled_toolsets=list(scope), disabled_toolsets=None)+++def test_bridge_tui_scope_names_the_gui_surface():+ err = json.loads(_bridge("drive_preview", TUI_SCOPE))["error"]+ assert "desktop" in err.lower()+ assert "use tool_search to find" not in err.lower()+++def test_bridge_tui_scope_desktop_preview_names_the_gui_surface():+ err = json.loads(_bridge("desktop_preview", TUI_SCOPE))["error"]+ assert "desktop" in err.lower()+ assert "use tool_search to find" not in err.lower()+++def test_unwrap_tui_scope_names_the_gui_surface():+ _, _, block = _unwrap_tool_search_call(+ _Agent(TUI_SCOPE), "tool_call",+ {"name": "drive_preview", "arguments": {"action": "elements"}})+ assert block is not None and "desktop" in block.lower()+ assert "use tool_search to find" not in block.lower()+++def test_bridge_desktop_scope_still_reaches_the_tool():+ # Headless: no GUI callback, so the tool itself fail-fasts with its+ # desktop-only reason — the point is the scope gate let it through.+ err = json.loads(_bridge("drive_preview", DESKTOP_SCOPE))["error"]+ assert "desktop" in err.lower()+++def test_unwrap_desktop_scope_not_blocked():+ name, _, block = _unwrap_tool_search_call(+ _Agent(DESKTOP_SCOPE), "tool_call",+ {"name": "drive_preview", "arguments": {"action": "elements"}})+ assert block is None and name == "drive_preview"+++def test_non_surface_tool_keeps_generic_message():+ err = json.loads(_bridge("computer_use", TUI_SCOPE))["error"]+ assert "not available in this session" in errdiff --git a/tools/tool_search.py b/tools/tool_search.pyindex 3772ff090d4..9f7998dcfab 100644--- a/tools/tool_search.py+++ b/tools/tool_search.py@@ -540,6 +540,21 @@ def scoped_deferrable_names(tool_defs: List[Dict[str, Any]]) -> frozenset[str]:if n and is_deferrable_tool_name(n, defer_tools))+def out_of_scope_reason(name: str) -> Optional[str]:+ """Fail-fast reason for a registered session-gated GUI tool outside this session's+ scope (#120413). The generic scope block ("not available ... Use tool_search") sends+ the model on a round-trip tool_search can never satisfy — the tool is not in this+ session's catalog at all. Name the missing surface instead. None for anything else+ (the generic message still applies). Fail-open: never raises."""+ try:+ if _registry_toolset(name) in _DIRECT_SURFACE_TOOLSETS:+ return (f"'{name}' needs a desktop-app session with a GUI surface (preview/terminal panes). "+ "This session has none: use it only from the Hermes desktop app, not via tool_search.")+ except Exception:+ pass+ return None++def resolve_underlying_call(args: Dict[str, Any]) -> Tuple[Optional[str], Dict[str, Any], Optional[str]]:"""Parse a ``tool_call`` invocation into (underlying_name, args, error_msg).@@ -579,5 +594,5 @@ __all__ = ["build_catalog_listing_with_form", "listing_token_budget", "search_catalog","bridge_tool_schemas", "assemble_tool_defs", "is_bridge_tool", "dispatch_tool_search","dispatch_tool_describe", "resolve_underlying_call", "scoped_deferrable_names",- "validate_deferred_call_args", "normalize_tool_call_entries",+ "out_of_scope_reason", "validate_deferred_call_args", "normalize_tool_call_entries","CONNECTOR_BATCH_SENTINEL", "is_connector_name"]