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.

12.7 KB
Raw Blame
"""Behavioural coverage for every best-effort lifespan startup guard.

FastAPI reports ``lifespan.startup.failed`` whenever an exception escapes the
lifespan before its ``yield``.  These startup steps are deliberately optional,
so merely proving that their calls are textually inside ``try`` statements is
not enough: each realistic failure must still leave an application that serves
requests and then performs the rest of its shutdown.

``socketio_asgi.init_lock()`` creates process-global state tied to the running
event loop.  The probe therefore gets a fresh interpreter and enters the real
lifespan exactly once.  It injects all five independent guarded failures into
that one boot; if any guard lets its exception escape, the later probes and the
serving boundary are never reached.  The optional background services are tiny
recording fakes, keeping the test deterministic while still exercising the real
lifespan control flow, logging, ASGI startup boundary, and teardown.
"""

# allow: no-sut-import — the real FastAPI lifespan runs in the probe process

from __future__ import annotations

import json
import os
import subprocess
import sys
from pathlib import Path

import pytest


REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
SRC_ROOT = REPOSITORY_ROOT / "src"


_PROBE_SOURCE = r"""
import asyncio
import json
import sys
from pathlib import Path

import anyio.to_thread


MODE = sys.argv[1]
OUT = Path(sys.argv[2])
WORK_DIR = OUT.parent
LIVE_TRACE = WORK_DIR / "live-trace.json"
trace = []


def record(event, **fields):
    fields["event"] = event
    trace.append(fields)
    LIVE_TRACE.write_text(json.dumps(trace, indent=1), encoding="utf-8")


class RecordingLogger:
    def _record(self, level, message, *args, **kwargs):
        try:
            rendered = str(message).format(*args)
        except (IndexError, KeyError, ValueError):
            rendered = str(message)
        record("log", level=level, message=rendered)

    def info(self, message, *args, **kwargs):
        self._record("INFO", message, *args, **kwargs)

    def warning(self, message, *args, **kwargs):
        self._record("WARNING", message, *args, **kwargs)

    def debug(self, message, *args, **kwargs):
        self._record("DEBUG", message, *args, **kwargs)

    def exception(self, message, *args, **kwargs):
        self._record("ERROR", message, *args, **kwargs)


from local_deep_research.database import encrypted_db, sqlcipher_utils
from local_deep_research.settings import env_registry
from local_deep_research.settings import manager as settings_manager_module
from local_deep_research.scheduler import background
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


# Keep all writes inside the probe's temporary directory.  The lifespan always
# regenerates themes.css, even though CSS generation is unrelated to this test.
static_dir = WORK_DIR / "static"
(static_dir / "css").mkdir(parents=True)
fastapi_app.STATIC_DIR = str(static_dir)
fastapi_app.logger = RecordingLogger()


# The first two calls are deliberately left in the real lifespan sequence, but
# their process-global products are irrelevant to these guard tests.
socketio_asgi.set_main_loop = lambda loop: record(
    "set_main_loop", closed=loop.is_closed()
)
socketio_asgi.init_lock = lambda: record("init_lock")


class InvalidThreadCount:
    def __bool__(self):
        return True

    def __int__(self):
        record("invalid_threadpool_value_converted")
        raise ValueError("simulated invalid threadpool setting")


def get_env_setting(key, default=None):
    if key == "web.threadpool_max_threads":
        return InvalidThreadCount() if MODE == "all" else None
    if key == "web.queue_processor.enabled":
        return False
    if key == "news.scheduler.enabled":
        return MODE == "all"
    return default


class Registry:
    @staticmethod
    def get_setting_object(key):
        return None


env_registry.get_env_setting = get_env_setting
env_registry.registry = Registry()


# Exercise the weak-KDF branch only in its own mode.  Encryption availability
# is a deployment property, so force it on for this probe rather than depending
# on whether the test host has SQLCipher installed.
encrypted_db.db_manager.has_encryption = MODE == "all"


def weak_kdf_check(data_dir):
    record("weak_kdf_check", data_dir=str(data_dir))
    if MODE == "all":
        raise RuntimeError("simulated weak-KDF check failure")


sqlcipher_utils.warn_if_weak_kdf_with_existing_databases = weak_kdf_check


def start_log_queue_processor(*args, **kwargs):
    record("start_log_queue_processor")
    if MODE == "all":
        record("log_queue_start_failure_injected")
        raise RuntimeError("simulated log queue thread start failure")


def flush_log_queue(*args, **kwargs):
    record("flush_log_queue")


def stop_log_queue_processor(*args, **kwargs):
    record("stop_log_queue_processor")


log_utils.start_log_queue_processor = start_log_queue_processor
log_utils.flush_log_queue = flush_log_queue
log_utils.stop_log_queue_processor = stop_log_queue_processor


queue_processor.start = lambda *args, **kwargs: record("queue_processor.start")
queue_processor.stop = lambda *args, **kwargs: record("queue_processor.stop")


class FakeSettingsManager:
    def __init__(self):
        record("SettingsManager")


class NewsScheduler:
    def initialize_with_settings(self, settings_manager):
        record("news_scheduler.initialize_with_settings")
        if MODE == "all":
            record("news_scheduler_init_failure_injected")
            raise RuntimeError("simulated news scheduler init failure")

    def start(self):
        record("news_scheduler.start")

    def stop(self):
        record("news_scheduler.stop")


news_scheduler = NewsScheduler()
settings_manager_module.SettingsManager = FakeSettingsManager
background.get_background_job_scheduler = lambda: news_scheduler


class CleanupScheduler:
    def shutdown(self, wait=True):
        record("cleanup_scheduler.shutdown", wait=wait)


cleanup_scheduler = CleanupScheduler()


def start_connection_cleanup_scheduler(*args, **kwargs):
    record("start_connection_cleanup_scheduler")
    if MODE == "all":
        record("cleanup_scheduler_start_failure_injected")
        raise RuntimeError("simulated cleanup scheduler start failure")
    return cleanup_scheduler


connection_cleanup.start_connection_cleanup_scheduler = (
    start_connection_cleanup_scheduler
)


def close_all_databases():
    record("close_all_databases")


encrypted_db.db_manager.close_all_databases = close_all_databases


result = {"mode": MODE}


async def run_lifespan_once():
    # Exercise the exact context manager wired into FastAPI.  Entering the
    # body proves startup reached its yield; an escaped guard failure would
    # raise here and FastAPI/uvicorn would report lifespan.startup.failed.
    limiter = anyio.to_thread.current_default_thread_limiter()
    result["threadpool_tokens_before"] = limiter.total_tokens
    async with fastapi_app.app.router.lifespan_context(fastapi_app.app):
        result["boot_completed"] = True
        result["threadpool_tokens_during"] = limiter.total_tokens
        record("serving")


asyncio.run(run_lifespan_once())

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


def _run_guard_probe(mode: str, workdir: Path) -> dict:
    """Run one guarded failure through a real ASGI lifespan cycle."""
    script = workdir / "guard_probe.py"
    script.write_text(_PROBE_SOURCE, encoding="utf-8")
    out = workdir / "result.json"
    data_dir = workdir / "data"
    data_dir.mkdir()

    env = dict(os.environ)
    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)

    completed = subprocess.run(
        [sys.executable, str(script), mode, str(out)],
        env=env,
        capture_output=True,
        text=True,
        timeout=180,
    )
    if completed.returncode != 0 or not out.exists():
        pytest.fail(
            f"lifespan guard probe ({mode}) failed with exit code "
            f"{completed.returncode}\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 guarded_failure_boot(tmp_path_factory) -> dict:
    """One isolated boot with every best-effort startup step failing."""
    return _run_guard_probe(
        "all", tmp_path_factory.mktemp("guarded_failure_boot")
    )


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


def _matching_logs(result: dict, text: str) -> list[dict]:
    return [
        entry
        for entry in result["trace"]
        if entry["event"] == "log" and text in entry["message"]
    ]


def _assert_booted_and_completed_shutdown(result: dict) -> None:
    events = _events(result)
    assert result["boot_completed"] is True, (
        f"guarded failure stopped the lifespan reaching its serving state: "
        f"{result}"
    )
    assert "serving" in events

    required_shutdown = [
        "queue_processor.stop",
        "flush_log_queue",
        "stop_log_queue_processor",
        "close_all_databases",
    ]
    missing = [event for event in required_shutdown if event not in events]
    assert not missing, (
        f"the guarded startup failure skipped later shutdown steps {missing}; "
        f"trace={events}"
    )
    close_at = events.index("close_all_databases")
    assert all(
        events.index(event) < close_at for event in required_shutdown[:-1]
    )


def test_invalid_threadpool_setting_is_logged_and_boot_uses_the_default(
    guarded_failure_boot,
):
    result = guarded_failure_boot

    _assert_booted_and_completed_shutdown(result)
    events = _events(result)
    assert "invalid_threadpool_value_converted" in events
    assert (
        result["threadpool_tokens_during"] == result["threadpool_tokens_before"]
    )
    logs = _matching_logs(result, "Could not apply web.threadpool_max_threads")
    assert [entry["level"] for entry in logs] == ["ERROR"]
    assert result["trace"].index(logs[0]) < events.index("serving")


def test_weak_kdf_check_failure_is_logged_without_blocking_boot(
    guarded_failure_boot,
):
    result = guarded_failure_boot

    _assert_booted_and_completed_shutdown(result)
    assert _events(result).count("weak_kdf_check") == 1
    logs = _matching_logs(result, "Weak-KDF startup configuration check failed")
    assert [entry["level"] for entry in logs] == ["ERROR"]
    assert result["trace"].index(logs[0]) < _events(result).index("serving")


def test_log_queue_start_failure_still_flushes_and_stops_at_shutdown(
    guarded_failure_boot,
):
    result = guarded_failure_boot

    _assert_booted_and_completed_shutdown(result)
    events = _events(result)
    assert events.count("log_queue_start_failure_injected") == 1
    assert events.index("log_queue_start_failure_injected") < events.index(
        "serving"
    )
    assert events.index("flush_log_queue") < events.index(
        "stop_log_queue_processor"
    )
    logs = _matching_logs(result, "Failed to start log queue processor")
    assert [entry["level"] for entry in logs] == ["ERROR"]
    assert result["trace"].index(logs[0]) < events.index("serving")


def test_news_scheduler_init_failure_stops_the_partially_acquired_scheduler(
    guarded_failure_boot,
):
    result = guarded_failure_boot

    _assert_booted_and_completed_shutdown(result)
    events = _events(result)
    assert events.count("news_scheduler_init_failure_injected") == 1
    assert "news_scheduler.start" not in events
    assert events.count("news_scheduler.stop") == 1
    assert events.index("news_scheduler.stop") < events.index(
        "close_all_databases"
    )
    logs = _matching_logs(result, "Failed to initialize news scheduler")
    assert [entry["level"] for entry in logs] == ["ERROR"]
    assert result["trace"].index(logs[0]) < events.index("serving")


def test_cleanup_scheduler_start_failure_skips_its_shutdown_but_not_the_rest(
    guarded_failure_boot,
):
    result = guarded_failure_boot

    _assert_booted_and_completed_shutdown(result)
    events = _events(result)
    assert events.count("cleanup_scheduler_start_failure_injected") == 1
    assert "cleanup_scheduler.shutdown" not in events
    logs = _matching_logs(result, "Failed to start cleanup scheduler")
    assert [entry["level"] for entry in logs] == ["WARNING"]
    assert result["trace"].index(logs[0]) < events.index("serving")