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.

14.1 KB
Raw Blame
"""Guardian for ``security/egress/README.md``'s PEP tables.

The README's own words: "**Keep this table accurate — it is load-bearing, not
documentation.** ... the model-discovery gate was silently lost in a merge
(its Flask file was deleted ...) and this table is the index an auditor uses
to notice that an enforcement point has gone missing."

That instruction — "If you add, move, or remove a PEP, update this row in the
same commit" — is currently enforced by trust alone. This test automates it:

1. Every source file the tables cite must exist on disk.
2. Every function/class a table row names must be defined in a file cited on
   that same row (for section 3's "Consults" column: somewhere in the egress
   package, which is where the PDP lives).

Pure stdlib AST — no app import, so it runs in every environment and fails
for checkouts that could not even boot the app. Under-extraction is preferred
over noise: tokens the parser does not understand (wildcards like
``evaluate_*()``, ellipsis paths like ``…/implementations/…``) are skipped
rather than guessed at.
"""

# 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 ast
import re
from functools import lru_cache
from pathlib import Path

import pytest

SRC_ROOT = Path(__file__).resolve().parents[2] / "src" / "local_deep_research"
PKG_DIR = SRC_ROOT / "security" / "egress"
README = PKG_DIR / "README.md"

# Backticked token that looks like a code identifier (no path separators,
# no dots). Trailing call parens are stripped before matching, so both
# `classify_engine` and `classify_engine()` are recognised.
_BARE_IDENT_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")

# Tokens that are deliberate prose references, not symbols the parser should
# require. Every entry needs a reason.
_PROSE_IDENTIFIERS = {
    # Settings key named in the SearXNG row's prose ("a `url_setting`"),
    # not a function or class.
    "url_setting",
    # Module named in audit_hook.py's prose ("new contributors using raw
    # `requests`"), not a symbol defined in that file.
    "requests",
}


def _is_abbreviated_path(token: str) -> bool:
    """True for deliberately abbreviated paths like ``…/implementations/…``
    (the embeddings row cites three files; the middle one is elided).
    These are skipped rather than resolved — guessing the prefix would
    trade a silent pass for a wrong failure.
    """
    return "…" in token


def _strip_fences(text: str) -> str:
    """Remove fenced code blocks — their contents are examples, not table
    rows, and the Public API block lists import names that section 2 already
    covers file-by-file."""
    return re.sub(r"```.*?```", "", text, flags=re.DOTALL)


def _split_table_row(line: str) -> list[str]:
    parts = [p.strip() for p in line.strip().strip("|").split("|")]
    return parts


def _backticked(text: str) -> list[str]:
    return re.findall(r"`([^`]+)`", text)


def _split_call_suffix(token: str) -> str:
    """``classify_engine()`` / ``set_active_context(ctx)`` -> bare name."""
    return token.split("(", 1)[0].strip()


def _iter_table_rows(lines: list[str], start_marker: str) -> list[str]:
    """Rows (as raw strings) of the first markdown table after
    ``start_marker``; the header and ``|---|`` separator are skipped."""
    rows: list[str] = []
    in_section = False
    table_started = False
    for line in lines:
        stripped = line.strip()
        if not in_section:
            if start_marker in line:
                in_section = True
            continue
        if stripped.startswith("|"):
            if set(stripped) <= {"|", "-", ":", " "}:
                table_started = True
                continue
            if rows or table_started or stripped.startswith("|---") is False:
                rows.append(line)
        elif rows:
            break
        elif stripped.startswith("#") and table_started is False and rows == []:
            # a heading or paragraph before the table — keep scanning
            continue
    # Drop a leading header row (the first row is always the header).
    return rows[1:] if rows else []


