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.

54.1 KB
Raw Blame
"""Configuration / environment-variable contracts for the FastAPI port.

The Flask -> FastAPI port replaced ``app.config`` with module-level
constants plus ``LDR_*`` environment variables.  Operators upgrading carry
old settings forward, so three classes of silent breakage matter:

1. a variable the code reads that no operator-facing document mentions
   (worst when it changes security behaviour);
2. a ``app.config`` key ``main`` honoured that the port neither reads nor
   deliberately removed -- the operator's setting stops taking effect with
   no error;
3. a value frozen at *import* time where the operator reasonably expects
   it to be consulted later.

What already exists, and what it does NOT cover
-----------------------------------------------
``.pre-commit-hooks/check-env-vars.py`` and
``tests/settings/env_vars/test_env_var_usage.py`` both answer one question:
*which files are allowed to touch ``os.environ`` directly*.  Both are pure
allowlists keyed on path.  Neither checks

* whether a variable the code reads is **documented** anywhere;
* whether a variable the registry **declares** is read by anything;
* whether the many hand-rolled boolean parsers **agree**;
* whether a read happens at **import** time or per call;
* anything at all inside ``settings/`` or ``config/`` -- those subtrees are
  wholly exempt from the hook, and that is exactly where the env layer
  lives.

This module fills those four gaps.  It is deliberately static (AST + text)
except where a behaviour is cheap and pure to execute; nothing here opens a
database or builds an app.

Every inventory assertion carries a plausible-minimum guard, because an
inventory test that discovers zero items and passes is worse than no test.
"""

import ast
import importlib
import pkgutil
from pathlib import Path

import pytest

from local_deep_research.settings import env_definitions
from local_deep_research.settings.env_registry import registry as env_registry
from local_deep_research.settings.env_settings import BooleanSetting, EnvSetting
from local_deep_research.settings.exceptions import SettingsValueError
from local_deep_research.settings.manager import (
    check_env_setting,
    get_typed_setting_value,
    parse_boolean,
)
from local_deep_research.utilities.type_utils import to_bool

REPO_ROOT = Path(__file__).resolve().parents[1]
SRC_PKG = REPO_ROOT / "src" / "local_deep_research"


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


def _iter_source_files():
    """Yield every production ``.py`` file under the package."""
    for path in sorted(SRC_PKG.rglob("*.py")):
        if "__pycache__" in path.parts:
            continue
        yield path


def _parse(path):
    try:
        return ast.parse(path.read_text(encoding="utf-8"))
    except (SyntaxError, UnicodeDecodeError):  # pragma: no cover
        return None


def _env_var_name(node):
    """Return the env-var name a node reads, ``"<dynamic>"``, or ``None``.

    Recognises ``os.getenv(X)``, ``os.environ.get(X)`` (and ``.setdefault`` /
    ``.pop``) and ``os.environ[X]``.
    """
    if isinstance(node, ast.Call):
        func = node.func
        if (
            isinstance(func, ast.Attribute)
            and func.attr == "getenv"
            and isinstance(func.value, ast.Name)
            and func.value.id == "os"
        ):
            arg = node.args[0] if node.args else None
            if isinstance(arg, ast.Constant):
                return arg.value
            return "<dynamic>"
        if (
            isinstance(func, ast.Attribute)
            and func.attr in ("get", "setdefault", "pop")
            and isinstance(func.value, ast.Attribute)
            and func.value.attr == "environ"
        ):
            arg = node.args[0] if node.args else None
            if isinstance(arg, ast.Constant):
                return arg.value
            return "<dynamic>"
    if isinstance(node, ast.Subscript):
        value = node.value
        if isinstance(value, ast.Attribute) and value.attr == "environ":
            if isinstance(node.slice, ast.Constant):
                return node.slice.value
            return "<dynamic>"
    return None


def _parent_map(tree):
    parents = {}
    for node in ast.walk(tree):
        for child in ast.iter_child_nodes(node):
            parents[child] = node
    return parents


def _scope_of(node, parents):
    """``"function"`` if *node* sits inside a def, else ``"module"``.

    A class body counts as module scope: it runs once, at import.
    """
    current = parents.get(node)
    while current is not None:
        if isinstance(current, (ast.FunctionDef, ast.AsyncFunctionDef)):
            return "function"
        current = parents.get(current)
    return "module"


def _collect_env_reads():
    """Return ``{(relpath, var_name, scope)}`` for every env read in src."""
    found = set()
    for path in _iter_source_files():
        tree = _parse(path)
        if tree is None:
            continue
        parents = _parent_map(tree)
        for node in ast.walk(tree):
            name = _env_var_name(node)
            if name is None:
                continue
            rel = path.relative_to(SRC_PKG).as_posix()
            found.add((rel, name, _scope_of(node, parents)))
    return found


ENV_READS = _collect_env_reads()
LDR_VARS_READ = {name for _, name, _ in ENV_READS if name.startswith("LDR_")}

# Floor against mass removal or a registry that failed to load, not a count
# of today's keys: 30 are registered, so 27 absorbs a few deliberate removals
# but trips when a whole category (security 11, db_config 10, server 4) is lost.
MIN_REGISTRY_SETTINGS = 27


# ---------------------------------------------------------------------------
# 1. Inventory -- with minimum-count guards so it cannot pass vacuously
# ---------------------------------------------------------------------------


def test_env_read_sweep_finds_a_plausible_inventory():
    """The AST sweep itself must not silently find nothing.

    Every later test in this module is derived from ``ENV_READS``; if the
    collector broke (a renamed package directory, a changed AST shape) the
    downstream set-comparisons would all trivially pass.  Pin a floor and a
    handful of names that are load-bearing elsewhere in this file.
    """
    assert len(ENV_READS) >= 25, (
        f"env-read sweep found only {len(ENV_READS)} sites under {SRC_PKG}; "
        "the collector is probably broken"
    )
    assert len(LDR_VARS_READ) >= 8, (
        f"only {sorted(LDR_VARS_READ)} LDR_* literals found; expected the "
        "hand-rolled readers (fastapi_app, log_utils, paths, server_config)"
    )
    for expected in (
        "LDR_TEST_MODE",
        "LDR_EXPOSE_DOCS",
        "LDR_DATA_DIR",
        "LDR_DISABLE_RATE_LIMITING",
        "LDR_ENABLE_FILE_LOGGING",
        "LDR_APP_ALLOW_REGISTRATIONS",
    ):
        assert expected in LDR_VARS_READ, (
            f"{expected} is read by src/ but the sweep missed it"
        )


