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.

46.3 KB
Raw Blame
# allow: no-sut-import - this is a guardian test. It compares what the
# app OFFERS (the settings catalogue JSON, docs/CONFIGURATION.md, the JS
# dropdowns, the route decorators) against what src/ actually READS, so
# it must treat the tree as DATA rather than import it: importing a
# module executes its side effects and can make a key look "read" that
# no runtime path reaches. The route census here is therefore a static
# parse (314 routes) rather than a live app.routes read; it is
# corroborated by tests/web/test_js_fetch_parity.py, which DOES import
# the app and counts 317 -- the gap is accounted for by the fastapi pin
# comment in the app module. Floors and per-detector positive/negative
# controls guard against a parser that silently stops matching.
"""Sweep for features that are ADVERTISED but cannot work.

The defect shape hunted here is narrow and deliberate: something that is
*offered to someone* — a checkbox in the settings UI, a row in
``docs/CONFIGURATION.md``, an HTTP route, a registry entry a caller can
name — and that, once selected, does nothing at all.

This is not a dead-code detector. A private helper with no caller is
"unused but correct" and is explicitly out of scope; several such helpers
are used below as *negative controls* precisely because they must NOT be
flagged. What counts is the gap between the promise and the wiring.

Every check is static (``ast`` / ``json`` / text). Nothing here imports
the application, starts a server, or touches a database, so the module is
cheap enough to run in any environment.

Each sweep carries three anti-vacuity guards:

1. a *floor* on how much was actually examined (a broken walk that finds
   nothing must fail, not pass);
2. a *positive control* — a synthetic dead thing the detector is fed and
   must flag;
3. a *negative control* — a live thing it must not flag.
"""

from __future__ import annotations

import ast
import functools
import io
import json
import re
import tokenize
from pathlib import Path

import pytest

REPO_ROOT = Path(__file__).resolve().parents[1]
SRC = REPO_ROOT / "src" / "local_deep_research"
DOCS = REPO_ROOT / "docs"
DEFAULTS = SRC / "defaults"
ROUTERS = SRC / "web" / "routers"
WEB = SRC / "web"


# ---------------------------------------------------------------------------
# Shared helpers
# ---------------------------------------------------------------------------


@functools.cache
def _read(path: Path) -> str:
    return path.read_text(encoding="utf-8", errors="ignore")


# ---------------------------------------------------------------------------
# Section 1 — the assembled FastAPI route table
#
# FastAPI/Starlette routing is first-match-wins. A second registration of the
# same (method, path), or a literal path registered *after* a parameterised
# path that already matches it, is unreachable: the endpoint appears in the
# OpenAPI schema and in the frontend's URL registry, but no request ever
# reaches it.
# ---------------------------------------------------------------------------

# Mount order as assembled by ``fastapi_app._mount_all``: ``api_v1`` seeds the
# dict, then ``_router_modules`` is appended in order. Dicts preserve insertion
# order, and ``include_router`` appends to ``app.routes`` in iteration order.
ROUTER_MOUNT_ORDER = [
    "api_v1",
    "auth",
    "research",
    "history",
    "settings",
    "metrics",
    "api",
    "context_overflow_api",
    "news_flask_api",
    "news_pages",
    "benchmark",
    "followup",
    "library",
    "rag",
    "library_delete",
    "library_search",
    "zotero",
    "notes",
    "unified_search",
    "scheduler",
    "chat",
]

_HTTP_DECORATORS = {
    "get",
    "post",
    "put",
    "delete",
    "patch",
    "options",
    "head",
    "api_route",
    "websocket",
}


def _router_prefixes(tree: ast.Module) -> dict[str, str]:
    """Map local variable name -> APIRouter(prefix=...) literal."""
    prefixes: dict[str, str] = {}
    for node in ast.walk(tree):
        if not (
            isinstance(node, ast.Assign) and isinstance(node.value, ast.Call)
        ):
            continue
        func = node.value.func
        name = (
            func.attr
            if isinstance(func, ast.Attribute)
            else getattr(func, "id", None)
        )
        if name != "APIRouter":
            continue
        prefix = ""
        for kw in node.value.keywords:
            if kw.arg == "prefix" and isinstance(kw.value, ast.Constant):
                prefix = kw.value.value
        for target in node.targets:
            if isinstance(target, ast.Name):
                prefixes[target.id] = prefix
    return prefixes


@functools.cache
def collect_routes() -> list[dict]:
    """Extract (method, path) for every router endpoint, in registration order.

    Ordering is (mount order of the module, source line inside the module),
    which is exactly the order ``include_router`` appends them to
    ``app.routes`` — and therefore exactly the order Starlette matches them.
    """
    order = ROUTER_MOUNT_ORDER
    routes: list[dict] = []
    for mount_index, module_name in enumerate(order):
        path = ROUTERS / f"{module_name}.py"
        tree = ast.parse(_read(path))
        prefixes = _router_prefixes(tree)
        for node in ast.walk(tree):
            if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
                continue
            for deco in node.decorator_list:
                if not (
                    isinstance(deco, ast.Call)
                    and isinstance(deco.func, ast.Attribute)
                    and deco.func.attr in _HTTP_DECORATORS
                    and deco.args
                    and isinstance(deco.args[0], ast.Constant)
                ):
                    continue
                owner = getattr(deco.func.value, "id", None)
                if owner not in prefixes:
                    continue
                verb = deco.func.attr
                full = prefixes[owner] + deco.args[0].value
                if verb == "websocket":
                    methods = ["WEBSOCKET"]
                elif verb == "api_route":
                    methods = ["GET"]
                    for kw in deco.keywords:
                        if kw.arg == "methods" and isinstance(
                            kw.value, (ast.List, ast.Tuple)
                        ):
                            methods = [
                                e.value
                                for e in kw.value.elts
                                if isinstance(e, ast.Constant)
                            ]
                else:
                    methods = [verb.upper()]
                for method in methods:
                    routes.append(
                        {
                            "method": method,
                            "path": full,
                            "module": module_name,
                            "endpoint": node.name,
                            "lineno": node.lineno,
                            "sort": (mount_index, node.lineno),
                        }
                    )
    routes.sort(key=lambda r: r["sort"])
    return routes


