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.

42.0 KB
Raw Blame
"""The lifespan contract: what startup acquires, shutdown must release.

``web/fastapi_app.lifespan`` is the FastAPI port's replacement for Flask's
``app_factory`` boot sequence, and it is the only place the process-wide
resources are created. On startup it registers uvicorn's event loop for
background Socket.IO emits, creates the Socket.IO subscription lock, resizes
the AnyIO worker pool, regenerates ``static/css/themes.css``, starts the
log-queue drain daemon, the research queue processor, the news scheduler and
the connection-cleanup scheduler. After ``yield`` it must stop every one of
them and close the databases, in a specific order.

The app runs single-worker (``workers=1`` in ``web/app.py``, required for
Socket.IO without a Redis message queue), so there is no second process to
absorb a bad boot: a startup step that fails silently, or a shutdown that
strands a daemon holding DB handles, is a whole-instance problem. On a
``uvicorn --reload`` cycle a stranded drain daemon from the old process
races the new one over the same queue and the same SQLCipher files.

What this file protects, and what breaks if it regresses:

* **Startup completeness** — each documented step really ran and the app
  serves. A step quietly dropped from ``lifespan`` (e.g. ``set_main_loop``)
  produces no error, just background emits that never reach the browser.
* **Shutdown symmetry** — the thread inventory returns to its pre-boot
  state. A missing ``stop()`` leaks a daemon per reload.
* **Shutdown ordering** — ``flush_log_queue`` must run BEFORE
  ``close_all_databases`` (``database_sink`` swallows write errors, so logs
  flushed after the close are silently dropped), and the cleanup scheduler
  must be shut down with ``wait=True`` before the close so in-flight jobs
  do not hit a disposed pool.
* **Loud failure** — a failure in an UNGUARDED startup step must abort the
  boot (ASGI ``lifespan.startup.failed``, which makes uvicorn exit) rather
  than leave a half-initialised app serving traffic; a failure in a
  deliberately GUARDED step (the themes.css regeneration, the threadpool
  tuning knob) must NOT stop the server from starting.
* **``.secret_key`` fail-closed** — this PR deliberately removed the
  fall-back-to-an-ephemeral-key path. An ephemeral key silently invalidates
  every session on restart with no operator signal, so read/write failure
  now raises, and because ``SECRET_KEY = _load_secret_key()`` runs at module
  scope that failure aborts the import, i.e. the boot.
* **``timeout_graceful_shutdown``** — hardcoded to 10 in ``web/app.py`` with
  no env override, and the lifespan's own comment quotes that number as the
  request-drain budget that the Compose ``stop_grace_period`` is sized
  against. Pinned here so a silent change to either side is visible.

WHY A CHILD PROCESS. ``socketio_asgi.init_lock()`` only assigns when
``_lock is None``, so a process may complete at most ONE real lifespan cycle
(see the ``lifespan`` marker in pyproject.toml and
``tests/web/test_lifespan_boot.py``). The boot probes here therefore run in a
FRESH interpreter, one lifespan each, and report back as JSON. That also
gives an uncontaminated thread inventory — the pytest process has xdist,
fixture and DB threads of its own — and lets the probe drop
``PYTEST_CURRENT_TEST`` so the research queue processor takes its production
default (``lifespan`` disables it under pytest) and its shutdown is covered
too. These tests deliberately do NOT carry the ``lifespan`` marker: they
enter no lifespan in the pytest process, so the one-per-process budget that
``test_lifespan_boot.py`` spends is untouched.
"""

from __future__ import annotations

import ast
import json
import os
import re
import subprocess
import sys
import threading
from pathlib import Path

import pytest

from local_deep_research.utilities import log_utils
from local_deep_research.web import fastapi_app
from local_deep_research.web.services import socketio_asgi

FASTAPI_APP_PATH = Path(fastapi_app.__file__).resolve()
SERVER_ENTRYPOINT_PATH = FASTAPI_APP_PATH.with_name("app.py")
SRC_ROOT = FASTAPI_APP_PATH.parents[2]

# Leave time to kill/reap the child and report the failure before pytest's
# 180-second deadline terminates the whole worker.
_BOOT_PROBE_TIMEOUT_SECONDS = 120

# Threads the app is expected to own while it is up. Matched as substrings
# because APScheduler and the queue processor name their workers by
# target/index ("Thread-3 (_process_queue_loop)").
EXPECTED_RUNNING_THREADS = {
    "log queue drain daemon": "log-queue-processor",
    "APScheduler worker (news + cleanup schedulers)": "APScheduler",
    "research queue processor v2": "_process_queue_loop",
}


# ---------------------------------------------------------------------------
# Boot probe — runs the REAL lifespan once, in a fresh interpreter.
# ---------------------------------------------------------------------------

