Skip to content

The agent that grows with you

fix(doctor): refuse the WAL checkpoint while a live writer holds state.db (#103339)

Halldrixb02f3aa00b5b
diff --git a/hermes_cli/doctor_state.py b/hermes_cli/doctor_state.py
index 8851ee4b7cf..40bb61d0d45 100644
--- a/hermes_cli/doctor_state.py
+++ b/hermes_cli/doctor_state.py
@@ -243,10 +243,24 @@ def _state_db_wal(f: Finding, should_fix: bool, state_db_path: Path) -> None:
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): a bare connect runs WAL recovery and the checkpoint joins the
+ # live WAL — under a running gateway that second-writer handling corrupts state.db. Skip instead.
+ from hermes_state_holders import live_writer_holds_db
+ from hermes_state_repair import _connect_repair_durable
+ 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" — the DatabaseError lane fires when SQLite
+ # cannot open the file at all, with nobody holding it. Never
+ # assert a live writer as fact.
+ check_warn("WAL checkpoint skipped: cannot prove state.db is quiet",
+ "(a live writer holds it, or it is unreadable — stop the profile's gateway "
+ "and re-run 'hermes doctor --fix')")
+ return f.issues.append("Large WAL file — cannot prove state.db is quiet (stop the profile's "
+ "gateway first, then re-run 'hermes doctor --fix' to checkpoint)")
+ import contextlib
import sqlite3
- conn = sqlite3.connect(str(state_db_path))
- conn.execute("PRAGMA wal_checkpoint(PASSIVE)")
- conn.close()
+ with contextlib.closing(sqlite3.connect(str(state_db_path))) as conn:
+ conn.execute("PRAGMA wal_checkpoint(PASSIVE)")
check_ok(f"WAL checkpoint performed ({size // 1024}K → {wal_size() // 1024}K)")
f.fixed += 1
elif size > 10 * 1024 * 1024: # 10 MB
diff --git a/tests/hermes_cli/test_doctor_wal_holder_guard.py b/tests/hermes_cli/test_doctor_wal_holder_guard.py
new file mode 100644
index 00000000000..f08e6858611
--- /dev/null
+++ b/tests/hermes_cli/test_doctor_wal_holder_guard.py
@@ -0,0 +1,51 @@
+"""Regression: ``hermes doctor --fix`` must not checkpoint the live WAL under a running gateway.
+
+Checkpoint-lock premise (#40177): a bare ``sqlite3.connect`` runs WAL recovery and
+``PRAGMA wal_checkpoint(PASSIVE)`` joins the live WAL — that second-writer handling
+on a gateway-held state.db is the corruption class #103339 tracks. The check must
+skip with an actionable finding while a live writer holds the database.
+"""
+
+from __future__ import annotations
+
+import sqlite3
+from pathlib import Path
+
+from hermes_cli.doctor_report import Finding
+from hermes_cli.doctor_state import _state_db_wal
+
+
+# NOTE: no ``requires_wal`` marker here on purpose. That gate exists for tests
+# that depend on Hermes *choosing* WAL mode (declined on vulnerable SQLite
+# builds). This test forces WAL explicitly through raw SQL and asserts only on
+# the holder-guard skip, so the probe mechanics work on any build.
+def test_wal_checkpoint_skipped_while_live_writer_holds_db(tmp_path):
+ """A held database skips the checkpoint; nothing is checkpointed or fixed."""
+ db = tmp_path / "state.db"
+ setup = sqlite3.connect(str(db))
+ try:
+ setup.execute("CREATE TABLE t(x)")
+ setup.execute("PRAGMA journal_mode=WAL")
+ setup.execute("INSERT INTO t VALUES (1)")
+ setup.commit()
+ finally:
+ setup.close()
+ holder = sqlite3.connect(str(db))
+ holder.execute("SELECT count(*) FROM t").fetchone()
+ try:
+ wal = Path(f"{db}-wal")
+ assert wal.exists()
+ # Push past the 50 MB fix threshold without 50 MB of real frames: the
+ # guard runs before any WAL byte is parsed, so padding is never read.
+ with open(wal, "ab") as handle:
+ handle.truncate(51 * 1024 * 1024)
+ finding = Finding()
+ _state_db_wal(finding, True, db)
+ finally:
+ try:
+ holder.close()
+ except Exception:
+ pass
+
+ assert finding.fixed == 0
+ assert any("gateway" in issue for issue in finding.issues)