Files
hermes-agent/tests/plugins/test_transform_tool_result_hook.py
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

167 lines
5.1 KiB
Python

"""Tests for the ``transform_tool_result`` plugin hook wired into
``model_tools.handle_function_call``.
Mirrors the ``transform_terminal_output`` hook tests from Phase 1 but
targets the generic tool-result seam that runs for every tool dispatch.
"""
import os
from pathlib import Path
import hermes_cli.plugins as plugins_mod
import model_tools
_UNSET = object()
def _run_handle_function_call(
monkeypatch,
*,
tool_name="dummy_tool",
tool_args=None,
dispatch_result='{"output": "original"}',
invoke_hook=_UNSET,
):
"""Drive ``handle_function_call`` with a mocked registry dispatch."""
from tools.registry import registry
monkeypatch.setattr(
registry, "dispatch",
lambda name, args, **kw: dispatch_result,
)
# Skip unrelated side effects (read-loop tracker).
monkeypatch.setattr(model_tools, "_READ_SEARCH_TOOLS", frozenset())
if invoke_hook is not _UNSET:
# Patch the symbol actually imported inside handle_function_call.
monkeypatch.setattr("hermes_cli.plugins.invoke_hook", invoke_hook)
# Supplying a custom invoke_hook means the test expects hooks to
# fire — make has_hook agree so the has_hook gate doesn't skip the
# post_tool_call / transform_tool_result emit paths.
monkeypatch.setattr("hermes_cli.plugins.has_hook", lambda name: True)
return model_tools.handle_function_call(
tool_name,
tool_args or {},
task_id="t1",
session_id="s1",
tool_call_id="tc1",
skip_pre_tool_call_hook=True,
)
def test_first_valid_string_return_replaces_result(monkeypatch):
out = _run_handle_function_call(
monkeypatch,
invoke_hook=lambda hook_name, **kw: [None, {"x": 1}, "first", "second"],
)
assert out == "first"
def test_hook_receives_expected_kwargs(monkeypatch):
captured = {}
def _hook(hook_name, **kwargs):
if hook_name == "transform_tool_result":
captured.update(kwargs)
return []
out = _run_handle_function_call(
monkeypatch,
tool_name="my_tool",
tool_args={"a": 1, "b": "x"},
dispatch_result='{"ok": true}',
invoke_hook=_hook,
)
assert out == '{"ok": true}'
assert captured["tool_name"] == "my_tool"
assert captured["args"] == {"a": 1, "b": "x"}
assert captured["result"] == '{"ok": true}'
assert captured["task_id"] == "t1"
assert captured["session_id"] == "s1"
assert captured["tool_call_id"] == "tc1"
def test_post_tool_call_remains_observational(monkeypatch):
"""post_tool_call return values must NOT replace the result."""
def _hook(hook_name, **kw):
if hook_name == "post_tool_call":
# Observers returning a string must be ignored.
return ["observer return should be ignored"]
return []
out = _run_handle_function_call(
monkeypatch,
invoke_hook=_hook,
)
assert out == '{"output": "original"}'
def test_transform_tool_result_runs_after_post_tool_call(monkeypatch):
"""post_tool_call sees ORIGINAL result; transform_tool_result sees same and may replace."""
observed = []
def _hook(hook_name, **kw):
if hook_name == "post_tool_call":
observed.append(("post_tool_call", kw["result"]))
return []
if hook_name == "transform_tool_result":
observed.append(("transform_tool_result", kw["result"]))
return ["rewritten"]
return []
out = _run_handle_function_call(
monkeypatch,
dispatch_result='{"raw": "value"}',
invoke_hook=_hook,
)
assert out == "rewritten"
# Both hooks saw the ORIGINAL (untransformed) result.
assert observed == [
("post_tool_call", '{"raw": "value"}'),
("transform_tool_result", '{"raw": "value"}'),
]
def test_transform_tool_result_integration_with_real_plugin(monkeypatch, tmp_path):
"""End-to-end: load a real plugin from HERMES_HOME and verify it rewrites results."""
import yaml
hermes_home = Path(os.environ["HERMES_HOME"])
plugins_dir = hermes_home / "plugins"
plugin_dir = plugins_dir / "transform_result_canon"
plugin_dir.mkdir(parents=True)
(plugin_dir / "plugin.yaml").write_text("name: transform_result_canon\n", encoding="utf-8")
(plugin_dir / "__init__.py").write_text(
"def register(ctx):\n"
' ctx.register_hook("transform_tool_result", '
'lambda **kw: f\'CANON[{kw["tool_name"]}]\' + kw["result"])\n',
encoding="utf-8",
)
# Plugins are opt-in — must be listed in plugins.enabled to load.
cfg_path = hermes_home / "config.yaml"
cfg_path.write_text(
yaml.safe_dump({"plugins": {"enabled": ["transform_result_canon"]}}),
encoding="utf-8",
)
# Force a fresh plugin manager so the new config is picked up.
plugins_mod._plugin_manager = plugins_mod.PluginManager()
plugins_mod.discover_plugins()
out = _run_handle_function_call(
monkeypatch,
tool_name="some_tool",
dispatch_result='{"payload": 42}',
)
assert out == 'CANON[some_tool]{"payload": 42}'