_PROBE_SOURCE = r'''
"""Boot the real app once and report what startup/shutdown did.

argv: <mode> <output json path>
  mode "clean"         -- untouched boot
  mode "themes-broken" -- theme_registry.get_combined_css() raises, which
                          exercises the try/except the lifespan wraps the
                          themes.css regeneration in.
"""

import json
import os
import sys
import threading
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from stat import S_IMODE

MODE = sys.argv[1]
OUT = Path(sys.argv[2])

trace = []


def record(event, **fields):
    fields["event"] = event
    trace.append(fields)


def snapshot():
    """Every live thread, name-sorted, as plain JSON-able dicts."""
    return [
        {"name": t.name, "daemon": t.daemon, "alive": t.is_alive()}
        for t in sorted(threading.enumerate(), key=lambda t: t.name)
    ]


from fastapi.testclient import TestClient

from local_deep_research.database.encrypted_db import db_manager
from local_deep_research.embeddings import model_cache
from local_deep_research.utilities import log_utils
from local_deep_research.web import fastapi_app
from local_deep_research.web.auth import connection_cleanup
from local_deep_research.web.queue.processor_v2 import queue_processor
from local_deep_research.web.services import socketio_asgi
from local_deep_research.web.themes import theme_registry

# Pytest workers share the checkout, so another boot can rewrite the real
# themes.css between this probe's before/after snapshots. Give each child its
# own stylesheet path while still running the real lifespan code.
probe_static_dir = OUT.parent / "static"
(probe_static_dir / "css").mkdir(parents=True)
fastapi_app.STATIC_DIR = str(probe_static_dir)
css_path = probe_static_dir / "css" / "themes.css"
if MODE == "themes-broken":
    css_path.write_text("/* unchanged on generation failure */", encoding="utf-8")

# --- instrumentation -------------------------------------------------------
# Every wrapper calls through to the real implementation, so the observed
# behaviour (threads actually started, actually joined) is the real one.
# `lifespan` imports each of these INSIDE the function body, so patching the
# module attribute here is picked up when the lifespan runs.

_real_set_main_loop = socketio_asgi.set_main_loop
_real_init_lock = socketio_asgi.init_lock
_real_start_log_queue = log_utils.start_log_queue_processor
_real_flush_log_queue = log_utils.flush_log_queue
_real_stop_log_queue = log_utils.stop_log_queue_processor
_real_start_cleanup = connection_cleanup.start_connection_cleanup_scheduler
_real_qp_start = queue_processor.start
_real_qp_stop = queue_processor.stop
_real_close_all = db_manager.close_all_databases
embedding_entered = threading.Event()
embedding_release = threading.Event()


if MODE == "embedding-busy":
    class BlockingBackend:
        def __init__(self, **kwargs):
            pass

        def embed_query(self, text):
            embedding_entered.set()
            if not embedding_release.wait(timeout=60):
                raise RuntimeError("Embedding shutdown probe timed out")
            return [1.0]

    embedding_handle = model_cache.shared_sentence_transformer_embeddings(
        BlockingBackend,
        model_name="synthetic/shutdown",
        cache_folder=str(OUT.parent),
        model_kwargs={},
    )

if MODE == "embedding-cache-error":
    def _broken_cache_clear(**kwargs):
        record("embedding_cache.clear_failed")
        raise RuntimeError("simulated cache cleanup failure")

    model_cache.clear_sentence_transformer_cache = _broken_cache_clear


def _set_main_loop(loop):
    record("set_main_loop", closed=loop.is_closed())
    return _real_set_main_loop(loop)


def _init_lock():
    record("init_lock")
    return _real_init_lock()


def _start_log_queue(app=None):
    thread = _real_start_log_queue(app)
    record(
        "start_log_queue_processor",
        thread=thread.name,
        alive=thread.is_alive(),
        daemon=thread.daemon,
    )
    return thread


def _flush_log_queue():
    record("flush_log_queue")
    return _real_flush_log_queue()


def _stop_log_queue(*args, **kwargs):
    record("stop_log_queue_processor")
    return _real_stop_log_queue(*args, **kwargs)


def _start_cleanup(*args, **kwargs):
    scheduler = _real_start_cleanup(*args, **kwargs)
    record("start_connection_cleanup_scheduler", running=bool(scheduler.running))
    real_shutdown = scheduler.shutdown

    def _shutdown(wait=True, *a, **kw):
        record("cleanup_scheduler.shutdown", wait=bool(wait))
        return real_shutdown(wait, *a, **kw)

    scheduler.shutdown = _shutdown
    return scheduler


def _qp_start(*args, **kwargs):
    record("queue_processor.start")
    return _real_qp_start(*args, **kwargs)


def _qp_stop(*args, **kwargs):
    record("queue_processor.stop")
    return _real_qp_stop(*args, **kwargs)


def _close_all(*args, **kwargs):
    record("close_all_databases")
    try:
        return _real_close_all(*args, **kwargs)
    finally:
        if MODE == "embedding-busy":
            result["closed_databases_before_inference_finished"] = (
                not embedding_release.is_set()
            )
            embedding_release.set()


socketio_asgi.set_main_loop = _set_main_loop
socketio_asgi.init_lock = _init_lock
log_utils.start_log_queue_processor = _start_log_queue
log_utils.flush_log_queue = _flush_log_queue
log_utils.stop_log_queue_processor = _stop_log_queue
connection_cleanup.start_connection_cleanup_scheduler = _start_cleanup
queue_processor.start = _qp_start
queue_processor.stop = _qp_stop
db_manager.close_all_databases = _close_all

if MODE == "themes-broken":

    def _broken_css():
        record("get_combined_css_failed")
        raise RuntimeError("simulated themes.css generation failure")

    theme_registry.get_combined_css = _broken_css

def _css_stat():
    if not css_path.exists():
        return None
    stat = css_path.stat()
    return {"mtime_ns": stat.st_mtime_ns, "size": stat.st_size}


# An upgraded install: research_outputs/ already holds a report at 0755.
# Loosened here, after every import, so only the lifespan startup itself can
# make it private again.
outputs_dir = Path(os.environ["LDR_DATA_DIR"]) / "research_outputs"
outputs_dir.mkdir(exist_ok=True)
legacy_report = outputs_dir / "legacy.md"
legacy_report.write_text("old report", encoding="utf-8")
outputs_dir.chmod(0o755)


def _outputs_mode():
    return S_IMODE(outputs_dir.stat().st_mode)


result = {
    "mode": MODE,
    "css_before": _css_stat(),
    "outputs_mode_before": _outputs_mode(),
}

result["threads_before"] = snapshot()

with TestClient(fastapi_app.app) as client:
    record("serving")
    result["threads_during"] = snapshot()
    result["health_status"] = client.get("/api/v1/health").status_code
    loop = socketio_asgi._main_loop
    result["loop_registered"] = loop is not None and not loop.is_closed()
    result["lock_initialised"] = socketio_asgi._lock is not None
    drain = log_utils._queue_processor_thread
    result["log_queue_alive_during"] = drain is not None and drain.is_alive()
    result["css_after_startup"] = _css_stat()
    result["outputs_mode_during"] = _outputs_mode()
    result["legacy_report_after_startup"] = legacy_report.read_text(
        encoding="utf-8"
    )
    if MODE == "embedding-busy":
        import asyncio

        def _limit_executor():
            asyncio.get_running_loop().set_default_executor(
                ThreadPoolExecutor(max_workers=1)
            )

        client.portal.call(_limit_executor)
        # Release only after database cleanup, with a watchdog to let a
        # broken implementation finish and report the ordering failure.
        watchdog = threading.Timer(20, embedding_release.set)
        watchdog.start()
        client.portal.start_task_soon(
            embedding_handle.aembed_query, "held during shutdown"
        )
        assert embedding_entered.wait(timeout=10)

if MODE == "embedding-busy":
    watchdog.cancel()
    watchdog.join(timeout=2)

# Immediately after __exit__, then again after giving anything still alive a
# bounded join. The join absorbs the exit race of a thread that WAS signalled
# to stop; it cannot mask a thread nobody told to stop, because those loop
# forever.
result["threads_after_raw"] = snapshot()
for thread in threading.enumerate():
    if thread is not threading.main_thread():
        thread.join(timeout=2.0)
result["threads_after"] = snapshot()

drain = log_utils._queue_processor_thread
result["log_queue_alive_after"] = drain is not None and drain.is_alive()
result["trace"] = trace

OUT.write_text(json.dumps(result, indent=1), encoding="utf-8")
'''


