Skip to content

The agent that grows with you

fix(cli): route doctor's journal-mode probe through the pre-open reader

briandevanseae17277215b
diff --git a/hermes_cli/doctor.py b/hermes_cli/doctor.py
index e16de6060c9..6153aa56e31 100644
--- a/hermes_cli/doctor.py
+++ b/hermes_cli/doctor.py
@@ -124,12 +124,25 @@ def _read_journal_mode(db_path: Path) -> tuple[str | None, str | None]:
Header byte 18 is 2 for WAL and 1 for a rollback journal. Opening the
database through the SQLite engine — even read-only — creates -wal/-shm
sidecar files, which a diagnostic must not do.
+
+ The byte read is routed through ``read_header_bytes_preopen`` rather than
+ a bare ``open()``: closing *any* descriptor for a database file cancels
+ this process's POSIX advisory locks on it, so a raw read would drop the
+ locks a live connection is holding (see ``hermes_cli.sqlite_safe_read``).
+ ``run_doctor`` is also called in-process by the dashboard console, which
+ holds live ``SessionDB`` connections. The helper refuses in that case and
+ the mode is reported as unreadable instead.
"""
- try:
- with open(db_path, "rb") as fh:
- header = fh.read(20)
- except OSError as exc:
- return None, str(exc)
+ from hermes_cli.sqlite_safe_read import (
+ has_live_connection,
+ read_header_bytes_preopen,
+ )
+
+ header = read_header_bytes_preopen(db_path, length=20)
+ if header is None:
+ if has_live_connection(db_path):
+ return None, "database is open in this process"
+ return None, "file could not be read"
if len(header) == 0:
return None, "file is empty"
if len(header) < 20 or not header.startswith(_SQLITE_HEADER_MAGIC):