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.

34.0 KB
Raw Blame
"""Contracts for APScheduler-backed background jobs after the FastAPI port.

Under Flask every APScheduler job ran inside an app context, so a job could
reach the request user, the settings manager and the user's decrypted
database implicitly. FastAPI has no app context: an APScheduler worker
thread starts with *nothing*, so each job has to acquire (a) its own
``request_user`` contextvar via ``BackgroundJobScheduler._wrap_job`` and
(b) its own credentials from the scheduler's in-memory credential store.

``_wrap_job`` was dropped in the port and later restored, so a job losing
its wrapper is a demonstrated regression class here, not a hypothetical.
The existing suite covers ``_wrap_job`` in isolation
(``tests/news/test_scheduler.py::TestWrapJobPropagatesUsernameContext``)
and the lifespan shutdown ordering
(``tests/web/test_lifespan_startup_shutdown.py``). What it does not cover,
and what this module pins, is:

* every ``add_job`` registration actually routes its callable through
  ``_wrap_job`` -- and every *user-bound* registration passes
  ``username=`` so the job is scoped to the right user;
* ``replace_existing=True`` on every per-user registration, so a re-login
  (or a double-clicked manual trigger) replaces the job instead of raising
  ``ConflictingIdError`` into a blanket ``except`` that silently drops the
  scheduling;
* a job that raises reaches APScheduler as an error event and leaves the
  scheduler running, and ``_wrap_job`` neither masks the exception nor
  leaks the contextvar on the failure path (the existing unit tests only
  cover a clean return);
* ``stop()`` releases every job and every plaintext credential;
* a job dispatched around a logout can no longer reopen that user's
  decrypted database, and the 5-minute settings TTL cache does not
  outlive the session either.

Determinism: no test sleeps or waits for a scheduled fire time. Jobs are
registered on a *real* ``BackgroundScheduler`` started with
``paused=True`` -- registrations hit the live ``MemoryJobStore`` (so
``replace_existing`` semantics are the real ones) but no worker thread
ever fires them. Where a job must actually execute, it is pushed through
APScheduler's own ``apscheduler.executors.base.run_job`` synchronously.
"""

import ast
import contextlib
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
from typing import Optional

import pytest
from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_EXECUTED
from apscheduler.executors.base import run_job
from apscheduler.jobstores.base import ConflictingIdError
from apscheduler.schedulers.background import BackgroundScheduler

import local_deep_research
from local_deep_research.scheduler.background import (
    BackgroundJobScheduler,
    DocumentSchedulerSettings,
)
from local_deep_research.utilities.request_context import (
    get_current_username,
)

_PKG = Path(local_deep_research.__file__).parent
_BACKGROUND_PY = _PKG / "scheduler" / "background.py"
_NEWS_API_PY = _PKG / "web" / "routers" / "news_flask_api.py"

# Every module that registers jobs on the shared BackgroundJobScheduler.
# (web/auth/connection_cleanup.py builds its OWN private BackgroundScheduler
# for a stateless idle-connection sweep -- it has no user scoping and no
# _wrap_job, by design -- so it is deliberately out of scope here.)
_JOB_REGISTERING_SOURCES = (_BACKGROUND_PY, _NEWS_API_PY)


# ---------------------------------------------------------------------------
# AST scanner over the real production sources
# ---------------------------------------------------------------------------


@dataclass(frozen=True)
class JobSite:
    """One ``<something>.scheduler.add_job(...)`` call site."""

    module: str
    lineno: int
    target: Optional[str]  # source of the callable being scheduled
    wrapped: bool  # routed through _wrap_job(...)
    wrap_username: bool  # _wrap_job(..., username=...)
    user_bound: bool  # the job receives a username
    job_id: Optional[str]
    replace_existing: Optional[str]

    def __str__(self) -> str:  # pragma: no cover - only used in failure text
        return f"{self.module}:{self.lineno} add_job(func={self.target})"


