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.

17.7 KB
Raw Blame
"""
Thread-safe global state management.

Wraps two module-level dicts (`_active_research`, `_termination_flags`)
with accessor functions protected by a single ``threading.RLock``.
All external code should use the accessor functions instead of touching the
dicts directly.
"""

import threading
from contextlib import contextmanager

# ---------------------------------------------------------------------------
# Internal state — never import these directly from other modules
# ---------------------------------------------------------------------------
_active_research: dict[int, dict] = {}
_termination_flags: dict[int, bool] = {}

# A single lock protects both dicts. They have strongly correlated lifecycles
# (entries are created/destroyed together) and operations are fast dict lookups,
# so a single lock is simpler and eliminates any deadlock risk from lock ordering.
_lock = threading.RLock()

# Per-user gate serialising a user's password-change rekey against that user's
# own newly-starting research threads. Deliberately NOT the process-global
# ``_lock`` above: the SQLCipher ``PRAGMA rekey`` can take seconds, and holding
# ``_lock`` across it would freeze EVERY user's research (every log-append /
# progress-update / status read takes ``_lock``). Keyed by username;
# ``check_and_start_research`` acquires the gate for the research's owner, and
# ``change_password`` (web/routers/auth.py) holds it across its "is research
# active?" check + rekey, so a rekey blocks only the SAME user's new-research
# registration. A dedicated small lock guards the dict so gate lookups never
# touch ``_lock`` — keeping the two consistently orderable: a caller always
# takes a gate BEFORE ``_lock``, never the reverse.
#
# INTENTIONALLY NEVER POPPED. This is a per-user mutual-exclusion primitive
# that may be HELD across a multi-second SQLCipher ``PRAGMA rekey``. A gate
# that may be held across a long operation cannot be safely removed from its
# registry: if a logout / idle sweep popped a gate while one tab holds it
# across the rekey, a concurrent same-user ``check_and_start_research`` would
# find no entry, CREATE A SECOND ``threading.Lock`` instance, acquire it
# uncontended, and start a worker that writes the user's DB concurrently with
# the in-flight rekey — defeating the exact exclusion this gate exists to
# provide. Any lookup-then-acquire scheme has this lookup->acquire gap, so the
# only correct policy is to never remove the gate. It also serialises the
# per-user capacity count -> DB claim used by fresh requests and queue replay;
# an ``RLock`` is required because the outer admission section can reach
# ``check_and_start_research``, which takes the same gate before spawning.
# The dict is bounded by the user population (one small lock per distinct
# username), so it does not grow unbounded.


class _ReentrantUserLock:
    """Small ``RLock`` adapter with ``locked()`` on supported Python 3.12.

    Native ``threading.RLock.locked`` was only added in Python 3.14, while the
    project and CI support 3.12. Tracking acquisition depth preserves the
    existing lock-introspection contract without relying on private runtime
    methods.
    """

    def __init__(self) -> None:
        self._lock = threading.RLock()
        self._state_lock = threading.Lock()
        self._depth = 0

    def acquire(self, blocking: bool = True, timeout: float = -1) -> bool:
        if timeout == -1:
            acquired = self._lock.acquire(blocking)
        else:
            acquired = self._lock.acquire(blocking, timeout)
        if acquired:
            with self._state_lock:
                self._depth += 1
        return acquired

    def release(self) -> None:
        self._lock.release()
        with self._state_lock:
            self._depth -= 1

    def locked(self) -> bool:
        with self._state_lock:
            return self._depth > 0

    def __enter__(self):
        self.acquire()
        return self

    def __exit__(self, exc_type, exc_value, traceback) -> None:
        del exc_type, exc_value, traceback
        self.release()


_user_research_start_gates: dict[str, _ReentrantUserLock] = {}
_user_research_start_gates_lock = threading.Lock()


# ===================================================================
# active_research accessors
# ===================================================================


def is_research_active(research_id):
    """Return True if *research_id* is in the active-research dict."""
    with _lock:
        return research_id in _active_research


def get_active_research_ids():
    """Return a list of all active research IDs (snapshot)."""
    with _lock:
        return list(_active_research.keys())


def get_active_research_snapshot(research_id):
    """Return a safe snapshot of an active-research entry, or ``None``.

    The returned dict contains only serialisable fields (``thread`` is
    excluded).  The ``log`` list is shallow-copied — individual entries
    are never mutated after creation, so this is safe.
    """
    with _lock:
        entry = _active_research.get(research_id)
        if entry is None:
            return None
        return {
            "progress": entry.get("progress", 0),
            "status": entry.get("status"),
            "log": list(entry.get("log", [])),
            "settings": dict(s)
            if (s := entry.get("settings")) is not None
            else None,
        }