def _run_boot_probe(mode: str, workdir: Path) -> dict:
    """Run one real startup/shutdown cycle in a fresh interpreter."""
    script = workdir / "boot_probe.py"
    script.write_text(_PROBE_SOURCE, encoding="utf-8")
    out = workdir / "probe.json"

    data_dir = workdir / "data"
    data_dir.mkdir()

    env = dict(os.environ)
    # A fresh, isolated data directory: the lifespan creates .secret_key and
    # the encrypted_databases/ tree under it.
    env["LDR_DATA_DIR"] = str(data_dir)
    env.setdefault("LDR_BOOTSTRAP_ALLOW_UNENCRYPTED", "true")
    env["PYTHONPATH"] = os.pathsep.join(
        [str(SRC_ROOT), env.get("PYTHONPATH", "")]
    ).rstrip(os.pathsep)
    # Deliberate: `lifespan` skips the research queue processor when
    # PYTEST_CURRENT_TEST is set. Dropping it makes the child take the
    # PRODUCTION path, so this probe covers the processor's shutdown too.
    env.pop("PYTEST_CURRENT_TEST", None)

    try:
        completed = subprocess.run(
            [sys.executable, str(script), mode, str(out)],
            env=env,
            capture_output=True,
            text=True,
            timeout=_BOOT_PROBE_TIMEOUT_SECONDS,
        )
    except subprocess.TimeoutExpired as exc:
        stderr = exc.stderr or ""
        if isinstance(stderr, bytes):
            stderr = stderr.decode("utf-8", errors="replace")
        pytest.fail(
            f"boot probe ({mode}) timed out after {exc.timeout}s; "
            "the app did not complete a startup/shutdown cycle.\n"
            f"--- stderr ---\n{stderr[-3000:]}",
            pytrace=False,
        )
    if completed.returncode != 0 or not out.exists():
        pytest.fail(
            f"boot probe ({mode}) failed with exit code "
            f"{completed.returncode}; the app did not complete a "
            f"startup/shutdown cycle.\n--- stdout ---\n"
            f"{completed.stdout[-3000:]}\n--- stderr ---\n"
            f"{completed.stderr[-3000:]}"
        )
    return json.loads(out.read_text(encoding="utf-8"))