def _segments(path: str) -> list[str]:
    return path.strip("/").split("/")


def find_unreachable_routes(routes: list[dict]) -> list[str]:
    """Return human-readable descriptions of routes no request can reach."""
    problems: list[str] = []
    for i, route in enumerate(routes):
        for earlier in routes[:i]:
            if earlier["method"] != route["method"]:
                continue
            if earlier["path"] == route["path"]:
                problems.append(
                    f"{route['method']} {route['path']} "
                    f"({route['module']}:{route['endpoint']}) duplicates "
                    f"{earlier['module']}:{earlier['endpoint']}"
                )
                break
            mine, theirs = _segments(route["path"]), _segments(earlier["path"])
            if len(mine) != len(theirs):
                continue
            covered = True
            has_param = False
            for a, b in zip(mine, theirs):
                if b.startswith("{"):
                    has_param = True
                    continue
                if a != b:
                    covered = False
                    break
            if covered and has_param:
                problems.append(
                    f"{route['method']} {route['path']} "
                    f"({route['module']}:{route['endpoint']}) is shadowed by "
                    f"earlier {earlier['path']} "
                    f"({earlier['module']}:{earlier['endpoint']})"
                )
                break
    return problems


def test_route_extractor_examines_the_whole_route_table():
    """Floor: a broken walk that silently finds nothing must fail here."""
    routes = collect_routes()
    assert len(routes) >= 300, (
        f"only {len(routes)} routes extracted; the AST walk is broken and "
        "every downstream route assertion would be vacuous"
    )
    modules = {r["module"] for r in routes}
    assert modules == set(ROUTER_MOUNT_ORDER), (
        "routers contributing no routes: "
        f"{sorted(set(ROUTER_MOUNT_ORDER) - modules)}"
    )
    # Named landmarks: if these three disappear the extractor is not looking
    # at the real handlers any more.
    pairs = {(r["method"], r["path"]) for r in routes}
    for expected in [
        ("POST", "/api/start_research"),
        ("GET", "/settings/api/{key}"),
        ("GET", "/library/api/collections"),
    ]:
        assert expected in pairs, f"landmark route missing: {expected}"


def test_route_shadowing_detector_positive_control():
    """The detector must flag a synthetic dead route."""
    synthetic = [
        {
            "method": "GET",
            "path": "/settings/api/{key}",
            "module": "settings",
            "endpoint": "get_setting",
            "lineno": 1,
            "sort": (0, 1),
        },
        {
            "method": "GET",
            "path": "/settings/api/types",
            "module": "settings",
            "endpoint": "get_types",
            "lineno": 2,
            "sort": (0, 2),
        },
        {
            "method": "POST",
            "path": "/api/start_research",
            "module": "research",
            "endpoint": "start_research",
            "lineno": 3,
            "sort": (1, 3),
        },
        {
            "method": "POST",
            "path": "/api/start_research",
            "module": "chat",
            "endpoint": "start_research_again",
            "lineno": 4,
            "sort": (2, 4),
        },
    ]
    found = find_unreachable_routes(synthetic)
    assert any("/settings/api/types" in f and "shadowed" in f for f in found)
    assert any("/api/start_research" in f and "duplicates" in f for f in found)


def test_route_shadowing_detector_negative_control():
    """Correctly ordered literal-before-parameter routes must not be flagged."""
    synthetic = [
        {
            "method": "GET",
            "path": "/settings/api/types",
            "module": "settings",
            "endpoint": "get_types",
            "lineno": 1,
            "sort": (0, 1),
        },
        {
            "method": "GET",
            "path": "/settings/api/{key}",
            "module": "settings",
            "endpoint": "get_setting",
            "lineno": 2,
            "sort": (0, 2),
        },
        # Same path, different verbs — both reachable.
        {
            "method": "PUT",
            "path": "/settings/api/{key}",
            "module": "settings",
            "endpoint": "put_setting",
            "lineno": 3,
            "sort": (0, 3),
        },
    ]
    assert find_unreachable_routes(synthetic) == []


def test_no_advertised_route_is_unreachable():
    """RESULT: clean. The real route table has no dead endpoint.

    Recorded as a deliberate negative finding: /settings/api/{key} and
    /notes/api/notes/{note_id} are both declared *after* every literal
    sibling that would otherwise be swallowed, so the ordering hazard the
    port could easily have introduced was not introduced.
    """
    routes = collect_routes()
    problems = find_unreachable_routes(routes)
    assert problems == [], "unreachable routes:\n" + "\n".join(problems)