def _scan_add_job_sites(source: str, module: str) -> list[JobSite]:
    """Collect every ``*.scheduler.add_job(...)`` call site in ``source``."""
    sites: list[JobSite] = []
    for node in ast.walk(ast.parse(source)):
        if not (
            isinstance(node, ast.Call)
            and isinstance(node.func, ast.Attribute)
            and node.func.attr == "add_job"
        ):
            continue
        receiver = ast.unparse(node.func.value)
        # Only the shared BackgroundJobScheduler's APScheduler instance.
        if not receiver.endswith("scheduler"):
            continue

        kwargs = {kw.arg: kw.value for kw in node.keywords if kw.arg}
        func_arg = node.args[0] if node.args else kwargs.get("func")

        wrapped = (
            isinstance(func_arg, ast.Call)
            and isinstance(func_arg.func, ast.Attribute)
            and func_arg.func.attr == "_wrap_job"
        )
        wrap_username = wrapped and any(
            kw.arg == "username" for kw in func_arg.keywords
        )
        target = None
        if wrapped and func_arg.args:
            target = ast.unparse(func_arg.args[0])
        elif func_arg is not None:
            target = ast.unparse(func_arg)

        job_args = kwargs.get("args")
        arg_names = (
            [ast.unparse(el) for el in job_args.elts]
            if isinstance(job_args, ast.List)
            else []
        )
        job_id = ast.unparse(kwargs["id"]) if "id" in kwargs else None
        # A job is user-bound if the username is handed to the callable, or
        # if the job id is namespaced per user.
        user_bound = "username" in arg_names or (
            job_id is not None and "username" in job_id
        )

        sites.append(
            JobSite(
                module=module,
                lineno=node.lineno,
                target=target,
                wrapped=wrapped,
                wrap_username=wrap_username,
                user_bound=user_bound,
                job_id=job_id,
                replace_existing=(
                    ast.unparse(kwargs["replace_existing"])
                    if "replace_existing" in kwargs
                    else None
                ),
            )
        )
    return sites


def _all_job_sites() -> list[JobSite]:
    sites: list[JobSite] = []
    for path in _JOB_REGISTERING_SOURCES:
        sites.extend(
            _scan_add_job_sites(path.read_text(encoding="utf-8"), path.name)
        )
    return sites


# Sanity floor: if the scanner silently stops matching (a rename of
# ``add_job``, a receiver that no longer ends in ``scheduler``), every
# contract below would pass over an empty list. Pin the count so that
# failure mode is loud.
_MIN_EXPECTED_JOB_SITES = 12


class TestJobSiteScanner:
    """The scanner itself must actually see the production call sites."""

    def test_scanner_finds_every_registration_module(self):
        sites = _all_job_sites()
        assert len(sites) >= _MIN_EXPECTED_JOB_SITES, (
            f"scanner found only {len(sites)} add_job sites across "
            f"{[p.name for p in _JOB_REGISTERING_SOURCES]}; the contracts "
            "in this module would be vacuous. Did add_job get renamed or "
            "moved?"
        )
        modules = {s.module for s in sites}
        assert modules == {p.name for p in _JOB_REGISTERING_SOURCES}, (
            f"expected job registrations in both modules, saw {modules}"
        )
        assert any(s.user_bound for s in sites), (
            "no user-bound job site detected -- the username-scoping "
            "contracts below would be vacuous"
        )


