hermes-agent/tests/hermes_state/test_auto_vacuum_holder_gate.py
ethernet 59624b7ace tests: replace every bare host skipif with platforms()
The OS lanes are marker-driven: list_os_marked_tests.py picks the files
a lane imports from their platforms() specs and the lane selects with
-m platforms. A test gated with skipif(sys.platform != "win32") is
therefore never imported on the Windows lane and skipped everywhere else
— it runs on no host. skipif(sys.platform == "win32") tests were merely
invisible to the lane bookkeeping, but the rule the tree now follows is
one host marker, never a bare skipif.

Mechanical mapping, semantics preserved: skip-on-Windows → "posix",
skip-off-Windows → "windows", skip-off-Linux → "linux", skip-on-macOS →
"not macos". The former skip reasons stay as trailing comments. A
non-host condition (os.geteuid() == 0) stays a separate skipif beside
the marker, spelled getattr(os, "geteuid", ...) so the decorator still
imports on Windows.

Where the conversion would stack two platforms() marks on one test (the
conftest rejects that at collection) the narrower mark wins:
- test_update_wedged_gateway: the class is already platforms("linux");
  its per-test "needs UNIX sockets" marks were redundant and are gone.
- test_process_registry.TestSystemdCgroupIsolation: the class-level
  skip-on-Windows moves onto the 11 methods that had no host mark; the
  11 platforms("linux") methods keep theirs.
- test_file_ops_single_roundtrip: the two fifo tests drop their
  platforms("linux") in favour of the module's "posix" (mkfifo exists on
  macOS; both tests already skip when it does not).
- test_linux_desktop_entry / test_gateway_job_teardown_live: duplicate
  or wider marks removed.
2026-09-21 19:18:15 -04:00

68 lines
2.3 KiB
Python

"""Automatic state.db maintenance never VACUUMs under a foreign writer (#110054).
``maybe_auto_prune_and_vacuum`` runs the SAME store rewrite as ``hermes sessions optimize``
(VACUUM + TRUNCATE checkpoint) from CLI and gateway startup. The manual command refuses while a
sibling writer holds the store; the automatic producer must skip for the same reason — a rewritten
generation under a live holder is what leaves every agent refusing turns. It only ever skips.
"""
import subprocess
import sys
import pytest
pytestmark = pytest.mark.platforms("posix") # holder scan is unavailable on Windows
_HOLDER = (
"import sqlite3, sys\n"
"conn = sqlite3.connect(sys.argv[1])\n"
"conn.execute('SELECT count(*) FROM sqlite_master')\n"
"print('ready', flush=True)\n"
"sys.stdin.readline()\n"
)
def _seeded_db(tmp_path):
from hermes_state import SessionDB
db = SessionDB(db_path=tmp_path / "state.db")
db.create_session("old", "cli")
db.append_message("old", "user", "hello")
db.end_session("old", "done")
return db
def _auto_maintenance(db):
# retention_days=0 makes the ended row prunable; the freelist floor is disabled so only the
# holder scan can stop the VACUUM.
return db.maybe_auto_prune_and_vacuum(
retention_days=0, min_interval_hours=0, min_vacuum_interval_days=0,
min_vacuum_freelist_ratio=-1.0)
def test_auto_vacuum_skips_while_a_foreign_process_holds_the_store(tmp_path):
db = _seeded_db(tmp_path)
holder = subprocess.Popen(
[sys.executable, "-c", _HOLDER, str(db.db_path)],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
try:
assert holder.stdout.readline().strip() == "ready"
result = _auto_maintenance(db)
assert result["pruned"] == 1
assert result["vacuumed"] is False
assert result.get("vacuum_skipped_holders")
# Control: the same call VACUUMs once the holder is gone.
holder.stdin.close()
holder.wait(timeout=10)
db.set_meta("last_auto_prune", "0")
db.create_session("old2", "cli")
db.end_session("old2", "done")
again = _auto_maintenance(db)
assert again["vacuumed"] is True
assert "vacuum_skipped_holders" not in again
finally:
if holder.poll() is None:
holder.stdin.close()
holder.wait(timeout=10)
db.close()