def test_registry_inventory_is_populated_and_holds_the_security_gates():
    """The declarative registry must still carry the operator gates.

    ``docs/CONFIGURATION.md`` is generated from this registry
    (``scripts/generate_config_docs.py``), so a setting silently dropped
    here disappears from the documentation in the same commit.
    """
    declared = set(env_registry.get_all_env_vars())
    assert len(declared) >= MIN_REGISTRY_SETTINGS, (
        f"registry declares only {len(declared)} env vars; expected at "
        f"least {MIN_REGISTRY_SETTINGS}"
    )
    for gate in (
        "LDR_NOTIFICATIONS_ALLOW_OUTBOUND",
        "LDR_NOTIFICATIONS_ALLOW_PRIVATE_IPS",
        "LDR_POLICY_ALLOW_UNPROTECTED_EGRESS",
        "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS",
        "LDR_SEARCH_ALLOW_PRIVATE_RESULT_FETCH",
        "LDR_SECURITY_ALLOW_NAT64",
        "LDR_SECURITY_CORS_ALLOWED_ORIGINS",
        "LDR_SECURITY_WEBSOCKET_ALLOWED_ORIGINS",
        "LDR_BOOTSTRAP_ALLOW_UNENCRYPTED",
    ):
        assert gate in declared, f"security gate {gate} left the registry"

    # Shared-root PDF reads were removed, including their opt-in switch.
    # Keep the retirement explicit instead of restoring a no-op setting to
    # satisfy the inventory of security gates that still exist.
    assert "LDR_RESEARCH_LIBRARY_ALLOW_LEGACY_READ_FALLBACK" not in declared


# ---------------------------------------------------------------------------
# 2. Documentation coverage
# ---------------------------------------------------------------------------

_OPERATOR_DOC_FILES = (
    "README.md",
    "SECURITY.md",
    "Dockerfile",
    "docker-compose.yml",
    "docs/CONFIGURATION.md",
    "docs/env_configuration.md",
    "docs/installation.md",
    "docs/install-pip.md",
    "docs/troubleshooting.md",
    "docs/docker-compose-guide.md",
    "docs/faq.md",
    "src/local_deep_research/defaults/.env.template",
)


def _operator_docs_text():
    chunks = []
    for rel in _OPERATOR_DOC_FILES:
        path = REPO_ROOT / rel
        if path.exists():
            chunks.append(path.read_text(encoding="utf-8", errors="replace"))
    deployment = REPO_ROOT / "docs" / "deployment"
    if deployment.exists():
        for path in sorted(deployment.rglob("*.md")):
            chunks.append(path.read_text(encoding="utf-8", errors="replace"))
    return "\n".join(chunks)


OPERATOR_DOCS = _operator_docs_text()


def test_operator_doc_corpus_is_actually_loaded():
    """Guard the corpus the two documentation tests below read from."""
    assert len(OPERATOR_DOCS) > 100_000, (
        f"operator doc corpus is only {len(OPERATOR_DOCS)} chars; the "
        "documentation tests below would pass vacuously"
    )
    assert "LDR_WEB_PORT" in OPERATOR_DOCS


def test_every_registry_declared_var_reaches_operator_documentation():
    """Registry -> docs is the path that works; keep it working.

    ``docs/CONFIGURATION.md`` is generated from the registry, so this is
    really a check that the generated file is not stale relative to
    ``settings/env_definitions/``.
    """
    missing = sorted(
        var
        for var in env_registry.get_all_env_vars()
        if var not in OPERATOR_DOCS
    )
    assert not missing, (
        "registry-declared env vars absent from operator documentation "
        f"(regenerate docs/CONFIGURATION.md): {missing}"
    )


# Variables read by hand (``os.getenv`` in production code) rather than
# declared in the registry.  Every one of them changes security-relevant or
# security-adjacent behaviour, and NONE of them can reach
# docs/CONFIGURATION.md, because the generator only walks the registry.
_HAND_ROLLED_SECURITY_VARS = {
    # unauthenticated Swagger UI + /openapi.json
    "LDR_EXPOSE_DOCS",
    # turns off ALL HTTP rate limiting, login brute-force included
    "LDR_DISABLE_RATE_LIMITING",
    "DISABLE_RATE_LIMITING",
    # relaxes the SQLCipher KDF floor
    "LDR_TEST_MODE",
    # verbose logging / frame-locals in tracebacks
    "LDR_APP_DEBUG",
    "LDR_LOGURU_DIAGNOSE",
    "LDR_LOG_SETTINGS",
    # writes an unencrypted log file next to the encrypted databases
    "LDR_ENABLE_FILE_LOGGING",
    # open registration
    "LDR_APP_ALLOW_REGISTRATIONS",
    # trusts attacker-controlled X-Forwarded-For for rate-limit keying
    "TRUST_PROXY_HEADERS",
    # shares login-lockout counters across workers / restarts
    "RATE_LIMIT_STORAGE_URI",
    "RATELIMIT_STORAGE_URL",
    # turns a dead nav link into a boot failure
    "LDR_STRICT_TEMPLATE_LINKS",
}

# Known gap at the time of writing.  Set-equality (not a skip) so that
# documenting one of these, or adding a new undocumented switch, both fail
# loudly and force this list to be revisited.
_KNOWN_UNDOCUMENTED = {
    "LDR_EXPOSE_DOCS",
    "LDR_DISABLE_RATE_LIMITING",
    "DISABLE_RATE_LIMITING",
    "LDR_TEST_MODE",
    "LDR_LOG_SETTINGS",
    "LDR_STRICT_TEMPLATE_LINKS",
}


def test_rate_limit_storage_vars_are_explained_not_just_named():
    """Naming a variable is not documenting it.

    ``RATE_LIMIT_STORAGE_URI`` and ``RATELIMIT_STORAGE_URL`` left
    ``_KNOWN_UNDOCUMENTED`` when the upgrade guide named them in a rollback
    reminder. The corpus check above only looks for the name, so pin the
    explanation in the reverse-proxy guide's notes as well.
    """
    doc = " ".join(
        (REPO_ROOT / "docs" / "deployment" / "reverse-proxy.md")
        .read_text(encoding="utf-8")
        .split()
    )
    for phrase in (
        "`RATE_LIMIT_STORAGE_URI`",
        "`RATELIMIT_STORAGE_URL`",
        "including the per-client limit on login requests, in process "
        "memory by default",
        "survives restarts",
        "still works when `RATE_LIMIT_STORAGE_URI` is unset",
        "per-account lockout after repeated failed passwords stays in "
        "process memory either way",
        # The storage-URI redaction details differ between current main and
        # the rate-limit redaction rework (#4870), so the guide gives only
        # guidance that holds on both: the error names the variable, the
        # redaction is best-effort, and the variables belong in the process
        # environment rather than a .env file (slowapi reads that file itself).
        "startup fails with an error that names the variable",
        "redaction of credentials in it is best-effort",
        "Set them in the process environment, not in a `.env` file",
    ):
        assert phrase in doc, f"reverse-proxy.md lost {phrase!r}"
    # Redaction behaviour is version-dependent (see above); never promise
    # what it covers or what it misses.
    assert "redacts any password" not in doc, (
        "reverse-proxy.md overclaims the storage-URI redaction"
    )
    for stale in (
        "replaced by `***@`",
        "neither names the variable nor redacts the URI",
        "appears in that error and the logs",
    ):
        assert stale not in doc, (
            f"reverse-proxy.md pins storage-URI redaction detail {stale!r}"
        )
    assert "including the login-attempt limit" not in doc, (
        "the account lockout is not stored in the rate-limit backend"
    )


