Files
hermes-agent/tests/run_agent/test_tool_name_db_persistence.py
justincc a61420952e fix(agent): set tool_name on tool-result messages at construction time
Introduces make_tool_result_message() in tool_dispatch_helpers.py as the
single place where tool-result message dicts are built. All six construction
sites in tool_executor.py, agent_runtime_helpers.py, and mini_swe_runner.py
now use it, so tool_name is set in memory from the moment a message is
created rather than relying on fallback logic in the flush paths.

Fixes blank tool_name in both state.db and JSON session logs.

Adds tests.
2026-05-19 20:49:11 +01:00

46 lines
1.5 KiB
Python

"""Test that tool_name is correctly persisted to the session DB for tool-result messages.
make_tool_result_message() sets tool_name on every tool-result dict at construction
time. This test verifies that the value survives the flush path into the session DB.
"""
from unittest.mock import MagicMock, patch
from run_agent import AIAgent
from agent.tool_dispatch_helpers import make_tool_result_message
def _make_agent(session_db):
with (
patch("run_agent.get_tool_definitions", return_value=[]),
patch("run_agent.check_toolset_requirements", return_value={}),
patch("run_agent.OpenAI"),
):
return AIAgent(
api_key="test-key",
base_url="https://openrouter.ai/api/v1",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
session_db=session_db,
)
def test_tool_name_persisted_to_session_db():
"""tool_name set by make_tool_result_message must be passed through to
append_message so the column is populated on first flush to the session DB."""
session_db = MagicMock()
agent = _make_agent(session_db)
messages = [
{"role": "user", "content": "run a command"},
make_tool_result_message("terminal", "$ ls\nfile.txt", "c1"),
]
agent._flush_messages_to_session_db(messages)
tool_appends = [
c for c in session_db.append_message.call_args_list
if c.kwargs.get("role") == "tool"
]
assert len(tool_appends) == 1
assert tool_appends[0].kwargs["tool_name"] == "terminal"