Files
hermes-agent/tests/tools/test_code_execution_hook_identity.py
alt-glitch d2480ea076 refactor(code-files): inline _ship_file_to_remote, move the kernel kill into code_kernel
_ship_file_to_remote was _remote_write(..., check=True) under another
name; its five call sites (code_execution_tool, code_kernel_remote) call
_remote_write with check=True directly. code_kernel_remote now imports
_remote_write, _execute_checked and _private_dirs_cmd from
code_execution_rpc at module top, which drops two function-level imports
back into the tool file.

_kill_process_group lived in code_execution_tool but its one caller is
SessionKernel.teardown in code_kernel, which always passed escalate=True.
It moves to code_kernel without the parameter (always SIGTERM, wait 5s,
SIGKILL), with signal and agent.deadline imported at module top. The
escalate=False branch was reached only by tests.

Tests patch _remote_write in both modules where they patched the wrapper.
test_code_kernel_owned_group stubs the teardown kill to a SIGTERM-only
tree kill for its precondition instead of passing escalate=False.
2026-11-16 21:55:16 +05:30

106 lines
4.3 KiB
Python

"""Tool calls made from inside an execute_code cell fire pre_tool_call hooks under the calling
conversation's identity (#145870): without it a hook sees ``session_id=""`` and, in a
multiplexed gateway, can only fall back to the process-wide HERMES_SESSION_ID of whichever
conversation bound a turn last."""
import json
from unittest.mock import patch
import pytest
from model_tools import handle_function_call
from tools.code_execution_rpc import _default_dispatch
from tools.code_kernel import shutdown_all_kernels
@pytest.fixture(autouse=True)
def _local_kernel(monkeypatch):
monkeypatch.setenv("TERMINAL_ENV", "local")
shutdown_all_kernels()
with patch("tools.code_execution_tool._load_config", return_value={"mode": "strict", "timeout": 30}):
yield
shutdown_all_kernels()
@pytest.fixture
def hook_calls():
calls = []
def _record(tool_name, args, **hook_kwargs):
calls.append((tool_name, hook_kwargs))
return None, None
with patch("hermes_cli.plugins._dispatch_pre_tool_call_hooks", side_effect=_record):
yield calls
def _nested(calls, tool):
return [kw for name, kw in calls if name == tool]
def test_nested_call_in_a_local_cell_carries_the_outer_session_and_turn(tmp_path, hook_calls):
target = tmp_path / "note.txt"
target.write_text("hello\n", encoding="utf-8")
code = f"from hermes_tools import read_file\nprint(read_file({str(target)!r})['content'])"
result = json.loads(handle_function_call(
"execute_code", {"code": code}, task_id="task-A", session_id="sess-A", turn_id="turn-A",
conversation_id="conv-A", enabled_tools=["execute_code", "read_file"]))
assert result["status"] == "success", result
nested = _nested(hook_calls, "read_file")
assert len(nested) == 1
assert nested[0]["session_id"] == "sess-A"
assert nested[0]["turn_id"] == "turn-A"
assert nested[0]["conversation_id"] == "conv-A"
def test_nested_call_over_the_remote_rpc_dispatch_carries_the_outer_session_and_turn(tmp_path, hook_calls):
target = tmp_path / "note.txt"
target.write_text("hello\n", encoding="utf-8")
dispatch = _default_dispatch("task-A", "sess-A", "turn-A", "conv-A")
assert "hello" in json.loads(dispatch("read_file", {"path": str(target)}))["content"]
nested = _nested(hook_calls, "read_file")
assert len(nested) == 1
assert nested[0]["session_id"] == "sess-A"
assert nested[0]["turn_id"] == "turn-A"
assert nested[0]["conversation_id"] == "conv-A"
@pytest.mark.parametrize("route", ["kernel", "per_call"])
def test_both_remote_routes_hand_the_outer_identity_to_the_rpc_dispatch(route, monkeypatch):
import inspect
from contextlib import ExitStack
from tests.tools.test_code_kernel_remote import ScriptedEnv, _cell, _spawn_ok_handlers
from tools import code_execution_rpc
from tools.code_kernel_remote import shutdown_all_remote_kernels
real, seen = code_execution_rpc._default_dispatch, []
def _recording(*args, **kwargs):
seen.append(inspect.signature(real).bind(*args, **kwargs).arguments)
return real(*args, **kwargs)
handlers = [("command -v python3", lambda c: {"output": "OK\n", "returncode": 0})]
if route == "kernel":
handlers += _spawn_ok_handlers([_cell(stdout="ran\n")])
monkeypatch.setenv("TERMINAL_ENV", "ssh")
with ExitStack() as stack:
stack.callback(shutdown_all_remote_kernels)
stack.enter_context(patch("tools.code_execution_tool._get_or_create_env",
return_value=(ScriptedEnv(handlers), "ssh")))
stack.enter_context(patch("tools.code_execution_tool._remote_write"))
stack.enter_context(patch("tools.code_kernel_remote._remote_write"))
stack.enter_context(patch.object(code_execution_rpc, "_default_dispatch", side_effect=_recording))
if route == "per_call":
stack.enter_context(patch("tools.code_kernel_remote.execute_in_remote_kernel", return_value=None))
result = json.loads(handle_function_call(
"execute_code", {"code": "print(1)"}, task_id="task-A", session_id="sess-A", turn_id="turn-A",
conversation_id="conv-A", enabled_tools=["execute_code", "read_file"]))
assert result["status"] == "success", result
assert [(a["session_id"], a["turn_id"], a["conversation_id"]) for a in seen] == [("sess-A", "turn-A", "conv-A")]