class TestEveryScheduledJobGoesThroughWrapJob:
    """An APScheduler worker thread inherits no contextvars.

    Every job therefore has to be pushed through ``_wrap_job``; a bare
    callable runs with ``get_current_username() is None``, which breaks
    metric/log attribution and makes any no-argument settings or database
    read from that thread raise the background-thread guard in
    ``utilities/db_utils.py``.
    """

    def test_every_registered_job_is_wrapped(self):
        unwrapped = [s for s in _all_job_sites() if not s.wrapped]
        assert unwrapped == [], (
            "these scheduler jobs are registered without _wrap_job, so they "
            "run with no request_user context on the APScheduler worker "
            "thread:\n  " + "\n  ".join(str(s) for s in unwrapped)
        )

    def test_scanner_reports_a_job_that_loses_its_wrapper(self):
        """Negative control for the contract above.

        Strips ``self._wrap_job(...)`` from one registration in a copy of
        the real source and asserts the scanner reports that site. Without
        this, a scanner that never flags anything would make
        ``test_every_registered_job_is_wrapped`` unfalsifiable.
        """
        source = _BACKGROUND_PY.read_text(encoding="utf-8")
        original = (
            "            self._wrap_job(self._run_cleanup_with_tracking),"
        )
        assert original in source, (
            "the cleanup-job registration this control mutates has moved; "
            "update the control so it keeps exercising the scanner"
        )
        mutated = source.replace(
            original, "            self._run_cleanup_with_tracking,", 1
        )

        before = [s for s in _scan_add_job_sites(source, "x.py") if s.wrapped]
        after = _scan_add_job_sites(mutated, "x.py")
        regressed = [s for s in after if not s.wrapped]

        assert len(regressed) == 1, (
            "removing exactly one _wrap_job call must produce exactly one "
            f"unwrapped site, got {regressed}"
        )
        assert regressed[0].target == "self._run_cleanup_with_tracking"
        # ...and nothing else changed: exactly one site flipped from
        # wrapped to unwrapped.
        assert len([s for s in after if s.wrapped]) == len(before) - 1
        assert len(after) == len(_scan_add_job_sites(source, "x.py"))


# No user-bound registration may omit ``username=`` from ``_wrap_job``.
# Kept as an explicit set so the negative-control census below still reports
# a future exception as a deliberate contract change rather than silently
# weakening the rule.
_KNOWN_UNSCOPED_USER_BOUND_JOBS: set[tuple[str, str]] = set()


def _unscoped_user_bound_jobs() -> set[tuple[str, Optional[str]]]:
    return {
        (s.module, s.target)
        for s in _all_job_sites()
        if s.user_bound and not s.wrap_username
    }


class TestUserBoundJobsAreScopedToTheirUser:
    """``_wrap_job(func)`` and ``_wrap_job(func, username=u)`` differ.

    Only the second pushes a ``request_user`` context. A job that receives
    a username as a *positional argument* but is wrapped without
    ``username=`` looks scoped and is not: everything it triggers
    downstream (token metrics, log binding, no-arg settings reads) resolves
    the user as ``None``.
    """

    def test_no_new_user_bound_job_loses_its_username_scope(self):
        offenders = _unscoped_user_bound_jobs()
        new = offenders - _KNOWN_UNSCOPED_USER_BOUND_JOBS
        assert new == set(), (
            "new user-bound scheduler job(s) wrapped without username=; "
            "they will run with get_current_username() is None:\n  "
            + "\n  ".join(f"{m}: {t}" for m, t in sorted(new))
        )

    def test_all_user_bound_jobs_pass_username_to_wrap_job(self):
        assert _unscoped_user_bound_jobs() == set()

    def test_wrap_job_without_username_really_leaves_the_user_unset(self):
        """The mechanism behind the xfail above, executed rather than parsed.

        Same callable, same scheduler, two wrappings: the defect is that the
        second form is what the two sites above use.
        """
        sched = BackgroundJobScheduler.__new__(BackgroundJobScheduler)
        seen: list[Optional[str]] = []

        def job(username):
            seen.append(get_current_username())

        BackgroundJobScheduler._wrap_job(sched, job, username="alice")("alice")
        BackgroundJobScheduler._wrap_job(sched, job)("alice")

        assert seen == ["alice", None], (
            "wrapping with username= must scope the job and wrapping "
            f"without it must not; got {seen}"
        )


# ---------------------------------------------------------------------------
# Fixtures: a real, paused APScheduler behind a fresh scheduler singleton
# ---------------------------------------------------------------------------