def test_hand_rolled_security_vars_are_all_still_read_by_the_code():
    """Pin the premise of the documentation test that follows.

    If one of these were deleted from the code the doc-coverage test would
    quietly shrink; assert each is genuinely read somewhere in src/ first.
    """
    all_names = {name for _, name, _ in ENV_READS}
    unread = sorted(_HAND_ROLLED_SECURITY_VARS - all_names)
    assert not unread, (
        "these variables are listed as read-by-hand but no longer appear in "
        f"any os.environ/os.getenv call in src/: {unread}"
    )


def test_undocumented_security_relevant_env_vars_match_the_known_gap():
    """An undocumented switch that changes security behaviour.

    ``LDR_EXPOSE_DOCS`` is the sharpest of these: it is read at
    ``web/fastapi_app.py`` module scope to enable ``/api/docs`` and
    ``/openapi.json`` for unauthenticated callers, and the string does not
    occur in a single ``.md``, ``.yml``, ``Dockerfile`` or ``.env`` file in
    the repository -- only in source and in tests.
    """
    undocumented = {
        var for var in _HAND_ROLLED_SECURITY_VARS if var not in OPERATOR_DOCS
    }
    assert undocumented == _KNOWN_UNDOCUMENTED, (
        "the set of undocumented security-relevant env vars changed.\n"
        f"  newly undocumented: {sorted(undocumented - _KNOWN_UNDOCUMENTED)}\n"
        f"  now documented (remove from the known list): "
        f"{sorted(_KNOWN_UNDOCUMENTED - undocumented)}"
    )


# ---------------------------------------------------------------------------
# 3. Declared-but-never-read settings (silently dropped keys, inbound)
# ---------------------------------------------------------------------------


def _docstring_constant_ids(tree):
    """ids of the ``ast.Constant`` nodes that are docstrings, not code."""
    ids = set()
    for node in ast.walk(tree):
        body = getattr(node, "body", None)
        if not isinstance(
            node,
            (ast.Module, ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef),
        ):
            continue
        if (
            body
            and isinstance(body[0], ast.Expr)
            and isinstance(body[0].value, ast.Constant)
            and isinstance(body[0].value.value, str)
        ):
            ids.add(id(body[0].value))
    return ids


def _setting_objects():
    """``{dotted path: registry key}`` for each setting object that
    ``settings/env_definitions`` binds to a module-level name.

    ``TRUSTED_PROXY_COUNT`` is defined that way so that
    ``web/dependencies/rate_limit.py`` can import it and read it at import
    time, before any settings manager exists.  Its key string never appears
    outside ``env_definitions``, so a scan for string literals cannot see
    that read.
    """
    found = {}
    for info in pkgutil.iter_modules(env_definitions.__path__):
        module = importlib.import_module(
            f"{env_definitions.__name__}.{info.name}"
        )
        for name, value in vars(module).items():
            if isinstance(value, EnvSetting):
                found[f"{module.__name__}.{name}"] = value.key
    return found


def _dotted_name(node):
    """``"a.b.c"`` for a ``Name`` / ``Attribute`` chain, else ``None``."""
    parts = []
    while isinstance(node, ast.Attribute):
        parts.append(node.attr)
        node = node.value
    if not isinstance(node, ast.Name):
        return None
    parts.append(node.id)
    return ".".join(reversed(parts))


def _imported_paths(rel, tree):
    """``{local name: dotted path it refers to}`` for the imports in *tree*.

    *rel* is the file's path below the package; it fixes what a relative
    import means.
    """
    package = [SRC_PKG.name, *rel.parent.parts]
    bound = {}
    for node in ast.walk(tree):
        if isinstance(node, ast.Import):
            for alias in node.names:
                if alias.asname:
                    bound[alias.asname] = alias.name
                else:  # ``import a.b.c`` binds only the root name ``a``
                    root = alias.name.split(".")[0]
                    bound[root] = root
        elif isinstance(node, ast.ImportFrom):
            # Level 1 is the importing file's own package.
            keep = max(len(package) - max(node.level - 1, 0), 0)
            anchor = package[:keep] if node.level else []
            base = ".".join([*anchor, *([node.module] if node.module else [])])
            for alias in node.names:
                bound[alias.asname or alias.name] = f"{base}.{alias.name}"
    return bound


def _keys_read_through_setting_objects(rel, tree, objects):
    """Registry keys *tree* reads as ``<setting object>.get_value()``.

    The object must come from an ``env_definitions`` module, imported by
    name (``from ...env_definitions.server import NAME [as ALIAS]``) or
    reached through the module (``server.NAME``).  A same-named local, an
    object imported from anywhere else, an import that is never called and
    any other member (``NAME.is_set``) read nothing.
    """
    bound = _imported_paths(rel, tree)
    keys = set()
    for node in ast.walk(tree):
        if not (
            isinstance(node, ast.Call)
            and isinstance(node.func, ast.Attribute)
            and node.func.attr == "get_value"
        ):
            continue
        dotted = _dotted_name(node.func.value)
        if dotted is None:
            continue
        root, _, rest = dotted.partition(".")
        if root not in bound:
            continue
        path = f"{bound[root]}.{rest}" if rest else bound[root]
        if path in objects:
            keys.add(objects[path])
    return keys


def _consumer_trees():
    """``(path below the package, tree)`` for each production file that could
    consume a setting, which is every one but the definitions themselves."""
    for path in _iter_source_files():
        rel = path.relative_to(SRC_PKG)
        if "env_definitions" in rel.parts:
            continue
        tree = _parse(path)
        if tree is not None:
            yield rel, tree


def _registry_keys_without_a_consumer(trees=None):
    """Registry keys that nothing outside the registry definitions consumes.

    A key is consumed when it appears as a *code* string literal, or when
    the setting object it is defined as is imported and read with
    ``.get_value()``.  A genuine string consumer passes the key verbatim to
    ``get_env_setting`` / ``get_setting`` / ``get_typed_setting_value``.

    The literal scan is deliberately narrow on two axes, because both
    looser forms produce false negatives here:

    * AST constants, not raw text -- several gates are named in comments
      explaining when an operator would set them
      (``# ... LDR_SECURITY_ALLOW_NAT64=true would unblock ...``);
    * exact equality on non-docstring constants -- ``security.allow_nat64``
      is quoted in the prose of five unrelated docstrings, so a substring
      match over docstrings would call a key "consumed" on the strength of
      documentation alone.

    The object scan is narrow in the same way; see
    ``_keys_read_through_setting_objects``.  *trees* replaces the production
    tree with ``(path below the package, tree)`` pairs, for negative
    controls.
    """
    if trees is None:
        trees = _consumer_trees()
    objects = _setting_objects()
    consumed = set()
    for rel, tree in trees:
        docstrings = _docstring_constant_ids(tree)
        for node in ast.walk(tree):
            if (
                isinstance(node, ast.Constant)
                and isinstance(node.value, str)
                and id(node) not in docstrings
            ):
                consumed.add(node.value)
        consumed |= _keys_read_through_setting_objects(rel, tree, objects)
    return {
        key for key in env_registry.list_all_settings() if key not in consumed
    }


