Files
hermes-agent/tests/persistence_sqlite/housekeeping/test_delete_session_write_guards.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

126 lines
6.0 KiB
Python

"""Invariant tests for entry-side deletion refusal on active write guards on a SQLite backend store (#123583): ``delete_session``, ``delete_sessions`` and ``delete_session_if_empty`` against the coordination repository's turn lease and compression lock."""
import os
import pytest
from persistence.sqlite._state.errors import SessionActiveWriteGuardError
from persistence.sqlite.conversation._store.sessions import (
create_session, delete_session, delete_session_if_empty, delete_sessions, end_session, get_session,
)
from tests.contracts._scopes import MEDIA_ROOT
@pytest.fixture
def store(backend, scope_a):
return backend.open(scope_a)
@pytest.fixture
def db(store):
"""The store's StoreHolder, shared with its repositories."""
return store.holder()
def test_delete_session_refuses_when_write_guard_active(db, store):
"""Invariant 1: delete_session(..., exclude_active_write_guards=True) refuses inside
the transaction while an active turn lease or compression lock protects the row."""
create_session(db, "sess-lease", source="test")
create_session(db, "sess-cmp", source="test")
turn_holder = f"pid={os.getpid()}:turn=1"
cmp_holder = f"pid={os.getpid()}:cmp=1"
assert store.coordination.try_acquire_turn_lease(
session_id="sess-lease", holder=turn_holder, ttl_seconds=300.0) is True
assert store.coordination.try_acquire_compression_lock(
session_id="sess-cmp", holder=cmp_holder, ttl_seconds=300.0) is True
# 1. Active turn lease -> raises SessionActiveWriteGuardError and row survives
with pytest.raises(SessionActiveWriteGuardError):
delete_session(db, "sess-lease", exclude_active_write_guards=True, media_root=MEDIA_ROOT)
assert get_session(db, "sess-lease") is not None
# 2. Active compression lock -> raises SessionActiveWriteGuardError and row survives
with pytest.raises(SessionActiveWriteGuardError):
delete_session(db, "sess-cmp", exclude_active_write_guards=True, media_root=MEDIA_ROOT)
assert get_session(db, "sess-cmp") is not None
# 3. Released guards -> deletion succeeds
store.coordination.release_turn_lease(session_id="sess-lease", holder=turn_holder)
store.coordination.release_compression_lock(session_id="sess-cmp", holder=cmp_holder)
assert "sess-lease" in delete_session(db, "sess-lease", exclude_active_write_guards=True, media_root=MEDIA_ROOT)
assert get_session(db, "sess-lease") is None
assert "sess-cmp" in delete_session(db, "sess-cmp", exclude_active_write_guards=True, media_root=MEDIA_ROOT)
assert get_session(db, "sess-cmp") is None
# 4. An idle compression-ended row is a closed parent, not a live write: delete must succeed
# (no CompressionSessionClosedError leaking out of the guard check).
create_session(db, "sess-compressed", source="test")
end_session(db, "sess-compressed", "compression")
assert "sess-compressed" in delete_session(
db, "sess-compressed", exclude_active_write_guards=True, media_root=MEDIA_ROOT)
assert get_session(db, "sess-compressed") is None
def test_delete_sessions_bulk_skips_active_write_guards(db, store):
"""Invariant 2: delete_sessions(..., exclude_active_write_guards=True) atomically
skips rows with active guards and removes only the idle ones."""
create_session(db, "bulk-active", source="test")
create_session(db, "bulk-idle", source="test")
turn_holder = f"pid={os.getpid()}:turn=bulk"
assert store.coordination.try_acquire_turn_lease(
session_id="bulk-active", holder=turn_holder, ttl_seconds=300.0) is True
skipped: list[str] = []
deleted_count = delete_sessions(
db, ["bulk-active", "bulk-idle"], exclude_active_write_guards=True, skipped_ids=skipped,
media_root=MEDIA_ROOT)[0]
assert deleted_count == 1
assert skipped == ["bulk-active"] # reported to the caller, not silently dropped
# Protected row survived; idle row was deleted
assert get_session(db, "bulk-active") is not None
assert get_session(db, "bulk-idle") is None
store.coordination.release_turn_lease(session_id="bulk-active", holder=turn_holder)
# Delegate children cascade with their root, so a guarded child protects the root too:
# single delete refuses, bulk delete skips the root and never cascades the guarded child away.
create_session(db, "deleg-root", source="test")
create_session(
db, "deleg-child", source="test", parent_session_id="deleg-root",
model_config={"_delegate_from": "deleg-root"},
)
child_holder = f"pid={os.getpid()}:turn=child"
assert store.coordination.try_acquire_turn_lease(
session_id="deleg-child", holder=child_holder, ttl_seconds=300.0) is True
with pytest.raises(SessionActiveWriteGuardError):
delete_session(db, "deleg-root", exclude_active_write_guards=True, media_root=MEDIA_ROOT)
skipped = []
assert delete_sessions(
db, ["deleg-root"], exclude_active_write_guards=True, skipped_ids=skipped, media_root=MEDIA_ROOT)[0] == 0
assert skipped == ["deleg-root"]
assert get_session(db, "deleg-root") is not None and get_session(db, "deleg-child") is not None
store.coordination.release_turn_lease(session_id="deleg-child", holder=child_holder)
def test_delete_session_if_empty_refuses_when_write_guard_active(db, store):
"""delete_session_if_empty shares the guarded-delete refusal: the emptiness predicate
reads committed state, so a row whose first turn is leased but not yet flushed would
be deleted mid-turn (#123583)."""
create_session(db, "empty-live", source="test")
turn_holder = f"pid={os.getpid()}:turn=1"
assert store.coordination.try_acquire_turn_lease(
session_id="empty-live", holder=turn_holder, ttl_seconds=300.0) is True
assert delete_session_if_empty(db, "empty-live", media_root=MEDIA_ROOT) == ()
assert get_session(db, "empty-live") is not None
store.coordination.release_turn_lease(session_id="empty-live", holder=turn_holder)
assert "empty-live" in delete_session_if_empty(db, "empty-live", media_root=MEDIA_ROOT)
assert get_session(db, "empty-live") is None