@pytest.fixture
def sched():
    """A ``BackgroundJobScheduler`` backed by a real, paused APScheduler.

    ``start(paused=True)`` puts the jobstore into its live state -- so
    ``replace_existing`` / ``ConflictingIdError`` behave exactly as in
    production -- while guaranteeing no job ever fires on its own. Tests
    that need execution drive ``run_job`` directly.

    The singleton slot is saved and restored so this file cannot leak a
    scheduler into any other test module.
    """
    previous = BackgroundJobScheduler._instance
    BackgroundJobScheduler._instance = None
    instance = BackgroundJobScheduler()
    instance.scheduler = BackgroundScheduler()
    instance.scheduler.start(paused=True)
    instance.is_running = True
    try:
        yield instance
    finally:
        if instance.scheduler.running:
            instance.scheduler.shutdown(wait=False)
        BackgroundJobScheduler._instance = previous


def _register(sched, username, password="SchedTestPass1"):  # noqa: S107
    """Give ``username`` the state a logged-in user has in the scheduler."""
    sched.user_sessions[username] = {
        "last_activity": datetime.now(UTC),
        "scheduled_jobs": set(),
    }
    sched._credential_store.store(username, password)
    return password


def _noop():
    return None


# ---------------------------------------------------------------------------
# replace_existing
# ---------------------------------------------------------------------------


# Fixed-id jobs registered without replace_existing. Each is guarded by a
# caller-side precondition instead (``start()`` is gated on ``is_running``;
# ``_reload_config`` only re-adds when the retention setting changed). New
# entries here are a re-registration hazard, so the set is pinned.
_KNOWN_FIXED_ID_JOBS_WITHOUT_REPLACE = {
    ("background.py", "'cleanup_inactive_users'"),
    ("background.py", "'reload_config'"),
    ("background.py", "'initial_cleanup'"),
    ("background.py", "'immediate_cleanup_config_change'"),
}