@lru_cache(maxsize=None)
def _defined_names(py_file: Path) -> frozenset[str]:
    """All function/class names (including methods) plus module-level
    assignment targets in ``py_file``."""
    try:
        tree = ast.parse(py_file.read_text(encoding="utf-8"))
    except (OSError, SyntaxError):
        return frozenset()

    names: set[str] = set()
    for node in ast.walk(tree):
        if isinstance(
            node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)
        ):
            names.add(node.name)
        elif isinstance(node, (ast.Assign, ast.AnnAssign)):
            targets = (
                node.targets if isinstance(node, ast.Assign) else [node.target]
            )
            for target in targets:
                if isinstance(target, ast.Name):
                    names.add(target.id)
    return frozenset(names)


def _resolve_source_path(token: str) -> Path | None:
    """Resolve a backticked path token to an existing file under src/.

    Tries the token relative to the package root, the egress package dir,
    and the security package dir (the README mixes all three conventions:
    ``web/routers/research.py``, ``audit_hook.py``, ``egress/validators.py``,
    ``security/__init__``). Returns None for deliberately unresolvable
    tokens (ellipsis paths), which callers skip.
    """
    if "…" in token or token.startswith("…"):
        return None
    token = token.strip()
    candidates = []
    for base in (SRC_ROOT, PKG_DIR, SRC_ROOT / "security"):
        candidate = (base / token).resolve()
        candidates.append(candidate)
        if not token.endswith(".py"):
            candidates.append((base / (token + ".py")).resolve())
    for candidate in candidates:
        if candidate.is_file():
            return candidate
    return None


def _is_pathish(token: str) -> bool:
    return token.endswith(".py") or "/__init__" in token or "/" in token


def _package_surface() -> frozenset[str]:
    """Every name defined in any module of the egress package."""
    surface: set[str] = set()
    for py in PKG_DIR.glob("*.py"):
        surface |= _defined_names(py)
    return frozenset(surface)


class TestPackageFileTable:
    """Section 2 — "Files in this package": each cited file exists and the
    names its role column mentions are defined in it."""

    def test_cited_files_exist_and_named_symbols_are_defined(self):
        text = _strip_fences(README.read_text(encoding="utf-8"))
        rows = _iter_table_rows(text.splitlines(), "### Files in this package")
        assert rows, "could not find the 'Files in this package' table"

        failures: list[str] = []
        for row in rows:
            cols = _split_table_row(row)
            if len(cols) < 2:
                continue
            file_tokens = [
                t
                for t in _backticked(cols[0])
                if _is_pathish(t) and not _is_abbreviated_path(t)
            ]
            if not file_tokens:
                continue
            resolved = _resolve_source_path(file_tokens[0])
            if resolved is None:
                failures.append(
                    f"section 2 row cites {file_tokens[0]!r}, which does "
                    "not resolve to an existing file under src/"
                )
                continue
            names = _defined_names(resolved)
            for token in _backticked(cols[1]):
                ident = _split_call_suffix(token)
                if not _BARE_IDENT_RE.match(ident):
                    continue  # dotted/wildcard/prose token — see docstring
                if ident in _PROSE_IDENTIFIERS:
                    continue
                if ident not in names:
                    failures.append(
                        f"{resolved.relative_to(SRC_ROOT.parent.parent)}: "
                        f"README section 2 cites {ident!r}, which is not "
                        "defined in that file"
                    )
        assert not failures, "\n".join(failures)


