"""Storage contract for the durable, user-owned session workflow state.""" from __future__ import annotations from typing import Protocol, Sequence from tht.decisions import DecisionInput, DecisionRecord from tht.session.models import PrincipalContext, SessionManifest, SessionSnapshot class SessionRepository(Protocol): """Persistence boundary shared by local files and the server database adapter.""" principal: PrincipalContext def create(self, manifest: SessionManifest) -> SessionSnapshot: ... def get(self, session_id: str) -> SessionSnapshot: ... def save_manifest(self, manifest: SessionManifest) -> SessionSnapshot: ... def read_artifact(self, session_id: str, key: str) -> str | None: ... def write_artifact(self, session_id: str, key: str, content: str) -> None: ... def append_decisions( self, session_id: str, decisions: Sequence[DecisionInput | dict] ) -> list[DecisionRecord]: ... def get_preferences(self) -> dict: ... def set_preferences(self, preferences: dict) -> None: ... def delete(self, session_id: str) -> None: ... def build_session_repository(config, principal: PrincipalContext, *, home=None) -> SessionRepository: """Build the configured private session persistence adapter.""" session_storage = getattr(config, "session_storage", None) if session_storage is not None: from tht.session.postgres_repository import PostgresSessionRepository return PostgresSessionRepository.from_config(session_storage.connection, principal) from tht.config import local_tht_home from tht.session.filesystem_repository import FilesystemSessionRepository workspace = getattr(config, "_workspace_id", "default") return FilesystemSessionRepository(home or local_tht_home(), workspace, principal)