Files
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

180 lines
7.7 KiB
Python

"""Live-DB test-isolation guard and the per-process "last init error" record.
Every store holder resolves its path through _ensure_test_isolation
so a pytest-context process (env OR ancestry) can never open a production
state.db; env-based so subprocess children are protected too."""
import os
import sys
import threading
import weakref
from pathlib import Path
from typing import Any, Optional
try: # Hard dependency, but tolerate scaffold-phase imports before pip install.
import psutil
except ImportError: # pragma: no cover - stripped/scaffold installs only
psutil = None # type: ignore[assignment]
# Field evidence: pytest fixture rows landed in the production state.db and a
# pytest-spawned child flipped the journal mode under the live WAL writer.
#: Env twin of ``_STATE_DB_GUARD_BYPASS`` for child processes (a module global
#: cannot cross a process boundary, and ancestry arms the guard there).
_STATE_DB_GUARD_BYPASS_ENV = "HERMES_STATE_DB_GUARD_BYPASS"
def _real_platform_state_root() -> Optional[Path]:
"""The REAL platform-default Hermes root. Avoids ``Path.home()`` /
``hermes_constants`` (tests monkeypatch Path.home to a tempdir); ``expanduser``
reads HOME/passwd, which the conftest never rewrites."""
try:
home = Path(os.path.expanduser("~"))
if sys.platform == "win32":
base = os.environ.get("LOCALAPPDATA", "").strip()
root = Path(base) / "hermes" if base else home / "AppData" / "Local" / "hermes"
else:
root = home / ".hermes"
return root.resolve()
except Exception:
return None
#: Exported by the hermetic conftest alongside the HERMES_HOME redirect. Unlike
#: PYTEST_* it is OURS and inherits by default, so a child carrying it that
#: resolves a production DB is by definition an isolation escape.
# : Env marker exported by the hermetic test conftest at the same moment it : redirects ``HERMES_HOME`` to
# the per-session tmp isolation root. Unlike ``PYTEST_*`` (owned by pytest, and : routinely scrubbed by
# tests that rebuild a child environment), this marker : is OURS: it declares "this process tree is running
# under Hermes test : isolation", and it inherits into subprocess children by default — so a : child that
# received the patched ``HERMES_HOME`` also received the marker, : and a child that resolves a production DB
# while carrying it is, by : definition, an isolation escape (#82770).
_TEST_ISOLATION_MARKER_ENV = "HERMES_TEST_ISOLATION"
def _running_under_pytest() -> bool:
"""True when this process (or a parent test process) is a pytest run."""
return bool(
os.environ.get("PYTEST_CURRENT_TEST")
or os.environ.get("PYTEST_VERSION")
or os.environ.get(_TEST_ISOLATION_MARKER_ENV)
)
#: pytest launcher names, matched against each argv token's *basename* so
#: ``/tmp/pytest-of-dev/...`` paths cannot false-positive.
_PYTEST_LAUNCHER_NAMES = frozenset({"pytest", "py.test", "pytest.exe", "py.test.exe"})
#: The repo's test runner: under its fork server (HERMES_TEST_FORKSERVER) a test
#: process has no ``python -m pytest`` ancestor, only ``python .../run_tests_parallel.py``.
#: It counts only as the script a Python interpreter runs: a prompt or an editor
#: naming the file (``hermes chat -q "fix scripts/run_tests_parallel.py"``) is no test.
_TEST_RUNNER_SCRIPT = "run_tests_parallel.py"
#: Memoised ancestry answer: the tree above us doesn't change; keep the hot path free.
_PYTEST_ANCESTOR: Optional[bool] = None
def _process_looks_like_pytest(proc: Any) -> bool:
"""True when *proc*'s command line is a pytest invocation. Unreadable cmdline
=> not pytest: guessing the other way would refuse production opens."""
try:
cmdline = proc.cmdline() or []
except Exception:
return False
names = []
for arg in cmdline:
try:
# Split on both separators on every host so the answer is platform-independent.
names.append(str(arg).strip('"').strip("'").replace("\\", "/").rsplit("/", 1)[-1].lower())
except Exception:
names.append("")
if any(name in _PYTEST_LAUNCHER_NAMES for name in names):
return True
if not names or not names[0].startswith("python"):
return False
script = next((name for name in names[1:] if not name.startswith("-")), "")
return script == _TEST_RUNNER_SCRIPT
def _has_pytest_ancestor() -> bool:
"""True when an ancestor process is a pytest run: a child spawned with a
rebuilt env loses PYTEST_* and the HERMES_HOME redirect together, ancestry
survives that. Fails open without psutil / on walk errors.
``_running_under_pytest`` reads ``PYTEST_*`` env vars, which a child spawned with a rebuilt environment
loses at the same moment it loses the ``HERMES_HOME`` redirect: that child aims at the production DB
*and* disarms the guard in one step (#82770). Ancestry is the one test-context signal that survives an
env rebuild, so it backs the env check up.
"""
global _PYTEST_ANCESTOR
if _PYTEST_ANCESTOR is not None:
return _PYTEST_ANCESTOR
found = False
if psutil is not None:
try:
found = any(_process_looks_like_pytest(p) for p in psutil.Process().parents())
except Exception:
found = False
_PYTEST_ANCESTOR = found
return found
def _in_test_context() -> bool:
"""Test run by environment or ancestry (memoised; env checked first)."""
return _running_under_pytest() or _has_pytest_ancestor()
def _is_production_state_db(resolved: Path, root: Path) -> bool:
"""*resolved* is ``<root>/state.db`` or ``<root>/profiles/<name>/state.db``;
deeper scratch paths (repo worktrees) are deliberately NOT matched."""
if resolved.parent == root:
return True
try:
parts = resolved.relative_to(root).parts
except ValueError:
return False
return len(parts) == 3 and parts[0] == "profiles"
# Test-only store holder registry. Under the hermetic suite every
# successfully opened store holder is added to this WeakSet so the autouse
# teardown in tests/conftest.py (_close_leaked_session_dbs) can close whatever
# a test forgot to close. Dozens of tests open a store directly and never
# close it; each instance holds a writer connection plus pooled readers, and a
# single-process run over tests/hermes_cli/ accumulated 16-25 GB RSS (OOM
# incident 20260816). The per-file runner masks this in CI; the registry fixes
# the class at the source instead of patching ~40 test files.
#
# Population is gated on the isolation marker (exported by tests/conftest.py
# before any test module imports). Production processes never populate it;
# do not "simplify" the gate away. WeakSet membership never pins an instance.
_test_instance_registry: weakref.WeakSet[Any] = weakref.WeakSet()
def _register_test_instance(db: Any) -> None:
"""Track *db* for suite-level teardown closing (test-isolation runs only)."""
if os.environ.get(_TEST_ISOLATION_MARKER_ENV):
try:
_test_instance_registry.add(db)
except Exception: # pragma: no cover — registry must never break init
pass
# Last state.db init error, per-process, as ``_set_last_init_error`` records it;
# ``get_last_init_error`` reads it.
_last_init_error: Optional[str] = None
_last_init_error_lock = threading.Lock()
def _set_last_init_error(msg: Optional[str]) -> None:
"""Record (or clear with None) the most recent init failure. __init__ never
clears on success: a concurrent open would erase the cause another thread's
/resume is about to format."""
global _last_init_error
with _last_init_error_lock:
_last_init_error = msg
def get_last_init_error() -> Optional[str]:
"""Most recent state.db init failure (None if none/never attempted)."""
return _last_init_error