The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(doctor): a large WAL under a live writer is reported as normal, never as "run --fix"
kshitijk4poor84a5f1cd1e9d
diff --git a/hermes_cli/doctor_state.py b/hermes_cli/doctor_state.pyindex 132ef9b6344..9a5f4d7512b 100644--- a/hermes_cli/doctor_state.py+++ b/hermes_cli/doctor_state.py@@ -298,24 +298,27 @@ def _state_db_wal(f: Finding, should_fix: bool, state_db_path: Path) -> None:with warn_on_error(""):size = wal_size()if size > 50 * 1024 * 1024: # 50 MB- check_warn(f"WAL file is large ({size // (1024*1024)} MB)", "(may indicate missed checkpoints)")- if not should_fix:- return f.issues.append("Large WAL file — run 'hermes doctor --fix' to checkpoint")# Checkpoint-lock premise (#40177, #103339): a bare connect runs WAL recovery and the checkpoint# joins the live WAL — under a running gateway that second-writer handling corrupts state.db.# Holder scan first (any other process holding the DB, or an unknown, fails closed), then run the# checkpoint on the exclusive repair guard so an opener arriving in between is refused, not joined.from hermes_state_holders import live_writer_holds_dbfrom hermes_state_repair import _connect_repair_durable, _exclusive_repair_db_guard+ # Honest disjunction (gate C1): a True here means "held OR unprovable" — never assert a live+ # writer as fact.+ held = live_writer_holds_db(state_db_path, connect_repair_durable=_connect_repair_durable)_SKIP = ("Large WAL file — cannot prove state.db is quiet (stop the profile's gateway first, then ""re-run 'hermes doctor --fix' to checkpoint)")- if live_writer_holds_db(state_db_path, connect_repair_durable=_connect_repair_durable):- # Honest disjunction (gate C1): a True here means "held OR unprovable" — never assert a live- # writer as fact.- check_warn("WAL checkpoint skipped: cannot prove state.db is quiet",- "(another process holds it, or it is unreadable — stop the profile's gateway "- "and re-run 'hermes doctor --fix')")+ if held:+ # A large WAL is normal while Desktop or the gateway is running; a bare "run --fix" here sent+ # users straight into the second-writer trap (#110054).+ check_warn(f"WAL file is large ({size // (1024*1024)} MB)",+ "(normal while Desktop or the gateway is running — only checkpoint with them stopped)")return f.issues.append(_SKIP)+ check_warn(f"WAL file is large ({size // (1024*1024)} MB)", "(may indicate missed checkpoints)")+ if not should_fix:+ return f.issues.append(+ "Large WAL file — stop the profile's gateway, then run 'hermes doctor --fix' to checkpoint")with _exclusive_repair_db_guard(state_db_path) as (guard, guard_error):if guard is None:check_warn("WAL checkpoint skipped: could not take exclusive ownership of state.db",diff --git a/tests/hermes_cli/test_doctor_wal_checkpoint_guard.py b/tests/hermes_cli/test_doctor_wal_checkpoint_guard.pyindex 4d9192d5348..26667ef7308 100644--- a/tests/hermes_cli/test_doctor_wal_checkpoint_guard.py+++ b/tests/hermes_cli/test_doctor_wal_checkpoint_guard.py@@ -64,3 +64,40 @@ def test_session_count_reads_a_home_with_uri_reserved_characters(tmp_path):conn.commit()conn.close()assert _session_count(db) == 2+++def _large_wal_db(tmp_path):+ db = tmp_path / "state.db"+ setup = sqlite3.connect(str(db))+ setup.execute("CREATE TABLE t(x)")+ setup.execute("PRAGMA journal_mode=WAL")+ setup.execute("INSERT INTO t VALUES (1)")+ setup.commit()+ setup.close()+ with open(Path(f"{db}-wal"), "ab") as handle:+ handle.truncate(51 * 1024 * 1024)+ return db+++def test_large_wal_warning_under_a_live_writer_never_suggests_a_bare_fix(tmp_path, monkeypatch, capsys):+ """`hermes doctor` (no --fix) on a large WAL while Desktop/gateway hold the DB must say it is normal and+ order "stop" before any `--fix` — the bare "run 'hermes doctor --fix'" nudge is how users became the+ second writer (#110054)."""+ import hermes_state_holders++ monkeypatch.setattr(hermes_state_holders, "live_writer_holds_db", lambda *a, **k: True)+ finding = Finding()+ _state_db_wal(finding, False, _large_wal_db(tmp_path))+ assert len(finding.issues) == 1 and not finding.fixed+ assert "normal while Desktop or the gateway is running" in capsys.readouterr().out+ assert finding.issues[0].index("stop the profile's gateway") < finding.issues[0].index("hermes doctor --fix")+++def test_large_wal_warning_without_a_holder_still_orders_stop_before_fix(tmp_path, monkeypatch):+ import hermes_state_holders++ monkeypatch.setattr(hermes_state_holders, "live_writer_holds_db", lambda *a, **k: False)+ finding = Finding()+ _state_db_wal(finding, False, _large_wal_db(tmp_path))+ assert len(finding.issues) == 1 and not finding.fixed+ assert finding.issues[0].index("stop the profile's gateway") < finding.issues[0].index("hermes doctor --fix")