class TestReplaceExistingOnReRegistration:
    def test_apscheduler_really_rejects_a_duplicate_id(self, sched):
        """Mechanism control: this is what ``replace_existing`` defends
        against, on the real jobstore these tests use."""
        sched.scheduler.add_job(
            _noop, "date", run_date=datetime(2099, 1, 1, tzinfo=UTC), id="dup"
        )
        with pytest.raises(ConflictingIdError):
            sched.scheduler.add_job(
                _noop,
                "date",
                run_date=datetime(2099, 1, 1, tzinfo=UTC),
                id="dup",
            )
        sched.scheduler.add_job(
            _noop,
            "date",
            run_date=datetime(2099, 1, 2, tzinfo=UTC),
            id="dup",
            replace_existing=True,
        )
        assert len(sched.scheduler.get_jobs()) == 1

    def test_every_user_bound_job_sets_replace_existing(self):
        """A per-user job id is re-registered on every login, on every
        settings change and on every manual trigger. Without
        ``replace_existing=True`` the second registration raises
        ``ConflictingIdError`` into the blanket ``except Exception`` that
        wraps each of these code paths -- the user's jobs then silently
        stop being (re)scheduled."""
        bad = [
            s
            for s in _all_job_sites()
            if s.user_bound and s.replace_existing != "True"
        ]
        assert bad == [], (
            "per-user scheduler job(s) registered without "
            "replace_existing=True; re-registration will raise "
            "ConflictingIdError into a swallowing except:\n  "
            + "\n  ".join(f"{s} id={s.job_id}" for s in bad)
        )

    def test_fixed_id_jobs_without_replace_existing_are_the_known_set(self):
        actual = {
            (s.module, s.job_id)
            for s in _all_job_sites()
            if not s.user_bound and s.replace_existing != "True"
        }
        assert actual == _KNOWN_FIXED_ID_JOBS_WITHOUT_REPLACE, (
            "the set of fixed-id scheduler jobs registered without "
            "replace_existing=True changed. A new entry is a "
            "ConflictingIdError hazard on re-registration; a removed entry "
            "means this pin is stale.\n"
            f"  added:   {sorted(actual - _KNOWN_FIXED_ID_JOBS_WITHOUT_REPLACE)}\n"
            f"  removed: {sorted(_KNOWN_FIXED_ID_JOBS_WITHOUT_REPLACE - actual)}"
        )

    def test_scanner_reports_a_dropped_replace_existing(self):
        """Negative control for the two contracts above."""
        source = _BACKGROUND_PY.read_text(encoding="utf-8")
        keep = (
            "                name=f"
            '"Manual Document Processing for {username}",\n'
        )
        needle = keep + "                replace_existing=True,\n"
        assert needle in source, (
            "the manual document-processing registration this control "
            "mutates has moved; update the control"
        )
        mutated = source.replace(needle, keep, 1)
        regressed = [
            s
            for s in _scan_add_job_sites(mutated, "background.py")
            if s.user_bound and s.replace_existing != "True"
        ]
        assert len(regressed) == 1, (
            "dropping one replace_existing=True must yield exactly one "
            f"offending user-bound site, got {regressed}"
        )
        assert regressed[0].target == "self._process_user_documents"

    def test_double_manual_trigger_replaces_instead_of_duplicating(self, sched):
        """Behavioural counterpart: the manual document-processing trigger
        has a fixed per-user job id and a run date one second out, so a
        double-click lands inside the previous job's window. It must
        succeed twice and leave exactly one job."""
        username = "alice"
        _register(sched, username)

        assert sched.trigger_document_processing(username) is True
        first = sched.scheduler.get_jobs()
        assert len(first) == 1, first

        assert sched.trigger_document_processing(username) is True, (
            "the second manual trigger failed -- a ConflictingIdError was "
            "swallowed by trigger_document_processing's except clause, so "
            "the caller sees a failure for an ordinary double-click"
        )

        jobs = sched.scheduler.get_jobs()
        assert len(jobs) == 1, (
            "re-triggering document processing duplicated the job instead "
            f"of replacing it: {[j.id for j in jobs]}"
        )
        job = jobs[0]
        assert job.id == f"{username}_document_processing_manual"
        # The stored callable is the _wrap_job wrapper around the real
        # method, not the bare method.
        assert (
            job.func.__wrapped__.__func__
            is BackgroundJobScheduler._process_user_documents
        ), (
            "the manual trigger scheduled something other than a "
            f"_wrap_job-wrapped _process_user_documents: {job.func!r}"
        )
        assert job.args == (username,)

    def test_rescheduling_document_jobs_does_not_accumulate(self, sched):
        """A user's document jobs are re-registered on every login and on
        every ``document_scheduler.*`` settings change."""
        username = "alice"
        _register(sched, username)
        settings = DocumentSchedulerSettings(
            enabled=True,
            interval_seconds=900,
            sweep_library_collections=True,
        )
        sched._settings_cache[username] = settings

        for _ in range(3):
            sched._schedule_document_processing(username)

        ids = sorted(job.id for job in sched.scheduler.get_jobs())
        assert ids == [
            f"{username}_document_processing",
            f"{username}_library_sweep",
        ], f"document job registration is not idempotent: {ids}"
        assert sched.user_sessions[username]["scheduled_jobs"] == set(ids), (
            "the scheduler's own per-user job bookkeeping drifted from the "
            "jobstore; unregister_user removes jobs by walking this set, so "
            "drift leaves orphaned jobs running after logout"
        )


# ---------------------------------------------------------------------------
# Failing jobs
# ---------------------------------------------------------------------------


