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.

10.9 KB
Raw Blame
"""Every environment variable the operator docs document must actually be
read by the application.

Why this guard exists
---------------------

``check-env-vars.py`` (pre-commit) polices *where* ``os.environ`` may be
read — it does not compare documentation against code. The FastAPI
migration renamed ``RATELIMIT_STORAGE_URL`` to ``RATE_LIMIT_STORAGE_URI``
and made the old name a fallback; a docs page that keeps documenting only
the old name hands operators a variable that silently does less than it
claims. That drift class is invisible until someone copies the variable
into a production deployment and nothing changes.

This test scans the operator-facing docs (``docs/deployment/``,
``docs/troubleshooting.md``) for backticked ALL_CAPS_WITH_UNDERSCORES
tokens outside fenced code blocks, and requires each to appear somewhere
under ``src/`` — as a literal the code reads (``os.environ``/``os.getenv``
usage, env_registry definitions, or any other textual reference).
An occurrence immediately followed by "has been removed." or "has been
removed;" is an upgrade warning, not a claim that the variable still works.
Only that occurrence is excluded; later usage advice still gets checked.
Backticked tokens that match the shape but are not environment variables
at all (e.g. Linux capability names) are listed in ``_NOT_ENV_VARS``.

Deliberately one-directional: the reverse direction (code reads a variable
no doc mentions) is legitimate — internal and test-mode knobs outnumber
documented ones and churn too fast for docs to track.
"""

# allow: no-sut-import — a documentation-reference guardian over
# static files, not behaviour; importing the app would add side effects
# this guard deliberately avoids (see module docstring).

import re
from pathlib import Path

import pytest

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

#: Docs whose env-var mentions are operator-facing claims.
DOC_PATHS = sorted((REPO_ROOT / "docs" / "deployment").glob("*.md")) + [
    REPO_ROOT / "docs" / "troubleshooting.md",
]

#: At least one underscore: excludes plain words like ``GET`` or ``HTTP``
#: that happen to be backticked. All-caps-enforced by the same regex.
#: Anchored (full-token) — used to validate backticked doc tokens.
_ENV_VAR_RE = re.compile(r"^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$")

#: Unanchored twin for scanning source files: finds every such token
#: anywhere in a line (string literals, registry definitions, comments).
_SRC_ENV_TOKEN_RE = re.compile(r"[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+")

#: Documented variables that are legitimately NOT read from src/, each
#: with the reason. Keep this list short and justified.
_DOCKER_COMPOSE_ONLY = {
    # Read by .github/workflows (CI), not by the application.
}

#: Backticked doc tokens that match the env-var shape but are not
#: environment variables at all, each with the reason. Keep this short.
_NOT_ENV_VARS = frozenset(
    {
        "CAP_SYS_PTRACE",  # Linux capability name (docs/troubleshooting.md)
    }
)


def _strip_fences(text: str) -> str:
    return re.sub(r"```.*?```", "", text, flags=re.DOTALL)


def _documented_env_vars() -> dict[str, list[str]]:
    """{env_var: [doc file:line, ...]} for backticked candidates."""
    found: dict[str, list[str]] = {}
    for doc in DOC_PATHS:
        if not doc.is_file():
            continue
        text = _strip_fences(doc.read_text(encoding="utf-8"))
        for match in re.finditer(r"`([^`\n]+)`", text):
            token = match.group(1).strip()
            if not _ENV_VAR_RE.match(token):
                continue
            # Preserve explicit migration notices without exempting every
            # mention of the variable or other variables in the paragraph.
            if re.match(r"\s+has\s+been\s+removed[.;]", text[match.end() :]):
                continue
            line = text.count("\n", 0, match.start()) + 1
            found.setdefault(token, []).append(
                f"{doc.relative_to(REPO_ROOT)}:{line}"
            )
    return found


def _tokens_read_by_src() -> frozenset[str]:
    """Every ALL_CAPS_UNDERSCORES token appearing anywhere under src/.

    One regex pass per file; membership is a superset of "read via
    os.environ" but that is the right direction for a docs-consistency
    guard: a variable the source never mentions at all is certainly not
    read, while one mentioned in a comment next to its ``os.getenv`` call
    (the common style here) counts.
    """
    tokens: set[str] = set()
    for py in SRC_ROOT.rglob("*.py"):
        try:
            text = py.read_text(encoding="utf-8", errors="replace")
        except OSError:
            continue
        tokens.update(_SRC_ENV_TOKEN_RE.findall(text))
    return frozenset(tokens)


def _setting_key_candidates(var: str) -> set[str] | None:
    """Every settings key whose LDR_-prefixed env name would be ``var``.

    ``settings/env_settings.py`` derives env names dynamically as
    ``"LDR_" + key.upper().replace(".", "_")`` — so ``LDR_LLM_PROVIDER``
    is read, but never appears as a literal. The derivation is not
    uniquely reversible (``llm.openai.api_key`` and ``llm.openai.api.key``
    both map to ``LDR_LLM_OPENAI_API_KEY``), so return ALL dot/underscore
    partitions of the lowercased remainder; a documented variable passes
    the guard when ANY candidate appears literally in src.

    Combinatorics are tiny (2^(n-1) for n segments; env names have ≤ 6) and
    results are memoised by the caller.
    """
    if not var.startswith("LDR_"):
        return None
    parts = var[len("LDR_") :].lower().split("_")
    candidates: set[str] = set()

    def _build(index: int, acc: str) -> None:
        if index == len(parts):
            candidates.add(acc)
            return
        if not acc:
            _build(index + 1, parts[index])
        else:
            _build(index + 1, acc + "." + parts[index])
            _build(index + 1, acc + "_" + parts[index])

    _build(0, "")
    return candidates


