Files
hermes-agent/persistence/sqlite/services.py
yoniebans be3d6affc8 refactor(state)!: remove the old session store
hermes_state.py becomes the stand-in plugins import, the rest of the legacy store and its test folder go, and the per-process modules the backend imported from main move under persistence/sqlite/_state/. The backend checks PID namespaces and lease holders through process_identity and its WAL setup through sqlite_support, and its error module takes the shared predicates and classification from storage_errors, so neither helper set has a second copy. The backend, sqlite_support.py and the tests that pin those modules reach their final imports, and the backend-import guard allows no file outside persistence/. The modules no caller imports any more go with it: the agent's onboarding rules (now in session_rules) and gateway/session_admission.py.

The stand-in's SessionDB releases its store on close() and, used again afterwards, opens it again with a warning, as the old class reopened its connection, unless the file at its path was replaced or the store is latched corrupt; a read-only one raises. A close during a call releases the store when the call returns. A named source wins over a source list in its session listing and count.

An agent given a writable stand-in SessionDB as session_db= and no persistence= takes the store handle that SessionDB forwards to as its persistence, so its turn flush, token counts, compression and session row writes go through the storage interface, as an agent given the old SessionDB wrote through it.

The backend-import guard also holds the backend's remaining reads of the process or task home, each with its reason, and refuses any other and any import of the stand-in inside persistence/sqlite/.

Plugin context engines get the agent's store as persistence= beside the deprecated session_db=, in each hook whose signature accepts that keyword, including the rebind after /new, /resume and /branch, and the context engine guide and the plugin guide's replacement table name it. A test fails once the running release reaches the stand-in's removal release, listing what goes with the stand-in.

release_unheld_media and collect_unheld_api_images take a store only: their SessionDB branches, which no caller reaches, go with the gateway's own copies of the admission and transcript queries.
2026-11-17 19:10:57 +01:00

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 persistence.sqlite._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 persistence.sqlite._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))