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.

10.8 KB
Raw Blame
import functools
from typing import Any, Callable

from cachetools import LRUCache
from loguru import logger
from sqlalchemy.orm import Session

from ..config.paths import get_data_directory
from ..exceptions import NoUserDatabaseError
from ..database.encrypted_db import db_manager
from .threading_utils import g_thread_local_store, thread_specific_cache
from .request_context import get_current_username

# Database paths using new centralized configuration
DATA_DIR = get_data_directory()
# DB_PATH removed - use per-user encrypted databases instead


def get_db_session(
    _namespace: str = "", username: str | None = None
) -> Session:
    """
    Get database session - uses encrypted per-user database if authenticated.

    Args:
        _namespace: This can be specified to an arbitrary string in order to
                   force the caching mechanism to create separate settings even in
                   the same thread. Usually it does not need to be specified.
        username: Optional username for thread context (e.g., background research threads).
                 If not provided, will be resolved from the request-context contextvar.

    Returns:
        The database session for the current user/context.
    """
    import threading

    if not username:
        # Resolve the authenticated user from the request contextvar.
        # DatabaseMiddleware sets it per request, and Starlette copies the
        # request context into the threadpool workers that run sync route
        # handlers — so this resolves there too, not just on MainThread.
        username = get_current_username()

    # Only refuse callers with NO request context off the main thread:
    # true background threads must use get_user_db_session() (or receive a
    # settings_snapshot). Under Flask the equivalent guard was
    # has_app_context(); the FastAPI port initially refused EVERY
    # non-MainThread caller — but sync route handlers run in the anyio
    # threadpool, so every no-arg settings read from a sync route silently
    # fell back to anonymous defaults instead of the user's settings.
    if not username and threading.current_thread().name != "MainThread":
        thread_id = threading.get_ident()
        raise RuntimeError(
            f"Database access attempted from background thread "
            f"'{threading.current_thread().name}' (ID: {thread_id}) with no "
            f"request context. Use get_user_db_session() or pass all "
            f"required data to the thread at creation time."
        )

    if not username:
        # MainThread without an authenticated user (CLI, startup).
        logger.warning(
            "get_db_session() is deprecated. Use get_user_db_session() from database.session_context"
        )
        return None

    session = _get_cached_user_session(username, _namespace)

    # Keep an owner-thread registry in addition to the bounded shared cache.
    # The cache can evict this thread's entry while the session is still in
    # use, so scanning the cache alone at request teardown is insufficient:
    # an evicted SQLAlchemy Session retains its QueuePool connection until a
    # cyclic-GC pass happens to reclaim it.  The registry lets the worker that
    # acquired the session close it deterministically at its cleanup boundary.
    tracked = getattr(g_thread_local_store, "db_sessions", None)
    if tracked is None:
        tracked = {}
        g_thread_local_store.db_sessions = tracked
    tracked[id(session)] = session
    return session


@thread_specific_cache(cache=LRUCache(maxsize=10))
def _get_cached_user_session(username: str, _namespace: str = "") -> Session:
    """Per-thread session cache, keyed by the RESOLVED username.

    The cache must sit below username resolution: caching at the outer
    call (where username is usually None) would key two different users'
    requests served by the same threadpool worker to one entry, handing
    user B a session opened for user A.

    ``NoUserDatabaseError`` (which ``get_settings_manager`` answers with
    the defaults file) is raised only for ``get_session``'s None: no usable
    engine is cached. A rekey sidecar or epoch fault on a cached engine is
    raised by ``get_session`` itself as a ``RekeyOperationalError`` and
    passes through here untouched, so it never reads as "no database".
    """
    user_session = db_manager.get_session(username)
    if user_session:
        return user_session
    raise NoUserDatabaseError(f"No database found for user {username}")


