The SQLite backend's copies take main's bodies: the session row counts, the runtime-ledger rows probe, the doctor's session count, the write-health snapshot source, the integrity check, the journal-mode read and the journal-mode switch open through hermes_cli.sqlite_safe_read.connect_tracked with tracking_path, so has_live_connection() and offline_file_access() see them before any raw byte probe. The readiness probe opens read_only_db_uri(path) through connect_tracked, so a profile path containing a reserved URI character opens the tracked file. The offline opens main keeps raw (the restore staging copy, the preflight staged copy, the write-health temp snapshot and the downgrade-guard snapshot probe) carry main's HX015 waivers.
61 lines
2.6 KiB
Python
61 lines
2.6 KiB
Python
"""Store services that answer without a healthy store: the corruption latch and the readiness probe.
|
|
|
|
Neither creates, opens writable, migrates or repairs a database; the probe reads the schema through a ``mode=ro``
|
|
connection only when the file already exists.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextlib import closing
|
|
from pathlib import Path
|
|
|
|
from hermes_state_health import STORAGE_CORRUPT, note_storage_error, storage_state
|
|
|
|
from persistence import StorageProbe, StorageState
|
|
|
|
from ._backend import register_repository
|
|
|
|
__all__ = ["SQLiteStorageHealth", "probe_state_db"]
|
|
|
|
|
|
def probe_state_db(path: Path) -> StorageProbe:
|
|
"""Read-only schema probe plus the process-wide corruption latch (``persistence.sqlite._state.health``).
|
|
|
|
The schema read only catches an unreadable header or schema; damage deeper in the file surfaces when a reader
|
|
or writer touches it, and those publish into the latch."""
|
|
if not path.exists():
|
|
return StorageProbe(status="ok", detail="not initialized")
|
|
if storage_state(path) == STORAGE_CORRUPT:
|
|
return StorageProbe(status="degraded", detail=STORAGE_CORRUPT)
|
|
try:
|
|
# ``closing`` is required: sqlite3's context manager only commits or rolls back, never closes.
|
|
from hermes_cli.sqlite_safe_read import connect_tracked # tracked: raw byte probes must see this reader
|
|
from hermes_state_holders import read_only_db_uri
|
|
|
|
with closing(connect_tracked(read_only_db_uri(path), uri=True, timeout=1.0, tracking_path=path)) as conn:
|
|
# A readiness probe must never compete with normal state writers.
|
|
conn.execute("PRAGMA query_only = ON")
|
|
conn.execute("SELECT name FROM sqlite_master LIMIT 1").fetchone()
|
|
return StorageProbe(status="ok", detail=None)
|
|
except Exception as exc: # health: allow BLE001 -- any probe failure is reported, never raised, as the readiness probe does
|
|
if note_storage_error(path, exc):
|
|
return StorageProbe(status="degraded", detail=STORAGE_CORRUPT)
|
|
return StorageProbe(status="degraded", detail=type(exc).__name__)
|
|
|
|
|
|
class SQLiteStorageHealth:
|
|
"""``StorageHealth`` over the process-wide latch, bound to one ``state.db``."""
|
|
|
|
def __init__(self, path: Path) -> None:
|
|
self._path = path
|
|
|
|
def storage_state(self) -> StorageState:
|
|
corrupt = storage_state(self._path) == STORAGE_CORRUPT
|
|
return StorageState.CORRUPT if corrupt else StorageState.OK
|
|
|
|
def probe(self) -> StorageProbe:
|
|
return probe_state_db(self._path)
|
|
|
|
|
|
register_repository("health", lambda store: SQLiteStorageHealth(store.store_path))
|