Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

1.8 KB
Raw Blame
"""Read-only authorization identity projection for encrypted user databases.

Session and socket identity validation use these projections to fence
identities issued before a password change.

The durable token, pending/committed epoch, and fencing state machine lives
in ``rekey_lock``. These projections read without taking its sidecar lock;
its pending-first reads prevent a concurrent commit and pending cleanup
from exposing a superseded epoch as a stable authorization identity.
"""

from pathlib import Path

from .rekey_lock import RekeyRecoveryRequiredError, read_rekey_epoch_state


def read_authorization_epoch(db_path: Path) -> str:
    """Project the epoch that may authorize identities without repairing state."""
    state = read_rekey_epoch_state(db_path)
    committed_epoch = state.committed_epoch
    if committed_epoch is None or (
        state.pending_epoch is not None
        and state.pending_epoch != committed_epoch
    ):
        raise RekeyRecoveryRequiredError(db_path)
    return committed_epoch


def read_user_authorization_epoch(username: str) -> str:
    """Resolve a user's database path and project its authorization epoch.

    The path MUST be resolved through ``db_manager`` (lazy-imported to
    keep this module's import graph shallow): the manager may carry a
    pinned ``data_dir`` override while ``get_data_directory()`` resolves
    dynamically from the environment, and epochs are written beside the
    databases under the MANAGER's resolution. Reading via the env-only
    path projected a different directory whenever the two diverged
    (any process that pinned the override), which would fence perfectly
    healthy sessions with ``RekeyRecoveryRequiredError``.
    """
    from .encrypted_db import db_manager

    db_path = db_manager._get_user_db_path(username)
    return read_authorization_epoch(db_path)