# ---------------------------------------------------------------------------
# Section 2 — settings the UI offers and nothing consumes
#
# ``defaults/**/*.json`` is the settings catalogue. An entry with
# ``visible: true`` and ``editable: true`` is rendered as a live control on
# the settings page (``static/js/components/settings.js`` skips only
# ``!setting.visible``) and almost all of them additionally get a row in
# ``docs/CONFIGURATION.md``. Flipping one is therefore an explicit promise.
#
# The detector below is deliberately CONSERVATIVE — it errs towards calling a
# setting live — so that anything it does flag is a real, hand-checkable
# defect rather than a lint opinion:
#
#   * comments and docstrings are stripped, so a key merely *mentioned* in
#     prose does not count as a reader;
#   * an f-string / template-literal prefix (``notifications.on_``,
#     ``app.warnings.dismiss_``, ``news.scheduler.``,
#     ``search.engine.web.``) marks every key under it as possibly read;
#   * migrations, ``env_definitions`` and the defaults themselves are not
#     readers: repairing stored values does not consume configuration.
# ---------------------------------------------------------------------------

SETTINGS_SOURCE_EXCLUDED_DIRS = {"defaults", "migrations", "env_definitions"}

# (path relative to the package root, function name) whose body names setting
# keys without ever acting on their values.
SETTINGS_NON_READER_FUNCTIONS: set[tuple[str, str]] = set()


@functools.cache
def _strip_prose(path: Path) -> str:
    """Return source with comments, docstrings and non-reader functions gone."""
    text = _read(path)
    if path.suffix != ".py":
        return re.sub(r"(?m)^\s*//.*$", "", text)

    try:
        tree = ast.parse(text)
    except SyntaxError:
        return text

    rel = path.relative_to(SRC).as_posix()
    drop: set[int] = set()
    for node in ast.walk(tree):
        if (
            isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
            and (
                rel,
                node.name,
            )
            in SETTINGS_NON_READER_FUNCTIONS
        ):
            drop.update(
                range(node.lineno - 1, (node.end_lineno or node.lineno))
            )
    if drop:
        text = "".join(
            line
            for i, line in enumerate(text.splitlines(True))
            if i not in drop
        )

    try:
        stripped = "\n".join(
            tok.string
            for tok in tokenize.generate_tokens(io.StringIO(text).readline)
            if tok.type != tokenize.COMMENT
        )
    except Exception:
        stripped = text

    try:
        tree = ast.parse(text)
    except SyntaxError:
        return stripped
    for node in ast.walk(tree):
        if isinstance(
            node,
            (ast.Module, ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef),
        ):
            doc = ast.get_docstring(node, clean=False)
            if doc:
                stripped = stripped.replace(doc, "")
    return stripped


@functools.cache
def _settings_reader_files() -> tuple[Path, ...]:
    out = []
    for path in SRC.rglob("*"):
        if not path.is_file() or path.suffix not in {".py", ".js", ".html"}:
            continue
        if "__pycache__" in path.parts:
            continue
        if SETTINGS_SOURCE_EXCLUDED_DIRS & set(path.parts):
            continue
        out.append(path)
    return tuple(sorted(out))


_KEYISH = re.compile(r"[a-z0-9_]+(\.[a-z0-9_]*)+")


def _constructed_key_prefixes(files: tuple[Path, ...], joined: str) -> set[str]:
    """Prefixes of dynamically built setting keys, e.g. ``notifications.on_``.

    Only prefixes specific enough to name a family are kept (three dotted
    components, or a trailing underscore). A bare ``app.`` from
    ``f"app.{name}"`` would excuse every ``app.*`` key and is discarded.
    """
    prefixes: set[str] = set()

    def offer(head: str) -> None:
        if not _KEYISH.fullmatch(head):
            return
        if len(head.split(".")) >= 3 or head.endswith("_"):
            prefixes.add(head)

    for path in files:
        if path.suffix != ".py":
            continue
        try:
            tree = ast.parse(_read(path))
        except SyntaxError:
            continue
        for node in ast.walk(tree):
            if not isinstance(node, ast.JoinedStr):
                continue
            head = ""
            for value in node.values:
                if isinstance(value, ast.Constant) and isinstance(
                    value.value, str
                ):
                    head += value.value
                else:
                    break
            offer(head)
    for match in re.finditer(r"`([a-z0-9_]+(?:\.[a-z0-9_]*)+)\$\{", joined):
        offer(match.group(1))
    return prefixes


@functools.cache
def load_settings_catalogue() -> dict[str, dict]:
    catalogue: dict[str, dict] = {}
    for path in DEFAULTS.rglob("*.json"):
        try:
            blob = json.loads(_read(path))
        except (ValueError, OSError):
            continue
        if not isinstance(blob, dict):
            continue
        for key, meta in blob.items():
            if isinstance(meta, dict) and "ui_element" in meta:
                catalogue[key] = meta
    return catalogue


@functools.cache
def _reader_corpus() -> tuple[str, frozenset[str]]:
    """The (prose-stripped source blob, constructed-key prefixes) pair.

    Cached: the parametrised findings below would otherwise re-read every
    source file in the package once per case.
    """
    files = _settings_reader_files()
    joined = "\n".join(_strip_prose(f) for f in files)
    return joined, frozenset(_constructed_key_prefixes(files, joined))


def find_settings_with_no_reader(
    catalogue: dict[str, dict],
) -> list[str]:
    joined, prefixes = _reader_corpus()
    offered = {
        k
        for k, meta in catalogue.items()
        if meta.get("visible") and meta.get("editable")
    }
    return sorted(
        key
        for key in offered
        if key not in joined and not any(key.startswith(p) for p in prefixes)
    )


