Files
hermes-agent/tests/agent/test_empty_response_recovery_persistence.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

167 lines
6.2 KiB
Python

"""Regression tests for empty-response recovery transcript persistence."""
from run_agent import AIAgent
class _CapturingSessionDB:
"""Minimal SessionDB stand-in that records every appended message."""
def __init__(self):
self.rows = []
def append_message(self, session_id, role, content=None, **kwargs):
self.rows.append({"role": role, "content": content})
return len(self.rows)
def append_messages_batch(self, session_id, messages, **kwargs):
# Mirror the real batch writer: same rows, one call.
for m in messages:
self.rows.append({"role": m.get("role"), "content": m.get("content")})
return list(range(len(self.rows) - len(messages) + 1, len(self.rows) + 1))
def _agent_with_capturing_db():
agent = AIAgent.__new__(AIAgent)
agent._persist_user_message_idx = None
agent._persist_user_message_override = None
agent._session_db = _CapturingSessionDB()
agent._session_db_created = True
agent._last_flushed_db_idx = 0
agent.session_id = "sess-test"
return agent
def _agent_with_stubbed_persistence():
agent = AIAgent.__new__(AIAgent)
agent._persist_user_message_idx = None
agent._persist_user_message_override = None
agent._session_db = None
agent._session_messages = []
agent.flushed_session_db_messages = []
agent._flush_messages_to_session_db = lambda messages, conversation_history=None: (
agent.flushed_session_db_messages.append([m.copy() for m in messages])
)
return agent
def test_persist_session_strips_trailing_empty_recovery_scaffolding():
"""After stripping scaffolding, also rewind past orphan trailing tool-result
messages that the failed iteration left behind. Otherwise the next user
message lands after a bare ``tool`` and produces a protocol-invalid
sequence that most providers silently fail on, retriggering the empty-
retry loop indefinitely.
"""
agent = _agent_with_stubbed_persistence()
messages = [
{"role": "user", "content": "run the task"},
{
"role": "assistant",
"content": "",
"tool_calls": [{"id": "call_1", "type": "function",
"function": {"name": "x", "arguments": "{}"}}],
},
{"role": "tool", "content": "{}", "tool_call_id": "call_1"},
{
"role": "assistant",
"content": "(empty)",
"_empty_recovery_synthetic": True,
},
{
"role": "user",
"content": (
"You just executed tool calls but returned an empty response. "
"Please process the tool results above and continue with the task."
),
"_empty_recovery_synthetic": True,
},
]
AIAgent._persist_session(agent, messages, conversation_history=[])
# After strip + rewind, only the original user message remains. The
# assistant(tool_calls) + tool pair is dropped because its iteration
# never produced a real response.
assert messages == [
{"role": "user", "content": "run the task"},
]
assert agent.flushed_session_db_messages[-1] == messages
assert all(not msg.get("_empty_recovery_synthetic") for msg in messages)
def test_persist_session_keeps_unmarked_terminal_empty_response():
agent = _agent_with_stubbed_persistence()
messages = [
{"role": "user", "content": "run the task"},
{"role": "assistant", "content": "(empty)"},
]
AIAgent._persist_session(agent, messages, conversation_history=[])
assert messages == [
{"role": "user", "content": "run the task"},
{"role": "assistant", "content": "(empty)"},
]
assert agent.flushed_session_db_messages[-1] == messages
def test_flush_never_writes_buried_empty_recovery_scaffolding():
"""When an empty-after-tools nudge is followed by a tool-calling response,
the synthetic ``(empty)`` + nudge pair stays buried in the live message
list (only the trailing copies are ever dropped). The append-only flush
must skip it regardless of position, otherwise the synthetic turns land in
the session store and pollute every resumed transcript.
"""
agent = _agent_with_capturing_db()
messages = [
{"role": "user", "content": "run the task"},
{
"role": "assistant",
"content": "",
"tool_calls": [{"id": "call_1", "type": "function",
"function": {"name": "x", "arguments": "{}"}}],
},
{"role": "tool", "content": "{}", "tool_call_id": "call_1"},
# Synthetic recovery scaffolding, now buried because the model answered
# the nudge with another tool call rather than terminating.
{"role": "assistant", "content": "(empty)", "_empty_recovery_synthetic": True},
{
"role": "user",
"content": "You just executed tool calls but returned an empty response.",
"_empty_recovery_synthetic": True,
},
{
"role": "assistant",
"content": "",
"tool_calls": [{"id": "call_2", "type": "function",
"function": {"name": "x", "arguments": "{}"}}],
},
{"role": "tool", "content": "{}", "tool_call_id": "call_2"},
{"role": "assistant", "content": "All done."},
]
agent._flush_messages_to_session_db(messages, conversation_history=[])
persisted = agent._session_db.rows
assert all(row["content"] != "(empty)" for row in persisted)
assert all("empty response" not in (row["content"] or "") for row in persisted)
# Only the genuine turns reach the store, in order.
assert [r["role"] for r in persisted] == [
"user", "assistant", "tool", "assistant", "tool", "assistant",
]
assert persisted[-1]["content"] == "All done."
def test_flush_skips_thinking_prefill_scaffolding():
agent = _agent_with_capturing_db()
messages = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "", "_thinking_prefill": True},
{"role": "assistant", "content": "Hello!"},
]
agent._flush_messages_to_session_db(messages, conversation_history=[])
assert [r["content"] for r in agent._session_db.rows] == ["hi", "Hello!"]