class TestAFailingJobDoesNotKillTheScheduler:
    def test_wrap_job_propagates_the_error_and_restores_context(self, sched):
        """The existing unit tests only cover a clean return.

        On the failure path the wrapper must (a) let the original exception
        through unchanged -- APScheduler needs it to emit EVENT_JOB_ERROR --
        and (b) still unwind the ``request_user`` context, or every
        subsequent job on that pooled worker thread inherits a dead user.
        """
        seen: list[Optional[str]] = []
        boom = RuntimeError("job exploded")

        def failing_job(username):
            seen.append(get_current_username())
            raise boom

        wrapped = sched._wrap_job(failing_job, username="alice")

        before = get_current_username()
        with pytest.raises(RuntimeError) as excinfo:
            wrapped("alice")
        after = get_current_username()

        assert excinfo.value is boom, (
            "_wrap_job replaced the job's exception; APScheduler's error "
            "event would carry the wrong error"
        )
        assert seen == ["alice"], (
            f"the raising job did not see its own user context: {seen}"
        )
        assert after == before, (
            "_wrap_job leaked the request_user context out of a FAILING "
            f"job: before={before!r} after={after!r}"
        )

    def test_raising_job_becomes_an_error_event_and_the_scheduler_lives(
        self, sched
    ):
        """Driven through APScheduler's own ``run_job``, so this is the real
        executor path -- no sleeping and no wall-clock dependency."""
        state = {"calls": 0}

        def flaky(username):
            state["calls"] += 1
            raise ValueError(f"boom for {username}")

        sched.scheduler.add_job(
            func=sched._wrap_job(flaky, username="alice"),
            args=["alice"],
            trigger="date",
            run_date=datetime(2099, 1, 1, tzinfo=UTC),
            id="alice_flaky",
            misfire_grace_time=None,
            replace_existing=True,
        )
        sched.scheduler.add_job(
            func=sched._wrap_job(lambda username: "ok", username="alice"),
            args=["alice"],
            trigger="date",
            run_date=datetime(2099, 1, 1, tzinfo=UTC),
            id="alice_healthy",
            misfire_grace_time=None,
            replace_existing=True,
        )

        now = datetime.now(UTC)
        bad_events = run_job(
            sched.scheduler.get_job("alice_flaky"), "default", [now], "t"
        )
        assert [e.code for e in bad_events] == [EVENT_JOB_ERROR]
        assert isinstance(bad_events[0].exception, ValueError)
        assert state["calls"] == 1

        assert sched.scheduler.running, (
            "a job raising took the scheduler down -- every other user's "
            "jobs die with it"
        )
        assert sched.scheduler.get_job("alice_healthy") is not None, (
            "the failing job's sibling was evicted from the jobstore"
        )

        good_events = run_job(
            sched.scheduler.get_job("alice_healthy"), "default", [now], "t"
        )
        assert [e.code for e in good_events] == [EVENT_JOB_EXECUTED]
        assert good_events[0].retval == "ok"

    def test_cleanup_job_absorbs_its_own_failure_and_runs_again(self, sched):
        """``_run_cleanup_with_tracking`` is the recurring system job. It
        must not propagate: an interval job that raises every tick floods
        the log and, with a jobstore that tracks failures, can be dropped."""
        calls = {"n": 0}

        def exploding_cleanup():
            calls["n"] += 1
            raise RuntimeError("cleanup blew up")

        sched._cleanup_inactive_users = exploding_cleanup

        # Positive control: the failure is real, not a stubbed no-op.
        with pytest.raises(RuntimeError):
            sched._cleanup_inactive_users()
        assert calls["n"] == 1

        # ...and the tracking wrapper -- the thing actually scheduled --
        # absorbs it.
        sched._wrap_job(sched._run_cleanup_with_tracking)()
        assert calls["n"] == 2

        sched._cleanup_inactive_users = lambda: 7
        sched._wrap_job(sched._run_cleanup_with_tracking)()


# ---------------------------------------------------------------------------
# Shutdown
# ---------------------------------------------------------------------------