# Hand-verified: every entry below is rendered as a live control on the
# settings page and (except where noted) documented in docs/CONFIGURATION.md,
# and nothing anywhere reads its stored value.
SETTINGS_OFFERED_BUT_UNREAD = {
    # --- flat-out dead switches -------------------------------------------
    "app.enable_file_logging": (
        "checkbox 'Enable File Logging'. The only file-sink gate is "
        "utilities/log_utils.py, which reads os.environ['LDR_ENABLE_FILE_"
        "LOGGING'] and states outright that 'Database settings are not "
        "available at logger initialization time'. The stored setting is "
        "never consulted; CONFIGURATION.md also advertises the env var as "
        "LDR_APP_ENABLE_FILE_LOGGING, which nothing reads either."
    ),
    "app.enable_notifications": (
        "checkbox 'Enable Notifications' — CONFIGURATION.md promises browser "
        "push alerts when research completes. No reader."
    ),
    "app.enable_web": (
        "checkbox 'Enable Web Server' — CONFIGURATION.md: 'When disabled, "
        "the application runs in CLI-only mode.' No reader; the web server "
        "starts regardless."
    ),
    "app.web_interface": (
        "checkbox 'Web Interface' — a second, separately documented control "
        "making the same promise as app.enable_web. Also unread."
    ),
    "general.knowledge_accumulation": (
        "select with ITERATION / QUESTION / NO_KNOWLEDGE, documented as "
        "controlling how knowledge is compressed between iterations. No "
        "reader; utilities/enums.py:KnowledgeAccumulationApproach (the enum "
        "those option values belong to) has zero usages, and search_system.py "
        "passes knowledge_accumulation_mode=True as a literal."
    ),
    "notifications.enabled": (
        "checkbox 'Enable Notifications', documented as 'When disabled, no "
        "notifications are sent regardless of individual event settings "
        "below.' NotificationManager.send_notification gates on the env-only "
        "LDR_NOTIFICATIONS_ALLOW_OUTBOUND, on notifications.on_<event> and on "
        "notifications.service_url — never on this master switch, so "
        "unchecking it does not stop outbound webhooks."
    ),
    "report.export_formats": (
        "multiselect, documented in CONFIGURATION.md *and* in "
        "text_optimization/README.md ('Export formats can be configured to "
        "automatically export reports in multiple formats'). The export "
        "endpoint validates against exporters.ExporterRegistry instead, so "
        "the picker neither adds nor removes a format."
    ),
    "report.searches_per_section": (
        "number input; searches_per_section is only ever a Python default "
        "argument (report_generator.py, mcp/server.py). No settings read."
    ),
    "search.searches_per_section": (
        "number input making the same promise as report.searches_per_section, "
        "separately documented, equally unread."
    ),
    # --- the news settings block ------------------------------------------
    "news.display.default_headline_max_length": "number input, no reader.",
    "news.display.max_query_length": (
        "number input, no reader (the similarly named read in "
        "source_based_strategy.py is app.max_user_query_length)."
    ),
    "news.preferences.max_stored": "number input, no reader.",
    "news.refresh.default_hours": "number input, no reader.",
    "news.storage.default_limit": (
        "number input, no reader — the news feed endpoints read "
        "news.feed.default_limit, a different key."
    ),
    "news.subscription.default_type": "select, no reader.",
    "news.trending.lookback_hours": "number input, no reader.",
}


def test_settings_catalogue_is_fully_walked():
    """Floor: the catalogue walk must actually see the settings."""
    catalogue = load_settings_catalogue()
    assert len(catalogue) >= 500, (
        f"only {len(catalogue)} settings parsed from {DEFAULTS}; the walk is "
        "broken and the dead-setting assertions would be vacuous"
    )
    offered = [
        k
        for k, m in catalogue.items()
        if m.get("visible") and m.get("editable")
    ]
    assert len(offered) >= 400, (
        f"only {len(offered)} user-editable settings found; expected the bulk "
        "of the catalogue to be user-facing"
    )
    assert len(_settings_reader_files()) >= 500, (
        "the reader-file walk collected too few files to trust a negative"
    )


def test_dead_setting_detector_positive_control():
    """A synthetic key nothing reads must be flagged."""
    catalogue = dict(load_settings_catalogue())
    catalogue["zzz.definitely_not_wired.flag"] = {
        "visible": True,
        "editable": True,
        "ui_element": "checkbox",
        "value": False,
    }
    found = find_settings_with_no_reader(catalogue)
    assert "zzz.definitely_not_wired.flag" in found


def test_dead_setting_detector_negative_control():
    """Live settings — including ones read only via a built key — stay clear."""
    found = set(find_settings_with_no_reader(load_settings_catalogue()))
    live = [
        # read as f"notifications.on_{event_type.value}"
        "notifications.on_research_completed",
        # read as f"app.warnings.dismiss_{name}_private_url"
        "app.warnings.dismiss_searxng_private_url",
        # read as f"news.scheduler.{key}"
        "news.scheduler.retention_hours",
        # plain literal reads
        "security.session_timeout_hours",
        "focused_iteration.adaptive_questions",
        "langgraph_agent.max_iterations",
        "news.feed.default_limit",
        "search.favorites",
    ]
    for key in live:
        assert key not in found, f"false positive: {key} does have a reader"


