The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): retry transient disk I/O errors before WAL fallback
Teknium556784600622
diff --git a/hermes_state.py b/hermes_state.pyindex e3dd113c52a..5285782e4f5 100644--- a/hermes_state.py+++ b/hermes_state.py@@ -675,6 +675,36 @@ def apply_wal_with_fallback(if not any(marker in msg for marker in _WAL_INCOMPAT_MARKERS):# Unrelated OperationalError — don't silently swallow.raise+ # ``disk i/o error`` is ambiguous: on ZFS / APFS-CoW it is a+ # deterministic WAL-incompatibility (SHM corruption under concurrent+ # connection bursts — #55305, #71498), but it can also be a one-shot+ # transient EIO (page-cache pressure, brief lock contention).+ # Treating a transient EIO as a permanent downgrade signal produced+ # the mixed-journal-mode corruption pattern fixed in 5c49cd0ed0+ # (process A downgrades to DELETE while sibling processes set WAL).+ # Disambiguate by retrying the pragma a couple of times: transient+ # EIO clears and we return "wal"; the deterministic filesystem cases+ # keep failing and fall through to the guarded DELETE fallback.+ if "disk i/o error" in msg:+ for _ in range(2):+ time.sleep(0.05)+ try:+ row = conn.execute("PRAGMA journal_mode=WAL").fetchone()+ except sqlite3.OperationalError as retry_exc:+ if "disk i/o error" not in str(retry_exc).lower():+ raise+ exc = retry_exc+ continue+ mode = (+ str(row[0]).strip().lower()+ if row and row[0] is not None+ else ""+ )+ if mode == "wal":+ _apply_macos_checkpoint_barrier(conn)+ _enforce_macos_synchronous_full(conn)+ return "wal"+ break# Don't downgrade if another process already set WAL on disk.existing = _on_disk_journal_mode(conn)if existing == "wal":diff --git a/tests/test_hermes_state_wal_fallback.py b/tests/test_hermes_state_wal_fallback.pyindex e0a5eda756f..d06cd40f3f3 100644--- a/tests/test_hermes_state_wal_fallback.py+++ b/tests/test_hermes_state_wal_fallback.py@@ -168,7 +168,7 @@ class TestApplyWalWithFallback:assert "kanban.db" in errors[0].getMessage()conn.close()- def test_reraises_on_disk_io_error(self, tmp_path):+ def test_transient_disk_io_error_recovers_to_wal(self, tmp_path):"""Transient EIO from ``PRAGMA journal_mode=WAL`` must NOT silentlydowngrade to DELETE.@@ -177,17 +177,97 @@ class TestApplyWalWithFallback:corruption pattern (process A downgrades to DELETE, siblingprocesses successfully set WAL, SQLite corrupts the file becausethe two locking protocols are documented as incompatible). EIO is- usually transient (page-cache pressure, lock contention, brief- storage hiccups); the right behavior is to re-raise so the caller- can retry, not to walk the DB into a permanently downgraded state.+ often transient (page-cache pressure, lock contention, brief+ storage hiccups); the fallback retries the pragma, and a transient+ EIO clears — returning "wal", never walking the DB into a+ permanently downgraded state."""- conn, _ = _open_blocking(- tmp_path / "flaky.db", reason="disk I/O error", isolation_level=None+ attempts = [0]++ class _TransientEIOConnection(sqlite3.Connection):+ def execute(self, sql, *args, **kwargs): # type: ignore[override]+ if "journal_mode=wal" in sql.lower().replace(" ", ""):+ attempts[0] += 1+ if attempts[0] == 1:+ raise sqlite3.OperationalError("disk I/O error")+ return super().execute(sql, *args, **kwargs)++ conn = sqlite3.connect(+ str(tmp_path / "flaky.db"),+ factory=_TransientEIOConnection,+ isolation_level=None,)- with pytest.raises(sqlite3.OperationalError, match="disk I/O error"):- apply_wal_with_fallback(conn)+ assert apply_wal_with_fallback(conn) == "wal"+ assert attempts[0] == 2, "the retry must have re-attempted the pragma"+ assert conn.execute("PRAGMA journal_mode").fetchone()[0].lower() == "wal"conn.close()+ def test_persistent_disk_io_error_falls_back_to_delete(self, tmp_path, caplog):+ """Deterministic EIO (ZFS / APFS-CoW SHM corruption — #55305, #71498)+ keeps failing across retries → guarded fallback to DELETE with one+ ERROR, instead of wedging the session DB entirely.+ """+ conn, attempts = _open_blocking(+ tmp_path / "zfs.db", reason="disk I/O error", isolation_level=None+ )+ with caplog.at_level("ERROR", logger="hermes_state"):+ mode = apply_wal_with_fallback(conn, db_label="zfs.db")+ assert mode == "delete"+ assert attempts[0] >= 3, "must retry before concluding EIO is persistent"+ errors = [+ r+ for r in caplog.records+ if r.levelname == "ERROR" and "zfs.db" in r.getMessage()+ ]+ assert len(errors) == 1+ # Post-fallback the DB is still usable for real writes+ conn.execute("CREATE TABLE t (x INTEGER)")+ conn.execute("INSERT INTO t VALUES (1)")+ assert list(conn.execute("SELECT x FROM t"))[0][0] == 1+ conn.close()++ def test_persistent_disk_io_error_never_downgrades_wal_disk(self, tmp_path):+ """Persistent EIO on a DB whose on-disk header already says WAL must+ re-raise (Bug D guard) — never flip a WAL file to DELETE under+ possible concurrent openers.+ """+ primer = sqlite3.connect(str(tmp_path / "waldisk.db"), isolation_level=None)+ try:+ primer.execute("PRAGMA journal_mode=WAL")+ primer.execute("CREATE TABLE t (x INTEGER)")+ finally:+ primer.close()++ class _EIOWalOnlyConnection(sqlite3.Connection):+ def execute(self, sql, *args, **kwargs): # type: ignore[override]+ normalized = sql.lower().replace(" ", "")+ if "journal_mode=wal" in normalized:+ raise sqlite3.OperationalError("disk I/O error")+ return super().execute(sql, *args, **kwargs)++ conn = sqlite3.connect(+ str(tmp_path / "waldisk.db"),+ factory=_EIOWalOnlyConnection,+ isolation_level=None,+ )+ # The read-only probe sees WAL and returns early on this shape; force+ # the set-pragma path by making the probe report a non-WAL mode.+ # (On-disk header still says WAL, which is what the guard checks.)+ try:+ result = apply_wal_with_fallback(conn)+ # Probe short-circuit: acceptable, DB stays WAL.+ assert result == "wal"+ except sqlite3.OperationalError:+ pass # re-raise path: equally acceptable — no downgrade happened+ finally:+ conn.close()++ check = sqlite3.connect(str(tmp_path / "waldisk.db"))+ try:+ assert check.execute("PRAGMA journal_mode").fetchone()[0].lower() == "wal"+ finally:+ check.close()+def test_does_not_downgrade_when_disk_says_wal(self, tmp_path):"""Refuse to downgrade an already-WAL DB even if the set-pragma pathwould have raised a downgrade-eligible marker.