hermes-agent/tests/gateway/test_auto_continue.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

117 lines
4.5 KiB
Python

"""Tests for the auto-continue feature (#4493 / #45232).
When the gateway restarts mid-agent-work, the session transcript can end on a
tool result that the agent never processed. The auto-continue logic detects
this and prepends an API-only system note to the next user message so the model
does not re-execute stale interrupted tool calls before addressing new input.
"""
def _simulate_auto_continue(agent_history: list, user_message: str) -> str:
"""Reproduce the auto-continue injection logic from _run_agent().
This mirrors the exact code in gateway/run.py so we can test the
detection and message transformation without spinning up a full
gateway runner.
"""
message = user_message
if agent_history and agent_history[-1].get("role") == "tool":
message = (
"[System note: A new message has arrived. The conversation "
"history contains pending tool outputs from an interrupted turn. "
"IGNORE those pending results. Address the user's NEW message "
"below FIRST. Do NOT re-execute old tool calls from the history.]\n\n"
+ message
)
return message
class TestAutoDetection:
"""Test that trailing tool results are correctly detected."""
def test_trailing_tool_result_triggers_note(self):
history = [
{"role": "user", "content": "deploy the app"},
{"role": "assistant", "content": None, "tool_calls": [
{"id": "call_1", "function": {"name": "terminal", "arguments": "{}"}}
]},
{"role": "tool", "tool_call_id": "call_1", "content": "deployed successfully"},
]
result = _simulate_auto_continue(history, "what happened?")
assert "[System note:" in result
assert "interrupted" in result
assert "NEW message" in result
assert "Do NOT re-execute" in result
assert "what happened?" in result
def test_empty_history_no_note(self):
result = _simulate_auto_continue([], "hello")
assert result == "hello"
class TestInterruptedReplayFiltering:
def test_interrupted_side_effect_is_replayed_as_unknown(self):
from gateway.run import _build_gateway_agent_history
history = [
{"role": "user", "content": "transcribe this video"},
{
"role": "assistant",
"content": None,
"tool_calls": [
{"id": "call_1", "function": {"name": "terminal", "arguments": "{}"}},
],
},
{
"role": "tool",
"tool_call_id": "call_1",
"content": '{"exit_code": 130, "output": "[Command interrupted]"}',
},
]
agent_history, observed_context = _build_gateway_agent_history(history)
assert observed_context is None
assert agent_history[:2] == history[:2]
assert agent_history[-1]["role"] == "tool"
assert agent_history[-1]["tool_call_id"] == "call_1"
assert agent_history[-1]["effect_disposition"] == "unknown"
def test_dangling_unanswered_side_effect_is_replayed_as_unknown(self):
"""A trailing side-effecting call gets an UNKNOWN result, not a retry.
This is the SIGKILL signature from #49201: the tool itself ran a
restart/shutdown command and killed the gateway before its result was
persisted. The transcript tail is an assistant message with tool_calls
and zero matching tool rows. A synthetic UNKNOWN result closes the tool
pair without claiming the restart did not happen or inviting a retry.
"""
from gateway.run import _build_gateway_agent_history
history = [
{"role": "user", "content": "restart the container"},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"function": {
"name": "terminal",
"arguments": '{"command": "docker restart hermes-agent"}',
},
},
],
},
]
agent_history, _observed_context = _build_gateway_agent_history(history)
assert agent_history[:2] == history
assert agent_history[-1]["role"] == "tool"
assert agent_history[-1]["tool_call_id"] == "call_1"
assert agent_history[-1]["effect_disposition"] == "unknown"