class TestStopReleasesJobsAndCredentials:
    def test_stop_drops_every_job_and_every_plaintext_credential(self, sched):
        alice_pw = _register(sched, "alice", "AlicePass1")
        bob_pw = _register(sched, "bob", "BobPass2")
        for user in ("alice", "bob"):
            sched.scheduler.add_job(
                func=sched._wrap_job(_noop, username=user),
                trigger="date",
                run_date=datetime(2099, 1, 1, tzinfo=UTC),
                id=f"{user}_job",
                replace_existing=True,
            )
            sched.user_sessions[user]["scheduled_jobs"].add(f"{user}_job")

        # Positive baseline: the plaintext SQLCipher passwords really are
        # in the process, so "gone after stop" is about stop().
        assert sched._credential_store.retrieve("alice") == alice_pw
        assert sched._credential_store.retrieve("bob") == bob_pw
        assert len(sched.scheduler.get_jobs()) == 2

        sched.stop()

        assert sched.is_running is False
        assert sched.scheduler.running is False, (
            "stop() left the APScheduler thread alive; jobs keep firing "
            "after shutdown"
        )
        assert sched.user_sessions == {}
        assert sched._credential_store.retrieve("alice") is None, (
            "shutdown left a user's PLAINTEXT SQLCipher password in the "
            "scheduler's process-global credential store"
        )
        assert sched._credential_store.retrieve("bob") is None
        assert sched.scheduler.get_jobs() == [], (
            "jobs survived shutdown; a restart would double-register them"
        )

    def test_stop_is_idempotent(self, sched):
        _register(sched, "alice", "AlicePass1")
        sched.stop()
        sched.stop()  # must not raise SchedulerNotRunningError
        assert sched.is_running is False


# ---------------------------------------------------------------------------
# Post-logout resurrection
# ---------------------------------------------------------------------------


class _DbOpened(Exception):
    """Raised by the spy so no test ever touches a real database."""


@pytest.fixture
def db_open_spy(monkeypatch):
    """Record every attempt to open a user's decrypted database.

    Raises immediately, so the production ``except Exception`` handlers
    around each job body swallow it exactly as they would a real SQLCipher
    error -- the recorded call is the whole signal.
    """
    calls: list[tuple] = []

    def _spy(username, password=None, *args, **kwargs):
        calls.append((username, password))
        raise _DbOpened(username)

    monkeypatch.setattr(
        "local_deep_research.database.session_context.get_user_db_session",
        _spy,
    )
    return calls


_SWEEP_ON = DocumentSchedulerSettings(
    enabled=True,
    download_pdfs=True,
    extract_text=True,
    sweep_library_collections=True,
)