def _dotted_key_appears_in_src(key: str, cached: dict[str, bool] = {}) -> bool:
    """Whether the dotted settings key appears literally in any src file.

    Results are memoised in ``cached`` (a default-argument dict — the
    classic read-only-cache idiom; this helper is module-private and
    never mutated by callers).
    """
    if key not in cached:
        cached[key] = any(
            key in py.read_text(encoding="utf-8", errors="replace")
            for py in SRC_ROOT.rglob("*.py")
        )
    return cached[key]


def test_documented_env_vars_are_known_to_src():
    documented = _documented_env_vars()
    # Anti-vacuity: these docs demonstrably document a meaningful number of
    # variables today; an extraction regression should not pass silently.
    assert len(documented) >= 5, (
        f"only {len(documented)} documented env vars found in "
        f"{[str(p) for p in DOC_PATHS]} — the extractor is likely broken"
    )

    read_by_src = _tokens_read_by_src()
    unknown = {}
    for var, locations in documented.items():
        if (
            var in read_by_src
            or var in _DOCKER_COMPOSE_ONLY
            or var in _NOT_ENV_VARS
        ):
            continue
        # Dynamic LDR_* derivation: the env name is constructed from a
        # settings key at runtime, so accept any reverse-mapped key
        # candidate appearing literally (see _setting_key_candidates).
        candidates = _setting_key_candidates(var)
        if candidates is not None and any(
            _dotted_key_appears_in_src(key) for key in candidates
        ):
            continue
        unknown[var] = locations
    assert not unknown, (
        "Environment variables documented in operator docs but never "
        "referenced anywhere under src/ (operators would set no-ops):\n"
        + "\n".join(
            f"  {var}  (documented at {', '.join(locs)})"
            for var, locs in sorted(unknown.items())
        )
        + "\nEither the docs are stale (rename/remove the variable) or the "
        "code lost its read — or, if it is genuinely consumed elsewhere "
        "(docker-compose/CI), add it to _DOCKER_COMPOSE_ONLY with a reason. "
        "If the token is not an environment variable at all (e.g. a Linux "
        "capability name), add it to _NOT_ENV_VARS with a reason."
    )


def test_dynamic_env_derivation_mechanism_still_exists():
    """The reverse-mapping fallback above is only sound while
    ``settings/env_settings.py`` still derives env names as
    ``"LDR_" + key.upper().replace(".", "_")``. If that mechanism
    changes, the fallback silently stops matching — so pin it."""
    env_settings = (
        SRC_ROOT / "local_deep_research" / "settings" / "env_settings.py"
    )
    assert env_settings.is_file(), (
        "settings/env_settings.py moved — update "
        "test_documented_env_vars.py's reverse-mapping fallback"
    )
    source = env_settings.read_text(encoding="utf-8")
    assert "LDR_" in source and ".upper().replace" in source, (
        "The LDR_* env-name derivation expression changed; the "
        "reverse-mapping fallback in test_documented_env_vars.py is "
        "now unsound and must be revisited"
    )


def test_scanned_docs_exist():
    """The guard is only meaningful while it scans the files that carry the
    operator claims — fail loudly if they move."""
    existing = [p for p in DOC_PATHS if p.is_file()]
    assert existing, (
        f"none of the configured doc paths exist: {[str(p) for p in DOC_PATHS]} "
        "— update DOC_PATHS in this test when operator docs move"
    )
    assert (REPO_ROOT / "docs" / "deployment").is_dir(), (
        "docs/deployment/ no longer exists — update DOC_PATHS"
    )


@pytest.mark.parametrize(
    ("text", "expected"),
    [
        ("`LDR_RETIRED_OPTION` has been removed.", set()),
        ("`LDR_RETIRED_OPTION`\nhas been removed; do not set it.", set()),
        (
            "`LDR_RETIRED_OPTION` has been removed.\n"
            "Set `LDR_RETIRED_OPTION` to enable this feature.",
            {"LDR_RETIRED_OPTION"},
        ),
        (
            "`LDR_RETIRED_OPTION` has been removed; set `LDR_UNKNOWN_OPTION`.",
            {"LDR_UNKNOWN_OPTION"},
        ),
        (
            "`LDR_RETIRED_OPTION` has not been removed.",
            {"LDR_RETIRED_OPTION"},
        ),
        (
            "`LDR_RETIRED_OPTION` will be removed in the next release.",
            {"LDR_RETIRED_OPTION"},
        ),
        (
            "`LDR_RETIRED_OPTION` has been removed from the examples.",
            {"LDR_RETIRED_OPTION"},
        ),
        (
            "Set `LDR_UNKNOWN_OPTION` to enable this feature.",
            {"LDR_UNKNOWN_OPTION"},
        ),
    ],
)
def test_documented_env_extractor_distinguishes_removal_notices(
    monkeypatch, tmp_path, text, expected
):
    doc = tmp_path / "upgrading.md"
    doc.write_text(text, encoding="utf-8")
    monkeypatch.setitem(globals(), "REPO_ROOT", tmp_path)
    monkeypatch.setitem(globals(), "DOC_PATHS", [doc])

    assert set(_documented_env_vars()) == expected


if __name__ == "__main__":  # pragma: no cover
    pytest.main([__file__, "-v"])