Skip to content

The agent that grows with you

fix(state): a WAL sidecar unlinked mid-preflight is gone, not read-only

Teknium31f8ae70f103
diff --git a/hermes_state_repair.py b/hermes_state_repair.py
index f5075dcd6f9..059e5f72002 100644
--- a/hermes_state_repair.py
+++ b/hermes_state_repair.py
@@ -534,6 +534,18 @@ def _backup_db_file(db_path: Path) -> tuple[Optional[Path], Optional[str]]:
return None, f"backup copy failed: {exc}"
+def _rw_or_vanished(p: Path) -> bool:
+ """True when *p* is read-write for this user OR no longer exists.
+
+ The preflight lists the files first and checks them after, and a live sibling connection's last ``close()``
+ unlinks ``-wal``/``-shm`` in between (a concurrent quarantine can rename ``state.db`` aside the same way).
+ ``os.access`` on the vanished path returns False, which is "gone", not "read-only". The final re-check covers
+ a sidecar that vanished and was recreated by the next opener before ``exists()`` ran.
+ """
+ mode = os.R_OK | os.W_OK
+ return os.access(p, mode) or not p.exists() or os.access(p, mode)
+
+
def preflight_db_writability(db_path: Path, *, db_label: str = "state.db", directory_only: bool = False) -> None:
"""Refuse-or-repair read-only DB files BEFORE the first connection opens.
@@ -555,7 +567,7 @@ def preflight_db_writability(db_path: Path, *, db_label: str = "state.db", direc
sidecars = (db_path.with_name(db_path.name + "-wal"), db_path.with_name(db_path.name + "-shm"))
files = () if directory_only else tuple((p, False) for p in (db_path, *sidecars) if p.is_file())
for p, is_dir in [(db_path.parent, True), *files]:
- if (is_dir and not p.is_dir()) or os.access(p, os.R_OK | os.W_OK):
+ if (is_dir and not p.is_dir()) or _rw_or_vanished(p):
continue
x = "x" if is_dir else ""
in_scope = False
diff --git a/tests/hermes_state/test_hermes_state_readonly_preflight.py b/tests/hermes_state/test_hermes_state_readonly_preflight.py
index cc88161d477..0efc347c41e 100644
--- a/tests/hermes_state/test_hermes_state_readonly_preflight.py
+++ b/tests/hermes_state/test_hermes_state_readonly_preflight.py
@@ -143,8 +143,30 @@ class TestRefusalOutsideScope:
class TestSkips:
+ def test_sidecars_unlinked_by_sibling_close_mid_preflight_are_not_readonly(
+ self, hermes_home, monkeypatch
+ ):
+ """A sibling connection's last close() unlinks -wal/-shm after the preflight
+ listed them; the vanished sidecar is gone, not read-only (CI flake in
+ test_concurrent_openers_zero_byte_startup_serialization)."""
+ db = hermes_home / "state.db"
+ sibling = sqlite3.connect(str(db))
+ sibling.execute("PRAGMA journal_mode=WAL")
+ sibling.execute("CREATE TABLE t (x)")
+ sibling.commit()
+ wal = db.with_name(db.name + "-wal")
+ assert wal.is_file(), "fixture precondition: the live sibling holds a -wal"
+ real_access = os.access
+ open_sibling = [sibling]
+ def access_racing_sibling_close(path, mode, *args, **kwargs):
+ if Path(path) == wal and open_sibling:
+ open_sibling.pop().close() # last connection: SQLite unlinks -wal/-shm
+ return real_access(path, mode, *args, **kwargs)
+ monkeypatch.setattr(os, "access", access_racing_sibling_close)
+ preflight_db_writability(db, db_label="state.db")
+ assert not open_sibling and not wal.exists()
def test_healthy_db_untouched(self, hermes_home):
db = hermes_home / "state.db"