def get_research_field(research_id, field, default=None):
    """Return a single field from an active-research entry.

    For mutable fields (``list``, ``dict``) a shallow copy is returned so
    callers cannot accidentally mutate shared state.
    """
    with _lock:
        entry = _active_research.get(research_id)
        if entry is None:
            return default
        value = entry.get(field, default)
        # We explicitly check for list/dict rather than using copy.copy()
        # because entries may contain threading.Thread objects —
        # copy.copy(Thread) creates a broken shallow copy sharing the same
        # OS thread ident.  Scalars (int, str, bool) are immutable and safe.
        # For Thread objects, access them via iter_active_research() snapshots.
        if isinstance(value, list):
            return list(value)
        if isinstance(value, dict):
            return dict(value)
        return value


def set_active_research(research_id, data):
    """Insert or replace the active-research entry for *research_id*."""
    with _lock:
        _active_research[research_id] = data


def _get_user_research_start_gate(username: str) -> _ReentrantUserLock:
    """Return the per-user research-start gate, creating it on first use."""
    with _user_research_start_gates_lock:
        gate = _user_research_start_gates.get(username)
        if gate is None:
            gate = _ReentrantUserLock()
            _user_research_start_gates[username] = gate
        return gate


def get_user_research_start_lock(username: str):
    """Return the stable lock shared by admission and worker registration."""
    return _get_user_research_start_gate(username)


@contextmanager
def user_research_start_gate(username):
    """Hold the per-user research-start gate across a check-then-act sequence.

    ``check_and_start_research`` acquires this same per-user gate before it
    registers and starts a new research thread for a user. Fresh HTTP starts
    and persisted queue replay also hold it across their live-count -> DB-claim
    sections, so neither can consume the same last user slot. A caller that
    holds the gate across its own "is this user researching?" check and a
    password-change rekey likewise closes that TOCTOU window for that user.

    Unlike the process-global ``_lock`` this does NOT block other users'
    research — essential because the rekey it guards can take seconds and
    ``_lock`` is taken on every progress/log update.

    ``username`` of ``None`` yields without acquiring anything: there is no user
    to gate, and real research always carries an owning username.
    """
    if username is None:
        yield
        return
    with _get_user_research_start_gate(username):
        yield


def check_and_start_research(research_id, data) -> bool:
    """Atomically register a research entry iff no live thread exists.

    If ``_active_research[research_id]`` already holds an entry whose
    ``thread`` is alive, returns ``False`` without mutating state and
    without calling ``.start()``. Otherwise, starts ``data['thread']``
    and writes *data* into the active-research dict, then returns ``True``.

    The entire check-and-start is done under the single shared lock, so
    two concurrent callers with the same *research_id* cannot both pass
    the liveness check and end up with two live threads for the same ID.

    Registration is additionally wrapped in the research owner's per-user
    ``user_research_start_gate``, so a new thread cannot register (and start
    writing to the pre-rekey engine) while that user's password-change rekey
    is in flight — see ``change_password`` in web/routers/auth.py.
    """
    thread = data.get("thread") if isinstance(data, dict) else None
    if thread is None:
        raise ValueError("data must contain a 'thread' entry")
    # Owning username drives the per-user rekey gate below. Every real research
    # start passes settings["username"] (via start_research_process); a
    # missing/None username degrades to no gating (nothing to serialise
    # against, since the rekey gate is keyed by username).
    username = None
    if isinstance(data, dict):
        settings = data.get("settings")
        if isinstance(settings, dict):
            username = settings.get("username")
    # Serialise against an in-flight password-change rekey for this user. The
    # gate is per-user, so this never stalls other users' research; the inner
    # ``_lock`` still provides the atomic check-and-start for research_id.
    with user_research_start_gate(username):
        with _lock:
            entry = _active_research.get(research_id)
            if entry is not None:
                existing = entry.get("thread")
                if existing is not None and existing.is_alive():
                    return False
            thread.start()
            _active_research[research_id] = data
    return True


def update_active_research(research_id, **fields):
    """Update one or more fields on an existing entry.

    Silently does nothing if *research_id* is not active.
    """
    with _lock:
        entry = _active_research.get(research_id)
        if entry is not None:
            entry.update(fields)


def append_research_log(research_id, log_entry):
    """Append *log_entry* to the ``log`` list for *research_id*.

    Silently does nothing if *research_id* is not active.
    """
    with _lock:
        entry = _active_research.get(research_id)
        if entry is not None:
            entry.setdefault("log", []).append(log_entry)


def update_progress_if_higher(research_id, new_progress):
    """Atomically update bounded progress only if it exceeds current.

    Returns the resulting progress value, or ``None`` if the research is
    not active.
    """
    return update_progress_and_check_active(research_id, new_progress)[0]


def remove_active_research(research_id):
    """Remove *research_id* from the active-research dict (if present)."""
    with _lock:
        _active_research.pop(research_id, None)


def iter_active_research():
    """Yield ``(research_id, snapshot)`` pairs for all active research.

    Each *snapshot* is a shallow copy of the entry dict, safe to read
    outside the lock.
    """
    with _lock:
        items = [
            (
                rid,
                {
                    "progress": entry.get("progress", 0),
                    "status": entry.get("status"),
                    "log": list(entry.get("log", [])),
                    "settings": dict(s)
                    if (s := entry.get("settings")) is not None
                    else None,
                },
            )
            for rid, entry in _active_research.items()
        ]
    for rid, data in items:
        yield rid, data