class TestPepLocationTable:
    """Section 3 — "Where the PEPs live": every cited file exists, every
    named enforcement function is defined in a file on the same row, and
    every "Consults" name is part of the egress package surface."""

    def test_pep_rows_resolve(self):
        text = _strip_fences(README.read_text(encoding="utf-8"))
        rows = _iter_table_rows(text.splitlines(), "## 3. Where the PEPs live")
        assert rows, "could not find the 'Where the PEPs live' table"

        package_names = _package_surface()
        failures: list[str] = []

        for row in rows:
            cols = _split_table_row(row)
            if len(cols) < 3:
                continue
            location, consults = cols[1], cols[2]

            # Files cited in the PEP-location column.
            row_files: list[Path] = []
            for token in _backticked(location):
                if "." in token and not token.endswith(".py"):
                    # dotted path.name form, e.g.
                    # `egress/validators.resolve_engine_allow_private_ips`
                    left, _, name = token.rpartition(".")
                    ident = _split_call_suffix(name)
                    target = _resolve_source_path(left)
                    if target is not None:
                        row_files.append(target)
                        if _BARE_IDENT_RE.match(ident) and (
                            ident not in _defined_names(target)
                        ):
                            failures.append(
                                f"README PEP table: {token!r} names "
                                f"{ident!r}, not defined in "
                                f"{target.name}"
                            )
                    continue
                if _is_pathish(token) and not _is_abbreviated_path(token):
                    target = _resolve_source_path(token)
                    if target is None:
                        failures.append(
                            f"README PEP table cites {token!r}, which does "
                            "not resolve to an existing file under src/"
                        )
                    else:
                        row_files.append(target)

            # Bare identifiers in the location column: enforcement call
            # sites, which must live in one of the files cited on the row.
            for token in _backticked(location):
                ident = _split_call_suffix(token)
                if "." in token or not _BARE_IDENT_RE.match(ident):
                    continue
                if ident in _PROSE_IDENTIFIERS:
                    continue
                if not any(ident in _defined_names(f) for f in row_files):
                    failures.append(
                        f"README PEP table names {ident!r} but it is not "
                        "defined in any file cited on the same row ("
                        f"{', '.join(f.name for f in row_files) or 'no files cited'})"
                    )

            # Consults column: PDP surface — must exist somewhere in the
            # egress package.
            for token in _backticked(consults):
                ident = _split_call_suffix(token)
                if not _BARE_IDENT_RE.match(ident):
                    continue
                if ident not in package_names:
                    failures.append(
                        f"README PEP table 'Consults' column cites "
                        f"{ident!r}, which is not defined anywhere in the "
                        "egress package (policy.py / fetch.py / validators.py)"
                    )

        assert not failures, "\n".join(failures)


class TestAdjacentReferences:
    """The bullets after the PEP table cite general security utils; the
    files must exist and the names they mention must be defined in them."""

    def test_adjacent_util_files_and_names_resolve(self):
        text = _strip_fences(README.read_text(encoding="utf-8"))
        # Non-greedy .* stopping at the *first* blank line would end right
        # after the heading (the heading is immediately followed by a blank
        # line, before any bullets) and capture an empty block, making this
        # test vacuous. Stop instead at the section's real terminator: the
        # next heading or the "---" divider that follows the bullets and
        # the orchestrator prose line.
        match = re.search(
            r"Adjacent \(general security utils.*?(?=\n---|\n#{1,6} )",
            text,
            flags=re.DOTALL,
        )
        assert match, "could not find the 'Adjacent' bullets paragraph"
        block = match.group(0)

        bullets = [
            line for line in block.splitlines() if line.strip().startswith("- ")
        ]
        assert bullets, "found no 'Adjacent' bullet lines to check"

        failures: list[str] = []
        # The orchestrator mention is prose inside the same paragraph tail.
        for line in block.splitlines():
            tokens = _backticked(line)
            current_file: Path | None = None
            for token in tokens:
                if _is_pathish(token) and not _is_abbreviated_path(token):
                    resolved = _resolve_source_path(token)
                    if resolved is None:
                        failures.append(
                            f"adjacent-references bullet cites {token!r}, "
                            "which does not resolve under src/"
                        )
                    else:
                        current_file = resolved
                    continue
                ident = _split_call_suffix(token)
                if not _BARE_IDENT_RE.match(ident):
                    continue
                if current_file is not None and (
                    ident not in _defined_names(current_file)
                ):
                    failures.append(
                        f"adjacent-references bullet: {ident!r} is not "
                        f"defined in {current_file.name}"
                    )
        assert not failures, "\n".join(failures)


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