@pytest.fixture(scope="module")
def clean_boot(tmp_path_factory) -> dict:
    """One untouched startup/shutdown cycle of the real app."""
    return _run_boot_probe("clean", tmp_path_factory.mktemp("clean_boot"))


@pytest.fixture(scope="module")
def themes_broken_boot(tmp_path_factory) -> dict:
    """A cycle where the guarded themes.css step raises."""
    return _run_boot_probe(
        "themes-broken", tmp_path_factory.mktemp("themes_broken_boot")
    )


def _events(boot: dict) -> list[str]:
    return [entry["event"] for entry in boot["trace"]]


def _thread_names(snapshot: list[dict]) -> list[str]:
    return sorted(entry["name"] for entry in snapshot)


def _matching(snapshot: list[dict], needle: str) -> list[dict]:
    return [entry for entry in snapshot if needle in entry["name"]]


def test_busy_embedding_cache_does_not_delay_database_cleanup(tmp_path):
    boot = _run_boot_probe("embedding-busy", tmp_path)
    assert boot["closed_databases_before_inference_finished"], (
        "Lifespan queued cache cleanup behind busy inference, delaying "
        "log flushing and database cleanup until the watchdog released it"
    )
    assert "flush_log_queue" in _events(boot)


def test_embedding_cache_failure_does_not_skip_database_cleanup(tmp_path):
    boot = _run_boot_probe("embedding-cache-error", tmp_path)
    events = _events(boot)
    assert "embedding_cache.clear_failed" in events
    assert events.index("flush_log_queue") < events.index("close_all_databases")


# ---------------------------------------------------------------------------
# 1. Startup ran, completely, in the documented order.
# ---------------------------------------------------------------------------


def test_startup_completes_and_the_app_serves(clean_boot):
    """The whole lifespan startup ran and the app answers a real request."""
    assert clean_boot["health_status"] == 200, (
        "the app entered its lifespan but /api/v1/health returned "
        f"{clean_boot['health_status']} -- startup left it unable to serve"
    )
    assert clean_boot["loop_registered"], (
        "lifespan did not register a LIVE event loop; background research "
        "and log-queue threads dispatch Socket.IO emits onto it via "
        "run_coroutine_threadsafe, and silently no-op without it"
    )
    assert clean_boot["lock_initialised"], (
        "lifespan did not eagerly create the Socket.IO subscription lock; "
        "the first concurrent connect/subscribe events then race on lazy init"
    )


def test_startup_ran_every_documented_step_before_serving(clean_boot):
    """Each instrumented startup step fired, and all of them before traffic."""
    events = _events(clean_boot)
    startup_steps = [
        "set_main_loop",
        "init_lock",
        "start_log_queue_processor",
        "queue_processor.start",
        "start_connection_cleanup_scheduler",
    ]
    missing = [step for step in startup_steps if step not in events]
    assert not missing, (
        f"lifespan startup never called {missing}. A step dropped from the "
        f"boot sequence raises nothing -- it just leaves the corresponding "
        f"subsystem dead. Observed: {events}"
    )

    assert "serving" in events, (
        "probe premise broken: the marker recorded inside the running app "
        f"is missing, so 'before serving' cannot be checked. Trace: {events}"
    )
    serving_at = events.index("serving")
    late = [step for step in startup_steps if events.index(step) > serving_at]
    assert not late, (
        f"{late} ran AFTER the app began serving requests -- the lifespan "
        "must finish startup before `yield`, or the first requests hit a "
        "half-initialised app"
    )

    assert events.index("set_main_loop") < events.index("init_lock"), (
        "the Socket.IO lock must be created after the running loop is "
        f"registered, so it binds to uvicorn's loop. Trace: {events}"
    )


def test_startup_registers_a_live_not_closed_event_loop(clean_boot):
    """The loop handed to set_main_loop was open at the moment of the call."""
    calls = [e for e in clean_boot["trace"] if e["event"] == "set_main_loop"]
    assert len(calls) == 1, (
        f"expected exactly one set_main_loop call per boot, got {len(calls)}"
    )
    assert calls[0]["closed"] is False, (
        "lifespan registered a CLOSED event loop; every background emit "
        "scheduled onto it would raise or be dropped"
    )


def test_startup_regenerates_themes_css(clean_boot):
    """themes.css is rewritten on every boot, not just when absent."""
    after = clean_boot["css_after_startup"]
    assert after is not None, (
        "startup did not produce static/css/themes.css; every page that "
        "links it renders unthemed"
    )
    assert after["size"] > 0, "startup wrote an EMPTY themes.css"
    before = clean_boot["css_before"]
    assert before is None or after["mtime_ns"] != before["mtime_ns"], (
        "themes.css was not rewritten during startup (mtime unchanged at "
        f"{after['mtime_ns']}); a theme added to the registry would never "
        "reach the served stylesheet"
    )