def test_every_registry_setting_has_a_consumer():
    """Documented environment settings must be read by application code."""
    dead = _registry_keys_without_a_consumer()
    total = len(env_registry.list_all_settings())
    assert total >= MIN_REGISTRY_SETTINGS, (
        f"registry shrank to {total} settings unexpectedly"
    )
    assert not dead, (
        f"documented environment settings without consumers: {sorted(dead)}"
    )


_PROBE_DEFINITIONS = "local_deep_research.settings.env_definitions.server"

_OBJECT_READ_PROBE = f"""
import {_PROBE_DEFINITIONS} as defs
from local_deep_research.settings.env_definitions import server
from {_PROBE_DEFINITIONS} import DIRECT, IMPORTED_ONLY, OTHER_METHOD
from {_PROBE_DEFINITIONS} import ALIASED as renamed
from local_deep_research.settings.env_definitions.security import WRONG_MODULE
from ...settings.env_definitions.server import RELATIVE
from ..settings.env_definitions.server import WRONG_LEVEL
from somewhere_else import LOOKALIKE

LOCAL = object()

COUNTED = [
    DIRECT.get_value(),
    renamed.get_value(),
    server.QUALIFIED.get_value(),
    defs.VIA_MODULE_ALIAS.get_value(),
    RELATIVE.get_value(),
]
NOT_COUNTED = [
    OTHER_METHOD.describe(),
    WRONG_MODULE.get_value(),
    WRONG_LEVEL.get_value(),
    LOOKALIKE.get_value(),
    LOCAL.get_value(),
    {_PROBE_DEFINITIONS}.UNIMPORTED.get_value(),
]
__all__ = ["IMPORTED_ONLY"]
"""

_PROBE_OBJECTS = {
    f"{_PROBE_DEFINITIONS}.{name}": f"probe.{name.lower()}"
    for name in (
        "DIRECT",
        "ALIASED",
        "QUALIFIED",
        "VIA_MODULE_ALIAS",
        "RELATIVE",
        "IMPORTED_ONLY",
        "OTHER_METHOD",
        "WRONG_MODULE",
        "WRONG_LEVEL",
        "LOOKALIKE",
        "LOCAL",
        "UNIMPORTED",
        "FULL_CHAIN",
    )
}


def test_object_read_scanner_counts_imported_setting_objects_and_only_those():
    """Negative controls for the scanner that spares ``TRUSTED_PROXY_COUNT``.

    Without them a scanner that over-counts would mask a dead setting
    silently: any read of any object, or a bare import, would clear it.
    """
    probe = Path("web/dependencies/probe.py")
    reads = _keys_read_through_setting_objects(
        probe, ast.parse(_OBJECT_READ_PROBE), _PROBE_OBJECTS
    )
    assert reads == {
        "probe.direct",
        "probe.aliased",
        "probe.qualified",
        "probe.via_module_alias",
        "probe.relative",
    }
    # ``import a.b.c`` binds the root ``a``, so the full chain below it is
    # reachable; the aliased import in the probe above binds only the alias.
    plain = (
        f"import {_PROBE_DEFINITIONS}\n"
        f"{_PROBE_DEFINITIONS}.FULL_CHAIN.get_value()\n"
    )
    assert _keys_read_through_setting_objects(
        probe, ast.parse(plain), _PROBE_OBJECTS
    ) == {"probe.full_chain"}


def test_data_dir_settings_api_reports_the_variable_that_moves_the_directory():
    """``/settings/api/data-location`` reports ``LDR_DATA_DIR`` and no other.

    Only ``LDR_DATA_DIR`` relocates the data directory
    (``config/paths.py::get_data_directory``).  The response used to name
    ``LDR_DATA_DIR`` while sourcing ``custom_env_value`` / ``is_custom`` from
    a registered ``bootstrap.data_dir`` setting (``LDR_BOOTSTRAP_DATA_DIR``),
    so a variable that moved nothing changed the report and the one that
    mattered did not.  That setting is gone; the response now asks
    ``get_custom_data_directory``, which sits beside the only reader of
    ``LDR_DATA_DIR``.
    """
    source = (SRC_PKG / "web" / "routers" / "settings.py").read_text(
        encoding="utf-8"
    )
    assert '"custom_env_var": "LDR_DATA_DIR"' in source
    assert "get_custom_data_directory()" in source
    assert "bootstrap.data_dir" not in source
    assert env_registry.get_env_var("bootstrap.data_dir") is None
    assert "LDR_BOOTSTRAP_DATA_DIR" not in env_registry.get_all_env_vars()
    paths_source = (SRC_PKG / "config" / "paths.py").read_text(encoding="utf-8")
    assert 'os.getenv("LDR_DATA_DIR")' in paths_source
    assert "LDR_BOOTSTRAP_DATA_DIR" not in paths_source, (
        "paths.py learned to read LDR_BOOTSTRAP_DATA_DIR -- document it in "
        "docs/env_configuration.md and re-register the setting"
    )


# ---------------------------------------------------------------------------
# 4. Flask app.config parity
# ---------------------------------------------------------------------------

# Every ``app.config`` key ``origin/main`` read or wrote in ``src/`` (from
# ``git grep 'app\.config' origin/main -- 'src/**/*.py'``), mapped to a
# literal that must still be present somewhere in the ported source.  This
# is a historical inventory, not a reimplementation of any logic.
_APP_CONFIG_PORTED = {
    "SECRET_KEY": "SECRET_KEY = _load_secret_key()",
    # #7133 deliberately changes the ported cookie policy to Lax after the
    # forced-refresh GET path becomes CSRF-protected POST. Runtime cookie
    # behavior is checked in test_session_cookie_behavior.py.
    "SESSION_COOKIE_SAMESITE": 'same_site="lax"',
    "SESSION_COOKIE_SECURE": "class SecureCookieMiddleware",
    "PERMANENT_SESSION_LIFETIME": "security.session_remember_me_days",
    "MAX_CONTENT_LENGTH": "class BodySizeLimitMiddleware",
    "WTF_CSRF_ENABLED": "class CSRFMiddleware",
    "RATELIMIT_STRATEGY": "moving-window",
    "SECURITY_CORS_ALLOWED_ORIGINS": "security.cors.allowed_origins",
    "VITE_DEV_MODE": "vite.dev_mode",
    "STATIC_DIR": "STATIC_DIR",
    "LDR_TESTING_MODE": "is_testing",
}