@pytest.mark.xfail(
    strict=True,
    reason=(
        "KNOWN DEFECT, pre-existing (not introduced by #3299). Each key "
        "here renders a visible, editable control -- most with a row in "
        "docs/CONFIGURATION.md -- that nothing in src/ ever reads, so "
        "flipping it changes nothing. strict=True on purpose: fixing any "
        "one flips this to XPASS and fails the run, so the fixer is told "
        "to move the key out of SETTINGS_OFFERED_BUT_UNREAD rather than "
        "leave a stale entry. The floor that stops the list growing "
        "silently is test_no_further_settings_are_offered_but_unread, "
        "which PASSES."
    ),
)
@pytest.mark.parametrize(
    "key", sorted(SETTINGS_OFFERED_BUT_UNREAD), ids=lambda k: k
)
def test_settings_offered_in_the_ui_are_actually_read(key):
    """Each of these renders a control that changes nothing when flipped."""
    catalogue = load_settings_catalogue()
    meta = catalogue.get(key)
    assert meta is not None, f"{key} vanished from the settings catalogue"
    assert meta.get("visible") and meta.get("editable"), (
        f"{key} is no longer offered to the user; drop it from this sweep"
    )
    found = set(find_settings_with_no_reader(catalogue))
    assert key not in found, (
        f"'{key}' is offered in the settings UI but nothing reads it — "
        f"{SETTINGS_OFFERED_BUT_UNREAD[key]}"
    )


def test_no_further_settings_are_offered_but_unread():
    """Guard against the list above growing silently."""
    found = set(find_settings_with_no_reader(load_settings_catalogue()))
    unexpected = sorted(found - set(SETTINGS_OFFERED_BUT_UNREAD))
    assert unexpected == [], (
        "new user-facing settings with no reader: " + ", ".join(unexpected)
    )


# ---------------------------------------------------------------------------
# Section 3 — documented environment variables that set nothing
#
# A settings entry may carry an ``env_var`` field. That field is the sole
# source of the "Environment Variable" column in docs/CONFIGURATION.md, so an
# operator reading the docs will export exactly that name.
#
# Nothing in the codebase reads the field. The two mechanisms that do consult
# the environment both DERIVE the name from the setting key:
#   settings/manager.py:434     -> f"LDR_{'_'.join(key.split('.')).upper()}"
#   settings/env_settings.py:53 -> "LDR_" + key.upper().replace(".", "_")
# so an ``env_var`` that is not the derived name, not an env-registry key, and
# not read via os.environ anywhere, is an environment variable the docs
# promise and the program ignores.
# ---------------------------------------------------------------------------


def _derived_env_var(key: str) -> str:
    return "LDR_" + key.upper().replace(".", "_")


@functools.cache
def _env_registry_var_names() -> set[str]:
    names = set()
    for path in (SRC / "settings" / "env_definitions").glob("*.py"):
        for match in re.finditer(r'key\s*=\s*"([^"]+)"', _read(path)):
            names.add(_derived_env_var(match.group(1)))
    return names


@functools.cache
def _python_source_blob() -> str:
    return "\n".join(
        _read(p) for p in SRC.rglob("*.py") if "__pycache__" not in p.parts
    )


@functools.cache
def collect_declared_env_vars() -> tuple[tuple[str, str], ...]:
    """(setting key, declared env var) for every catalogue entry with one."""
    declared = []
    for path in DEFAULTS.rglob("*.json"):
        try:
            blob = json.loads(_read(path))
        except (ValueError, OSError):
            continue
        if not isinstance(blob, dict):
            continue
        for key, meta in blob.items():
            if isinstance(meta, dict) and isinstance(meta.get("env_var"), str):
                declared.append((key, meta["env_var"]))
    return tuple(sorted(declared))


@functools.cache
def find_inert_env_vars() -> tuple[tuple[str, str], ...]:
    registry = _env_registry_var_names()
    code = _python_source_blob()
    inert = []
    for key, env_var in collect_declared_env_vars():
        if env_var == _derived_env_var(key):
            continue
        if env_var in registry:
            continue
        pattern = (
            r"environ(?:\.get)?\(\s*[\"']"
            + re.escape(env_var)
            + r"[\"']|getenv\(\s*[\"']"
            + re.escape(env_var)
            + r"[\"']"
        )
        if re.search(pattern, code):
            continue
        inert.append((key, env_var))
    return tuple(inert)


# Hand-verified. For each, the derived LDR_<KEY> name *does* work — it is only
# the name the documentation prints that is inert.
ENV_VARS_DOCUMENTED_BUT_INERT = {
    "LDR_MAX_CONCURRENT": "app.max_concurrent_researches",
    "LDR_QUEUE_MODE": "app.queue_mode",
    "SEARXNG_DELAY": (
        "search.engine.web.searxng.default_params.delay_between_requests"
    ),
    "RESEARCH_LIBRARY_STORAGE_PATH": "research_library.storage_path",
    "RESEARCH_LIBRARY_SHARED_LIBRARY": "research_library.shared_library",
    "RESEARCH_LIBRARY_PDF_STORAGE_MODE": "research_library.pdf_storage_mode",
    "RESEARCH_LIBRARY_UPLOAD_PDF_STORAGE": (
        "research_library.upload_pdf_storage"
    ),
    # Worse than the others: search_engine_paperless.py's own docstring says
    # "If not provided, will look for PAPERLESS_API_TOKEN env var" and that
    # module contains no os.environ / os.getenv call at all.
    "PAPERLESS_API_TOKEN": "search.engine.web.paperless.api_key",
}


def test_env_var_audit_examines_every_declaration():
    """Floor + landmarks for the env-var audit."""
    declared = collect_declared_env_vars()
    assert len(declared) >= 10, (
        f"only {len(declared)} env_var declarations found; the catalogue walk "
        "is broken"
    )
    keys = {k for k, _ in declared}
    for landmark in ("app.timezone", "zotero.api_key", "app.queue_mode"):
        assert landmark in keys, f"missing landmark declaration: {landmark}"
    assert len(_env_registry_var_names()) >= 20, (
        "env_definitions walk found too few registered variables"
    )