@pytest.mark.skipif(os.name != "posix", reason="POSIX mode bits")
def test_startup_makes_an_existing_research_outputs_directory_private(
    clean_boot,
):
    """An upgraded install's 0755 research_outputs/ is 0700 once serving.

    The probe loosens the directory after importing the app, so neither an
    import nor an earlier lookup can be what restricted it.
    """
    assert clean_boot["outputs_mode_before"] == 0o755, (
        "premise broken: the probe did not start from a 0755 directory"
    )
    assert clean_boot["outputs_mode_during"] == 0o700, (
        "startup left research_outputs/ at "
        f"{clean_boot['outputs_mode_during']:04o}; plaintext report backups "
        "saved by an older release stay reachable by other OS accounts"
    )
    assert clean_boot["legacy_report_after_startup"] == "old report"


# ---------------------------------------------------------------------------
# 2. Shutdown released what startup acquired.
# ---------------------------------------------------------------------------


def test_the_app_really_holds_background_threads_while_running(clean_boot):
    """Positive control for every shutdown assertion below.

    Without this, "no such thread survives shutdown" would also pass on an
    app that never started one.
    """
    during = clean_boot["threads_during"]
    assert during, "probe premise broken: empty thread snapshot while running"
    absent = {
        label: needle
        for label, needle in EXPECTED_RUNNING_THREADS.items()
        if not _matching(during, needle)
    }
    assert not absent, (
        f"the running app was expected to own {absent}, but the live thread "
        f"inventory was {_thread_names(during)}. Either a subsystem no "
        "longer starts, or its thread was renamed and the shutdown checks "
        "below have stopped watching anything."
    )


def test_shutdown_stops_the_log_queue_processor(clean_boot):
    """The drain daemon is running while up and gone once shut down."""
    assert clean_boot["log_queue_alive_during"], (
        "positive control: the log-queue drain daemon was not alive while "
        "the app was running, so its absence afterwards proves nothing"
    )
    assert not clean_boot["log_queue_alive_after"], (
        "the log-queue drain daemon outlived shutdown. On a uvicorn reload "
        "the old daemon keeps draining the same queue into databases the "
        "new process is reopening."
    )
    survivors = _matching(clean_boot["threads_after"], "log-queue-processor")
    assert not survivors, (
        f"a log-queue-processor thread is still in the inventory after "
        f"shutdown: {survivors}"
    )


def test_shutdown_stops_the_schedulers_and_the_queue_processor(clean_boot):
    """APScheduler workers and the research queue loop do not survive."""
    for label, needle in EXPECTED_RUNNING_THREADS.items():
        survivors = _matching(clean_boot["threads_after"], needle)
        assert not survivors, (
            f"shutdown stranded the {label} ({needle}): {survivors}. It "
            "holds DB handles and per-user locks the next process needs."
        )


def test_shutdown_returns_the_thread_inventory_to_its_pre_boot_state(
    clean_boot,
):
    """Nothing at all is left behind — named or anonymous."""
    before = _thread_names(clean_boot["threads_before"])
    during = _thread_names(clean_boot["threads_during"])
    after = _thread_names(clean_boot["threads_after"])

    assert len(during) > len(before), (
        "probe premise broken: startup added no threads at all "
        f"(before={before}, during={during}), so the comparison below is "
        "vacuous"
    )
    assert after == before, (
        f"the thread inventory did not return to its pre-boot state.\n"
        f"  before   : {before}\n  during   : {during}\n"
        f"  right after __exit__: "
        f"{_thread_names(clean_boot['threads_after_raw'])}\n"
        f"  after a 2s join     : {after}\n"
        "Each surviving thread is a per-reload leak."
    )


def test_no_non_daemon_thread_survives_shutdown(clean_boot):
    """A stranded non-daemon thread would hang the interpreter on exit."""
    blockers = [
        entry
        for entry in clean_boot["threads_after"]
        if not entry["daemon"] and entry["name"] != "MainThread"
    ]
    assert not blockers, (
        f"non-daemon threads survived shutdown: {blockers}. The interpreter "
        "joins these at exit, so the container would hang past SIGTERM and "
        "need SIGKILL."
    )


def test_shutdown_flushes_logs_and_drains_the_scheduler_before_closing_dbs(
    clean_boot,
):
    """Ordering the shutdown comments claim, pinned.

    ``database_sink`` swallows its write errors, so anything flushed after
    ``close_all_databases`` is dropped without a trace; and cleanup jobs
    still in flight when the pool is disposed hit a closed pool.
    """
    events = _events(clean_boot)
    required = [
        "flush_log_queue",
        "stop_log_queue_processor",
        "cleanup_scheduler.shutdown",
        "queue_processor.stop",
        "close_all_databases",
    ]
    missing = [step for step in required if step not in events]
    assert not missing, (
        f"lifespan shutdown never called {missing}; observed {events}"
    )

    close_at = events.index("close_all_databases")
    too_late = [
        step
        for step in required
        if step != "close_all_databases" and events.index(step) > close_at
    ]
    assert not too_late, (
        f"{too_late} ran AFTER close_all_databases. Log writes past the "
        "close are silently dropped by database_sink, and background jobs "
        f"still running past it hit a disposed pool. Trace: {events}"
    )
    assert events.index("flush_log_queue") < events.index(
        "stop_log_queue_processor"
    ), (
        "the queue must be flushed before the drain daemon is stopped, "
        f"otherwise whatever it had not written is lost. Trace: {events}"
    )

    shutdowns = [
        e
        for e in clean_boot["trace"]
        if e["event"] == "cleanup_scheduler.shutdown"
    ]
    assert shutdowns[0]["wait"] is True, (
        "the connection-cleanup scheduler must be shut down with wait=True "
        "so in-flight cleanup jobs finish before the databases close; got "
        f"{shutdowns[0]}"
    )