# Keys the port drops on purpose, each replaced by a hardcoded value.  The
# literal named here is that hardcode: if it disappears, the key became
# configurable again (or the behaviour changed) without documentation.
_APP_CONFIG_HARDCODED = {
    # main: app.config.setdefault("SECURITY_CSP_CONNECT_SRC", "'self'")
    "SECURITY_CSP_CONNECT_SRC": "\"connect-src 'self'; \"",
    # main: app.config.setdefault("SECURITY_COEP_POLICY", "credentialless")
    "SECURITY_COEP_POLICY": 'b"credentialless"',
    # main: app.config.setdefault("SECURITY_CORS_ALLOW_CREDENTIALS", False)
    "SECURITY_CORS_ALLOW_CREDENTIALS": "allow_credentials = False",
}

# Flask-only keys with no ASGI meaning; their names must not linger.
_APP_CONFIG_RETIRED = (
    "SQLALCHEMY_TRACK_MODIFICATIONS",
    "SQLALCHEMY_ECHO",
    "PREFERRED_URL_SCHEME",
    "SESSION_COOKIE_HTTPONLY",
    "SECURITY_CORS_ENABLED",
)


def _src_blob():
    return "\n".join(
        p.read_text(encoding="utf-8", errors="replace")
        for p in _iter_source_files()
    )


SRC_BLOB = _src_blob()


def test_src_blob_is_loaded():
    assert len(SRC_BLOB) > 500_000, (
        f"source blob is {len(SRC_BLOB)} chars; parity checks below would "
        "pass vacuously"
    )


@pytest.mark.parametrize(
    ("config_key", "evidence"), sorted(_APP_CONFIG_PORTED.items())
)
def test_flask_app_config_key_has_a_port_equivalent(config_key, evidence):
    """Each ``app.config`` key main honoured still has a landing place."""
    assert evidence in SRC_BLOB, (
        f"main's app.config[{config_key!r}] has no port equivalent: the "
        f"marker {evidence!r} is gone from src/. An operator setting that "
        "value would stop taking effect with no error."
    )


@pytest.mark.parametrize(
    ("config_key", "hardcode"), sorted(_APP_CONFIG_HARDCODED.items())
)
def test_removed_app_config_key_is_replaced_by_a_hardcode(config_key, hardcode):
    """A deliberate removal must be a hardcode, not a hole.

    These three were overridable on main (via ``app.config``, i.e. by an
    embedder, not by an env var).  The port fixes them, which is defensible
    -- but the fixed value has to actually be there, and the old key must
    not survive as dead config.
    """
    assert hardcode in SRC_BLOB, (
        f"{config_key} was dropped from app.config but its replacement "
        f"hardcode {hardcode!r} is not in src/ either"
    )
    assert config_key not in SRC_BLOB, (
        f"{config_key} still appears in src/ although the port removed the "
        "Flask config layer that read it -- dead configuration surface"
    )


@pytest.mark.parametrize("config_key", _APP_CONFIG_RETIRED)
def test_flask_only_app_config_key_left_no_residue(config_key):
    assert config_key not in SRC_BLOB, (
        f"{config_key} is a Flask-only app.config key with no ASGI meaning; "
        "its presence in src/ means something still expects Flask config"
    )


def _app_config_access_sites():
    """Modules with a real ``<something>app.config`` attribute access.

    AST-based on purpose: ``web/routers/api.py`` *mentions*
    ``app.config["LLM_CONFIG"]`` in a comment explaining what the Flask
    version used to read, and a text scan would count that as live config.
    """
    sites = set()
    for path in _iter_source_files():
        tree = _parse(path)
        if tree is None:
            continue
        for node in ast.walk(tree):
            if not isinstance(node, ast.Attribute) or node.attr != "config":
                continue
            try:
                base = ast.unparse(node.value)
            except Exception:  # pragma: no cover - unparse is stable
                continue
            if base.split(".")[-1] == "app":
                sites.add(path.relative_to(SRC_PKG).as_posix())
    return sites


def test_no_reachable_app_config_access_remains():
    """The only surviving ``app.config`` reads are unreachable.

    ``web/utils/vite_helper.py`` still carries the Flask-era ``init_app`` /
    ``_load_manifest`` pair, which read ``app.config["VITE_DEV_MODE"]`` and
    ``app.config["STATIC_DIR"]``.  Nothing constructs ``ViteHelper(app)`` or
    calls ``init_app`` on this branch -- ``fastapi_app`` calls
    ``vite.init_for_fastapi(...)`` instead -- so those two config keys are
    orphaned code, not live configuration.  Assert the residue stays
    confined to that one file.
    """
    offenders = sorted(_app_config_access_sites())
    assert offenders == ["web/utils/vite_helper.py"], (
        f"unexpected app.config access in the FastAPI tree: {offenders}"
    )
    helper = (SRC_PKG / "web" / "utils" / "vite_helper.py").read_text(
        encoding="utf-8"
    )
    assert "def init_for_fastapi" in helper
    assert "vite.init_app" not in SRC_BLOB, (
        "ViteHelper.init_app is called again -- the Flask app.config path "
        "is live and its keys need documenting"
    )


# ---------------------------------------------------------------------------
# 5. Boolean parsing consistency
# ---------------------------------------------------------------------------

_FALSY_TOKENS = ("0", "false", "no", "off", "", "  ", "False", "FALSE")
_TRUTHY_TOKENS = ("true", "1", "yes", "on", "enabled", "TRUE", " true ")


@pytest.mark.parametrize("raw", _FALSY_TOKENS)
def test_to_bool_never_enables_on_a_falsy_string(raw):
    assert to_bool(raw) is False, f"to_bool({raw!r}) enabled a toggle"


@pytest.mark.parametrize("raw", _TRUTHY_TOKENS)
def test_to_bool_enables_on_every_documented_truthy_spelling(raw):
    assert to_bool(raw) is True, f"to_bool({raw!r}) failed to enable"


@pytest.mark.parametrize("raw", ("0", "false", "no", "off", ""))
def test_registry_boolean_setting_never_enables_on_a_falsy_string(
    raw, monkeypatch
):
    """Every registry gate defaults False; a falsy string must keep it off.

    Exercised through the real ``BooleanSetting`` used by every
    ``LDR_*_ALLOW_*`` operator gate.
    """
    setting = BooleanSetting(
        key="security.allow_nat64", description="probe", default=False
    )
    monkeypatch.setenv(setting.env_var, raw)
    assert setting.get_value() is False, (
        f"{setting.env_var}={raw!r} enabled a security gate"
    )


