Files
hermes-agent/tests/test_tests_tree_layout.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

88 lines
3.6 KiB
Python

"""The tests/ tree mirrors the source tree.
``scripts/run_tests.sh tests/<dir>/`` is how a change gets its regression
coverage run, so a test filed under a directory that does not correspond to
the code it exercises is a test nobody runs when that code changes. Two
drifts had accumulated: parallel directories for one source package
(``tests/cli`` beside ``tests/hermes_cli``, ``tests/run_agent`` beside
``tests/agent``, ``tests/state`` beside ``tests/hermes_state``) and ~250
loose files at ``tests/`` root that belonged to a package.
Filenames also stopped carrying issue numbers: ``test_89315_x.py`` reads as
noise in a directory listing and the number belongs in the docstring, where
``git log -S`` and a reader can both find it with context.
"""
from __future__ import annotations
import re
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
TESTS_ROOT = REPO_ROOT / "tests"
# tests/<name>/ directories that do not mirror a source directory but are
# legitimate homes: cross-cutting suites, fixtures, and script-family tests.
# "compat": frozen pre-PM updater import surface (tests/compat/README.md) — the
# audited names live only in history, so there is no source package to mirror.
# "installation": python driver for the tests/install shell e2e assets.
# "_fixtures": topic modules tests/conftest.py is split into — suite-wide
# plumbing (env filtering, live-system guard, platform gating), not tests.
# "contracts": the storage contract suites, run against every backend of the
# contracts in persistence/ through one fixture registry.
# "neutral": the backend-neutral helper modules (session_rules and the modules
# beside it that import no application package).
# "persistence_sqlite": the SQLite backend, persistence/sqlite/.
_NON_MIRROR_DIRS = {
"compat", "installation", "_fixtures", "contracts", "neutral", "persistence_sqlite",
"ci", "conformance", "dashboard", "desktop", "docker", "e2e", "evals",
"fakes", "fixtures", "install", "integration", "manual",
"monitoring", "perf_guards", "scripts", "secret_sources",
"security", "skills", "verify", "website", "computer_use",
}
# Root-level modules whose tests sit directly in tests/ (no package to mirror).
_ROOT_MODULE_STEMS = {p.stem for p in REPO_ROOT.glob("*.py")}
_ISSUE_NUMBER = re.compile(r"(^test_\d{4,6}_)|(_\d{4,6}\.py$)")
def _source_dirs() -> set[str]:
return {
p.name
for p in REPO_ROOT.iterdir()
if p.is_dir() and (p / "__init__.py").exists()
}
def test_every_test_directory_mirrors_a_source_directory_or_is_declared():
"""No sibling directory for a source package that already has one."""
source_dirs = _source_dirs()
offenders = sorted(
d.name
for d in TESTS_ROOT.iterdir()
if d.is_dir()
and d.name not in ("__pycache__",)
and d.name not in source_dirs
and d.name not in _NON_MIRROR_DIRS
)
assert not offenders, (
"tests/ directories that mirror no source package: "
f"{offenders}. Put the tests under tests/<source dir>/ (tests/agent, "
"tests/hermes_cli, ...) or, for a genuinely cross-cutting suite, add the "
"name to _NON_MIRROR_DIRS in this file with a reason."
)
def test_no_issue_numbers_in_test_filenames():
"""Issue numbers live in docstrings, where the reader gets context."""
offenders = sorted(
str(p.relative_to(REPO_ROOT))
for p in TESTS_ROOT.rglob("test_*.py")
if _ISSUE_NUMBER.search(p.name)
)
assert not offenders, (
f"Issue numbers in test filenames: {offenders}. Drop the number from the "
"name and cite the issue in the module docstring instead."
)