def test_starting_the_log_queue_processor_twice_reuses_one_daemon(clean_boot):
    """Startup must be safe to re-run: one drain daemon, never two.

    Two daemons on the same queue interleave ``get()`` calls, so a reload
    that started a second one would split each research's log stream across
    two writers. Exercised in-process against the real functions; the
    ``clean_boot`` dependency is only for ordering, so the child-process
    inventory above is not observing this test's thread.
    """
    already_running = (
        log_utils._queue_processor_thread is not None
        and log_utils._queue_processor_thread.is_alive()
    )
    first = log_utils.start_log_queue_processor()
    try:
        assert first.is_alive(), (
            "positive control: start_log_queue_processor returned a thread "
            "that is not running"
        )
        second = log_utils.start_log_queue_processor()
        assert second is first, (
            "a second start spawned a NEW drain daemon "
            f"({second.name}) instead of reusing {first.name}"
        )
        live = [
            t
            for t in threading.enumerate()
            if t.name == "log-queue-processor" and t.is_alive()
        ]
        assert len(live) == 1, (
            f"expected exactly one live log-queue-processor, found {live}"
        )

        log_utils.stop_log_queue_processor()
        assert not first.is_alive(), (
            "stop_log_queue_processor returned while the drain daemon was "
            "still running -- shutdown does not actually stop it"
        )
    finally:
        if already_running:
            log_utils.start_log_queue_processor()
        else:
            log_utils.stop_log_queue_processor()


# ---------------------------------------------------------------------------
# 3. Startup failure is loud; guarded steps are genuinely optional.
# ---------------------------------------------------------------------------


def _drive_lifespan_protocol(app):
    """Speak the raw ASGI lifespan protocol to ``app``; return (msgs, exc).

    This is what uvicorn does: it sends ``lifespan.startup`` and refuses to
    serve unless it gets ``lifespan.startup.complete`` back.
    """
    import asyncio

    messages: list[dict] = []
    pending = [{"type": "lifespan.startup"}]

    async def receive():
        return pending.pop(0) if pending else {"type": "lifespan.shutdown"}

    async def send(message):
        messages.append(message)

    async def drive():
        try:
            await app(
                {"type": "lifespan", "asgi": {"version": "3.0"}},
                receive,
                send,
            )
        except Exception as exc:
            # Returned to the caller, not swallowed: the test asserts on it.
            return exc
        return None

    error = asyncio.run(drive())
    return messages, error


def test_a_failing_startup_step_aborts_the_boot_instead_of_serving(
    monkeypatch,
):
    """An unguarded lifespan step that raises must fail the whole startup.

    ``set_main_loop`` is the first real step and is deliberately NOT wrapped
    in try/except. Failing there also keeps this test cheap and safe to run
    in the pytest process: the lifespan aborts BEFORE ``init_lock()``, so the
    one-lifespan-per-process budget is untouched.
    """
    boom = RuntimeError("simulated lifespan step failure")

    def explode(loop):
        raise boom

    monkeypatch.setattr(socketio_asgi, "set_main_loop", explode)

    messages, error = _drive_lifespan_protocol(fastapi_app.app)
    types = [message["type"] for message in messages]

    assert "lifespan.startup.complete" not in types, (
        "the app reported a SUCCESSFUL startup although a startup step "
        f"raised; uvicorn would begin serving a half-initialised app. "
        f"Messages: {types}"
    )
    assert "lifespan.startup.failed" in types, (
        "a raising startup step produced no lifespan.startup.failed; "
        f"uvicorn keys its abort on exactly that message. Messages: {types}"
    )
    assert error is not None, (
        "the startup exception was swallowed by the lifespan instead of "
        "propagating -- nothing in the logs would name the failing step"
    )


def test_the_test_client_refuses_to_enter_when_startup_fails(monkeypatch):
    """The same failure, seen the way application code sees it."""
    from fastapi.testclient import TestClient

    def explode(loop):
        raise RuntimeError("simulated lifespan step failure")

    monkeypatch.setattr(socketio_asgi, "set_main_loop", explode)

    with pytest.raises(RuntimeError, match="simulated lifespan step failure"):
        with TestClient(fastapi_app.app):
            pytest.fail(
                "TestClient entered its context although lifespan startup "
                "raised -- a failed boot would serve requests"
            )


