hermes-agent/tests/agent/test_turn_finalizer_cleanup_guard.py
Teknium 6b81590c55
test: prune low-value tests suite-wide (wave 1) — 46,820 → 28,106 test functions
Systematic prune per AGENTS.md test policy, one pass over every major
test tree (gateway, hermes_cli, tools, agent, run_agent, plugins, cli,
cron, tui_gateway, honcho/openviking, root-level):

- DELETE: source-reading tests (read_text/getsource on prod files),
  change-detector tests (exact catalog counts, model-name snapshots,
  config version literals), mock-echo tests (assert a mock returns what
  it was told), assertion-free/trivial tests, near-duplicate
  parametrizations (boundaries + one representative kept), async/sync
  twin duplicates, cosmetic within-file variations.
- KEEP (mandatory): security/redaction/approval guards, message-role
  alternation invariants, prompt-caching/deterministic-call-id
  invariants, issue-number regression tests (deduped), E2E tests.
- 6 test files deleted outright (script-style/no-assert or fully
  redundant); conftest.py, fakes/, fixtures/ untouched.
- tests/acp/conftest.py added: autouse fixture stubs the live
  models.dev/GitHub/Copilot/Anthropic inventory fetches that ACP server
  tests performed on every session create — test_server.py 147s → 3.4s,
  and the tests are now genuinely hermetic.
- Sleep-based slowness shrunk where safe (codex_ttfb_watchdog,
  compression_concurrent_fork, etc.); no wall-clock assertion tightened.

Verification: full hermetic suite via scripts/run_tests.sh —
2439 files, 31,130 tests passed, 0 failed, 0 flaky retries, 315s wall
(baseline: 583s wall, 13,564s subprocess CPU).
2026-07-29 13:10:23 -07:00

166 lines
4.8 KiB
Python

"""Regression test for #8049.
When the post-loop cleanup chain in ``finalize_turn`` raises — trajectory
save (file I/O), resource teardown (remote VM/browser), or session
persistence (SQLite) — the partial ``final_response`` the caller is waiting
for must still be returned. Previously any of those raised straight out of
``run_conversation``, so a subprocess wrapper saw an empty stdout with no
traceback and lost the whole turn.
"""
import pytest
from agent.turn_finalizer import finalize_turn
class _StubBudget:
used = 5
max_total = 3
remaining = 0
class _StubCompressor:
last_prompt_tokens = 0
class _StubAgent:
"""Minimal agent surface that ``finalize_turn`` reads from."""
def __init__(self, *, raise_in):
self._raise_in = set(raise_in)
self.max_iterations = 3
self.iteration_budget = _StubBudget()
self.context_compressor = _StubCompressor()
self.model = "stub/model"
self.provider = "stub"
self.base_url = "http://stub"
self.session_id = "sess-1"
self.quiet_mode = True
self.platform = "cli"
self._interrupt_requested = False
self._interrupt_message = None
self._tool_guardrail_halt_decision = None
self._response_was_previewed = False
self._skill_nudge_interval = 0
self._iters_since_skill = 0
for attr in (
"session_input_tokens",
"session_output_tokens",
"session_cache_read_tokens",
"session_cache_write_tokens",
"session_reasoning_tokens",
"session_prompt_tokens",
"session_completion_tokens",
"session_total_tokens",
"session_estimated_cost_usd",
):
setattr(self, attr, 0)
self.session_cost_status = "ok"
self.session_cost_source = "stub"
# --- fallible cleanup surfaces -------------------------------------
def _save_trajectory(self, *a, **k):
if "save_trajectory" in self._raise_in:
raise RuntimeError("trajectory disk full")
def _cleanup_task_resources(self, *a, **k):
if "cleanup_task_resources" in self._raise_in:
raise RuntimeError("docker teardown EOF")
def _drop_trailing_empty_response_scaffolding(self, *a, **k):
pass
def _persist_session(self, *a, **k):
if "persist_session" in self._raise_in:
raise RuntimeError("sqlite database is locked")
# --- harmless no-ops ------------------------------------------------
def _emit_status(self, *a, **k):
pass
def _safe_print(self, *a, **k):
pass
def _handle_max_iterations(self, messages, n):
return "PARTIAL SUMMARY FROM MODEL"
def _file_mutation_verifier_enabled(self):
return False
def _turn_completion_explainer_enabled(self):
return False
def _drain_pending_steer(self):
return None
def clear_interrupt(self):
pass
def _sync_external_memory_for_turn(self, **k):
pass
def _run(
agent,
*,
final_response=None,
api_call_count=3,
turn_exit_reason="unknown",
):
messages = [
{"role": "user", "content": "do a thing"},
{
"role": "assistant",
"content": "",
"tool_calls": [
{"id": "c1", "function": {"name": "read_file", "arguments": "{}"}}
],
},
{"role": "tool", "tool_call_id": "c1", "content": "file contents"},
]
return finalize_turn(
agent,
final_response=final_response,
api_call_count=api_call_count,
interrupted=False,
failed=False,
messages=messages,
conversation_history=None,
effective_task_id="task-1",
turn_id="turn-1",
user_message="do a thing",
original_user_message="do a thing",
_should_review_memory=False,
_turn_exit_reason=turn_exit_reason,
)
@pytest.mark.parametrize(
"step", ["save_trajectory", "cleanup_task_resources", "persist_session"]
)
def test_single_cleanup_step_raises_does_not_skip_others(step):
agent = _StubAgent(raise_in=(step,))
result = _run(agent)
# Response survives.
assert result["final_response"] == "PARTIAL SUMMARY FROM MODEL"
# Exactly the failing step is recorded; the others ran without error.
assert result["cleanup_errors"] == [
next(
e
for e in result["cleanup_errors"]
if e.startswith(step)
)
]
assert len(result["cleanup_errors"]) == 1
def test_clean_turn_has_no_cleanup_errors_key():
agent = _StubAgent(raise_in=())
result = _run(agent)
assert result["final_response"] == "PARTIAL SUMMARY FROM MODEL"
assert result["completed"] is False
assert "cleanup_errors" not in result