def get_active_research_count():
    """Return the number of active research entries."""
    with _lock:
        return len(_active_research)


def get_usernames_with_active_research() -> set:
    """Return set of usernames that have research currently running."""
    with _lock:
        return {
            entry.get("settings", {}).get("username")
            for entry in _active_research.values()
            if entry.get("settings", {}).get("username")
        }


# ===================================================================
# termination_flags accessors
# ===================================================================


def is_termination_requested(research_id):
    """Return ``True`` if termination was requested for *research_id*."""
    with _lock:
        return _termination_flags.get(research_id, False)


def set_termination_flag(research_id):
    """Signal that *research_id* should be terminated."""
    with _lock:
        _termination_flags[research_id] = True


def clear_termination_flag(research_id):
    """Remove the termination flag for *research_id* (if present)."""
    with _lock:
        _termination_flags.pop(research_id, None)


# ===================================================================
# Compound / cleanup helpers
# ===================================================================


def is_research_thread_alive(research_id):
    """Return ``True`` if the research thread for *research_id* is alive.

    Returns ``False`` if the research is not active or has no thread.
    """
    with _lock:
        entry = _active_research.get(research_id)
        if entry is None:
            return False
        thread = entry.get("thread")
        return thread is not None and thread.is_alive()


def update_progress_and_check_active(research_id, new_progress):
    """Atomically bound progress to 0..100 and advance it monotonically.

    Returns ``(progress_value, is_active)`` where *progress_value* is the
    resulting progress (or ``None`` if not active) and *is_active* indicates
    whether *research_id* is still in the active-research dict.
    """
    with _lock:
        entry = _active_research.get(research_id)
        if entry is None:
            return (None, False)
        # A strategy can overshoot its phase allocation. Clamp before the
        # monotonic comparison: otherwise an overshoot becomes a permanent
        # floor and even the worker's final 100% update cannot correct it.
        current = min(100, max(0, entry.get("progress", 0)))
        if new_progress is not None:
            current = max(current, min(100, max(0, new_progress)))
        entry["progress"] = current
        return (current, True)


def cleanup_research(research_id, *, preserve_termination_flag=False):
    """Remove a research's process state atomically under the shared lock.

    The normal worker cleanup removes both registries.  The HTTP cancellation
    path may preserve the flag after removing the active entry so the worker
    that is still unwinding can observe the stop request at its next
    checkpoint and perform the final cleanup itself.
    """
    with _lock:
        _active_research.pop(research_id, None)
        if not preserve_termination_flag:
            _termination_flags.pop(research_id, None)


def reclaim_stale_user_active_research(
    db_session, username, *, grace_cutoff_dt=None, logger=None
):
    """Flip ``UserActiveResearch`` rows whose worker thread is dead.

    Shared between ``research_routes.start_research`` (no grace window)
    and ``chat.routes.send_message`` (30-second grace window) — both
    sites historically iterated the same query / status flip / cleanup
    pattern inline; consolidating here makes the difference (grace vs.
    no grace) explicit instead of two near-duplicate copies drifting.

    The helper does NOT commit — callers compose this with surrounding
    DB writes and commit once at the end. Returns ``True`` if any rows
    were reclaimed so the caller can decide whether a commit is needed.

    Args:
        db_session: open SQLAlchemy session against the user's DB.
        username: the per-user DB owner (used to scope the query).
        grace_cutoff_dt: optional ``datetime`` boundary; rows whose
            ``started_at`` is at-or-after this are skipped (avoids
            killing a sibling request's just-spawned thread). ``None``
            disables the grace filter — matches the
            ``research_routes.start_research`` original behaviour.
        logger: optional ``loguru``-style logger for the reclaim
            audit line. Both call sites log at WARNING so operators can
            trace why an active-research cap was released.

    Returns:
        ``True`` if any row was flipped (caller should commit), else
        ``False``.
    """
    # Lazy import to avoid pulling SQLAlchemy at module import time;
    # this helper is only called from the route paths that already have
    # the ORM models in scope.
    from ..constants import ResearchStatus
    from ..database.models import UserActiveResearch

    query = db_session.query(UserActiveResearch).filter(
        UserActiveResearch.username == username,
        UserActiveResearch.status == ResearchStatus.IN_PROGRESS,
    )
    if grace_cutoff_dt is not None:
        query = query.filter(UserActiveResearch.started_at < grace_cutoff_dt)

    reclaimed = False
    for row in query.all():
        if is_research_thread_alive(row.research_id):
            continue
        if logger is not None:
            logger.warning(
                "Reclaiming stale UserActiveResearch {short_id}... "
                "(thread dead) for user {user}",
                short_id=row.research_id[:8],
                user=username,
            )
        row.status = ResearchStatus.FAILED
        cleanup_research(row.research_id)
        reclaimed = True
    return reclaimed