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

97 lines
3.4 KiB
Python

"""note_turn_start / note_turn_persisted — the concurrent-turn tripwire.
Two turns interleaving on one session corrupt the durable transcript (flush
order races, identity-dedup row loss, stale history base). The tripwire does
not prevent the overlap; it names the occurrence with both turn ids so the
dispatch route that bypassed the busy guard can be identified from logs.
"""
import logging
import pytest
from agent import agent_runtime_helpers as _helpers
from agent.agent_runtime_helpers import note_turn_start, note_turn_persisted
class _FakeAgent:
session_id = "s1"
@pytest.fixture(autouse=True)
def _clear_inflight_registry():
"""Isolate the module-level session registry between tests."""
with _helpers._INFLIGHT_TURNS_LOCK:
_helpers._INFLIGHT_TURNS_BY_SESSION.clear()
yield
with _helpers._INFLIGHT_TURNS_LOCK:
_helpers._INFLIGHT_TURNS_BY_SESSION.clear()
def test_clean_serial_turns_no_warning(caplog):
agent = _FakeAgent()
with caplog.at_level(logging.WARNING, logger="agent.agent_runtime_helpers"):
assert note_turn_start(agent, "s1:t1:aaaa") is None
note_turn_persisted(agent)
assert note_turn_start(agent, "s1:t2:bbbb") is None
note_turn_persisted(agent)
assert not caplog.records
def test_cross_agent_same_session_overlap_warns(caplog):
"""#64934 route: two routing keys mapped to one session_id run their
turns on two different agent objects (the gateway agent cache is keyed
by routing key), so per-agent state alone can never see the overlap."""
agent_a, agent_b = _FakeAgent(), _FakeAgent()
with caplog.at_level(logging.WARNING, logger="agent.agent_runtime_helpers"):
assert note_turn_start(agent_a, "s1:t1:aaaa") is None
prev = note_turn_start(agent_b, "s1:t2:bbbb")
assert prev == "s1:t1:aaaa"
assert len(caplog.records) == 1
msg = caplog.records[0].getMessage()
assert "s1:t1:aaaa" in msg and "s1:t2:bbbb" in msg and "s1" in msg
assert "different agent object" in msg
def test_distinct_sessions_never_cross_warn(caplog):
"""Concurrent turns on different session_ids are legitimate parallelism."""
agent_a, agent_b = _FakeAgent(), _FakeAgent()
agent_b.session_id = "s2"
with caplog.at_level(logging.WARNING, logger="agent.agent_runtime_helpers"):
note_turn_start(agent_a, "s1:t1:aaaa")
assert note_turn_start(agent_b, "s2:t2:bbbb") is None
assert not caplog.records
def test_persist_disabled_fork_neither_registers_nor_warns(caplog):
"""Background-review forks share the live parent's session_id for
prompt-cache warmth but are _persist_disabled — they can never write
to the transcript, so they must not trip the cross-agent warning
against the parent's real in-flight turn (in either direction)."""
parent, fork = _FakeAgent(), _FakeAgent()
fork._persist_disabled = True
with caplog.at_level(logging.WARNING, logger="agent.agent_runtime_helpers"):
note_turn_start(parent, "s1:t1:aaaa") # real turn in flight
assert note_turn_start(fork, "s1:tr:ffff") is None # fork: silent
note_turn_persisted(fork) # fork's funnel still runs
# Reverse order on the next cycle: fork in flight, then real turn.
note_turn_persisted(parent)
note_turn_start(fork, "s1:tr:gggg")
assert note_turn_start(parent, "s1:t2:bbbb") is None
assert not caplog.records