test: achievements scan attaches read-only; read-only handles never trip the duplicate-writer warning

Both invariants fail on origin/main. Test doubles that stood in for
SessionDB() gain the read_only kwarg the production call sites now pass.
This commit is contained in:
teknium1
2026-09-14 07:34:26 -07:00
committed by Teknium
parent 939a2f64b4
commit 1ce2d25237
3 changed files with 59 additions and 1 deletions
+3
View File
@@ -234,6 +234,9 @@ def test_show_status_reports_gateway_session_last_activity(monkeypatch, capsys,
monkeypatch.setattr(gateway_mod, "find_gateway_pids", lambda exclude_pids=None: [], raising=False)
class _FakeDB:
def __init__(self, **_kwargs):
pass
def list_gateway_sessions(self, active_only=True):
return [
{"id": "gw-old", "last_active": time.time() - 7200},
@@ -650,3 +650,26 @@ def test_duplicate_handles_on_one_path_are_reported(db, caplog):
finally:
for d in extra:
d.close()
@pytest.mark.requires_wal
def test_read_only_handles_do_not_count_toward_the_duplicate_writer_warning(db, caplog):
"""The warning names the cost of WRITER handles (writer connection, write lock, close-time
checkpoint). Read-only attaches are the sanctioned per-request shape for dashboard routers
and CLI lookups, so any number of them must stay silent (#100896)."""
import logging
from hermes_state import SessionDB
from hermes_state_readpool import _HANDLES_PER_PATH_WARN
extra = []
try:
with caplog.at_level(logging.WARNING, logger="hermes_state"):
for _ in range(_HANDLES_PER_PATH_WARN + 2):
extra.append(SessionDB(db_path=db.db_path, read_only=True))
assert not any(
"live SessionDB handles on" in r.getMessage() for r in caplog.records
), "read-only attaches were counted as duplicate writers"
finally:
for d in extra:
d.close()
+33 -1
View File
@@ -123,7 +123,7 @@ def _install_fake_session_db(plugin_api, fake_db):
and cannot leak into unrelated tests in the same xdist worker.
"""
fake_module = type(sys)("hermes_state")
fake_module.SessionDB = lambda: fake_db
fake_module.SessionDB = lambda **_kw: fake_db
plugin_api._test_monkeypatch.setitem(sys.modules, "hermes_state", fake_module)
@@ -301,3 +301,35 @@ def test_partial_snapshots_do_not_persist_unlock_timestamps(plugin_api):
"partial scans must not record unlock timestamps — a later session "
"could change whether the badge deserves to be unlocked yet"
)
def test_scan_sessions_never_opens_a_writable_session_db(plugin_api, tmp_path, monkeypatch):
"""The scan is a pure read that runs inside the dashboard process (per background scan,
per /rescan). A writable ``SessionDB()`` there was one more writer connection on the
dashboard's own state.db each time — the same-process handle leak behind the
``N live SessionDB handles`` precursor (#100896). Real store, real open, no fakes."""
import hermes_state
from hermes_state import SessionDB
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", tmp_path / "state.db")
seed = SessionDB(db_path=tmp_path / "state.db")
seed.create_session("s1", source="cli")
seed.append_message("s1", "user", "hello")
seed.close()
writable_opens = []
real_init = SessionDB.__init__
def spy(self, *args, **kwargs):
if not kwargs.get("read_only"):
writable_opens.append(kwargs)
return real_init(self, *args, **kwargs)
monkeypatch.setattr(SessionDB, "__init__", spy)
result = plugin_api.scan_sessions()
assert result.get("error") is None
assert [s["session_id"] for s in result["sessions"]] == ["s1"]
assert writable_opens == [], "the achievements scan must attach read-only"