def test_inert_env_var_detector_controls():
    """Positive: an invented name is inert. Negative: live names are not."""
    inert = {env for _, env in find_inert_env_vars()}
    # Negative controls — all three reach real code by three different routes.
    assert "TZ" not in inert, "app.timezone -> TZ is read via os.environ"
    assert "LDR_ZOTERO_API_KEY" not in inert, "derived name, always honoured"
    assert "LDR_SERVER_MAX_CONCURRENT_RESEARCH" not in inert, (
        "declared on server.max_concurrent_research in the env registry"
    )
    # Positive control: every clause of the predicate rejects an invented name.
    fake_key, fake_env = "made.up.setting", "TOTALLY_NOT_READ_ANYWHERE"
    assert fake_env != _derived_env_var(fake_key)
    assert fake_env not in _env_registry_var_names()
    assert fake_env not in _python_source_blob()


@pytest.mark.xfail(
    strict=True,
    reason=(
        "KNOWN DEFECT, pre-existing. The 'Environment Variable' column in "
        "docs/CONFIGURATION.md comes from the catalogue's env_var field, "
        "but NO CODE READS THAT FIELD -- both env mechanisms derive the "
        "name from the key. Every name here is documented and ignored. "
        "The derived LDR_<KEY> name does work, so most of these are a "
        "docs fix; PAPERLESS_API_TOKEN is not -- that module has no "
        "os.environ/os.getenv call at all. Floor: "
        "test_no_further_inert_env_vars, which PASSES."
    ),
)
@pytest.mark.parametrize(
    "env_var", sorted(ENV_VARS_DOCUMENTED_BUT_INERT), ids=lambda v: v
)
def test_documented_env_vars_are_honoured(env_var):
    """docs/CONFIGURATION.md tells operators to export these; they do nothing."""
    setting_key = ENV_VARS_DOCUMENTED_BUT_INERT[env_var]
    inert = dict(find_inert_env_vars())
    assert setting_key not in inert, (
        f"docs/CONFIGURATION.md documents {env_var} as the environment "
        f"variable for '{setting_key}', but nothing reads that name — only "
        f"the derived {_derived_env_var(setting_key)} has any effect"
    )


def test_no_further_inert_env_vars():
    found = {env for _, env in find_inert_env_vars()}
    unexpected = sorted(found - set(ENV_VARS_DOCUMENTED_BUT_INERT))
    assert unexpected == [], (
        "new documented-but-inert environment variables: "
        + ", ".join(unexpected)
    )


def test_every_documented_setting_key_exists_in_the_catalogue():
    """RESULT: clean — recorded as a deliberate negative finding.

    Every setting row in docs/CONFIGURATION.md names a key that really is in
    defaults/**. The documentation does not invent settings; the defects are
    on the reader side (Section 2) and the env-var side (above).
    """
    catalogue = load_settings_catalogue()
    doc = _read(DOCS / "CONFIGURATION.md")
    rows = re.findall(
        r"^\|\s*`([a-z][a-z0-9_.]*\.[a-z0-9_.]+)`\s*\|", doc, re.M
    )
    assert len(rows) >= 500, (
        f"only {len(rows)} documented setting rows parsed; the table regex "
        "no longer matches and this test is vacuous"
    )
    missing = sorted({k for k in rows if k not in catalogue})
    assert missing == [], f"documented but non-existent settings: {missing}"


# ---------------------------------------------------------------------------
# Section 4 — the search-engine picker offers engines that cannot be built
#
# ``search.tool`` is the primary "Search Engine" select (visible, editable,
# ui_element="select"). Its ``options`` list is served verbatim by
# ``GET /settings/api`` and ``GET /settings/api/{key}``
# (routers/settings.py:879, :3204, :3246), and the chosen value is handed to
# ``search_engine_factory.get_search_engine`` via
# ``config/search_config.get_search``.
#
# The settings page normally repopulates this dropdown from
# ``/settings/api/available-search-engines`` (which is built from
# ``search_config()`` and therefore only lists constructible engines) — but
# when that call fails, components/settings.js falls back to a hardcoded
# list. Both sources must only offer engine ids the factory can resolve.
#
# An engine is constructible only if the settings catalogue defines
# ``search.engine.web.<name>.*`` keys for it: ``search_engines_config`` builds
# its config dict from those settings and only then decorates it from
# ENGINE_REGISTRY (``for name, entry in ENGINE_REGISTRY.items(): if name in
# search_engines``). A name with no such settings never enters the dict, and
# the factory then FAILS CLOSED with
# ``ValueError("Unknown search engine '<name>'")``.
# ---------------------------------------------------------------------------


@functools.cache
def _catalogue_all_entries() -> dict[str, dict]:
    """Every dict-valued entry in defaults/**, including non-UI ones."""
    entries: dict[str, dict] = {}
    for path in DEFAULTS.rglob("*.json"):
        try:
            blob = json.loads(_read(path))
        except (ValueError, OSError):
            continue
        if isinstance(blob, dict):
            entries.update(
                {k: v for k, v in blob.items() if isinstance(v, dict)}
            )
    return entries


@functools.cache
def configurable_engine_names() -> frozenset[str]:
    """Engine ids the settings catalogue actually defines a config block for."""
    names = set()
    for key in _catalogue_all_entries():
        match = re.match(r"search\.engine\.web\.([a-z0-9_]+)\.", key)
        if match:
            names.add(match.group(1))
    return frozenset(names)


