Files
hermes-agent/tests/agent/test_repair_tool_call_name.py
T
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

128 lines
3.7 KiB
Python

"""Tests for AIAgent._repair_tool_call — tool-name normalization.
Regression guard for #14784: Claude-style models sometimes emit
class-like tool-call names (``TodoTool_tool``, ``Patch_tool``,
``BrowserClick_tool``, ``PatchTool``). Before the fix they returned
"Unknown tool" even though the target tool was registered under a
snake_case name. The repair routine now normalizes CamelCase,
strips trailing ``_tool`` / ``-tool`` / ``tool`` suffixes (up to
twice to handle double-tacked suffixes like ``TodoTool_tool``), and
falls back to fuzzy match.
"""
from __future__ import annotations
from types import SimpleNamespace
import pytest
VALID = {
"todo",
"patch",
"browser_click",
"browser_navigate",
"web_search",
"read_file",
"write_file",
"terminal",
"execute_code",
"session_search",
}
@pytest.fixture
def repair():
"""Return a bound _repair_tool_call built on a minimal shell agent.
We avoid constructing a real AIAgent (which pulls in credential
resolution, session DB, etc.) because the repair routine only
reads self.valid_tool_names. A SimpleNamespace stub is enough to
bind the unbound function.
"""
from run_agent import AIAgent
stub = SimpleNamespace(valid_tool_names=VALID)
return AIAgent._repair_tool_call.__get__(stub, AIAgent)
class TestExistingBehaviorStillWorks:
"""Pre-existing repairs must keep working (no regressions)."""
def test_lowercase_already_matches(self, repair):
assert repair("browser_click") == "browser_click"
class TestClassLikeEmissions:
"""Regression coverage for #14784 — CamelCase + _tool suffix variants."""
def test_camel_case_no_suffix(self, repair):
assert repair("BrowserClick") == "browser_click"
class TestEdgeCases:
"""Edge inputs that must not crash or produce surprising results."""
def test_empty_string(self, repair):
assert repair("") is None
class TestVolcEngineXmlPollution:
"""Regression coverage for #33007 — VolcEngine ``api/plan`` endpoint
leaks raw XML attribute fragments into ``tool_use.name``.
Observed in production with the ``anthropic_messages`` API mode:
terminal" parameter="command" string="true
execute_code" parameter="code" string="true
session_search" parameter="session_id" string="true
The fix trims at the first ``"``/``'``/``<``/``>`` so the rest of
the repair pipeline can resolve the cleaned name to a real tool.
"""
def test_terminal_with_xml_attribute_pollution(self, repair):
# Exact pattern from the bug report (terminal call).
polluted = 'terminal" parameter="command" string="true'
assert repair(polluted) == "terminal"
def test_tool_name_with_trailing_quote_only(self, repair):
# Minimal leak — just a stray trailing quote, no full attribute.
assert repair('terminal"') == "terminal"
def test_clean_tool_name_unaffected_by_sanitizer(self, repair):
# Pure passthrough — no XML/quote chars, no change.
assert repair("execute_code") == "execute_code"
assert repair("session_search") == "session_search"
def test_space_separated_name_still_normalizes(self, repair):
# Critical: the XML strip must NOT consume whitespace, or the
# legitimate ``"write file" -> write_file`` repair path breaks.
assert repair("write file") == "write_file"
def test_leading_quote_falls_through_to_fuzzy_match(self, repair):
# Sanitizer only trims when the XML char is at idx > 0 — a
# name that *starts* with a quote is left untouched so the
# rest of the pipeline (fuzzy match at 0.7 cutoff) can still
# recover the obvious target.
assert repair('"terminal"') == "terminal"