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

190 lines
5.9 KiB
Python

#!/usr/bin/env python3
"""Tests for the cross-agent FileStateRegistry (tools/file_state.py).
Covers the three layers added for safe concurrent subagent file edits:
1. Cross-agent staleness detection via ``check_stale``
2. Per-path serialization via ``lock_path``
3. Delegate-completion reminder via ``writes_since``
Plus integration through the real ``read_file_tool`` / ``write_file_tool``
/ ``patch_tool`` handlers so the full hook wiring is exercised.
Run:
python -m pytest tests/tools/test_file_state_registry.py -v
"""
from __future__ import annotations
import json
import os
import tempfile
import threading
import time
import unittest
from tools import file_state
from tools.file_tools import (
read_file_tool,
write_file_tool,
patch_tool,
)
def _tmp_file(content: str = "initial\n") -> str:
fd, path = tempfile.mkstemp(prefix="hermes_file_state_test_", suffix=".txt")
with os.fdopen(fd, "w") as f:
f.write(content)
return path
class FileStateRegistryUnitTests(unittest.TestCase):
"""Direct unit tests on the registry singleton."""
def setUp(self) -> None:
file_state.get_registry().clear()
self._tmpfiles: list[str] = []
def tearDown(self) -> None:
for p in self._tmpfiles:
try:
os.unlink(p)
except OSError:
pass
file_state.get_registry().clear()
def _mk(self, content: str = "x\n") -> str:
p = _tmp_file(content)
self._tmpfiles.append(p)
return p
def test_record_read_then_check_stale_returns_none(self):
p = self._mk()
file_state.record_read("A", p)
self.assertIsNone(file_state.check_stale("A", p))
def test_sibling_write_flags_other_agent_as_stale(self):
p = self._mk()
file_state.record_read("A", p)
# Simulate sibling writing this file later
time.sleep(0.01) # ensure ts ordering across resolution
file_state.note_write("B", p)
warn = file_state.check_stale("A", p)
self.assertIsNotNone(warn)
self.assertIn("B", warn)
self.assertIn("sibling", warn.lower())
def test_lock_path_serializes_same_path(self):
p = self._mk()
events: list[tuple[str, int]] = []
lock = threading.Lock()
def worker(i: int) -> None:
with file_state.lock_path(p):
with lock:
events.append(("enter", i))
time.sleep(0.01)
with lock:
events.append(("exit", i))
threads = [threading.Thread(target=worker, args=(i,)) for i in range(4)]
for t in threads:
t.start()
for t in threads:
t.join()
# Every enter must be immediately followed by its matching exit.
self.assertEqual(len(events), 8)
for i in range(0, 8, 2):
self.assertEqual(events[i][0], "enter")
self.assertEqual(events[i + 1][0], "exit")
self.assertEqual(events[i][1], events[i + 1][1])
def test_lock_path_is_per_path_not_global(self):
a = self._mk()
b = self._mk()
b_entered = threading.Event()
def hold_a() -> None:
with file_state.lock_path(a):
b_entered.wait(timeout=2.0)
def enter_b() -> None:
time.sleep(0.02) # let A grab its lock
with file_state.lock_path(b):
b_entered.set()
ta = threading.Thread(target=hold_a)
tb = threading.Thread(target=enter_b)
ta.start()
tb.start()
self.assertTrue(b_entered.wait(timeout=3.0))
ta.join(timeout=3.0)
tb.join(timeout=3.0)
def test_kill_switch_env_var(self):
p = self._mk()
os.environ["HERMES_DISABLE_FILE_STATE_GUARD"] = "1"
try:
file_state.record_read("A", p)
file_state.note_write("B", p)
self.assertIsNone(file_state.check_stale("A", p))
self.assertEqual(file_state.known_reads("A"), [])
self.assertEqual(
file_state.writes_since("A", 0.0, [p]),
{},
)
finally:
del os.environ["HERMES_DISABLE_FILE_STATE_GUARD"]
class FileToolsIntegrationTests(unittest.TestCase):
"""Integration through the real file_tools handlers.
These exercise the wiring: read_file_tool → registry.record_read,
write_file_tool / patch_tool → check_stale + lock_path + note_write.
"""
def setUp(self) -> None:
file_state.get_registry().clear()
self._tmpdir = tempfile.mkdtemp(prefix="hermes_file_state_int_")
def tearDown(self) -> None:
import shutil
shutil.rmtree(self._tmpdir, ignore_errors=True)
file_state.get_registry().clear()
def _write_seed(self, name: str, content: str = "seed\n") -> str:
p = os.path.join(self._tmpdir, name)
with open(p, "w") as f:
f.write(content)
return p
def test_sibling_agent_write_surfaces_warning_through_handler(self):
p = self._write_seed("shared.txt")
r = json.loads(read_file_tool(path=p, task_id="agentA"))
self.assertNotIn("error", r)
w_b = json.loads(write_file_tool(path=p, content="B wrote\n", task_id="agentB"))
self.assertNotIn("error", w_b)
w_a = json.loads(write_file_tool(path=p, content="A stale\n", task_id="agentA"))
warn = w_a.get("_warning", "")
self.assertTrue(warn, f"expected warning, got: {w_a}")
# The cross-agent message names the sibling task_id.
self.assertIn("agentB", warn)
self.assertIn("sibling", warn.lower())
def test_net_new_file_no_warning(self):
p = os.path.join(self._tmpdir, "brand_new.txt")
# Nobody has read or written this before.
w = json.loads(write_file_tool(path=p, content="hi\n", task_id="agentX"))
self.assertFalse(w.get("_warning"))
self.assertNotIn("error", w)
if __name__ == "__main__":
unittest.main()