def test_registry_boolean_parser_diverges_from_to_bool_on_whitespace():
    """``BooleanSetting`` does not strip; ``to_bool`` does.

    ``env_settings.BooleanSetting._convert_value`` lowercases but never
    strips, while ``utilities.type_utils.to_bool`` -- the utility whose
    docstring says it exists to centralise "string-to-boolean conversion
    logic that was previously scattered throughout the codebase" -- strips
    first.  A ``.env`` line or Compose value carrying a trailing space
    therefore reads True through one path and False through the other.

    The divergence is in the fail-safe direction for every current gate
    (they all default False, so a padded ``true`` fails to enable rather
    than failing to disable), which is why this is pinned rather than
    treated as a defect -- but it is a real inconsistency, and pinning it
    means a future gate that defaults *True* cannot inherit it unnoticed.
    """
    setting = BooleanSetting(
        key="security.allow_nat64", description="probe", default=False
    )
    padded = " true "
    assert to_bool(padded) is True
    assert setting._convert_value(padded) is False, (
        "BooleanSetting started stripping whitespace -- it now agrees with "
        "to_bool; drop this test"
    )


def _boolean_vocabularies():
    """Every ``x in (<string literals>)`` boolean vocabulary in src/.

    Matches membership tests whose right-hand tuple/list/set is made only of
    string constants drawn from a boolean vocabulary -- which is what all
    the hand-rolled parsers look like.
    """
    vocab = {
        "true",
        "false",
        "1",
        "0",
        "yes",
        "no",
        "on",
        "off",
        "enabled",
        "disabled",
        "",
    }
    found = []
    for path in _iter_source_files():
        tree = _parse(path)
        if tree is None:
            continue
        for node in ast.walk(tree):
            if not isinstance(node, ast.Compare):
                continue
            if len(node.ops) != 1 or not isinstance(node.ops[0], ast.In):
                continue
            rhs = node.comparators[0]
            if not isinstance(rhs, (ast.Tuple, ast.List, ast.Set)):
                continue
            values = [
                e.value
                for e in rhs.elts
                if isinstance(e, ast.Constant) and isinstance(e.value, str)
            ]
            if not values or len(values) != len(rhs.elts):
                continue
            if not {v.lower() for v in values} <= vocab:
                continue
            found.append(
                (
                    path.relative_to(SRC_PKG).as_posix(),
                    node.lineno,
                    tuple(values),
                )
            )
    return found


BOOLEAN_VOCABULARIES = _boolean_vocabularies()


def test_boolean_vocabulary_sweep_found_the_hand_rolled_parsers():
    assert len(BOOLEAN_VOCABULARIES) >= 15, (
        f"only {len(BOOLEAN_VOCABULARIES)} boolean vocabularies found; the "
        "AST sweep is probably broken"
    )
    files = {rel for rel, _, _ in BOOLEAN_VOCABULARIES}
    for expected in (
        "utilities/type_utils.py",
        "settings/env_settings.py",
        "settings/env_registry.py",
        "web/dependencies/rate_limit.py",
        "web/fastapi_app.py",
        "security/secure_logging.py",
    ):
        assert expected in files, f"no boolean vocabulary found in {expected}"


def test_no_truthy_vocabulary_admits_a_falsy_token():
    """``0`` / ``false`` / ``no`` / ``off`` / ``disabled`` / ``""`` never enable.

    The port has at least seven independent string-to-bool implementations
    (``to_bool``, ``BooleanSetting``, ``parse_boolean``,
    ``security_settings._convert_value``, ``secure_logging.env_truthy``,
    ``egress.policy._coerce_bool``, plus a dozen inline
    ``os.environ.get(...).lower() in (...)`` expressions).  They disagree
    about which spellings mean *yes* -- ``{true,1,yes}`` vs
    ``{true,1,yes,on}`` vs ``{true,1,yes,on,enabled}`` -- and that
    inconsistency is merely confusing.  What would be a security defect is
    any of them treating a falsy spelling as *yes*.  Sweep them all.
    """
    falsy = {"0", "false", "no", "off", "disabled", ""}
    offenders = [
        (rel, line, values)
        for rel, line, values in BOOLEAN_VOCABULARIES
        # A falsy *list* (parse_boolean's FALSY_VALUES shape) is fine; only
        # a mixed vocabulary means a falsy spelling maps to True.
        if ({v.lower() for v in values} & falsy)
        and not ({v.lower() for v in values} <= falsy)
    ]
    assert not offenders, (
        "a boolean vocabulary mixes truthy and falsy spellings, so a falsy "
        f"value would enable the toggle: {offenders}"
    )


def test_html_checkbox_parser_keeps_form_semantics():
    """Form posts keep HTML semantics; environment reads validate tokens.

    ``parse_boolean`` remains the generic form/stored-value parser, where
    arbitrary non-empty checkbox values are true.  Managed environment
    overrides are checked separately and must not inherit that permissive
    behavior.
    """
    for spelling in ("off", "false", "0", "no", ""):
        assert parse_boolean(spelling) is False

    for spelling in ("disabled", "none", "flase", "2"):
        assert parse_boolean(spelling) is True


def test_app_debug_disabled_env_overrides_true_stored_value(monkeypatch):
    """The explicit false environment token overrides lower-priority true."""
    monkeypatch.setenv("LDR_APP_DEBUG", "disabled")
    assert check_env_setting("app.debug") == "disabled"
    result = get_typed_setting_value(
        "app.debug", True, "checkbox", default=True
    )
    assert result is False


@pytest.mark.parametrize("spelling", ["flase", "none", "2"])
def test_app_debug_invalid_env_cannot_select_stored_or_default(
    monkeypatch, spelling
):
    monkeypatch.setenv("LDR_APP_DEBUG", spelling)
    with pytest.raises(SettingsValueError) as error:
        get_typed_setting_value("app.debug", True, "checkbox", default=True)
    assert error.value.key == "app.debug"
    assert error.value.source == "environment"


def test_empty_env_var_is_treated_as_unset_not_as_false(monkeypatch):
    """``LDR_X=""`` must fall back to the DB/default, not override it.

    Orchestrators (Unraid, Compose, k8s manifests) frequently emit an empty
    string for an unconfigured variable.
    """
    monkeypatch.setenv("LDR_APP_DEBUG", "")
    assert check_env_setting("app.debug") is None
    assert (
        get_typed_setting_value("app.debug", True, "checkbox", default=False)
        is True
    )


# ---------------------------------------------------------------------------
# 6. Import-time vs request-time evaluation
# ---------------------------------------------------------------------------

