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.
256 lines
10 KiB
Python
256 lines
10 KiB
Python
"""The assembled SQLite backend: handles bound to one profile, one registry holder per file, read-only assemblies,
|
|
sweep recovery, error translation and the executor behind the awaitable form."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import contextvars
|
|
import inspect
|
|
import os
|
|
import shutil
|
|
import sqlite3
|
|
import threading
|
|
|
|
import pytest
|
|
|
|
from persistence import (
|
|
CorruptStoreError, MalformedSchema, PermanentStoreError, ProfileScope, SearchIndexCorrupt, StoreContention,
|
|
StoreQuarantined, StoreReplaced, StoreUnavailable, TransientStoreError,
|
|
)
|
|
from persistence.sqlite import SQLiteBackend, translated
|
|
from persistence.sqlite import _registry
|
|
from hermes_constants import get_hermes_home
|
|
from persistence.sqlite._state.errors import StateDbCorruptError, StateDbReplacedError
|
|
from tests.persistence_sqlite.conftest import store_for
|
|
|
|
|
|
def test_a_profile_the_backend_holds_no_store_for_is_refused(backend):
|
|
foreign = ProfileScope(profile_dir="/contract-homes/elsewhere", name="elsewhere")
|
|
with pytest.raises(ValueError):
|
|
backend.open(foreign)
|
|
assert _registry.stats()["live_generations"] == 0
|
|
|
|
|
|
def test_opening_a_handle_touches_no_file(backend, scope_a):
|
|
store = backend.open(scope_a)
|
|
assert store.scope == scope_a and store.store_path == store_for(scope_a)
|
|
assert not store_for(scope_a).exists()
|
|
assert _registry.stats()["live_generations"] == 0
|
|
|
|
|
|
def test_verbs_take_no_scope(backend, scope_a):
|
|
store = backend.open(scope_a)
|
|
for repository in (store.state_meta, store.health):
|
|
for name, verb in inspect.getmembers(repository, inspect.ismethod):
|
|
if not name.startswith("_"):
|
|
assert "scope" not in inspect.signature(verb).parameters, name
|
|
|
|
|
|
def test_each_profile_has_its_own_file(backend, scope_a, scope_b):
|
|
backend.open(scope_a).state_meta.set("k", "a")
|
|
assert store_for(scope_a).exists() and not store_for(scope_b).exists()
|
|
row = sqlite3.connect(store_for(scope_a)).execute("SELECT value FROM state_meta WHERE key = 'k'").fetchone()
|
|
assert row == ("a",)
|
|
|
|
|
|
def test_handles_and_backends_on_one_file_share_the_registry_holder(scope_a, scope_b, backend):
|
|
other = SQLiteBackend({scope_a.hermes_home_key: store_for(scope_a)})
|
|
try:
|
|
first, second = backend.open(scope_a), backend.open(scope_a)
|
|
first.state_meta.set("k", "1")
|
|
other.open(scope_a).state_meta.set("k", "2")
|
|
assert first.holder() is second.holder() is other.open(scope_a).holder()
|
|
assert _registry.stats() == {"live_generations": 1, "retired_generations": 0, "total_refcounts": 2}
|
|
assert second.state_meta.get("k") == "2"
|
|
finally:
|
|
other.close()
|
|
assert _registry.stats()["total_refcounts"] == 1
|
|
backend.close()
|
|
assert _registry.stats() == {"live_generations": 0, "retired_generations": 0, "total_refcounts": 0}
|
|
|
|
|
|
def test_reopen_drops_the_reference_and_keeps_the_data(backend, scope_a):
|
|
store = backend.open(scope_a)
|
|
store.state_meta.set("k", "kept")
|
|
backend.reopen()
|
|
assert _registry.stats()["live_generations"] == 0
|
|
assert store.state_meta.get("k") == "kept"
|
|
|
|
|
|
def test_a_swept_holder_is_acquired_again(backend, scope_a):
|
|
store = backend.open(scope_a)
|
|
store.state_meta.set("k", "before")
|
|
swept = store.holder()
|
|
assert _registry.close_all_under(store_for(scope_a).parent) == 1
|
|
store.state_meta.set("k", "after")
|
|
assert store.holder() is not swept
|
|
assert store.state_meta.get("k") == "after"
|
|
|
|
|
|
@pytest.mark.platforms("posix")
|
|
def test_a_handle_opened_after_its_file_is_replaced_writes_the_replacement_while_older_handles_stay_fenced(backend, scope_a):
|
|
old = backend.open(scope_a)
|
|
old.state_meta.set("k", "original")
|
|
path = store_for(scope_a)
|
|
copy = path.with_name("replacement.db")
|
|
shutil.copyfile(path, copy)
|
|
os.replace(copy, path)
|
|
new = backend.open(scope_a)
|
|
new.state_meta.set("k", "new")
|
|
assert new.state_meta.get("k") == "new"
|
|
with pytest.raises(StoreReplaced):
|
|
old.state_meta.set("k", "old")
|
|
assert sqlite3.connect(path).execute("SELECT value FROM state_meta WHERE key = 'k'").fetchone() == ("new",)
|
|
backend.close()
|
|
assert _registry.stats() == {"live_generations": 0, "retired_generations": 0, "total_refcounts": 0}
|
|
|
|
|
|
@pytest.mark.parametrize("read_only", [False, True])
|
|
def test_a_store_opens_with_its_own_profiles_database_settings_whichever_profile_the_caller_runs_as(backend, scope_a, read_only):
|
|
(get_hermes_home() / "config.yaml").write_text("database:\n synchronous: OFF\n cache_size: -1111\n")
|
|
(store_for(scope_a).parent / "config.yaml").write_text("database:\n synchronous: EXTRA\n cache_size: -2222\n")
|
|
backend.open(scope_a).state_meta.set("k", "v")
|
|
if read_only:
|
|
backend.close()
|
|
backend = SQLiteBackend({scope_a.hermes_home_key: store_for(scope_a)}, read_only=True)
|
|
try:
|
|
conn = backend.open(scope_a).holder()._conn
|
|
assert conn.execute("PRAGMA synchronous").fetchone()[0] == 3
|
|
assert conn.execute("PRAGMA cache_size").fetchone()[0] == -2222
|
|
finally:
|
|
backend.close()
|
|
|
|
|
|
def test_a_store_in_a_folder_without_its_own_settings_opens_without_setting_the_folder_up_as_a_home(backend, scope_a):
|
|
folder = store_for(scope_a).parent
|
|
before = set(os.listdir(folder))
|
|
backend.open(scope_a).state_meta.set("k", "v")
|
|
backend.close()
|
|
added = set(os.listdir(folder)) - before
|
|
assert not added & {"sessions", "cron", "logs", "memories", "skills", "SOUL.md"}, sorted(added)
|
|
|
|
|
|
def test_close_refuses_further_use(backend, scope_a):
|
|
store = backend.open(scope_a)
|
|
store.state_meta.set("k", "v")
|
|
backend.close()
|
|
with pytest.raises(RuntimeError):
|
|
store.state_meta.get("k")
|
|
with pytest.raises(RuntimeError):
|
|
backend.open(scope_a)
|
|
|
|
|
|
def test_read_only_backend_reads_refuses_writes_and_never_creates(backend, scope_a, scope_b):
|
|
backend.open(scope_a).state_meta.set("k", "v")
|
|
backend.close()
|
|
reader = SQLiteBackend({s.hermes_home_key: store_for(s) for s in (scope_a, scope_b)}, read_only=True)
|
|
try:
|
|
store_a = reader.open(scope_a)
|
|
assert store_a.read_only and store_a.state_meta.get("k") == "v"
|
|
with pytest.raises(PermanentStoreError):
|
|
store_a.state_meta.set("k", "w")
|
|
assert store_a.state_meta.get("k") == "v"
|
|
with pytest.raises(StoreUnavailable):
|
|
reader.open(scope_b).state_meta.get("k")
|
|
assert not store_for(scope_b).exists()
|
|
assert _registry.stats()["live_generations"] == 0
|
|
finally:
|
|
reader.close()
|
|
|
|
|
|
def test_list_prefix_answers_the_store_like_rows_in_table_order(backend, scope_a):
|
|
# The store's LIKE matches 'LOOP:x' for 'loop:' too, and the rows come back in the order they were stored.
|
|
repository = backend.open(scope_a).state_meta
|
|
for key in ("loop:b", "LOOP:x", "loop:a", "loopy"):
|
|
repository.set(key, key.upper())
|
|
repository.set("loop:b", "again")
|
|
assert [entry.key for entry in repository.list_prefix("loop:")] == ["loop:b", "LOOP:x", "loop:a"]
|
|
|
|
|
|
@pytest.mark.parametrize(("raised", "expected"), [
|
|
(StateDbReplacedError("replaced"), StoreReplaced),
|
|
(StateDbCorruptError("corrupt"), StoreQuarantined),
|
|
(sqlite3.OperationalError("database is locked"), StoreContention),
|
|
(sqlite3.OperationalError("attempt to write a readonly database"), PermanentStoreError),
|
|
(sqlite3.OperationalError("unable to open database file"), StoreUnavailable),
|
|
(sqlite3.OperationalError("disk I/O error"), TransientStoreError),
|
|
(sqlite3.DatabaseError("fts5: corruption found reading blob 7 from table \"messages_fts\""), SearchIndexCorrupt),
|
|
(sqlite3.DatabaseError("malformed database schema (idx_sessions) - near \"x\": syntax error"), MalformedSchema),
|
|
(sqlite3.DatabaseError("database disk image is malformed"), CorruptStoreError),
|
|
])
|
|
def test_store_errors_cross_the_boundary_typed(raised, expected):
|
|
with pytest.raises(expected) as caught:
|
|
with translated():
|
|
raise raised
|
|
# CONTRACT: each SQLite failure crosses as its own contract class, not a parent: a lost branch falls to a wider one.
|
|
assert type(caught.value) is expected
|
|
assert caught.value.__cause__ is raised
|
|
|
|
|
|
def test_value_errors_pass_through_untranslated():
|
|
with pytest.raises(ValueError) as caught:
|
|
with translated():
|
|
raise ValueError("bad input")
|
|
assert type(caught.value) is ValueError
|
|
|
|
|
|
_marker: contextvars.ContextVar[str] = contextvars.ContextVar("marker", default="unset")
|
|
|
|
|
|
def test_awaitable_form_runs_on_the_backend_executor_with_the_callers_context(backend, scope_a, monkeypatch):
|
|
store = backend.open(scope_a)
|
|
store.state_meta.set("k", "v")
|
|
holder, seen = store.holder(), []
|
|
real_get_meta = holder.get_meta
|
|
|
|
def get_meta(key):
|
|
seen.append((threading.current_thread().name, threading.get_ident(), _marker.get()))
|
|
return real_get_meta(key)
|
|
|
|
monkeypatch.setattr(holder, "get_meta", get_meta)
|
|
|
|
async def call():
|
|
_marker.set("caller")
|
|
return threading.get_ident(), await store.aio.state_meta.get("k")
|
|
|
|
loop_thread, value = asyncio.run(call())
|
|
[(name, ident, marker)] = seen
|
|
assert name.startswith("hermes-sqlite-store") and ident != loop_thread
|
|
assert (marker, value) == ("caller", "v")
|
|
assert _marker.get() == "unset"
|
|
|
|
|
|
def test_awaitable_form_returns_the_verbs_exception(backend, scope_a):
|
|
backend.open(scope_a).state_meta.set("k", "v")
|
|
backend.close()
|
|
reader = SQLiteBackend({scope_a.hermes_home_key: store_for(scope_a)}, read_only=True)
|
|
try:
|
|
store = reader.open(scope_a)
|
|
with pytest.raises(ValueError):
|
|
asyncio.run(store.aio.state_meta.get(""))
|
|
with pytest.raises(PermanentStoreError) as refused:
|
|
asyncio.run(store.aio.state_meta.set("k", "w"))
|
|
assert isinstance(refused.value.__cause__, sqlite3.OperationalError)
|
|
finally:
|
|
reader.close()
|
|
|
|
|
|
def test_awaitable_calls_share_one_bounded_pool(backend, scope_a, monkeypatch):
|
|
store = backend.open(scope_a)
|
|
store.state_meta.set("k", "v")
|
|
holder, names = store.holder(), set()
|
|
real_get_meta = holder.get_meta
|
|
|
|
def get_meta(key):
|
|
names.add(threading.current_thread().name)
|
|
return real_get_meta(key)
|
|
|
|
monkeypatch.setattr(holder, "get_meta", get_meta)
|
|
|
|
async def burst():
|
|
return await asyncio.gather(*(store.aio.state_meta.get("k") for _ in range(64)))
|
|
|
|
assert asyncio.run(burst()) == ["v"] * 64
|
|
assert names and all(name.startswith("hermes-sqlite-store") for name in names)
|
|
assert len(names) <= min(32, (os.cpu_count() or 1) + 4)
|