From 1ce2d252372651b0bfc2d48bd87b4476856ceeda Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Mon, 14 Sep 2026 07:34:26 -0700 Subject: [PATCH] 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. --- tests/hermes_cli/test_status.py | 3 ++ .../test_session_db_read_conn_pool.py | 23 +++++++++++++ tests/plugins/test_achievements_plugin.py | 34 ++++++++++++++++++- 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/tests/hermes_cli/test_status.py b/tests/hermes_cli/test_status.py index 5ae09ca96b..b2e7107398 100644 --- a/tests/hermes_cli/test_status.py +++ b/tests/hermes_cli/test_status.py @@ -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}, diff --git a/tests/hermes_state/test_session_db_read_conn_pool.py b/tests/hermes_state/test_session_db_read_conn_pool.py index 9d00ce58be..696d00caae 100644 --- a/tests/hermes_state/test_session_db_read_conn_pool.py +++ b/tests/hermes_state/test_session_db_read_conn_pool.py @@ -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() diff --git a/tests/plugins/test_achievements_plugin.py b/tests/plugins/test_achievements_plugin.py index b0842dca60..0e5fac9e3e 100644 --- a/tests/plugins/test_achievements_plugin.py +++ b/tests/plugins/test_achievements_plugin.py @@ -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"