def cleanup_cached_user_sessions_current_thread() -> int:
    """Close sessions cached or acquired by the current worker thread.

    ``_get_cached_user_session`` uses a process-wide LRU with the worker's
    UUID in each key.  FastAPI reuses those workers across requests, so the
    entries must be removed and closed on the worker itself when a request or
    owned background task finishes.  Sessions evicted from the LRU are also
    closed via the owner-thread registry populated by ``get_db_session``.

    Returns the number of distinct sessions closed.
    """
    thread_key = getattr(g_thread_local_store, "thread_id", None)
    tracked = getattr(g_thread_local_store, "db_sessions", {})
    sessions = dict(tracked)

    cache = _get_cached_user_session.cache
    lock = _get_cached_user_session.cache_lock
    if thread_key is not None:
        # Detach under cachetools' own lock, but close outside it.  Session
        # close can perform driver work and must not serialize unrelated
        # workers' cache access.
        with lock:
            current_keys = [
                key for key in list(cache) if key and key[0] == thread_key
            ]
            for key in current_keys:
                session = cache.pop(key)
                sessions[id(session)] = session

    if hasattr(g_thread_local_store, "db_sessions"):
        del g_thread_local_store.db_sessions

    for session in sessions.values():
        try:
            session.close()
        except Exception:
            # This session has already been dropped from both the LRU
            # cache and the owner-thread registry above, so this is the
            # only place the actual close failure is ever observable — a
            # bare warning with no traceback makes a real driver-level
            # failure indistinguishable from routine noise.
            #
            # logger.exception(), not logger.warning(..., exc_info=True):
            # this module imports raw loguru (see the module imports
            # above), which has no ``exc_info`` parameter, so that kwarg is
            # silently swallowed as a format argument and yields no
            # traceback whatsoever.
            #
            # ``logger.exception()`` DOES attach the traceback — this is
            # plain loguru, not SecureLogger, so nothing gates it on
            # diagnose mode. The traceback does not leave the process: the
            # encrypted-DB sink persists ``_exception_context(record) +
            # record["message"]`` (exception type/value prefix only, never
            # the frames) and the frontend sink forwards only
            # ``record["message"]``; see ``database_sink``,
            # ``_exception_context`` and the frontend sink in
            # ``utilities/log_utils.py``. It reaches the stderr/file sinks,
            # which is the point.
            logger.exception(
                "Failed to close a cached DB session on worker cleanup"
            )

    return len(sessions)


def get_settings_manager(
    db_session: Session | None = None, username: str | None = None
):
    """
    Get the settings manager for the current context.

    Args:
        db_session: Optional database session
        username: Optional username for caching (required for SettingsManager)

    Returns:
        The appropriate settings manager instance.
    """
    # Track whether we are borrowing a caller-provided session we don't own.
    # Borrowed sessions must NOT be closed by SettingsManager — their owner is
    # responsible for cleanup.
    borrowed_session = db_session is not None

    # Resolve the current user from the request-context contextvar when the
    # caller passed neither a session nor a username. Pre-migration this block
    # reused the Flask request's g.db_session and read the username from
    # flask.session; both are gone. get_db_session() below resolves the user
    # via the same contextvar, so there is no Flask request session to borrow.
    if db_session is None and username is None:
        username = get_current_username()

    if db_session is None:
        try:
            db_session = get_db_session(username=username)
        except NoUserDatabaseError:
            # This user has no database, so defaults are the only answer we
            # have. Deliberately NOT `except RuntimeError`: that also caught
            # the background-thread guard above and any failure from inside
            # db_manager.get_session, and answering either of those with
            # anonymous defaults is silently wrong rather than degraded --
            # a worker would read another configuration than the user's, and
            # an unopenable database would read as "every setting is at its
            # default" with nothing logged. Both now propagate. So does a
            # rekey sidecar or epoch fault on the user's cached engine (a
            # RekeyOperationalError from db_manager.get_session, e.g. an
            # fd-exhaustion spike): read as defaults, a stored "strict"
            # egress scope would let a public download through. The app
            # answers it with a retryable 503.
            db_session = None
            username = "anonymous"

    # Import here to avoid circular imports
    from ..settings import SettingsManager

    logger.debug(
        "get_settings_manager: session_source={}, owned={}",
        "borrowed" if borrowed_session else ("new" if db_session else "None"),
        not borrowed_session,
    )

    # Always use regular SettingsManager (now with built-in simple caching)
    return SettingsManager(db_session, owns_session=not borrowed_session)


def no_db_settings(func: Callable[..., Any]) -> Callable[..., Any]:
    """
    Decorator that runs the wrapped function with the settings database
    completely disabled. This will prevent the function from accidentally
    reading settings from the DB. Settings can only be read from environment
    variables or the defaults file.

    Args:
        func: The function to wrap.

    Returns:
        The wrapped function.

    """

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # Temporarily disable DB access in the settings manager.
        manager = get_settings_manager()
        db_session = manager.db_session
        manager.db_session = None

        try:
            return func(*args, **kwargs)
        finally:
            # Restore the original database session.
            manager.db_session = db_session

    return wrapper