# Env reads that execute once, at module import, and are then frozen for the
# life of the process.  Recorded as (module, variable).  For a variable set
# in the process environment before launch this is harmless; it matters for
# anything an operator, a test, or another module can change later -- and it
# matters for *pairs*, where the same variable is also read dynamically
# elsewhere and the two answers can disagree.
_KNOWN_IMPORT_TIME_READS = {
    # Cookie test mode is frozen from the explicit operator switch only.
    ("web/fastapi_app.py", "LDR_TEST_MODE"),
    # docs_url / openapi_url are decided when FastAPI() is constructed
    ("web/fastapi_app.py", "LDR_EXPOSE_DOCS"),
    # SETTINGS_LOG_LEVEL is computed at import and cached in a module global
    ("settings/logger.py", "LDR_LOG_SETTINGS"),
    # module constants; contrast the dynamic is_ci_environment() /
    # is_github_actions() helpers in settings/env_registry.py
    ("settings/env_definitions/testing.py", "CI"),
    ("settings/env_definitions/testing.py", "GITHUB_ACTIONS"),
    ("settings/env_definitions/testing.py", "TESTING"),
    ("settings/env_definitions/security.py", "PYTEST_CURRENT_TEST"),
    # slowapi limiter is built at import, so all three freeze
    ("web/dependencies/rate_limit.py", "TRUST_PROXY_HEADERS"),
    ("web/dependencies/rate_limit.py", "RATE_LIMIT_STORAGE_URI"),
    # Read at module scope and handed to ``_resolve_legacy_storage_uri()``,
    # which (also at import) falls back to the same key in ./.env, as
    # slowapi does; keep the environ read at module scope so this sweep,
    # which stops at function bodies, still sees the freeze.
    ("web/dependencies/rate_limit.py", "RATELIMIT_STORAGE_URL"),
    # Decides, alongside TRUSTED_PROXY_COUNT, whether the startup guard
    # above raises or only warns; must be read at import time for the
    # same reason TRUST_PROXY_HEADERS is.
    ("web/dependencies/rate_limit.py", "LDR_SERVER_FORWARDED_ALLOW_IPS"),
    # The other half of that guard's input. Its VALUE is resolved through
    # the typed ``TRUSTED_PROXY_COUNT`` setting (which validates it and
    # aborts startup on a malformed/out-of-range one); the plain
    # ``os.environ.get`` beside it exists so this sweep -- which matches
    # ``os.getenv``/``os.environ.get`` call sites and cannot see a
    # settings object's ``.get_value()`` -- records the freeze.
    ("web/dependencies/rate_limit.py", "LDR_SERVER_TRUSTED_PROXY_COUNT"),
}


def test_import_time_env_reads_match_the_known_inventory():
    """Any new frozen-at-import env read has to be justified here.

    The failure mode this guards is an operator- or test-visible setting
    that is captured once and then ignored.  Adding a module-level
    ``os.getenv`` is a two-character change; noticing that it froze a
    security switch is not.
    """
    module_level = {
        (rel, name) for rel, name, scope in ENV_READS if scope == "module"
    }
    assert module_level, "sweep found no module-level env reads at all"
    assert module_level == _KNOWN_IMPORT_TIME_READS, (
        "module-level (import-time) env reads changed.\n"
        f"  new: {sorted(module_level - _KNOWN_IMPORT_TIME_READS)}\n"
        f"  gone: {sorted(_KNOWN_IMPORT_TIME_READS - module_level)}"
    )


def test_settings_log_level_is_frozen_at_import(monkeypatch):
    """``LDR_LOG_SETTINGS`` cannot be changed after ``settings`` imports.

    ``settings/logger.py`` computes ``SETTINGS_LOG_LEVEL`` at module scope
    and ``get_settings_log_level()`` returns that global, so the value is
    fixed by whatever the environment held when the ``settings`` package was
    first imported.  ``settings/logger.py`` sits inside the ``settings/``
    subtree, which ``.pre-commit-hooks/check-env-vars.py`` exempts
    wholesale, so nothing else flags it.
    """
    from local_deep_research.settings import logger as settings_logger

    before = settings_logger.get_settings_log_level()
    monkeypatch.setenv("LDR_LOG_SETTINGS", "debug")
    assert settings_logger.get_settings_log_level() == before, (
        "get_settings_log_level() became dynamic; the module global is no "
        "longer authoritative"
    )


def test_ci_flag_has_a_frozen_constant_and_a_dynamic_helper(monkeypatch):
    """Two sources of truth for ``CI`` that can disagree.

    ``settings/env_definitions/testing.py`` freezes ``CI`` into a module
    constant at import; ``settings/env_registry.is_ci_environment()`` reads
    ``os.environ`` on every call.  A consumer picking the wrong one gets the
    wrong answer, and neither name hints at which is which.

    ``testing_with_mocks()`` in the same file documents the hazard and is
    deliberately a function for exactly this reason -- the constants next to
    it were never given the same treatment.
    """
    from local_deep_research.settings import env_registry
    from local_deep_research.settings.env_definitions import testing

    frozen = testing.CI
    monkeypatch.setenv("CI", "true")
    assert env_registry.is_ci_environment() is True
    assert testing.CI is frozen, (
        "testing.CI became dynamic; the divergence is resolved"
    )

    monkeypatch.setenv("CI", "false")
    assert env_registry.is_ci_environment() is False
    assert testing.CI is frozen


def test_testing_with_mocks_stayed_a_function(monkeypatch):
    """The one env flag in that module that is read per call.

    Positive control for the two tests above: the sweep and the reasoning
    are only meaningful if a correctly-shaped dynamic reader passes.
    """
    from local_deep_research.settings.env_definitions import testing

    monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "true")
    assert testing.testing_with_mocks() is True
    monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "false")
    assert testing.testing_with_mocks() is False
    monkeypatch.delenv("LDR_TESTING_WITH_MOCKS")
    assert testing.testing_with_mocks() is False


def _module_level_helper_calls():
    """Module-level calls to helpers that read the environment internally.

    ``os.getenv`` is not the only way to freeze a setting at import: calling
    a settings helper at module scope does the same thing, and the AST sweep
    above cannot see it.
    """
    targets = {
        "get_env_setting",
        "is_rate_limiting_enabled",
        "load_server_config",
        "get_security_default",
        "check_env_setting",
        "is_test_mode",
        "is_ci_environment",
    }
    found = set()
    for path in _iter_source_files():
        tree = _parse(path)
        if tree is None:
            continue
        parents = _parent_map(tree)
        for node in ast.walk(tree):
            if not isinstance(node, ast.Call):
                continue
            func = node.func
            name = None
            if isinstance(func, ast.Name):
                name = func.id
            elif isinstance(func, ast.Attribute):
                name = func.attr
            if name not in targets:
                continue
            if _scope_of(node, parents) != "module":
                continue
            found.add((path.relative_to(SRC_PKG).as_posix(), name))
    return found


_KNOWN_IMPORT_TIME_HELPER_CALLS = {
    # limiter `enabled=` and the rate-limit strings are fixed when the
    # module loads, so LDR_DISABLE_RATE_LIMITING / the LDR_SECURITY_
    # RATE_LIMIT_* strings cannot change without a restart
    ("web/dependencies/rate_limit.py", "is_rate_limiting_enabled"),
    ("web/dependencies/rate_limit.py", "load_server_config"),
    # session max_age and the non-remember-me expiry window
    ("web/fastapi_app.py", "get_security_default"),
    # server-wide research semaphore size
    ("web/services/research_service.py", "get_env_setting"),
    # Socket.IO CORS allowlist
    ("web/services/socketio_asgi.py", "get_env_setting"),
}


