The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): repair a read-only state.db directory before taking the quarantine lock
Teknium59b70c943a5f
diff --git a/hermes_state.py b/hermes_state.pyindex cefa7300f46..6cb267045f3 100644--- a/hermes_state.py+++ b/hermes_state.py@@ -683,6 +683,9 @@ class SessionDB(# Serialize zero-byte check, quarantine, connect and schema commit so concurrent# openers don't race the absent-path -> schema-commit window.if not self.db_path.exists() or has_invalid_sqlite_header_preopen(self.db_path):+ # The directory must be writable before the lock file can be created in it; the+ # directory never vanishes mid-quarantine, so checking it unlocked is race-free.+ preflight_db_writability(self.db_path, db_label="state.db", directory_only=True)with quarantine_cross_process_lock(self.db_path) as lock_acquired:if not lock_acquired:logger.warning(diff --git a/hermes_state_repair.py b/hermes_state_repair.pyindex c4f09f68f55..f5075dcd6f9 100644--- a/hermes_state_repair.py+++ b/hermes_state_repair.py@@ -534,7 +534,7 @@ def _backup_db_file(db_path: Path) -> tuple[Optional[Path], Optional[str]]:return None, f"backup copy failed: {exc}"-def preflight_db_writability(db_path: Path, *, db_label: str = "state.db") -> None:+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.A stray read-only ``state.db`` / ``-wal`` / ``-shm`` (sudo run, restored backup, copied dotfiles) otherwise@@ -553,7 +553,8 @@ def preflight_db_writability(db_path: Path, *, db_label: str = "state.db") -> Nohome = Path(get_hermes_home()).resolve()# SQLite needs a writable directory in every journal mode (WAL/SHM sidecars, or the DELETE-mode journal).sidecars = (db_path.with_name(db_path.name + "-wal"), db_path.with_name(db_path.name + "-shm"))- for p, is_dir in [(db_path.parent, True), *((p, False) for p in (db_path, *sidecars) if p.is_file())]:+ 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):continuex = "x" if is_dir else ""diff --git a/tests/hermes_state/test_hermes_state_readonly_preflight.py b/tests/hermes_state/test_hermes_state_readonly_preflight.pyindex 3cba015f001..cc88161d477 100644--- a/tests/hermes_state/test_hermes_state_readonly_preflight.py+++ b/tests/hermes_state/test_hermes_state_readonly_preflight.py@@ -155,6 +155,17 @@ class TestSkips:class TestSessionDBIntegration:+ def test_sessiondb_repairs_readonly_dir_before_fresh_db_lock(self, hermes_home):+ sub = hermes_home / "profiles" / "p1"+ sub.mkdir(parents=True)+ os.chmod(sub, 0o555)+ try:+ db = SessionDB(sub / "state.db") # absent DB: the quarantine lock lives in this dir+ db.close()+ assert os.access(sub, os.W_OK)+ finally:+ os.chmod(sub, 0o755)+def test_sessiondb_selfheals_readonly_db_in_home(self, hermes_home):db_path = hermes_home / "state.db"first = SessionDB(db_path)