hermes_state_guard.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """Live-DB test-isolation guard and the per-process "last init error" record.
  2. Every SessionDB construction resolves its path through _ensure_test_isolation
  3. so a pytest-context process (env OR ancestry) can never open a production
  4. state.db; env-based so subprocess children are protected too."""
  5. import os
  6. import sys
  7. import threading
  8. import weakref
  9. from pathlib import Path
  10. from typing import Any, Optional
  11. try: # Hard dependency, but tolerate scaffold-phase imports before pip install.
  12. import psutil
  13. except ImportError: # pragma: no cover - stripped/scaffold installs only
  14. psutil = None # type: ignore[assignment]
  15. # Field evidence: pytest fixture rows landed in the production state.db and a
  16. # pytest-spawned child flipped the journal mode under the live WAL writer.
  17. #: Env twin of ``_STATE_DB_GUARD_BYPASS`` for child processes (a module global
  18. #: cannot cross a process boundary, and ancestry arms the guard there).
  19. _STATE_DB_GUARD_BYPASS_ENV = "HERMES_STATE_DB_GUARD_BYPASS"
  20. def _real_platform_state_root() -> Optional[Path]:
  21. """The REAL platform-default Hermes root. Avoids ``Path.home()`` /
  22. ``hermes_constants`` (tests monkeypatch Path.home to a tempdir); ``expanduser``
  23. reads HOME/passwd, which the conftest never rewrites."""
  24. try:
  25. home = Path(os.path.expanduser("~"))
  26. if sys.platform == "win32":
  27. base = os.environ.get("LOCALAPPDATA", "").strip()
  28. root = Path(base) / "hermes" if base else home / "AppData" / "Local" / "hermes"
  29. else:
  30. root = home / ".hermes"
  31. return root.resolve()
  32. except Exception:
  33. return None
  34. #: Exported by the hermetic conftest alongside the HERMES_HOME redirect. Unlike
  35. #: PYTEST_* it is OURS and inherits by default, so a child carrying it that
  36. #: resolves a production DB is by definition an isolation escape.
  37. # : Env marker exported by the hermetic test conftest at the same moment it : redirects ``HERMES_HOME`` to
  38. # the per-session tmp isolation root. Unlike ``PYTEST_*`` (owned by pytest, and : routinely scrubbed by
  39. # tests that rebuild a child environment), this marker : is OURS: it declares "this process tree is running
  40. # under Hermes test : isolation", and it inherits into subprocess children by default — so a : child that
  41. # received the patched ``HERMES_HOME`` also received the marker, : and a child that resolves a production DB
  42. # while carrying it is, by : definition, an isolation escape (#82770).
  43. _TEST_ISOLATION_MARKER_ENV = "HERMES_TEST_ISOLATION"
  44. def _running_under_pytest() -> bool:
  45. """True when this process (or a parent test process) is a pytest run."""
  46. return bool(
  47. os.environ.get("PYTEST_CURRENT_TEST")
  48. or os.environ.get("PYTEST_VERSION")
  49. or os.environ.get(_TEST_ISOLATION_MARKER_ENV)
  50. )
  51. #: pytest launcher names, matched against each argv token's *basename* so
  52. #: ``/tmp/pytest-of-dev/...`` paths cannot false-positive.
  53. _PYTEST_LAUNCHER_NAMES = frozenset({"pytest", "py.test", "pytest.exe", "py.test.exe"})
  54. #: The repo's test runner: under its fork server (HERMES_TEST_FORKSERVER) a test
  55. #: process has no ``python -m pytest`` ancestor, only ``python .../run_tests_parallel.py``.
  56. #: It counts only as the script a Python interpreter runs: a prompt or an editor
  57. #: naming the file (``hermes chat -q "fix scripts/run_tests_parallel.py"``) is no test.
  58. _TEST_RUNNER_SCRIPT = "run_tests_parallel.py"
  59. #: Memoised ancestry answer: the tree above us doesn't change; keep the hot path free.
  60. _PYTEST_ANCESTOR: Optional[bool] = None
  61. def _process_looks_like_pytest(proc: Any) -> bool:
  62. """True when *proc*'s command line is a pytest invocation. Unreadable cmdline
  63. => not pytest: guessing the other way would refuse production opens."""
  64. try:
  65. cmdline = proc.cmdline() or []
  66. except Exception:
  67. return False
  68. names = []
  69. for arg in cmdline:
  70. try:
  71. # Split on both separators on every host so the answer is platform-independent.
  72. names.append(str(arg).strip('"').strip("'").replace("\\", "/").rsplit("/", 1)[-1].lower())
  73. except Exception:
  74. names.append("")
  75. if any(name in _PYTEST_LAUNCHER_NAMES for name in names):
  76. return True
  77. if not names or not names[0].startswith("python"):
  78. return False
  79. script = next((name for name in names[1:] if not name.startswith("-")), "")
  80. return script == _TEST_RUNNER_SCRIPT
  81. def _has_pytest_ancestor() -> bool:
  82. """True when an ancestor process is a pytest run: a child spawned with a
  83. rebuilt env loses PYTEST_* and the HERMES_HOME redirect together, ancestry
  84. survives that. Fails open without psutil / on walk errors.
  85. ``_running_under_pytest`` reads ``PYTEST_*`` env vars, which a child spawned with a rebuilt environment
  86. loses at the same moment it loses the ``HERMES_HOME`` redirect: that child aims at the production DB
  87. *and* disarms the guard in one step (#82770). Ancestry is the one test-context signal that survives an
  88. env rebuild, so it backs the env check up.
  89. """
  90. global _PYTEST_ANCESTOR
  91. if _PYTEST_ANCESTOR is not None:
  92. return _PYTEST_ANCESTOR
  93. found = False
  94. if psutil is not None:
  95. try:
  96. found = any(_process_looks_like_pytest(p) for p in psutil.Process().parents())
  97. except Exception:
  98. found = False
  99. _PYTEST_ANCESTOR = found
  100. return found
  101. def _in_test_context() -> bool:
  102. """Test run by environment or ancestry (memoised; env checked first)."""
  103. return _running_under_pytest() or _has_pytest_ancestor()
  104. def _is_production_state_db(resolved: Path, root: Path) -> bool:
  105. """*resolved* is ``<root>/state.db`` or ``<root>/profiles/<name>/state.db``;
  106. deeper scratch paths (repo worktrees) are deliberately NOT matched."""
  107. if resolved.parent == root:
  108. return True
  109. try:
  110. parts = resolved.relative_to(root).parts
  111. except ValueError:
  112. return False
  113. return len(parts) == 3 and parts[0] == "profiles"
  114. # Test-only SessionDB instance registry. Under the hermetic suite every
  115. # successfully constructed SessionDB is added to this WeakSet so the autouse
  116. # teardown in tests/conftest.py (_close_leaked_session_dbs) can close whatever
  117. # a test forgot to close. Dozens of tests build SessionDB() directly and never
  118. # close it; each instance holds a writer connection plus pooled readers, and a
  119. # single-process run over tests/hermes_cli/ accumulated 16-25 GB RSS (OOM
  120. # incident 20260816). The per-file runner masks this in CI; the registry fixes
  121. # the class at the source instead of patching ~40 test files.
  122. #
  123. # Population is gated on the isolation marker (exported by tests/conftest.py
  124. # before any test module imports). Production processes never populate it;
  125. # do not "simplify" the gate away. WeakSet membership never pins an instance.
  126. _test_instance_registry: weakref.WeakSet[Any] = weakref.WeakSet()
  127. def _register_test_instance(db: Any) -> None:
  128. """Track *db* for suite-level teardown closing (test-isolation runs only)."""
  129. if os.environ.get(_TEST_ISOLATION_MARKER_ENV):
  130. try:
  131. _test_instance_registry.add(db)
  132. except Exception: # pragma: no cover — registry must never break init
  133. pass
  134. # Last SessionDB() init error, per-process; surfaced by /resume-style slash
  135. # commands so users know WHY. Only SessionDB.__init__ writes it.
  136. _last_init_error: Optional[str] = None
  137. _last_init_error_lock = threading.Lock()
  138. def _set_last_init_error(msg: Optional[str]) -> None:
  139. """Record (or clear with None) the most recent init failure. __init__ never
  140. clears on success: a concurrent open would erase the cause another thread's
  141. /resume is about to format."""
  142. global _last_init_error
  143. with _last_init_error_lock:
  144. _last_init_error = msg
  145. def get_last_init_error() -> Optional[str]:
  146. """Most recent state.db init failure (None if none/never attempted)."""
  147. return _last_init_error