def test_a_guarded_startup_step_failure_does_not_stop_the_server(
    themes_broken_boot,
):
    """themes.css generation is best-effort and must not abort the boot."""
    assert themes_broken_boot["health_status"] == 200, (
        "a failure in the themes.css regeneration took the whole app down "
        f"(health={themes_broken_boot['health_status']}); that step is "
        "deliberately wrapped in try/except because a stale stylesheet is "
        "not worth refusing to boot over"
    )

    before = themes_broken_boot["css_before"]
    after = themes_broken_boot["css_after_startup"]
    assert before is not None and after is not None, (
        "premise broken: themes.css was missing around the broken-themes "
        f"boot (before={before}, after={after}), so 'the injected failure "
        "really happened' cannot be checked"
    )
    assert after["mtime_ns"] == before["mtime_ns"], (
        "positive control: themes.css was rewritten even though "
        "get_combined_css was made to raise, so this boot did not actually "
        "exercise the guarded path"
    )

    events = _events(themes_broken_boot)
    assert events.count("get_combined_css_failed") == 1, (
        "the broken theme generator was never reached exactly once; "
        f"trace was {events}"
    )
    assert "serving" in events and "close_all_databases" in events, (
        "after a guarded-step failure the app must still complete a full "
        f"startup AND shutdown cycle; trace was {events}"
    )


# ---------------------------------------------------------------------------
# 4. .secret_key: fail closed, never fall back to an ephemeral key.
# ---------------------------------------------------------------------------


def test_a_generated_secret_key_round_trips_and_is_never_regenerated(
    tmp_path, monkeypatch
):
    """O_EXCL means an existing key file is read, never clobbered."""
    monkeypatch.setattr(
        "local_deep_research.config.paths.get_data_directory",
        lambda: tmp_path,
    )
    key_file = tmp_path / ".secret_key"

    generated = fastapi_app._load_secret_key()
    assert key_file.exists(), (
        "the loader returned a key without persisting it; every restart "
        "would then invalidate all sessions"
    )
    stamp = key_file.stat().st_mtime_ns

    reloaded = fastapi_app._load_secret_key()
    assert reloaded == generated, (
        "a second load returned a DIFFERENT key "
        f"({reloaded[:8]}... vs {generated[:8]}...), so sessions signed "
        "before a restart would no longer validate"
    )
    assert key_file.stat().st_mtime_ns == stamp, (
        "the existing .secret_key was rewritten; O_EXCL is there precisely "
        "so an established key is never overwritten"
    )
    assert key_file.read_text(encoding="utf-8").strip() == generated


def test_a_new_secret_key_file_is_created_owner_only(tmp_path, monkeypatch):
    """Mode 0o600 — the session signing key must not be world-readable."""
    monkeypatch.setattr(
        "local_deep_research.config.paths.get_data_directory",
        lambda: tmp_path,
    )
    fastapi_app._load_secret_key()

    mode = (tmp_path / ".secret_key").stat().st_mode & 0o777
    assert mode == 0o600, (
        f"the .secret_key file was created with mode {oct(mode)}; anyone "
        "who can read it can forge session cookies for every user"
    )


def test_an_unreadable_secret_key_is_fatal_not_ephemeral(tmp_path, monkeypatch):
    """A directory mounted over .secret_key (a real Docker mistake) is fatal.

    Before this PR the loader fell back to an in-memory key here, which
    silently invalidated every session on every restart with no operator
    signal. It must raise instead.
    """
    monkeypatch.setattr(
        "local_deep_research.config.paths.get_data_directory",
        lambda: tmp_path,
    )
    (tmp_path / ".secret_key").mkdir()

    with pytest.raises(RuntimeError, match="Cannot read SECRET_KEY"):
        fastapi_app._load_secret_key()


def _load_secret_key_ast() -> ast.FunctionDef:
    tree = ast.parse(FASTAPI_APP_PATH.read_text(encoding="utf-8"))
    for node in tree.body:
        if (
            isinstance(node, ast.FunctionDef)
            and node.name == "_load_secret_key"
        ):
            return node
    raise AssertionError(
        f"_load_secret_key is no longer a module-level function in "
        f"{FASTAPI_APP_PATH}; this file's guards cannot locate it"
    )


def test_no_error_path_in_the_secret_key_loader_returns_a_key():
    """Structural pin on the removal of the fail-open fallback.

    The behavioural tests above cover the two reachable failures. This one
    fails if ANY future error branch is added that returns instead of
    raising — the shape the old fall-back-to-ephemeral code had.
    """
    func = _load_secret_key_ast()
    error_handlers = [
        handler
        for handler in ast.walk(func)
        if isinstance(handler, ast.ExceptHandler)
        # `except FileExistsError` is the normal "key already exists" path,
        # not an error path.
        and "FileExistsError" not in ast.unparse(handler.type or ast.Pass())
    ]
    assert len(error_handlers) >= 2, (
        "premise broken: expected the read-failure and write-failure "
        f"handlers, found {len(error_handlers)} error handlers"
    )
    for handler in error_handlers:
        rendered = ast.unparse(handler)
        raises = any(isinstance(n, ast.Raise) for n in ast.walk(handler))
        returns = any(isinstance(n, ast.Return) for n in ast.walk(handler))
        assert raises and not returns, (
            "a .secret_key error path returns instead of raising, which is "
            "the fail-open behaviour this PR removed: a silent ephemeral "
            "key invalidates every session on restart with no operator "
            f"alert.\n{rendered}"
        )