class TestLoggedOutUserCannotBeResurrected:
    """``unregister_user`` is what logout calls.

    A job already dispatched to a worker thread, or one whose removal
    raced the logout, still holds a bound method on the scheduler. Running
    it after unregistration must not reopen the user's decrypted database:
    the session entry and the credential are dropped together under the
    scheduler lock, and every job body re-reads both before touching the
    DB.
    """

    @pytest.mark.parametrize(
        "job_name, job_args",
        [
            ("_process_user_documents", ("alice",)),
            ("_reconcile_unindexed_documents", ("alice",)),
            ("_check_user_overdue_subscriptions", ("alice",)),
        ],
    )
    def test_job_opens_no_database_after_unregister(
        self, sched, db_open_spy, job_name, job_args
    ):
        username = "alice"
        password = _register(sched, username, "AlicePass1")
        sched._get_document_scheduler_settings = lambda *a, **k: _SWEEP_ON

        job = getattr(sched, job_name)
        wrapped = sched._wrap_job(job, username=username)

        # Phase 1 -- positive control. While the user is registered the job
        # DOES reach the database with their plaintext password. Without
        # this half, phase 2 would pass for a job that never opens a DB at
        # all.
        wrapped(*job_args)
        assert db_open_spy == [(username, password)], (
            f"{job_name} never reached get_user_db_session while the user "
            f"was registered, so the post-logout assertion below would be "
            f"vacuous; recorded: {db_open_spy}"
        )
        db_open_spy.clear()

        # Phase 2 -- logout. The already-bound job runs again.
        sched.unregister_user(username)
        wrapped(*job_args)

        assert db_open_spy == [], (
            f"{job_name} reopened {username}'s decrypted database AFTER "
            f"logout; the scheduler resurrected a session that was revoked. "
            f"Recorded opens: {db_open_spy}"
        )

    def test_unregister_clears_the_credential_and_the_session_together(
        self, sched
    ):
        password = _register(sched, "alice", "AlicePass1")
        bystander_pw = _register(sched, "bob", "BobPass2")
        assert sched._credential_store.retrieve("alice") == password

        sched.unregister_user("alice")

        assert "alice" not in sched.user_sessions
        assert sched._credential_store.retrieve("alice") is None, (
            "logout left the plaintext SQLCipher password in the "
            "scheduler's credential store"
        )
        # Scoping control: one user's logout must not disarm everyone.
        assert "bob" in sched.user_sessions
        assert sched._credential_store.retrieve("bob") == bystander_pw

    def test_unregister_removes_only_that_users_jobs(self, sched):
        for user in ("alice", "bob"):
            _register(sched, user, f"{user}Pass1")
            for suffix in ("document_processing", "library_sweep"):
                job_id = f"{user}_{suffix}"
                sched.scheduler.add_job(
                    func=sched._wrap_job(_noop, username=user),
                    trigger="date",
                    run_date=datetime(2099, 1, 1, tzinfo=UTC),
                    id=job_id,
                    replace_existing=True,
                )
                sched.user_sessions[user]["scheduled_jobs"].add(job_id)
        assert len(sched.scheduler.get_jobs()) == 4

        sched.unregister_user("alice")

        remaining = sorted(job.id for job in sched.scheduler.get_jobs())
        assert remaining == ["bob_document_processing", "bob_library_sweep"], (
            "logout must cancel exactly the logging-out user's jobs; "
            f"jobstore now holds {remaining}"
        )

    def test_settings_cache_does_not_outlive_the_session(
        self, sched, db_open_spy
    ):
        """The document-scheduler settings cache has a 5-minute TTL and is
        keyed by username alone. If logout did not invalidate it, a job
        firing in that window would keep operating on the logged-out
        user's settings snapshot."""
        username = "alice"
        _register(sched, username, "AlicePass1")
        sched._settings_cache[username] = DocumentSchedulerSettings(
            enabled=True, interval_seconds=4242
        )

        # Positive baseline: the cache really is being served.
        assert (
            sched._get_document_scheduler_settings(username).interval_seconds
            == 4242
        )
        assert db_open_spy == [], "cache hit should not touch the database"

        sched.unregister_user(username)

        assert username not in sched._settings_cache, (
            "logout left the user's settings snapshot in the scheduler's "
            "TTL cache"
        )
        assert (
            sched._get_document_scheduler_settings(username)
            == DocumentSchedulerSettings.defaults()
        ), (
            "a post-logout settings read returned something other than "
            "defaults -- the logged-out user's configuration is still live"
        )
        assert db_open_spy == [], (
            "a post-logout settings read tried to open the user's "
            f"decrypted database: {db_open_spy}"
        )

    def test_expired_credentials_stop_a_job_even_with_a_live_session(
        self, sched, db_open_spy
    ):
        """The credential store has its own TTL, independent of the session
        dict. A job must consult it on every run rather than trusting the
        session entry -- otherwise TTL expiry (and the
        ``clear``-without-``del`` ordering inside ``_cleanup_inactive_users``)
        would leave a window where the job proceeds without a password."""
        username = "alice"
        _register(sched, username, "AlicePass1")
        sched._get_document_scheduler_settings = lambda *a, **k: _SWEEP_ON

        sched._credential_store.clear(username)
        assert username in sched.user_sessions  # session deliberately intact

        with contextlib.suppress(_DbOpened):
            sched._process_user_documents(username)
        with contextlib.suppress(_DbOpened):
            sched._reconcile_unindexed_documents(username)

        assert db_open_spy == [], (
            "a scheduled job opened the user's database with no credential "
            f"in the store: {db_open_spy}"
        )