@functools.cache
def engine_registry_names() -> frozenset[str]:
    tree = ast.parse(_read(SRC / "web_search_engines" / "engine_registry.py"))
    for node in ast.walk(tree):
        if (
            isinstance(node, ast.AnnAssign)
            and getattr(node.target, "id", None) == "ENGINE_REGISTRY"
            and isinstance(node.value, ast.Dict)
        ):
            return {
                k.value for k in node.value.keys if isinstance(k, ast.Constant)
            }
    raise AssertionError("ENGINE_REGISTRY literal not found")


@functools.cache
def search_tool_option_values() -> tuple[str, ...]:
    entry = _catalogue_all_entries()["search.tool"]
    return tuple(
        opt["value"]
        for opt in (entry.get("options") or [])
        if isinstance(opt, dict) and isinstance(opt.get("value"), str)
    )


def test_search_engine_inventories_are_populated():
    """Floor + landmarks for the engine cross-check."""
    configurable = configurable_engine_names()
    registry = engine_registry_names()
    options = search_tool_option_values()
    assert len(configurable) >= 25, (
        f"only {len(configurable)} configurable engines parsed from defaults"
    )
    assert len(registry) >= 25, (
        f"only {len(registry)} ENGINE_REGISTRY entries parsed"
    )
    # DDG/local_all are removed by the picker fix; Wayback is now a fetch
    # tool. Retain a meaningful floor for the remaining search providers.
    assert len(options) >= 14, (
        f"only {len(options)} search.tool dropdown options parsed"
    )
    for landmark in ("searxng", "wikipedia", "brave"):
        assert landmark in configurable and landmark in registry
        assert landmark in options


def test_every_search_engine_dropdown_option_can_be_constructed():
    """Both the stored options and discovery fallback name configured engines."""
    configurable = configurable_engine_names()
    registry = engine_registry_names()
    fallback_block = re.search(
        r"optionsSource = typeof searchEngineOptions.*?searchEngineOptions : \[(.*?)\];",
        _read(WEB / "static/js/components/settings.js"),
        re.DOTALL,
    )
    assert fallback_block is not None, "search.tool fallback block not found"
    fallback_entries = re.findall(
        r"""\bvalue\s*:\s*(['"])(.*?)\1""", fallback_block.group(1)
    )
    fallback = [value for _, value in fallback_entries]
    assert len(fallback) == len(
        re.findall(r"\bvalue\s*:", fallback_block.group(1))
    ), "every discovery fallback ID must be parsed"
    assert len(fallback) >= 2, "search fallback must offer usable choices"
    for source, options in (
        ("settings catalogue", search_tool_option_values()),
        ("discovery fallback", fallback),
    ):
        unbuildable = [
            value
            for value in options
            if value not in configurable or value not in registry
        ]
        assert unbuildable == [], (
            f"{source} offers engine ids the factory cannot resolve: "
            f"{unbuildable}"
        )


def _replace_fallback_provider_for_guard(monkeypatch, literal):
    original_read = _read

    def read_with_fallback(path):
        source = original_read(path)
        if path == WEB / "static/js/components/settings.js":
            source, count = re.subn(
                r"value: 'wikipedia'", f"value: {literal}", source
            )
            assert count >= 1, "Wikipedia fallback control not found"
        return source

    monkeypatch.setitem(globals(), "_read", read_with_fallback)


@pytest.mark.parametrize("quote", ["'", '"'])
def test_fallback_guard_rejects_unknown_ids_in_either_quote_style(
    monkeypatch, quote
):
    _replace_fallback_provider_for_guard(
        monkeypatch, f"{quote}review_nonexistent_engine{quote}"
    )

    with pytest.raises(
        AssertionError, match="discovery fallback offers engine ids"
    ):
        test_every_search_engine_dropdown_option_can_be_constructed()


@pytest.mark.parametrize("quote", ["'", '"'])
def test_fallback_guard_accepts_configured_ids_in_either_quote_style(
    monkeypatch, quote
):
    _replace_fallback_provider_for_guard(
        monkeypatch, f"{quote}wikipedia{quote}"
    )

    test_every_search_engine_dropdown_option_can_be_constructed()


def test_fallback_guard_rejects_unreadable_provider_id(monkeypatch):
    _replace_fallback_provider_for_guard(monkeypatch, "not_a_literal")

    with pytest.raises(AssertionError, match="every discovery fallback ID"):
        test_every_search_engine_dropdown_option_can_be_constructed()


@pytest.mark.xfail(
    strict=True,
    reason=(
        "KNOWN DEFECT, pre-existing. ENGINE_REGISTRY calls itself the "
        "single source of truth for which class implements each engine, "
        "but 'guardian' has no settings block, so "
        "search_config()'s 'if name in search_engines' guard never fires "
        "and the factory rejects that name. The class is correct and "
        "importable; it is the offer that cannot be taken up. "
    ),
)
def test_engine_registry_entries_are_reachable():
    """Registry entries that name a real class but can never be selected.

    engine_registry.py calls itself "the single source of truth for which
    Python module/class implements each search engine". Its ``guardian`` entry
    has no ``search.engine.web.guardian.*`` settings,
    so ``search_engines_config``'s ``if name in search_engines`` guard never
    fires for it and the factory rejects that name.

    Category: caller-facing dead registry entry. The classes themselves are
    correct and importable; it is the offer that cannot be taken up.
    """
    configurable = configurable_engine_names()
    unreachable = sorted(engine_registry_names() - configurable)
    assert unreachable == [], (
        "ENGINE_REGISTRY entries with no settings block, therefore "
        f"unreachable through the factory: {unreachable}"
    )