def test_import_time_settings_helper_calls_match_the_known_inventory():
    calls = _module_level_helper_calls()
    assert calls, "sweep found no module-level settings-helper calls"
    assert calls == _KNOWN_IMPORT_TIME_HELPER_CALLS, (
        "module-level settings-helper calls changed (these freeze their "
        "env vars at import).\n"
        f"  new: {sorted(calls - _KNOWN_IMPORT_TIME_HELPER_CALLS)}\n"
        f"  gone: {sorted(_KNOWN_IMPORT_TIME_HELPER_CALLS - calls)}"
    )


# ---------------------------------------------------------------------------
# 7. Startup validation on a dangerous configuration
# ---------------------------------------------------------------------------


def test_missing_sqlcipher_aborts_unless_explicitly_allowed():
    """Unencrypted storage must be an explicit opt-in, loudly.

    ``database/encrypted_db.py`` raises ``RuntimeError`` when SQLCipher is
    unavailable and ``bootstrap.allow_unencrypted`` is not set, naming the
    variable that overrides it.  This is the one dangerous configuration
    the port fails closed on at startup rather than warning about.
    """
    source = (SRC_PKG / "database" / "encrypted_db.py").read_text(
        encoding="utf-8"
    )
    assert '"bootstrap.allow_unencrypted"' in source
    assert "raise RuntimeError(" in source
    assert "LDR_BOOTSTRAP_ALLOW_UNENCRYPTED=true to proceed" in source


def test_unencrypted_database_still_warns_at_app_startup():
    """The startup banner main printed survived the port.

    ``main``'s ``app_factory`` logged a SECURITY NOTICE when
    ``db_manager.has_encryption`` was False.  ``web/fastapi_app.py`` keeps
    it; without this the only signal of an unencrypted deployment would be
    the one-time ``encrypted_db`` warning at first DB open.
    """
    source = (SRC_PKG / "web" / "fastapi_app.py").read_text(encoding="utf-8")
    assert "SECURITY NOTICE: SQLCipher is not available" in source
    assert "db_manager.has_encryption" in source


def test_secret_key_load_fails_loudly_rather_than_going_ephemeral():
    """An unreadable or empty ``.secret_key`` aborts the process.

    The signing key is persisted in the data directory; there is no
    environment override for its value.
    """
    source = (SRC_PKG / "web" / "fastapi_app.py").read_text(encoding="utf-8")
    assert "Cannot read SECRET_KEY file at" in source
    assert "is empty. " in source
    assert "SECRET_KEY = _load_secret_key()" in source
    assert "LDR_BOOTSTRAP_SECRET_KEY" not in source, (
        "_load_secret_key learned about an environment override -- update "
        "the documented signing-key behavior"
    )


def test_registration_toggle_fails_closed_on_an_unrecognised_value():
    """Positive control for the fail-closed pattern.

    ``web/server_config.py`` coerces an unparseable
    ``LDR_APP_ALLOW_REGISTRATIONS`` to False and warns.  This is the shape
    the ``LDR_APP_DEBUG`` test above shows is missing elsewhere.
    """
    source = (SRC_PKG / "web" / "server_config.py").read_text(encoding="utf-8")
    assert "_RECOGNIZED_BOOL_VALUES" in source
    assert 'config["allow_registrations"] = False' in source
    assert "(registrations disabled) for security" in source


def test_env_var_hook_exempts_the_whole_settings_subtree():
    """Why the gaps above exist, pinned so the rationale stays true.

    ``check-env-vars.py`` allowlists any path containing a ``settings`` /
    ``config`` segment, so ``settings/logger.py``,
    ``settings/env_definitions/*.py`` and ``config/paths.py`` may read the
    environment however they like -- including the import-time reads and
    the four disagreeing boolean vocabularies inventoried above.  The hook
    is about *where* env vars are read; nothing in it is about what the
    values mean.
    """
    hook = (REPO_ROOT / ".pre-commit-hooks" / "check-env-vars.py").read_text(
        encoding="utf-8"
    )
    assert "ALLOWED_PATH_SEGMENTS" in hook
    for segment in ('"settings"', '"config"', '"scripts"'):
        assert segment in hook, f"{segment} left the hook allowlist"
    for absent in ("documented", "CONFIGURATION.md", "module-level"):
        assert absent not in hook, (
            f"the hook now mentions {absent!r} -- it may cover more than "
            "read-location, so revisit the gap analysis in this module"
        )


def test_env_var_hook_and_this_module_agree_on_the_allowlisted_files():
    """The files that read env vars by hand are exactly the exempted ones."""
    hook = (REPO_ROOT / ".pre-commit-hooks" / "check-env-vars.py").read_text(
        encoding="utf-8"
    )
    for anchored in (
        '"/web/app.py"',
        '"/web/fastapi_app.py"',
        '"/web/dependencies/rate_limit.py"',
        '"/security/secure_logging.py"',
    ):
        assert anchored in hook, (
            f"{anchored} is no longer allowlisted; either it stopped "
            "reading env vars directly or the hook now fails on it"
        )
    assert "sqlcipher_utils.py" in hook
    assert "server_config.py" in hook


def test_no_production_module_writes_to_the_process_environment():
    """Nothing in src/ mutates ``os.environ``.

    This is what makes the import-time inventory above tractable: because
    no production module sets or pops an env var, an import-time read can
    only disagree with a later read when something *outside* src/ (a test,
    a shell, an operator) changes it.  If that stops being true, the
    frozen-at-import reads become order-dependent inside a single process.
    """
    offenders = []
    for path in _iter_source_files():
        tree = _parse(path)
        if tree is None:
            continue
        for node in ast.walk(tree):
            rel = path.relative_to(SRC_PKG).as_posix()
            if isinstance(node, ast.Call):
                func = node.func
                if (
                    isinstance(func, ast.Attribute)
                    and func.attr in ("setdefault", "pop", "update", "clear")
                    and isinstance(func.value, ast.Attribute)
                    and func.value.attr == "environ"
                ):
                    offenders.append((rel, node.lineno, func.attr))
            if isinstance(node, (ast.Assign, ast.AugAssign)):
                targets = (
                    node.targets
                    if isinstance(node, ast.Assign)
                    else [node.target]
                )
                for target in targets:
                    if (
                        isinstance(target, ast.Subscript)
                        and isinstance(target.value, ast.Attribute)
                        and target.value.attr == "environ"
                    ):
                        offenders.append((rel, node.lineno, "assign"))
    assert not offenders, f"production code mutates os.environ: {offenders}"