Files
hermes-agent/tests/tools/test_code_execution_programmatic_reads.py
alt-glitch c81362cc59 refactor(tools): the tool list and one tool call become tools/schemas.py and tools/calls.py
model_tools.py held two jobs. tools/schemas.py now builds the tool list sent
to the model: importing it loads the built-in and plugin tools (the moment
`import model_tools` did), and get_tool_definitions selects, rewrites per
session, cleans and defers. tools/calls.py runs one model tool call through
argument repair, hooks, middleware and the registry. Argument repair stays in
tools/arg_coercion.py: the inline executors import it at module top, and in
tools/calls.py that import would load every tool. The error cleaner joins tools/results.py.
Definitions move unchanged. Imports of tools.context and the schema cleaner
that were inside the moved functions go to module top as module objects;
both are loaded by then already, and a patch on, say,
tools.context.hosted_tool_refusal still reaches the code. The imports of
tools.deferral stay inside their functions: Tool Search loads the connectors
on the first tool list, and at module top `import model_tools` would load
them too. calls.py reads get_tool_definitions and _last_resolved_tool_names
through the schemas module, so a patch on either still reaches the call path.

tools/schema_sanitizer.py stays a file. Provider adapters and a provider
plugin import it at module top; inside tools/schemas.py, loading a provider
would load every tool.

Old names, until v0.27.0. model_tools.py is a file of re-exports: the names
releases up to v0.23.5 import from it inside functions, and the public names
pinned catalog plugins import (get_tool_definitions, handle_function_call).
Importing it still loads the tools. No product code imports model_tools now; its own
compatibility tests do, as do ten tests and four eval workers that import it
only to load the tools (the evals also run against older checkouts).

The call path's log lines move from the model_tools logger to tools.calls and
tools.schemas: `hermes logs --component` shows them under tools, not agent.
Argument repair logs as "model_tools", as before.
2026-12-02 04:05:32 +05:30

59 lines
2.7 KiB
Python

"""Tool calls made from inside an execute_code cell are consumed by code, not shown to the model.
Regression for #82711 / #44843: a cell's repeated ``read_file(p)["content"]`` raised ``KeyError``
because it was answered with the conversation's "unchanged since last read" stub.
"""
import json
import pytest
from tools.calls import handle_function_call
from tools.code_execution_rpc import _default_dispatch
from tools.code_kernel import CellAuthority
from tools.file_tools_read_tracking import reset_file_dedup
def _call(dispatch, name, **args):
return json.loads(dispatch(name, args))
# Local cells dispatch through the kernel's CellAuthority; remote backends through the RPC loop.
@pytest.mark.parametrize("cell_dispatch", [lambda task: CellAuthority(task).dispatch, _default_dispatch],
ids=["session-kernel", "rpc"])
def test_cell_reads_always_carry_content_and_do_not_count_as_shown_to_the_model(tmp_path, cell_dispatch):
task = "cell-reads"
reset_file_dedup(task)
seen, unseen = tmp_path / "seen.txt", tmp_path / "unseen.txt"
seen.write_text("alpha\n", encoding="utf-8")
unseen.write_text("beta\n", encoding="utf-8")
cell = cell_dispatch(task)
def model(name, **args):
return json.loads(handle_function_call(name, args, task_id=task))
assert "alpha" in model("read_file", path=str(seen))["content"]
# The model already has this file; the cell still needs the text, however often it asks.
assert all("alpha" in _call(cell, "read_file", path=str(seen))["content"] for _ in range(5))
assert all("beta" in _call(cell, "read_file", path=str(unseen))["content"] for _ in range(5))
assert all("total_count" in _call(cell, "search_files", pattern="beta", path=str(tmp_path)) for _ in range(5))
# Only the cell saw unseen.txt, so the model's own first read gets the text, not the stub...
assert "beta" in model("read_file", path=str(unseen))["content"]
# ...and the model's repeat of a file it was shown is still deduplicated.
assert model("read_file", path=str(seen)).get("content_returned") is False
def test_cell_read_still_counts_as_a_read_before_overwriting(tmp_path):
task = "cell-read-then-write"
reset_file_dedup(task)
target = tmp_path / "notes.txt"
target.write_text("old\n", encoding="utf-8")
cell = _default_dispatch(task)
refused = _call(cell, "write_file", path=str(target), content="new\n")
assert "error" in refused and target.read_text(encoding="utf-8") == "old\n"
assert "old" in _call(cell, "read_file", path=str(target))["content"]
written = _call(cell, "write_file", path=str(target), content="new\n")
assert "error" not in written and target.read_text(encoding="utf-8") == "new\n"