def test_a_secret_key_failure_aborts_the_import_and_therefore_the_boot():
    """``SECRET_KEY = _load_secret_key()`` must stay at module scope.

    That placement is what turns "the key cannot be read" into a refused
    boot. Moved inside a function or a try/except and the same failure
    becomes a warning on an app that is already serving.
    """
    tree = ast.parse(FASTAPI_APP_PATH.read_text(encoding="utf-8"))
    module_level = [
        node
        for node in tree.body
        if isinstance(node, ast.Assign)
        and any(
            isinstance(t, ast.Name) and t.id == "SECRET_KEY"
            for t in node.targets
        )
    ]
    assert len(module_level) == 1, (
        "expected exactly one module-level SECRET_KEY assignment in "
        f"{FASTAPI_APP_PATH}, found {len(module_level)}"
    )
    assert ast.unparse(module_level[0].value) == "_load_secret_key()", (
        "SECRET_KEY is no longer assigned directly from _load_secret_key(); "
        f"got {ast.unparse(module_level[0].value)!r}. If the call is wrapped "
        "or deferred, a broken key file stops aborting the boot."
    )


# ---------------------------------------------------------------------------
# 5. timeout_graceful_shutdown is configured, and the docs agree with it.
# ---------------------------------------------------------------------------


def _uvicorn_run_call() -> ast.Call:
    tree = ast.parse(SERVER_ENTRYPOINT_PATH.read_text(encoding="utf-8"))
    calls = [
        node
        for node in ast.walk(tree)
        if isinstance(node, ast.Call)
        and ast.unparse(node.func) == "uvicorn.run"
    ]
    assert len(calls) == 1, (
        f"expected exactly one uvicorn.run() call in "
        f"{SERVER_ENTRYPOINT_PATH}, found {len(calls)}"
    )
    return calls[0]


def test_graceful_shutdown_timeout_is_pinned_at_ten_seconds():
    """Without it uvicorn waits forever on research SSE streams.

    Hardcoded on purpose (no env override), so pin the literal: a silent
    change to this number changes how long uvicorn drains requests before it
    sends the lifespan shutdown event, and that time counts against the
    Compose ``stop_grace_period`` (see ``test_deployment_surface_contracts``).
    """
    call = _uvicorn_run_call()
    kwargs = {kw.arg: kw.value for kw in call.keywords}
    assert "timeout_graceful_shutdown" in kwargs, (
        "uvicorn.run() no longer passes timeout_graceful_shutdown; uvicorn's "
        "default is None, i.e. wait forever for in-flight requests, and a "
        "long-lived research SSE stream then keeps the process alive past "
        f"SIGTERM. kwargs: {sorted(k for k in kwargs if k)}"
    )
    value = kwargs["timeout_graceful_shutdown"]
    assert isinstance(value, ast.Constant), (
        "timeout_graceful_shutdown is no longer a literal (got "
        f"{ast.unparse(value)!r}). If that is a deliberate env override, "
        "this test and the lifespan comment quoting '10s' both need updating."
    )
    assert value.value == 10, (
        f"timeout_graceful_shutdown changed from 10 to {value.value!r}"
    )

    assert kwargs.get("workers") is not None, (
        "premise broken: uvicorn.run() no longer pins workers, so the "
        "single-worker reasoning in this file's docstring may not hold"
    )
    assert (
        isinstance(kwargs["workers"], ast.Constant)
        and kwargs["workers"].value == 1
    ), (
        "the app is no longer single-worker; Socket.IO without a Redis "
        "message queue requires workers=1, and the whole-instance blast "
        f"radius argued here assumes it. Got {ast.unparse(kwargs['workers'])}"
    )


def test_the_lifespan_comment_quotes_the_real_shutdown_timeout():
    """Doc/code drift check on the number the lifespan reasons about."""
    call = _uvicorn_run_call()
    kwargs = {kw.arg: kw.value for kw in call.keywords}
    configured_node = kwargs.get("timeout_graceful_shutdown")
    assert isinstance(configured_node, ast.Constant), (
        "premise broken: web/app.py no longer passes a literal "
        "timeout_graceful_shutdown, so the comment in fastapi_app.py cannot "
        "be compared against it (the sibling test reports what changed). "
        f"Got {ast.unparse(configured_node) if configured_node else None!r}"
    )
    configured = configured_node.value

    source = FASTAPI_APP_PATH.read_text(encoding="utf-8")
    quoted = re.findall(r"timeout_graceful_shutdown`?\s*\((\d+)s", source)
    assert quoted, (
        "premise broken: the lifespan no longer quotes "
        "timeout_graceful_shutdown's value when explaining the request-drain "
        f"budget, so drift cannot be detected. {FASTAPI_APP_PATH}"
    )
    mismatched = [n for n in quoted if int(n) != configured]
    assert not mismatched, (
        f"the lifespan comment says timeout_graceful_shutdown is "
        f"{mismatched}s but web/app.py passes {configured}s. That comment is "
        "load-bearing: it is the request-drain budget that the Compose "
        "stop_grace_period is sized against."
    )