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

66 lines
2.2 KiB
Python

"""Tests for the per-plugin durable storage convention (plugins/plugin_storage).
The contract under test: data lives under ``<hermes home>/plugin-data/<name>/``
(NOT the ``plugins/<name>/`` install tree), names that could escape the root
are rejected, and the sqlite helper opens a WAL-mode connection inside the
data dir.
"""
from __future__ import annotations
import pytest
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
from plugins.plugin_storage import plugin_data_dir, plugin_db
@pytest.fixture
def hermes_home(tmp_path):
token = set_hermes_home_override(str(tmp_path))
try:
yield tmp_path
finally:
reset_hermes_home_override(token)
def test_data_dir_lives_outside_the_install_tree(hermes_home):
root = plugin_data_dir("my-plugin")
assert root == hermes_home / "plugin-data" / "my-plugin"
assert root.is_dir()
# The invariant that motivated the module: data must not live under the
# install tree that `hermes plugins remove` deletes.
assert (hermes_home / "plugins") not in root.parents
@pytest.mark.parametrize("bad", ["", ".", "..", "../escape", "a/b", "a\\b", "x" * 65])
def test_hostile_names_are_rejected(hermes_home, bad):
with pytest.raises(ValueError):
plugin_data_dir(bad)
def test_plugin_db_journal_mode_is_the_shared_fallback_verdict(hermes_home):
"""Plugin DBs take the journal mode the core WAL helper decides for this SQLite build and
filesystem (WAL normally; DELETE on WAL-reset-bug builds or network FS) — never a raw PRAGMA."""
from sqlite_support import is_sqlite_wal_reset_vulnerable
conn = plugin_db("board")
try:
conn.execute("CREATE TABLE t (x)")
conn.execute("INSERT INTO t VALUES (1)")
conn.commit()
mode = conn.execute("PRAGMA journal_mode").fetchone()[0]
assert mode == ("delete" if is_sqlite_wal_reset_vulnerable() else "wal")
finally:
conn.close()
assert (hermes_home / "plugin-data" / "board" / "data.db").exists()
def test_plugin_db_rejects_path_shaped_filenames(hermes_home):
with pytest.raises(ValueError):
plugin_db("board", filename="../outside.db")
with pytest.raises(ValueError):
plugin_db("board", filename="")