Files
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

65 lines
3.1 KiB
Python

"""The legacy store's refusals raised as the contract's error family."""
from __future__ import annotations
from contextlib import contextmanager
from typing import Iterator, Optional
from persistence.sqlite._state.errors import (
CompressionSessionBusyError, CompressionSessionClosedError, HeldRowRewrittenError, PruneRowStaleError,
PruneRowUnresolvedError, SessionActiveWriteGuardError, SessionCompressionInProgressError,
SessionTurnLeaseLostError,
)
from persistence import (
AuthorityRefusal, CompressionBusy, CompressionClosed, CompressionInProgress, ExportTooLarge, HeldRowRewritten,
PruneRowStale, PruneRowUnresolved, SessionWriteGuarded, TurnLeaseLost, UnknownExecution,
)
from persistence.conversation.messages import ResumeTooLarge, RewindTargetUnavailable
from .._backend import translated
from .._holder import StoreHolder
from ._store.compression import get_compression_tip
from ._store.core import SessionExportTooLargeError, SessionResumeTooLargeError
from ._store.rewind import RewindTargetUnavailableError
__all__ = ["refusals"]
@contextmanager
def refusals(holder: Optional[StoreHolder] = None, session_id: Optional[str] = None) -> Iterator[None]:
"""Raise the store's failures (``translated``) and its typed refusals as the contract's classes, chaining the
original. An authority refusal already is the contract's class and passes unchanged, except that a bare
``unknown_execution`` becomes ``UnknownExecution``. ``session_id`` names the session a session-level refusal
reports, and ``holder`` resolves the live continuation a compression-closed refusal carries."""
sid = session_id or ""
try:
with translated():
yield
except AuthorityRefusal as exc:
if type(exc) is AuthorityRefusal and exc.reason == "unknown_execution":
raise UnknownExecution("session", sid) from exc
raise
except SessionCompressionInProgressError as exc:
raise CompressionInProgress(sid, str(exc)) from exc
except CompressionSessionBusyError as exc:
raise CompressionBusy(sid, str(exc)) from exc
except CompressionSessionClosedError as exc:
tip = get_compression_tip(holder, exc.session_id) if holder is not None else exc.session_id
raise CompressionClosed(exc.session_id, tip) from exc
except SessionTurnLeaseLostError as exc:
raise TurnLeaseLost(sid, str(exc)) from exc
except SessionActiveWriteGuardError as exc:
raise SessionWriteGuarded(sid, str(exc)) from exc
except HeldRowRewrittenError as exc:
raise HeldRowRewritten(sid, str(exc)) from exc
except PruneRowStaleError as exc:
raise PruneRowStale(sid, str(exc)) from exc
except PruneRowUnresolvedError as exc:
raise PruneRowUnresolved(sid, str(exc)) from exc
except SessionResumeTooLargeError as exc:
raise ResumeTooLarge(exc.message_count, exc.limit, exc.scope) from exc
except SessionExportTooLargeError as exc:
raise ExportTooLarge(exc.session_id, exc.message_count, exc.limit) from exc
except RewindTargetUnavailableError as exc:
raise RewindTargetUnavailable(str(exc)) from exc