# ---------------------------------------------------------------------------
# Section 5 — named one-off offers with no implementation behind them
# ---------------------------------------------------------------------------


@pytest.mark.xfail(
    strict=True,
    reason=(
        "KNOWN DEFECT, pre-existing. "
        "app.warnings.dismiss_searxng_recommendation is a documented "
        "'dismiss this banner' checkbox for a banner nothing emits -- no "
        "producer in web/warning_checks/ or security/egress/ -- so "
        "research_form.js's branch for that warning type is unreachable "
        "too. "
    ),
)
def test_searxng_recommendation_warning_has_a_producer():
    """A dismiss switch (and a JS branch) for a banner nothing emits.

    ``app.warnings.dismiss_searxng_recommendation`` is a visible, editable
    checkbox documented in CONFIGURATION.md as "Dismiss recommendations about
    using more questions instead of iterations with SearXNG", and
    ``static/js/research_form.js`` styles ``warning.type ===
    'searxng_recommendation'`` as an info banner. No check in
    ``web/warning_checks/`` ever produces that warning type, so the checkbox
    dismisses nothing and the JS branch is unreachable.
    """
    catalogue = _catalogue_all_entries()
    assert "app.warnings.dismiss_searxng_recommendation" in catalogue, (
        "the setting was removed; drop this test"
    )
    warning_sources = "\n".join(
        _read(p) for p in (SRC / "web" / "warning_checks").rglob("*.py")
    ) + "\n".join(_read(p) for p in (SRC / "security" / "egress").rglob("*.py"))
    assert "searxng_recommendation" in warning_sources, (
        "no warning check emits type 'searxng_recommendation', so the "
        "dismiss_searxng_recommendation setting and the research_form.js "
        "branch that renders it are both dead"
    )


@pytest.mark.xfail(
    strict=True,
    reason=(
        "KNOWN DEFECT, pre-existing. EventType.RATE_LIMIT_WARNING is a "
        "public enum member with no Jinja2 template in TEMPLATE_FILES, no "
        "notifications.on_rate_limit_warning setting (so _should_notify's "
        "default=False can never be lifted), and no producer. A caller "
        "passing it always gets a silent EVENT_DISABLED. "
    ),
)
def test_every_notification_event_type_can_actually_be_delivered():
    """EventType.RATE_LIMIT_WARNING is offered and can never be sent.

    ``notifications.templates.EventType`` is the public vocabulary a caller
    passes to ``NotificationManager.send_notification``. Delivery of an event
    requires (a) a Jinja2 template registered in
    ``NotificationTemplate.TEMPLATE_FILES`` and (b) a
    ``notifications.on_<value>`` setting, because ``_should_notify`` reads
    ``f"notifications.on_{event_type.value}"`` with ``default=False``.

    ``RATE_LIMIT_WARNING`` has neither, and nothing in the codebase emits it.
    Category: caller-facing dead registry entry — a caller who passes it gets
    a silent ``EVENT_DISABLED`` no matter how the user configures the app.
    """
    templates_src = _read(SRC / "notifications" / "templates.py")
    tree = ast.parse(templates_src)
    members = set()
    template_keys = set()
    for node in ast.walk(tree):
        if isinstance(node, ast.ClassDef) and node.name == "EventType":
            for stmt in node.body:
                if isinstance(stmt, ast.Assign) and isinstance(
                    stmt.value, ast.Constant
                ):
                    members.add(stmt.value.value)
        if isinstance(node, ast.AnnAssign) and (
            getattr(node.target, "id", None) == "TEMPLATE_FILES"
        ):
            for key in node.value.keys:
                if isinstance(key, ast.Attribute):
                    template_keys.add(key.attr)
    assert len(members) >= 8, (
        f"only {len(members)} EventType members parsed; walk is broken"
    )
    assert "research_completed" in members and "test" in members

    catalogue = _catalogue_all_entries()
    undeliverable = sorted(
        value
        for value in members
        if f"notifications.on_{value}" not in catalogue and value != "test"
    )
    assert undeliverable == [], (
        "EventType members with no notifications.on_<event> setting, so "
        f"_should_notify's default=False can never be lifted: {undeliverable}"
        f" (TEMPLATE_FILES also omits them: {sorted(template_keys)})"
    )


@pytest.mark.xfail(
    strict=True,
    reason=(
        "KNOWN DEFECT, pre-existing. general.knowledge_accumulation is a "
        "visible select over the members of "
        "KnowledgeAccumulationApproach, an enum with ZERO usages "
        "anywhere; search_system.py passes "
        "knowledge_accumulation_mode=True as a literal instead. "
    ),
)
def test_knowledge_accumulation_enum_has_a_consumer():
    """utilities/enums.py:KnowledgeAccumulationApproach — four members, no user.

    Paired with the dead ``general.knowledge_accumulation`` select in Section
    2: the setting's documented option values (ITERATION / QUESTION /
    NO_KNOWLEDGE) are this enum's members, and neither the enum nor the
    setting is consulted anywhere. ``search_system.py`` passes
    ``knowledge_accumulation_mode=True`` as a literal instead.
    """
    users = [
        p
        for p in SRC.rglob("*.py")
        if "__pycache__" not in p.parts
        and p.name != "enums.py"
        and "KnowledgeAccumulationApproach" in _read(p)
    ]
    assert users, (
        "KnowledgeAccumulationApproach is imported nowhere, yet "
        "docs/CONFIGURATION.md documents general.knowledge_accumulation as "
        "selecting between its members"
    )