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.

84.3 KB
Raw Blame
#!/usr/bin/env python3
"""
Pre-commit hook: fence the web layer's HTTP file-serving references.

Report artifacts land in a single directory shared by the whole
install. Their cross-user safety posture rests on three facts: writes
are confined to the root, absolute paths are never disclosed, and --
what this hook fences -- **no HTTP handler streams files from that
directory**. A future "download the generated report file" feature
that hands a user-controlled or guessable path to ``FileResponse`` /
``StaticFiles`` would turn the shared directory into a cross-user read
primitive.

Scope: this hook is a syntax fence against ACCIDENTAL or IDIOMATIC
code -- the shapes a contributor writes without meaning to bypass
anything. Deliberate evasion by a contributor is out of scope and is
left to code review (CODEOWNERS); the hook does not try to be a
sandbox. Examples of deliberate evasion it does NOT catch (the list is
not exhaustive): a default supplied from outside the class body or
signature -- by a metaclass, decorator or ``__init_subclass__``, or set
after the class body (``Cls.attr = Depends()``, ``f.__defaults__ =
...``); walrus bindings in subscript targets or in
other annotations; module-level string or type aliases of a fenced name
(``R = "FileResponse"`` then ``r: R = Depends()``); and the gaps listed
under "Out of scope" below.

Tracking where a path value came from (local bindings, attributes,
return values) is not something a pre-commit AST check can do
reliably, so the fence is deliberately coarse. Under
``src/local_deep_research/web/`` it flags:

1. Any reference to a file-serving primitive (``FileResponse``,
   ``StaticFiles``, ``StreamingResponse`` -- imported under any alias,
   or reached as a module attribute such as ``responses.FileResponse``)
   and any star import, except in the files listed in
   ``ALLOWED_FILES``. The allowlist is per *file*: everything in an
   allowlisted file is exempt from this rule (all of
   ``web/fastapi_app.py``, not only its static-asset routes).
2. Any call to LDR's own serving wrappers -- a name ending in
   ``StreamingResponse`` / ``FileResponse`` that is not itself a
   primitive, e.g. ``WorkerCleanupStreamingResponse`` from the
   allowlisted ``web/dependencies/threadpool.py``, under any import
   alias or as a module attribute -- whose content argument (first
   positional or ``content=``) is not a direct call, by bare name, to a
   *same-module generator*: an undecorated ``def`` / ``async def``
   defined at module level or nested in another function -- NOT one
   anywhere inside a class body, so a method or a function nested in a
   method never qualifies (fail closed) -- whose own body contains
   ``yield`` / ``yield from`` (yields inside its nested functions,
   lambdas or classes do not count). The name qualifies only if EVERY
   ``def`` of it anywhere in the module (methods and defs nested in
   methods included) is an undecorated generator -- a decorator can
   replace the function, so a decorated def disqualifies its name --
   nothing else in the module binds it (assignment incl. a lambda,
   walrus, ``for``/``with`` target, ``del``, import or import alias,
   parameter, class, ``except`` target, ``global``/``nonlocal``, match
   capture) and it is not a Python builtin, so ``open`` or ``iter``
   never qualifies by colliding with a local function or method. A
   ``from x import *`` anywhere in the module disqualifies EVERY
   candidate, since a star import can rebind any name without the
   scanner being able to tell which ones statically.
   ``open(p)``, ``iter(open(p))``, a helper that returns ``open(p)``, a
   path, a variable or an attribute read are flagged. The *arguments*
   of the accepted generator call are not inspected:
   ``W(passthru(open(p)))`` with a same-module generator ``passthru``
   is accepted. This applies in every scanned file, allowlisted ones
   included.
   Outside the allowlist, any non-call reference to such a wrapper or
   to a ``SERVING_HELPERS`` helper (rebinding it to another name,
   subclassing it, passing it as a value) is flagged too.
3. Any serving call (primitive, wrapper, or a serving helper named in
   ``SERVING_HELPERS`` such as ``static_file_response`` -- under any
   import alias or as a module attribute) whose arguments reference the
   outputs directory inline (``OUTPUT_DIR``,
   ``get_research_outputs_directory()``, ``.outputs_dir``, or a name
   they are imported as, e.g. ``from x import OUTPUT_DIR as OD``). This
   also applies in every scanned file, allowlisted ones included. The
   helper arm is defensive: ``static_file_response`` lives in an
   allowlisted file, so rule 1 never sees the primitive it calls, and
   although its current signature takes only ``path`` (the root is
   always ``STATIC_DIR``), a call handing it the outputs directory in
   any position or keyword is still flagged, so a future signature
   that grows a directory parameter cannot silently serve the shared
   directory from a router.

4. Re-export under an unrecognised name, in every scanned file
   (allowlisted ones included): ``from x import <primitive or
   wrapper> as <alias>`` unless the alias is itself a serving name
   (a primitive, or ends in ``StreamingResponse`` / ``FileResponse``),
   and ``from x import <helper> as <alias>`` unless the alias is a
   serving or helper name. Otherwise a second module could import the
   alias (``from ..utils.streaming import CleanupStream``) and call it
   with no rule applying, since no rule follows a name across modules.
   Leading underscores are ignored when matching a helper name
   (``as _static_file_response`` keeps the role). Inside an allowlisted
   file, any loaded non-call reference to a primitive, wrapper or
   helper is flagged (``Download = WorkerCleanupStreamingResponse``,
   ``return FileResponse``, ``partial(static_file_response, ...)``),
   except in a type-only annotation, and a class may subclass one only if its
   own name ends in ``StreamingResponse`` / ``FileResponse`` (so the
   wrapper rules follow it into every importer). With rule 2's
   non-call rule outside the allowlist, a serving callable can reach
   another module of the fenced tree only under a name the rules
   recognise.
5. Evaluated annotations, in every scanned file. An annotation is not
   type-only when FastAPI or the interpreter evaluates it into more
   than a type: a parameter or class-body field annotation (wherever
   the field sits in the class body, also under ``if`` / ``try``, and
   whatever binds its name there: ``f: T`` then ``f = Depends()``)
   whose default is not a constant (``= Depends()`` makes FastAPI itself
   call the annotated class, filling its parameters from the request;
   FastAPI refuses some response classes, ``FileResponse`` among them,
   at route registration, but the hook does not rely on which classes
   it accepts and flags ``r: FileResponse = Depends()`` all the same),
   an ``Annotated[...]`` with a non-constant metadata element
   (``Annotated[T, Depends(static_file_response)]`` calls the helper
   with a request-supplied ``path``), a starred element or a non-tuple
   slice, any call, walrus or lambda inside it, a class field with no
   binding in its own class body on a class with a base other than
   ``object`` / ``Generic`` / ``Protocol`` (it may inherit a
   ``Depends()`` default), or a string, other than an exempt one
   (below), that does not parse. Every string inside such an annotation
   is parsed and checked like unquoted code, recursively, since FastAPI
   evaluates it (a wholly quoted ``"Annotated[T,
   Depends(static_file_response)]"`` is as live as the unquoted form),
   unless it is exempt. Exemption is an allowlist of shapes (see
   ``_approved_children``), so whatever it does not name fails closed: a
   string is exempt only if every step of its path from the annotation's
   root is approved -- subscripts of a proven generic head
   (``Optional``, ``Union``, ``list`` ...) and the first element of a
   proven ``Annotated`` whose metadata is inert (each element a
   constant, a call of a proven listed callee or a dict display:
   pydantic hands the first element to any other metadata object's
   ``__get_pydantic_core_schema__``), then one last step -- and it ends
   as a string member of a proven ``Literal[...]``; as metadata of such
   an ``Annotated`` (an element after the first, where the key has two
   or more elements and none is starred); as a string passed directly,
   in that metadata or in a type slot, to an argument
   ``VALUE_ARGUMENTS`` lists, of a proven callee
   (``Query(description="...")``, ``NewType("Name", str)``,
   ``ForwardRef("T", module="...")``); or as a string key or value of a
   dict display in that metadata. Any other node on the path -- an
   attribute read, a subscript of another head, an operator or
   comparison, an unlisted call, a lambda, walrus, conditional, starred
   or formatted value -- ends it, and every string below it is checked,
   whatever reads the string back out:
   ``Literal["FileResponse"].__args__[0]``, ``G |
   Literal["FileResponse"]``, ``G[Literal["FileResponse"]]``,
   ``f"{Literal['FileResponse']}"``, ``Annotated[*(), "FileResponse",
   Depends()]`` and ``Annotated[Literal["FileResponse"], G]`` can each
   hand the string on to a type position, which FastAPI resolves.
   ``Literal`` and ``Annotated`` are proven only when imported from
   ``typing`` or ``typing_extensions`` (``PLAIN_FORMS``), a
   ``VALUE_ARGUMENTS`` callee only when imported from its own module
   (``from fastapi import Query [as Q]``, or ``fastapi.Query`` after
   ``import fastapi``), and a generic head only from ``typing``,
   ``typing_extensions`` or ``collections.abc`` or as an unbound
   builtin, each under a name nothing else in the file binds (after
   ``from typing import Annotated as Literal``,
   ``Literal["FileResponse", Depends()]`` is checked as the
   ``Annotated`` it is). Nothing in a file is proven if it could change
   what a proven name denotes (see ``_proven_names``): a star import; a
   reference to a name that can look up or rewrite a namespace
   dynamically (such as ``setattr``, ``vars``, ``globals``, ``locals``,
   ``__dict__``, ``sys.modules``, ``importlib``, ``exec`` or ``eval``);
   an attribute or item store or delete rooted in an imported name
   (``typing.Literal = ...``), in an unbound name or in an expression;
   on any object, a store or delete whose target attribute is named like
   something a proof rests on (``mod.Literal = ...``), or whose target
   goes through a dunder or a private attribute of anything but ``self``
   / ``cls``; a proven module, or one a proof rests on reached as an
   attribute, used other than to read an attribute off it (``mod =
   typing``, ``mod = typing_extensions.typing``), or a private name of
   one; or a proven form, listed callee or subscripted generic head used
   outside a type position (``L = Literal``, ``type(typing.List)``).
   Every other call argument is checked, so an unknown call fails
   closed: ``typing.cast(object, "FileResponse")`` hands its string on
   to a type position, which FastAPI resolves. ``ForwardRef``'s type
   argument is a type position, under any name it is imported as or
   qualified. A string is parsed as it is (as ``ForwardRef`` compiles
   it, so ``"FileResponse"`` followed by a line continuation is checked)
   and stripped. In every scanned file, a string in a type position that
   parses neither way (``"*[FileResponse]"`` is a valid ForwardRef but
   not an expression), or one nested deeper than ``_MAX_QUOTE_DEPTH``,
   is reported on its own, as an annotation string that cannot be
   checked; elsewhere -- a call argument, or a ``Literal`` member or
   ``Annotated`` metadata that is not exempt -- a string is also parsed
   the way ``ForwardRef`` reads ``"*[FileResponse]"``, and one that
   parses no way is not reported (``typing`` cannot resolve it; it may
   be a regex).
   In an allowlisted file a serving name in
   such an annotation is a non-call reference (rule 4); outside the
   allowlist every reference is already flagged by rules 1 and 2. In
   every scanned file, a serving name QUOTED inside such an annotation
   (``r: "WorkerCleanupStreamingResponse" = Depends()``, which FastAPI
   resolves by name) is flagged.

Aliases are resolved file-wide with no scope tracking: a local name
imported as several serving names anywhere in the file (in different
functions, or once under ``if TYPE_CHECKING:``) is checked against
every rule that applies to ANY of them, and a local name that is
itself a wrapper name stays a wrapper whatever it is imported as, so
a ``SERVING_HELPERS`` alias can never mask a wrapper (fail closed).

A file that cannot be read or parsed (syntax error, undecodable bytes,
nesting too deep for the parser, an OS error) is reported as an
offender, never skipped, and never crashes the run. Files are parsed
from bytes, so a UTF-8 BOM or a PEP 263 coding cookie is honoured.

Out of scope, so NOT fenced: responses that carry file *content*
already read into memory (``Response(content=p.read_bytes())``,
``PlainTextResponse(p.read_text())``, ``HTMLResponse(...)``); a
same-module generator that itself opens and yields a file
(``WorkerCleanupStreamingResponse(generate())`` where ``generate``
reads the outputs directory) or that is handed a file object as an
argument (``W(passthru(open(p)))``); anything inside an allowlisted
file beyond rules 2, 3, 4 and 5 -- in particular, a new serving
*function* defined in an allowlisted file (``def download(p): return
FileResponse(p)`` in ``web/fastapi_app.py``) whose name is not in
``SERVING_HELPERS`` is not recognised, so calls to it anywhere are
unfenced; for rule 3, a rebinding of an outputs identifier, in the
same module or re-exported by another one (``root = OUTPUT_DIR`` then
``FileResponse(root / n)``; outside the allowlist rule 1 still flags
the primitive; the same gap applies to a ``SERVING_HELPERS`` call
handed such a rebinding); a serving helper defined in ``web/`` whose
name is not in ``SERVING_HELPERS``; a primitive, wrapper or helper
wrapped, renamed or re-exported by a module outside ``web/`` (which
this hook does not scan) and imported from there; ``Annotated`` reached
through an assignment alias (``A = Annotated``) with non-call metadata
in an allowlisted file; a ``VALUE_ARGUMENTS`` callee, ``PLAIN_FORMS``
form or generic head changed from another module, through a dynamic
lookup that uses none of the names under rule 5 (``getattr(m, "Lit" +
"eral")``), through a name used only inside a quoted annotation, or
through an object derived from it at runtime (``type(Literal["x"])``),
whose exempt strings stay unparsed; code that rewrites
``__annotations__`` after the fact, such as a decorator, which no check
of an annotation as written can see; a serving name assembled from
pieces as an annotation is evaluated (``"File" + "Response"``,
``f"{'File'}Response"``), since each string is checked on its own; and
dynamic lookups (``getattr``, ``importlib``, ``__import__``,
``module.__dict__[...]``, ``eval``).

The companion contract (tests/security/test_architecture_fences.py)
runs this same scanner over the same tree, pre-commit style (repo-root
cwd, repo-relative paths). Every path argument is made absolute
(relative ones against the cwd) and taken relative to the repository
root twice -- lexically (``..`` collapsed, symlinks not followed) and
resolved (symlinks followed) -- and the file is scanned if EITHER form
is in the fenced tree, so ``..`` segments, a non-root cwd, or a
symlink inside ``web/`` pointing elsewhere cannot move a file out of
scope. The allowlist is matched against the resolved path only (the
file whose content is scanned). Repo files outside the fenced tree
(both forms) are skipped; files outside the repository (mutation
fixtures) are always scanned, with no allowlist applied.
"""

import ast
import builtins
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent

#: The fenced tree (repo-relative; the trailing slash keeps
#: ``web_search_engines/`` out).
FENCED_PREFIX = "src/local_deep_research/web/"

#: Starlette/FastAPI primitives that serve files or arbitrary byte
#: streams over HTTP.
SERVING_PRIMITIVES = frozenset(
    {
        "FileResponse",
        "StaticFiles",
        "StreamingResponse",
    }
)

#: Name suffixes of LDR's own serving wrappers (subclasses of the
#: primitives, e.g. ``WorkerCleanupStreamingResponse``). Their content
#: argument must be a call to a generator defined in the same module.
WRAPPER_SUFFIXES = ("StreamingResponse", "FileResponse")

#: Function names of LDR's own file-serving helpers: functions (not
#: wrapper classes) that return a serving response, defined in an
#: allowlisted file, e.g. ``static_file_response(path)``. Rule 3
#: (outputs directory referenced inline) applies to their calls in any
#: argument position or keyword. That is defensive: the helper takes no
#: directory today, but the check stays correct if one is added.
SERVING_HELPERS = frozenset({"static_file_response"})

#: Repo-relative files exempt from the primitive-reference rule, and
#: why. The exemption covers the WHOLE file.
ALLOWED_FILES = {
    "src/local_deep_research/web/fastapi_app.py": (
        "only the favicon route uses FileResponse, for the fixed "
        "STATIC_DIR/favicon.ico; /static delegates to web/static_files.py "
        "(whole file exempt)"
    ),
    "src/local_deep_research/web/dependencies/threadpool.py": (
        "defines WorkerCleanupStreamingResponse (StreamingResponse "
        "subclass) for generated streaming bodies (whole file exempt)"
    ),
    "src/local_deep_research/web/static_files.py": (
        "shared /static and /redirect-static resolver; always serves from "
        "STATIC_DIR (no directory parameter) and only files whose resolved "
        "path is inside it (whole file exempt)"
    ),
}

#: Identifiers tying a path to the shared outputs directory.
OUTPUTS_IDENTIFIERS = (
    "get_research_outputs_directory",
    "OUTPUT_DIR",
    "outputs_dir",
)


def _is_wrapper(name: str | None) -> bool:
    return (
        name is not None
        and name not in SERVING_PRIMITIVES
        and name.endswith(WRAPPER_SUFFIXES)
    )


def _is_serving(name: str | None) -> bool:
    return name in SERVING_PRIMITIVES or _is_wrapper(name)


def _is_helper(name: str | None) -> bool:
    # Leading underscores are ignored, so a private-looking alias such
    # as ``_static_file_response`` is still recognised as the helper.
    return name is not None and name.lstrip("_") in SERVING_HELPERS


def _keeps_role(name: str, asname: str) -> bool:
    """True if importing ``name`` as ``asname`` keeps it recognisable.

    A primitive or wrapper must stay a serving name (a primitive or a
    ``...StreamingResponse`` / ``...FileResponse`` wrapper name), so
    every module that imports the alias applies the wrapper rules to
    it. A helper may become a helper name or any serving name (the
    wrapper rules are stricter than the helper rule).
    """
    if _is_serving(name):
        return _is_serving(asname)
    if _is_helper(name):
        return _is_serving(asname) or _is_helper(asname)
    return True


def _import_aliases(tree: ast.AST) -> dict[str, set[str]]:
    """Local alias -> EVERY imported serving name it is bound to
    (``FR`` -> ``{"FileResponse"}``, ``sfr`` -> ``{"static_file_response"}``).

    The map is file-wide with no scope tracking, so one alias imported
    as different serving names in different scopes (two functions, or a
    ``TYPE_CHECKING`` block) maps to all of them, and every rule that
    applies to ANY of them is applied (fail closed): a helper alias can
    never mask a wrapper alias of the same local name.
    """
    aliases: dict[str, set[str]] = {}
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            for alias in node.names:
                if alias.asname and (
                    _is_serving(alias.name) or _is_helper(alias.name)
                ):
                    aliases.setdefault(alias.asname, set()).add(alias.name)
    return aliases


def _outputs_names(tree: ast.AST) -> set[str]:
    """OUTPUTS_IDENTIFIERS plus any local name one is imported as
    (``from x import OUTPUT_DIR as OD`` adds ``OD``)."""
    names = set(OUTPUTS_IDENTIFIERS)
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            for alias in node.names:
                if alias.asname and alias.name in OUTPUTS_IDENTIFIERS:
                    names.add(alias.asname)
    return names


def _resolved_names(node: ast.AST, aliases: dict[str, set[str]]) -> set[str]:
    """Every name ``node`` may refer to: the bare name itself (so a local
    name that is itself a serving name is never masked by an alias) plus
    every serving name it is imported as anywhere in the file."""
    if isinstance(node, ast.Name):
        return {node.id} | aliases.get(node.id, set())
    if isinstance(node, ast.Attribute):
        return {node.attr}
    return set()


def _display(names: set[str]) -> str:
    return "/".join(sorted(names))


def _references_outputs(node: ast.AST, outputs_names: set[str]) -> bool:
    # ast.walk also reaches the callee Name of
    # get_research_outputs_directory(), so call forms need no extra arm.
    for child in ast.walk(node):
        if isinstance(child, ast.Name) and child.id in outputs_names:
            return True
        if (
            isinstance(child, ast.Attribute)
            and child.attr in OUTPUTS_IDENTIFIERS
        ):
            return True
    return False


def _content_argument(call: ast.Call) -> ast.AST | None:
    for kw in call.keywords:
        if kw.arg == "content":
            return kw.value
    if call.args and not isinstance(call.args[0], ast.Starred):
        return call.args[0]
    return None


_SCOPES = (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda, ast.ClassDef)


def _own_body_yields(func: ast.AST) -> bool:
    """True if ``func``'s own body yields (nested scopes excluded)."""
    stack = list(ast.iter_child_nodes(func))
    while stack:
        node = stack.pop()
        if isinstance(node, (ast.Yield, ast.YieldFrom)):
            return True
        if isinstance(node, _SCOPES):
            continue
        stack.extend(ast.iter_child_nodes(node))
    return False


_DEFS = (ast.FunctionDef, ast.AsyncFunctionDef)


def _candidate_defs(tree: ast.AST) -> list[ast.AST]:
    """Module-level and function-nested defs. Everything inside a class
    body (methods, and defs nested in methods) is skipped: a method is
    never reached by a bare-name call, and a generator nested in a
    method is not accepted either (fail closed)."""
    found: list[ast.AST] = []
    stack = [tree]
    while stack:
        node = stack.pop()
        if isinstance(node, ast.ClassDef):
            continue
        if isinstance(node, _DEFS):
            found.append(node)
        stack.extend(ast.iter_child_nodes(node))
    return found


def _is_generator_def(func: ast.AST) -> bool:
    # A decorator can replace the function with anything, so a
    # decorated def is never trusted as a generator.
    return not func.decorator_list and _own_body_yields(func)


def _other_bindings(tree: ast.AST) -> set[str]:
    """Names bound by anything other than a function ``def``."""
    bound: set[str] = set()
    for node in ast.walk(tree):
        if isinstance(node, ast.Name) and not isinstance(node.ctx, ast.Load):
            bound.add(node.id)
        elif isinstance(node, ast.arg):
            bound.add(node.arg)
        elif isinstance(node, ast.alias):
            bound.add(node.asname or node.name.split(".")[0])
        elif isinstance(node, ast.ClassDef):
            bound.add(node.name)
        elif isinstance(node, ast.ExceptHandler) and node.name:
            bound.add(node.name)
        elif isinstance(node, (ast.Global, ast.Nonlocal)):
            bound.update(node.names)
        elif isinstance(node, (ast.MatchAs, ast.MatchStar)) and node.name:
            bound.add(node.name)
        elif isinstance(node, ast.MatchMapping) and node.rest:
            bound.add(node.rest)
    return bound


def _has_star_import(tree: ast.AST) -> bool:
    """True if the module has a ``from x import *`` anywhere.

    A star import can rebind any name in the module -- including one
    that looks like an accepted same-module generator -- so its mere
    presence disqualifies every generator candidate, not just the
    names it happens to shadow (those aren't known statically).
    """
    return any(
        isinstance(node, ast.ImportFrom)
        and any(alias.name == "*" for alias in node.names)
        for node in ast.walk(tree)
    )


def _local_generators(tree: ast.AST) -> set[str]:
    """Names that can only mean a same-module generator function."""
    if _has_star_import(tree):
        return set()
    generators = {d.name for d in _candidate_defs(tree) if _is_generator_def(d)}
    # Disqualify over EVERY def in the module, methods and defs nested in
    # methods included: a name counts only if all of its defs anywhere
    # are undecorated generators. Any other binding of it (assignment,
    # import, parameter...) or a builtin name (open, iter) disqualifies
    # it too.
    not_generators = {
        d.name
        for d in ast.walk(tree)
        if isinstance(d, _DEFS) and not _is_generator_def(d)
    }
    return (
        generators - not_generators - _other_bindings(tree) - set(dir(builtins))
    )


def _is_local_generator_call(
    node: ast.AST | None, local_generators: set[str]
) -> bool:
    return (
        isinstance(node, ast.Call)
        and isinstance(node.func, ast.Name)
        and node.func.id in local_generators
    )


def _primitive_references(tree: ast.AST) -> list[tuple[int, str]]:
    hits: list[tuple[int, str]] = []
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            for alias in node.names:
                if alias.name in SERVING_PRIMITIVES:
                    hits.append((node.lineno, f"import of {alias.name}"))
                elif alias.name == "*":
                    # A star import can bring a primitive (or a wrapper)
                    # in without naming it.
                    hits.append(
                        (node.lineno, f"star import from {node.module}")
                    )
        elif isinstance(node, ast.Attribute):
            if node.attr in SERVING_PRIMITIVES:
                hits.append((node.lineno, f"reference to .{node.attr}"))
        elif isinstance(node, ast.Name):
            if node.id in SERVING_PRIMITIVES:
                hits.append((node.lineno, f"reference to {node.id}"))
    return hits


def _renaming_imports(tree: ast.AST) -> list[tuple[int, str]]:
    """``from x import <serving name> as <other>`` that loses the role.

    Every scanned file, allowlisted ones included. An alias that is not
    itself a serving or helper name would let ANOTHER module import the
    alias under a name no rule recognises (``from ..utils.streaming
    import CleanupStream``), so the rename is flagged where it happens.
    """
    hits: list[tuple[int, str]] = []
    for node in ast.walk(tree):
        if not isinstance(node, ast.ImportFrom):
            continue
        for alias in node.names:
            if (
                alias.asname
                and alias.asname != alias.name
                and not _keeps_role(alias.name, alias.asname)
            ):
                hits.append(
                    (
                        node.lineno,
                        f"import of {alias.name} renamed to {alias.asname} "
                        "(a serving primitive, wrapper or helper must keep a "
                        "name the fence recognises, e.g. a ...StreamingResponse "
                        "suffix)",
                    )
                )
    return hits


#: Annotation content that is evaluated into something other than a
#: type (see ``_annotations``): a call (``Depends(static_file_response)``), a walrus (binds in
#: the enclosing scope, since annotations are evaluated eagerly) or a
#: lambda.
_EVALUATED = (ast.Call, ast.NamedExpr, ast.Lambda)


def _annotation_names(tree: ast.AST, name: str) -> set[str]:
    """``name`` plus every local name it is imported as."""
    names = {name}
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            names.update(
                alias.asname
                for alias in node.names
                if alias.name == name and alias.asname
            )
    return names


#: Nesting bound for quoted annotations (a string inside a string ...):
#: a string nested deeper than this counts as evaluated (fail closed)
#: and is reported as unchecked (an exempt string is never parsed).
_MAX_QUOTE_DEPTH = 8

#: Offender text for a string the hook cannot check: one in a type
#: position that does not parse, or one nested too deep.
_UNCHECKED_STRING = (
    "annotation string that cannot be checked (it does not parse as an "
    "expression, or quotes nest deeper than "
    f"{_MAX_QUOTE_DEPTH}); write the annotation unquoted or as a valid "
    "forward reference"
)


def _is_literal_head(node: ast.AST) -> bool:
    """True if ``node`` is a subscript of a name spelled ``Literal`` (bare
    or qualified), whatever that name is bound to. Only a ``Literal``
    that ``_plain_form`` proves can hold exempt strings (see
    ``_approved_children``)."""
    if not isinstance(node, ast.Subscript):
        return False
    head = node.value
    return (isinstance(head, ast.Name) and head.id == "Literal") or (
        isinstance(head, ast.Attribute) and head.attr == "Literal"
    )


def _is_annotated_head(node: ast.AST, annotated: set[str]) -> bool:
    """True if ``node`` is a subscript of a name spelled ``Annotated``
    (under any import alias, or qualified as ``typing.Annotated``),
    whatever that name is bound to. ``_has_evaluated_metadata`` checks
    every such subscript (fail closed); only an ``Annotated`` that
    ``_plain_form`` proves can hold exempt strings (see
    ``_approved_children``)."""
    if not isinstance(node, ast.Subscript):
        return False
    head = node.value
    return (isinstance(head, ast.Name) and head.id in annotated) or (
        isinstance(head, ast.Attribute) and head.attr == "Annotated"
    )


#: Call arguments that only ever hold a plain value -- documentation, an
#: alias, a regex, example values, a ``NewType`` name or a module name --
#: which the callee (from FastAPI, pydantic or ``typing``) itself never
#: evaluates as a type and never calls, as ``{callee: positions}`` (an
#: index for a positional argument, a name for a keyword). A string
#: literal passed there directly is exempt from parsing only where the
#: call sits on an approved path and the callee is provably the listed
#: one (see ``_approved_children`` and ``_proven_names``); every other
#: argument is parsed and checked, so an unknown call fails closed. The
#: entries match the signatures of FastAPI 0.136, pydantic 2.13 and
#: Python 3.12 to 3.14; ``test_value_arguments_match_the_signatures`` in
#: tests/security/test_architecture_fences.py checks them against the
#: installed ones. ``ForwardRef``'s position 2 is ``module`` before
#: Python 3.14, which makes it keyword-only.
_PARAMETER_TEXT = frozenset(
    {"alias", "title", "description", "pattern", "regex", "example", "examples"}
)
VALUE_ARGUMENTS: dict[str, frozenset[int | str]] = {
    **{
        f"fastapi.{name}": _PARAMETER_TEXT
        for name in (
            "Query",
            "Path",
            "Header",
            "Cookie",
            "Body",
            "Form",
            "File",
        )
    },
    "fastapi.Depends": frozenset({"scope"}),
    "pydantic.Field": frozenset(
        {"alias", "title", "description", "pattern", "examples"}
    ),
    "pydantic.constr": frozenset({"pattern"}),
    "typing.NewType": frozenset({0, "name"}),
    "typing.ForwardRef": frozenset({2, "module"}),
}

#: Modules whose import makes ``module.callee`` a ``VALUE_ARGUMENTS`` callee.
_VALUE_MODULES = frozenset(
    callee.rpartition(".")[0] for callee in VALUE_ARGUMENTS
)

#: Subscript forms that can hold exempt strings, as ``module.name``: the
#: string members of ``Literal[...]`` and the metadata of
#: ``Annotated[...]`` (its elements after the first), which ``typing``
#: and FastAPI keep as values. Like a ``VALUE_ARGUMENTS`` callee, a form
#: counts only under a name ``_proven_names`` proves is imported from
#: its module, and only on an approved path (see ``_approved_children``).
PLAIN_FORMS = frozenset(
    f"{module}.{form}"
    for module in ("typing", "typing_extensions")
    for form in ("Literal", "Annotated")
)

#: Modules whose import makes ``module.Literal`` / ``module.Annotated`` a
#: ``PLAIN_FORMS`` form.
_FORM_MODULES = frozenset(form.rpartition(".")[0] for form in PLAIN_FORMS)

#: Modules whose subscriptable names (``Optional``, ``List``,
#: ``Sequence``, ``Literal`` ...) keep the key of a subscript a type
#: argument: subscripting one never hands its key to the project's code,
#: so it is a step of an approved path (see ``_approved_children``).
_GENERIC_MODULES = frozenset({"typing", "typing_extensions", "collections.abc"})

#: Builtins that keep the key of a subscript a type argument
#: (``list[T]``), where nothing in the file binds the name. Their types
#: cannot be changed, only the names rebound.
_GENERIC_BUILTINS = frozenset(
    {"list", "dict", "set", "frozenset", "tuple", "type"}
)

#: What ``_proven_names`` can prove a name denotes (and any name of a
#: ``_GENERIC_MODULES`` module).
_PROVABLE = frozenset(
    {
        *VALUE_ARGUMENTS,
        *_VALUE_MODULES,
        *PLAIN_FORMS,
        *_FORM_MODULES,
        *_GENERIC_MODULES,
    }
)

#: The modules a proof can rest on, and the last part of each module's
#: name, by which an attribute read can reach the module from another
#: one (``typing_extensions.typing``).
_PROOF_MODULES = _VALUE_MODULES | _FORM_MODULES | _GENERIC_MODULES
_PROOF_MODULE_NAMES = frozenset(
    module.rpartition(".")[2] for module in _PROOF_MODULES
)

#: Names through which a file can reach or rewrite a namespace -- a
#: module's, a class's, the builtins or ``sys.modules`` -- without naming
#: what it replaces (``setattr(typing, "Literal", ...)``,
#: ``vars(typing)[...]``, ``typing.__dict__[...]``, ``sys.modules[...]``,
#: ``importlib.import_module("typing")``, ``exec(...)``). A reference to
#: one, as a name, an attribute of anything or a part of an import,
#: voids every proof in the file (see ``_proven_names``).
_DYNAMIC_NAMES = frozenset(
    {
        "__builtins__",
        "__delattr__",
        "__dict__",
        "__import__",
        "__setattr__",
        "delattr",
        "eval",
        "exec",
        "globals",
        "import_module",
        "importlib",
        "locals",
        "modules",
        "setattr",
        "vars",
    }
)

#: Definitions that bind their own name.
_NAMED_DEFINITIONS = (
    ast.FunctionDef,
    ast.AsyncFunctionDef,
    ast.ClassDef,
    ast.ExceptHandler,
    ast.MatchAs,
    ast.MatchStar,
    ast.TypeVar,
    ast.ParamSpec,
    ast.TypeVarTuple,
)


def _proven_names(tree: ast.AST) -> dict[str, str]:
    """Local names that provably denote a ``VALUE_ARGUMENTS`` callee, a
    ``PLAIN_FORMS`` form, a name of a ``_GENERIC_MODULES`` module, a
    ``_GENERIC_BUILTINS`` builtin, or one of these modules: ``{"Q":
    "fastapi.Query", "fastapi": "fastapi", "Literal": "typing.Literal",
    "list": "builtins.list"}``.

    A name counts only if each import binding it in the file is the same
    ``from <module> import <name> [as <local>]`` (or ``import <module>
    [as <local>]``; a builtin, only if nothing imports it) and nothing
    else in the file binds it -- an assignment or ``del``, a ``def``,
    ``class``, parameter, ``for`` / ``with`` / ``except`` / ``match``
    target, walrus, ``global`` / ``nonlocal`` or type parameter (no
    scope tracking; fail closed) -- so a local pass-through that happens
    to be called ``Query`` is an unknown callee, and ``Literal =
    typing.Annotated`` is not a ``Literal``.

    Nothing in the file is proven at all (fail closed: every string is
    then checked) if it contains any of these, through which it could
    change what a proven name denotes:

    * a star import;
    * a ``_DYNAMIC_NAMES`` name, as a name, as an attribute of anything
      or imported by name (``setattr(typing, "Literal", ...)``,
      ``vars(typing)[...]``, ``typing.__dict__[...]``,
      ``sys.modules[...]``, ``exec(...)``, ``from importlib import
      import_module``);
    * a store or delete (an assignment, augmented assignment or
      ``del``, a loop or ``with`` target) of an attribute or item that
      is rooted in an imported name (``typing.Literal = ...``,
      ``builtins.list = ...``, ``Query.__code__ = ...``), in a name the
      file never binds, or in anything but a name
      (``type(Literal).__getitem__ = ...``);
    * a store or delete, on any object, of an attribute named like
      anything a proof could rest on -- the last part of a
      ``PLAIN_FORMS`` or ``VALUE_ARGUMENTS`` name, a
      ``_GENERIC_BUILTINS`` name, or the last part of a name proven here
      or read off a proven module (``mod.Literal = ...``, ``b.list =
      ...``, ``t.Optional = ...``) -- or one whose target reads a dunder
      attribute (``c.__getitem__ = ...``, ``a.__class__.x = ...``) or a
      ``_``-private attribute of anything but ``self`` or ``cls``
      (``L._getitem = ...``);
    * a proven module referenced other than as the object of an
      attribute read (``mod = typing``, ``getattr(typing, ...)``), an
      attribute named like a module a proof rests on (``typing``,
      ``typing_extensions``, ``fastapi``, ``pydantic``, ``abc``) read
      other than as the object of another attribute read
      (``t = typing_extensions.typing``), or a ``_``-private name read
      off a proven module or imported from a module a proof rests on
      (``typing._SpecialForm``, ``from typing import _GenericAlias``);
    * a ``PLAIN_FORMS`` form, a ``VALUE_ARGUMENTS`` callee or a generic
      head the file subscripts, referenced -- under a name proven to
      denote it, or as an attribute of a proven module -- other than in
      a type position: the head of a subscript, the callee of a call, a
      whole annotation (``-> Dict``), or the key, or an element of the
      key, of a subscript whose head is proven (``List[Dict]``,
      ``dict[str, Dict]``). So ``L = Literal``, ``type(typing.List)``,
      ``partial(Query, ...)`` or ``isinstance(x, Mapping)`` in a file
      that subscripts ``Mapping`` voids every proof.

    Not seen (deliberate evasion, beyond a per-file syntax check): a
    form, callee or generic head changed from another module, through a
    dynamic lookup (``getattr(m, "Lit" + "eral")``), through a name used
    only inside a quoted annotation, or through an object derived from it
    at runtime (``type(Literal["x"])``) whose public attributes are then
    replaced."""
    imported: dict[str, set[str]] = {}
    bound: set[str] = set()
    # Attribute and item store / delete targets; every load of a name or
    # attribute with the node holding it; annotation roots; and each
    # tuple that is a subscript's key, with the subscript.
    targets: list[ast.AST] = []
    loads: list[tuple[ast.AST, ast.AST]] = []
    annotations: set[int] = set()
    keys: dict[int, ast.Subscript] = {}
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            module = f"{'.' * node.level}{node.module or ''}"
            for alias in node.names:
                if (
                    alias.name == "*"
                    or alias.name in _DYNAMIC_NAMES
                    or (alias.name.startswith("_") and module in _PROOF_MODULES)
                ):
                    return {}
                imported.setdefault(alias.asname or alias.name, set()).add(
                    f"{module}.{alias.name}"
                )
        elif isinstance(node, ast.Import):
            for alias in node.names:
                local = alias.asname or alias.name.partition(".")[0]
                source = alias.name if alias.asname else local
                imported.setdefault(local, set()).add(source)
        elif isinstance(node, ast.arg):
            bound.add(node.arg)
            if node.annotation is not None:
                annotations.add(id(node.annotation))
        elif isinstance(node, (ast.Global, ast.Nonlocal)):
            bound.update(node.names)
        elif isinstance(node, ast.MatchMapping):
            if node.rest:
                bound.add(node.rest)
        elif isinstance(node, ast.AnnAssign):
            annotations.add(id(node.annotation))
        elif isinstance(node, ast.Subscript) and isinstance(
            node.slice, ast.Tuple
        ):
            keys[id(node.slice)] = node
        if isinstance(node, _NAMED_DEFINITIONS) and node.name:
            bound.add(node.name)
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and (
            node.returns is not None
        ):
            annotations.add(id(node.returns))
        for child in ast.iter_child_nodes(node):
            if isinstance(child, ast.Name):
                spelled = child.id
            elif isinstance(child, ast.Attribute):
                spelled = child.attr
            elif isinstance(child, ast.Subscript):
                if not isinstance(child.ctx, ast.Load):
                    targets.append(child)
                continue
            else:
                continue
            if spelled in _DYNAMIC_NAMES:
                return {}
            if isinstance(child.ctx, ast.Load):
                loads.append((child, node))
            elif isinstance(child, ast.Name):
                bound.add(child.id)
            else:
                targets.append(child)
    names: dict[str, str] = {}
    for name, sources in imported.items():
        if len(sources) != 1 or name in bound:
            continue
        (source,) = sources
        if source in _PROVABLE or source.rpartition(".")[0] in _GENERIC_MODULES:
            names[name] = source
    for name in _GENERIC_BUILTINS - imported.keys() - bound:
        names[name] = f"builtins.{name}"
    modules = {
        name for name, source in names.items() if source in _PROOF_MODULES
    }

    def denotes(node: ast.AST) -> str:
        """What ``node`` denotes by ``names``, or ``""``."""
        return _proven_name(node, names, _PROOF_MODULES) or ""

    # The last part of everything a proof here could rest on, and of the
    # forms, callees and generic heads to be used only in type positions.
    relied = {
        source.rpartition(".")[2]
        for source in (*VALUE_ARGUMENTS, *PLAIN_FORMS, *names.values())
    } | _GENERIC_BUILTINS
    used = {
        source.rpartition(".")[2] for source in (*VALUE_ARGUMENTS, *PLAIN_FORMS)
    }
    for node, parent in loads:
        if (
            isinstance(node, ast.Attribute)
            and isinstance(node.value, ast.Name)
            and node.value.id in modules
        ):
            relied.add(node.attr)
            if node.attr.startswith("_"):
                return {}
        if isinstance(parent, ast.Subscript) and parent.value is node:
            source = denotes(node)
            if (
                source
                and source not in _PROOF_MODULES
                and not (source.startswith("builtins."))
            ):
                used.add(source.rpartition(".")[2])
    for target in targets:
        root = target
        while isinstance(root, (ast.Attribute, ast.Subscript)):
            root = root.value
        if (
            not isinstance(root, ast.Name)
            or root.id in imported
            or root.id not in bound
        ):
            return {}
        if isinstance(target, ast.Attribute) and target.attr in relied:
            return {}
        step = target
        while isinstance(step, (ast.Attribute, ast.Subscript)):
            if (
                isinstance(step, ast.Attribute)
                and step.attr.startswith("_")
                and (step.attr.endswith("__") or root.id not in ("self", "cls"))
            ):
                return {}
            step = step.value
    for node, parent in loads:
        if isinstance(parent, ast.Attribute) and parent.value is node:
            if isinstance(node, ast.Name) and node.id in modules:
                continue
        elif isinstance(node, ast.Name) and node.id in modules:
            return {}
        elif (
            isinstance(node, ast.Attribute) and node.attr in _PROOF_MODULE_NAMES
        ):
            return {}
        source = denotes(node)
        if (
            not source
            or source in _PROOF_MODULES
            or source.startswith("builtins.")
            or source.rpartition(".")[2] not in used
        ):
            continue
        subscript = keys.get(id(parent), parent)
        if not (
            (isinstance(parent, ast.Subscript) and parent.value is node)
            or (isinstance(parent, ast.Call) and parent.func is node)
            or id(node) in annotations
            or (
                isinstance(subscript, ast.Subscript)
                and node is not subscript.value
                and denotes(subscript.value)
            )
        ):
            return {}
    return names


def _proven_name(
    node: ast.AST, proven: dict[str, str], modules: frozenset[str]
) -> str | None:
    """What ``node`` provably denotes (see ``_proven_names``): a proven
    name, or an attribute of a name proven to be one of ``modules``
    (``fastapi.Query``), as ``module.name``; else None."""
    if isinstance(node, ast.Name):
        return proven.get(node.id)
    if (
        isinstance(node, ast.Attribute)
        and isinstance(node.value, ast.Name)
        and proven.get(node.value.id) in modules
    ):
        return f"{proven[node.value.id]}.{node.attr}"
    return None


def _value_callee(func: ast.AST, proven: dict[str, str]) -> str | None:
    """The ``VALUE_ARGUMENTS`` callee ``func`` provably names (``Query``,
    an alias ``Q``, or ``fastapi.Query``; see ``_proven_names``), or
    None."""
    callee = _proven_name(func, proven, _VALUE_MODULES)
    return callee if callee in VALUE_ARGUMENTS else None


def _plain_form(node: ast.AST, proven: dict[str, str]) -> str | None:
    """``"Literal"`` or ``"Annotated"`` if ``node`` is a subscript of that
    ``PLAIN_FORMS`` form under a name ``_proven_names`` proves
    (``Literal``, an alias ``L``, or ``typing.Literal``), else None."""
    if not isinstance(node, ast.Subscript):
        return None
    form = _proven_name(node.value, proven, _FORM_MODULES)
    return form.rpartition(".")[2] if form in PLAIN_FORMS else None


def _generic_head(head: ast.AST, proven: dict[str, str]) -> bool:
    """True if ``head`` provably names something of a ``_GENERIC_MODULES``
    module or a ``_GENERIC_BUILTINS`` builtin (``Optional``,
    ``typing.List``, ``list``; see ``_proven_names``), whose subscript
    keeps its key a type argument."""
    name = _proven_name(head, proven, _GENERIC_MODULES)
    return name is not None and (
        name.rpartition(".")[0] in _GENERIC_MODULES
        or name.startswith("builtins.")
    )


def _is_forward_ref(func: ast.AST, forward_refs: set[str]) -> bool:
    """True if ``func`` is named ``ForwardRef``: bare, under any name it
    is imported as, or qualified (``typing.ForwardRef``)."""
    return (isinstance(func, ast.Name) and func.id in forward_refs) or (
        isinstance(func, ast.Attribute) and func.attr == "ForwardRef"
    )


def _call_arguments(call: ast.Call) -> list[tuple[ast.AST, int | str | None]]:
    """Each argument of ``call`` with the parameter it binds: an index, a
    keyword, or None where that is unknown.

    Literal unpacking is followed, nested too: ``*("a", "b")`` binds
    indexes and ``**{"k": v}`` binds keyword ``k``. Anything else that is
    unpacked (``*args``, ``**dict(k=v)``, a key that is not a string
    constant) binds None, and so does every positional argument after a
    ``*`` of unknown length. The unpacking syntax itself (the starred
    node, the unpacked tuple, list or dict, and a dict's keys) is listed
    with None too."""
    found: list[tuple[ast.AST, int | str | None]] = []
    index: int | None = 0
    positional = list(reversed(call.args))
    while positional:
        arg = positional.pop()
        if isinstance(arg, ast.Starred):
            found.extend([(arg, None), (arg.value, None)])
            if isinstance(arg.value, (ast.Tuple, ast.List)):
                positional.extend(reversed(arg.value.elts))
            else:
                index = None
            continue
        found.append((arg, index))
        if index is not None:
            index += 1
    mappings: list[ast.AST] = []
    for kw in call.keywords:
        if kw.arg is None:
            mappings.append(kw.value)
        else:
            found.append((kw.value, kw.arg))
    while mappings:
        mapping = mappings.pop()
        found.append((mapping, None))
        if not isinstance(mapping, ast.Dict):
            continue
        for key, value in zip(mapping.keys, mapping.values):
            if key is None:
                mappings.append(value)
                continue
            found.append((key, None))
            named = isinstance(key, ast.Constant) and isinstance(key.value, str)
            found.append((value, key.value if named else None))
    return found


def _parse_expression(text: str) -> ast.AST | None:
    """``text`` parsed as an expression, or None if it does not parse."""
    try:
        return ast.parse(text, mode="eval").body
    except (SyntaxError, ValueError, RecursionError, MemoryError):
        return None


def _parse_string(text: str, star: bool) -> list[ast.AST]:
    """The distinct trees ``text`` parses to as an expression, trying it
    as it is (``ForwardRef`` compiles the raw string, so one ending in a
    line continuation -- a backslash and a newline -- is valid there)
    and with surrounding whitespace stripped (``eval`` strips leading
    spaces). With ``star``, each of the two that does not parse is also
    tried the way ``ForwardRef`` reads a string starting with ``*``, as
    ``({text},)[0]`` (``"*[T]"``), which also tolerates leading
    whitespace before a line continuation. Empty only if every attempt
    fails."""
    trees: dict[str, ast.AST] = {}
    for form in (text, text.strip()):
        parsed = _parse_expression(form)
        if parsed is None and star:
            parsed = _parse_expression(f"({form},)[0]")
        if parsed is not None:
            trees.setdefault(ast.dump(parsed), parsed)
    return list(trees.values())


def _is_string(node: ast.AST) -> bool:
    """True if ``node`` is a string literal."""
    return isinstance(node, ast.Constant) and isinstance(node.value, str)


#: Where a node sits on an approved path from an annotation's root (see
#: ``_approved_children``): a type slot, an ``Annotated`` metadata slot,
#: or an exempt string. A node that has none is off the path.
_TYPE_SLOT, _METADATA_SLOT, _EXEMPT = "type slot", "metadata slot", "exempt"


def _listed_arguments(call: ast.Call, proven: dict[str, str]) -> list[ast.AST]:
    """The arguments ``VALUE_ARGUMENTS`` lists for the callee that
    ``_value_callee`` proves ``call`` names, passed directly: a keyword
    by its name, a positional argument by its index before any ``*``
    unpacking (nothing inside a ``*`` or ``**`` is listed)."""
    listed = VALUE_ARGUMENTS.get(_value_callee(call.func, proven), frozenset())
    found: list[ast.AST] = []
    for index, argument in enumerate(call.args):
        if isinstance(argument, ast.Starred):
            break
        if index in listed:
            found.append(argument)
    found.extend(kw.value for kw in call.keywords if kw.arg in listed)
    return found


def _inert_metadata(node: ast.AST, proven: dict[str, str]) -> bool:
    """True if ``node``, as ``Annotated`` metadata, is an object FastAPI
    and pydantic never hand the annotated type to project code through:
    a constant, a call of a callee ``_value_callee`` proves, or a dict
    display with no ``**`` unpacking. Any other object may define
    ``__get_pydantic_core_schema__``, which pydantic calls with the
    type (``Annotated[Literal["T"], G]`` hands ``Literal["T"]`` to
    ``G``, which can read ``"T"`` back out into the type it validates
    with), so its ``Annotated`` approves nothing."""
    return (
        isinstance(node, ast.Constant)
        or (
            isinstance(node, ast.Call)
            and _value_callee(node.func, proven) is not None
        )
        or (isinstance(node, ast.Dict) and None not in node.keys)
    )


def _approved_children(
    node: ast.AST, slot: str, proven: dict[str, str]
) -> dict[int, str]:
    """The allowlist: ``{id(descendant): slot}`` for what ``node``, on an
    approved path in ``slot`` (see ``_annotation_nodes``), passes the
    path on to. A string literal is exempt from parsing only where a
    rule here makes it ``_EXEMPT``. Whatever no rule names is off the
    path, and so is everything below it, whatever the node kind -- an
    attribute read, an operator, a comparison, an unlisted call, a
    subscript of another head, a lambda, a walrus, a conditional, a
    starred or formatted value -- so every string there is parsed and
    checked. The rules, where proven means provably the ``typing``,
    ``typing_extensions``, ``collections.abc``, builtin, FastAPI or
    pydantic name (see ``_proven_names``):

    * in a type slot, a subscript of a proven ``Literal``: each string
      member of its key is exempt;
    * in a type slot, a subscript of a proven ``Annotated`` whose key is
      a tuple of two or more elements, none starred, with only inert
      metadata (see ``_inert_metadata``): the first element is a type
      slot, and each later one is exempt if it is a string, else a
      metadata slot;
    * in a type slot, a subscript of another proven generic head
      (``Optional``, ``Union``, ``list`` ...; see ``_generic_head``):
      each element of its key is a type slot;
    * in a type or metadata slot, a call of a proven ``VALUE_ARGUMENTS``
      callee: each string passed directly in a listed position is exempt
      (see ``_listed_arguments``);
    * in a metadata slot, a dict display: each string key and value is
      exempt (one with ``**`` unpacking is not inert metadata, so its
      ``Annotated`` approves nothing)."""
    if slot == _TYPE_SLOT and isinstance(node, ast.Subscript):
        key = node.slice
        elements = key.elts if isinstance(key, ast.Tuple) else [key]
        form = _plain_form(node, proven)
        if form == "Literal":
            return {
                id(member): _EXEMPT for member in elements if _is_string(member)
            }
        if form == "Annotated":
            if (
                not isinstance(key, ast.Tuple)
                or len(elements) < 2
                or any(isinstance(e, ast.Starred) for e in elements)
                or not all(_inert_metadata(m, proven) for m in elements[1:])
            ):
                return {}
            return {
                id(elements[0]): _TYPE_SLOT,
                **{
                    id(meta): _EXEMPT if _is_string(meta) else _METADATA_SLOT
                    for meta in elements[1:]
                },
            }
        if _generic_head(node.value, proven):
            return {id(element): _TYPE_SLOT for element in elements}
        return {}
    if isinstance(node, ast.Call):
        return {
            id(argument): _EXEMPT
            for argument in _listed_arguments(node, proven)
            if _is_string(argument)
        }
    if slot == _METADATA_SLOT and isinstance(node, ast.Dict):
        return {
            id(item): _EXEMPT
            for item in (*node.keys, *node.values)
            if _is_string(item)
        }
    return {}


#: How ``_annotation_nodes`` checks a string that is not exempt, by where
#: it sits: in a type position it must parse (it is reported if it does
#: not); in the argument context -- a call argument, a ``Literal``
#: member or ``Annotated`` metadata -- it is also parsed the way
#: ``ForwardRef`` reads a starred string, and not reported if it does
#: not parse at all, since it may be a value such as a regex.
_TYPE, _ARGUMENT = "type", "argument"


def _child_contexts(
    node: ast.AST,
    context: str,
    annotated: set[str],
    forward_refs: set[str],
    proven: dict[str, str],
) -> list[tuple[ast.AST, str]]:
    """``(descendant, context)`` pairs for what ``node``, seen in
    ``context``, contains. The caller keeps the first pair given for a
    node, so the specific ones come first: the string members of a
    ``Literal`` and the metadata of an ``Annotated`` (the form
    ``_plain_form`` proves, else the name it is spelled with; see
    ``_is_literal_head`` and ``_is_annotated_head``) and every argument
    of a call are in the argument context, except a ``ForwardRef``'s
    type argument, which is a type position; a call's callee is in the
    call's context. Every other child inherits ``context``."""
    given: list[tuple[ast.AST, str]] = []
    form = _plain_form(node, proven)
    if form == "Literal" or (form is None and _is_literal_head(node)):
        members = (
            node.slice.elts
            if isinstance(node.slice, ast.Tuple)
            else [node.slice]
        )
        given.extend(
            (member, _ARGUMENT) for member in members if _is_string(member)
        )
    elif (
        form == "Annotated"
        or (form is None and _is_annotated_head(node, annotated))
    ) and isinstance(node.slice, ast.Tuple):
        given.extend((meta, _ARGUMENT) for meta in node.slice.elts[1:])
    elif isinstance(node, ast.Call):
        forward_ref = _is_forward_ref(node.func, forward_refs)
        given.append((node.func, context))
        for argument, position in _call_arguments(node):
            if forward_ref and position in (0, "arg", None):
                given.append((argument, _TYPE))
            else:
                given.append((argument, _ARGUMENT))
    given.extend((child, context) for child in ast.iter_child_nodes(node))
    return given


def _annotation_nodes(
    root: ast.AST,
    annotated: set[str],
    forward_refs: set[str],
    proven: dict[str, str],
) -> tuple[list[tuple[ast.AST, int | None]], bool, list[int]]:
    """Every node of an annotation, quoted parts included.

    Returns ``(nodes, unparsable, unchecked)``. ``nodes`` holds
    ``(node, line)`` pairs: ``line`` is None for a node of ``root``
    itself and, for a node parsed out of a string, the line of the
    outermost string literal holding it. FastAPI (via
    ``typing.get_type_hints``) evaluates a quoted annotation, so every
    string in it is parsed as an expression, recursively
    (``"Annotated[NoteService, Depends()]"``, ``Optional["NoteService"]``),
    and the parsed tree is inspected exactly like an unquoted one --
    unless the string is exempt.

    A string is exempt only if every step of its path from the
    annotation's root is one ``_approved_children`` approves (an
    allowlist: what it does not name fails closed). ``root`` is a type
    slot, and a string in a type slot is parsed into a tree whose root
    is a type slot again; the path ends at any other node. So
    ``Literal["NoteService"]``, ``Optional[Literal["NoteService"]]``,
    ``Annotated[str, "NoteService"]``, ``Annotated[str,
    Query(description="NoteService")]`` and ``Annotated[str,
    {"description": "NoteService"}]`` hold exempt strings, and the
    wholly quoted ``"Annotated[Literal['NoteService'], Query()]"`` too,
    while ``Literal["NoteService"].__args__[0]``, ``G |
    Literal["NoteService"]``, ``G[Literal["NoteService"]]`` (``G`` not a
    proven generic head), ``f"{Literal['NoteService']}"``,
    ``Annotated[*(), "NoteService", Depends()]``,
    ``Annotated[Literal["NoteService"], G]`` (see ``_inert_metadata``)
    and ``typing.cast(object, "NoteService")`` do not: below a node no
    rule approves, every string is checked, whatever reads it out, and a
    tree parsed out of a string that is not exempt is off the path as a
    whole.

    A string that is not exempt is parsed (see ``_parse_string``) and
    checked in one of two contexts (see ``_child_contexts``). In a type
    position -- ``root``, and whatever is reached from one other than as
    a call argument, a ``Literal`` member or ``Annotated`` metadata, and
    a ``ForwardRef``'s type argument (the first positional, ``arg=``, or
    an argument whose position is unknown; under any name it is imported
    as or qualified) wherever the call sits -- it is parsed as it is and
    stripped, and one that parses neither way, or that nests deeper than
    ``_MAX_QUOTE_DEPTH``, makes ``unparsable`` True (fail closed: the
    caller treats the annotation as evaluated) and is listed in
    ``unchecked`` by the line of its outermost string literal, which the
    caller reports as an offender. ``"*[NoteService]"`` is valid as a
    ``ForwardRef`` but not as an expression, so in a type position it is
    unchecked. In the argument context it is also parsed the way
    ``ForwardRef`` reads it, as ``(*[NoteService],)[0]``
    (``typing.cast(object, "*[NoteService]")`` hands it on to a type
    position); one that parses no way at all makes ``unparsable`` True
    but is not reported, since ``typing`` cannot resolve it and it may
    be a value, such as a regex, and one nested too deep is reported.
    Literal ``*`` / ``**`` unpacking of call arguments is followed to
    find a ``ForwardRef``'s type argument (see ``_call_arguments``).

    Not seen: a name assembled from pieces as the annotation is
    evaluated (``"Note" + "Service"``, ``f"{'Note'}Service"``), since
    each string is checked on its own; a form, callee or generic head
    changed in a way ``_proven_names`` does not see; and code that
    rewrites ``__annotations__`` after the fact, such as a decorator,
    which no check of the annotation as written can see.
    """
    nodes: list[tuple[ast.AST, int | None]] = []
    unparsable = False
    unchecked: list[int] = []
    pending: list[tuple[ast.AST, str, str | None, int, int | None]] = [
        (root, _TYPE, _TYPE_SLOT, 0, None)
    ]
    while pending:
        current, base, base_slot, depth, line = pending.pop()
        # Contexts and approved-path slots by node id. ast.walk is
        # breadth first, so a node's parent (or the subscript or call
        # whose key or arguments hold it) sets both before the node is
        # reached; a node with no slot is off the path.
        contexts = {id(current): base}
        slots = {id(current): base_slot} if base_slot else {}
        for node in ast.walk(current):
            nodes.append((node, line))
            context = contexts.get(id(node), _TYPE)
            slot = slots.get(id(node))
            if slot in (_TYPE_SLOT, _METADATA_SLOT):
                slots.update(_approved_children(node, slot, proven))
            for child, given in _child_contexts(
                node, context, annotated, forward_refs, proven
            ):
                contexts.setdefault(id(child), given)
            if slot == _EXEMPT or not _is_string(node):
                continue
            here = node.lineno if line is None else line
            if depth >= _MAX_QUOTE_DEPTH:
                unparsable = True
                unchecked.append(here)
                continue
            trees = _parse_string(node.value, star=context == _ARGUMENT)
            if not trees:
                unparsable = True
                if context == _TYPE:
                    unchecked.append(here)
                continue
            inner = _TYPE_SLOT if slot == _TYPE_SLOT else None
            for tree in trees:
                pending.append((tree, context, inner, depth + 1, here))
    return nodes, unparsable, unchecked


def _has_evaluated_metadata(
    nodes: list[tuple[ast.AST, int | None]], annotated: set[str]
) -> bool:
    """True if an ``Annotated[...]`` among ``nodes`` is anything but
    ``Annotated[T, constant, ...]``: a non-constant metadata element
    (``Annotated[T, dep]`` with ``dep = Depends()`` hands FastAPI a
    dependency without a call), a starred element (``Annotated[*(T,
    dep)]`` unpacks to the same thing) or a slice that is not a tuple
    (``Annotated[args]`` with ``args = (T, dep)``)."""
    for node, _line in nodes:
        if not _is_annotated_head(node, annotated):
            continue
        if not isinstance(node.slice, ast.Tuple):
            return True
        elements = node.slice.elts
        if any(isinstance(element, ast.Starred) for element in elements):
            return True
        if any(not isinstance(meta, ast.Constant) for meta in elements[1:]):
            return True
    return False


#: Nodes that open their own scope: a store inside them never binds a
#: class attribute.
_NEW_SCOPES = (
    ast.FunctionDef,
    ast.AsyncFunctionDef,
    ast.ClassDef,
    ast.Lambda,
    ast.ListComp,
    ast.SetComp,
    ast.DictComp,
    ast.GeneratorExp,
)
_BLOCKS = (ast.stmt, ast.excepthandler, ast.match_case)


def _class_body_blocks(cls: ast.ClassDef) -> list[ast.AST]:
    """Every statement (and ``except`` / ``case`` clause) executed in
    ``cls``'s own body, including those nested under ``if`` / ``try`` /
    ``with`` / ``for`` / ``while`` / ``match``, but not the bodies of a
    nested def or class (the def or class statement itself is
    included: it binds its name)."""
    blocks: list[ast.AST] = []
    pending: list[ast.AST] = list(cls.body)
    while pending:
        block = pending.pop()
        blocks.append(block)
        if not isinstance(block, _NEW_SCOPES):
            pending.extend(
                child
                for child in ast.iter_child_nodes(block)
                if isinstance(child, _BLOCKS)
            )
    return blocks


def _own_expressions(block: ast.AST) -> list[ast.AST]:
    """The expression nodes of one statement or clause, not descending
    into nested statements or into a nested scope."""
    nodes: list[ast.AST] = []
    pending = [
        child
        for child in ast.iter_child_nodes(block)
        if not isinstance(child, _BLOCKS)
    ]
    while pending:
        node = pending.pop()
        nodes.append(node)
        if not isinstance(node, _NEW_SCOPES):
            pending.extend(ast.iter_child_nodes(node))
    return nodes


#: Bases that never supply a class attribute (a field default).
_PLAIN_BASES = frozenset({"object", "Generic", "Protocol"})


def _may_inherit_defaults(cls: ast.ClassDef) -> bool:
    """True if ``cls`` has a base other than ``object`` / ``Generic`` /
    ``Protocol`` (bare, qualified or subscripted), so a field it leaves
    without a default may inherit one from that base."""
    for base in cls.bases:
        head = base.value if isinstance(base, ast.Subscript) else base
        if isinstance(head, ast.Name):
            name = head.id
        elif isinstance(head, ast.Attribute):
            name = head.attr
        else:
            return True
        if name not in _PLAIN_BASES:
            return True
    return False


def _class_field_defaults(cls: ast.ClassDef) -> dict[int, ast.AST]:
    """``id(field)`` -> its default, for every annotated assignment in
    ``cls``'s own body (see ``_class_body_blocks``).

    A dataclass (or FastAPI, via the generated ``__init__``) reads a
    field's default from the class attribute, so EVERY binding of the
    field's name in the class body counts, not just ``x: T = value``:
    ``svc: NoteService`` followed by ``svc = Depends()`` defaults the
    field to ``Depends()``. A plain ``name = value`` contributes
    ``value``; any other binding (augmented or unpacking assignment,
    loop / ``with`` / ``except`` / ``case`` target, import, def, class,
    walrus) contributes the binding statement itself, which is never a
    constant (fail closed). The default is the first non-constant
    contribution, else the field's own value. A field with no binding
    at all in the class body of a class with any base other than
    ``object`` / ``Generic`` / ``Protocol`` may inherit its default
    from that base, so its default is the class itself, which is never
    a constant (fail closed).
    """
    blocks = _class_body_blocks(cls)
    bound: dict[str, list[ast.AST]] = {}

    def bind(name: str | None, value: ast.AST) -> None:
        if name:
            bound.setdefault(name, []).append(value)

    fields: list[ast.AnnAssign] = []
    for block in blocks:
        if isinstance(block, ast.AnnAssign):
            fields.append(block)
            if block.value is not None and isinstance(block.target, ast.Name):
                bind(block.target.id, block.value)
            if block.value is not None:
                for node in ast.walk(block.value):
                    if isinstance(node, ast.NamedExpr):
                        bind(node.target.id, block)
            continue
        if (
            isinstance(block, ast.Assign)
            and len(block.targets) == 1
            and isinstance(block.targets[0], ast.Name)
        ):
            bind(block.targets[0].id, block.value)
            for node in ast.walk(block.value):
                if isinstance(node, ast.NamedExpr):
                    bind(node.target.id, block)
            continue
        if isinstance(block, _NEW_SCOPES):
            bind(block.name, block)
        elif isinstance(block, (ast.Import, ast.ImportFrom)):
            for alias in block.names:
                bind(alias.asname or alias.name.split(".")[0], block)
        elif isinstance(block, ast.excepthandler):
            bind(block.name, block)
        for node in _own_expressions(block):
            if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
                bind(node.id, block)
            elif isinstance(node, (ast.MatchAs, ast.MatchStar)):
                bind(node.name, block)
            elif isinstance(node, ast.MatchMapping):
                bind(node.rest, block)
    defaults: dict[int, ast.AST] = {}
    inherits = _may_inherit_defaults(cls)
    for field in fields:
        values = (
            bound.get(field.target.id, [])
            if isinstance(field.target, ast.Name)
            else []
        )
        chosen = next(
            (v for v in values if not isinstance(v, ast.Constant)),
            field.value,
        )
        if chosen is None and inherits:
            chosen = cls
        if chosen is not None:
            defaults[id(field)] = chosen
    return defaults


def _annotations(tree: ast.AST) -> list[tuple[ast.AST, bool]]:
    """Every annotation as ``(annotation, type_only)``.

    An annotation is type-only -- never a rebinding -- unless:

    * it contains a call, walrus or lambda anywhere
      (``Annotated[T, Depends(static_file_response)]`` hands the callable to
      FastAPI; ``x: (s := static_file_response) = 1`` binds it in the enclosing
      scope, since annotations are evaluated eagerly before 3.14);
    * it is a parameter's or a class-body field's annotation (a
      dataclass field becomes an ``__init__`` parameter) whose default
      is not a constant: ``r: FileResponse = Depends()``,
      ``= Depends(None)`` or ``= dep`` with ``dep = Depends()`` makes
      FastAPI CALL the annotated class, filling its parameters from the
      request (FastAPI refuses some classes, ``FileResponse`` among them,
      at route registration; this check does not rely on that);
    * an ``Annotated[...]`` in it (under any import alias) carries a
      non-constant metadata element (``Annotated[class, dep]``), a
      starred element (``Annotated[*(class, dep)]``) or a slice that is
      not a tuple (``Annotated[args]``);
    * a string in it that is not exempt does not parse (see
      ``_annotation_nodes``), or nests too deep (fail closed; one in a type
      position, or nested too deep, is also reported itself, see
      ``_string_annotation_hits``);
    * it is a class field with no binding in its own class body, on a
      class with a base other than ``object`` / ``Generic`` /
      ``Protocol``: the field may inherit a non-constant default.

    Every string that is not exempt is parsed and checked like
    unquoted code, recursively (see ``_annotation_nodes``), so a wholly
    quoted ``"Annotated[class, Depends()]"`` is not type-only either. A class
    field counts wherever it sits in the class body, also under ``if`` /
    ``try`` / ``with`` / loops, and its default is any binding of its
    name there (``svc: T`` then ``svc = Depends()``; see
    ``_class_field_defaults``).

    Callee names are not matched here, so ``Depends`` imported under
    another name cannot hide a dependency (fail closed). Return
    annotations and module- or function-level variable annotations have
    no default.
    """
    annotated = _annotation_names(tree, "Annotated")
    forward_refs = _annotation_names(tree, "ForwardRef")
    proven = _proven_names(tree)
    defaults: dict[int, ast.AST] = {}
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            args = node.args
            positional = args.posonlyargs + args.args
            offset = len(positional) - len(args.defaults)
            for arg, default in zip(positional[offset:], args.defaults):
                defaults[id(arg)] = default
            for arg, default in zip(args.kwonlyargs, args.kw_defaults):
                if default is not None:
                    defaults[id(arg)] = default
        elif isinstance(node, ast.ClassDef):
            defaults.update(_class_field_defaults(node))
    found: list[tuple[ast.AST, bool]] = []
    for node in ast.walk(tree):
        if (
            isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
            and node.returns is not None
        ):
            root, default = node.returns, None
        elif isinstance(node, ast.arg) and node.annotation is not None:
            root, default = node.annotation, defaults.get(id(node))
        elif isinstance(node, ast.AnnAssign):
            root, default = node.annotation, defaults.get(id(node))
        else:
            continue
        nodes, unparsable, _unchecked = _annotation_nodes(
            root, annotated, forward_refs, proven
        )
        type_only = (
            (default is None or isinstance(default, ast.Constant))
            and not unparsable
            and not any(isinstance(n, _EVALUATED) for n, _line in nodes)
            and not _has_evaluated_metadata(nodes, annotated)
        )
        found.append((root, type_only))
    return found


def _annotation_ids(tree: ast.AST) -> set[int]:
    """ids of every node inside a type-only annotation (see
    ``_annotations``), which is never a rebinding."""
    return {
        id(child)
        for root, type_only in _annotations(tree)
        if type_only
        for child in ast.walk(root)
    }


def _string_annotation_hits(
    tree: ast.AST, aliases: dict[str, set[str]]
) -> list[tuple[int, str]]:
    """Serving names quoted inside a STRING in a non-type-only
    annotation, in every scanned file.

    FastAPI resolves a forward reference (``r: "FileResponse" =
    Depends()``, or a wholly quoted ``"Annotated[FileResponse,
    Depends()]"``) in the module globals, so a quoted serving name is as
    live there as a bare one, and no other rule sees it. Strings are
    parsed recursively (see ``_annotation_nodes``); one in a type
    position that does not parse, or any that nests too deep, is
    reported itself, as an annotation string that cannot be checked."""
    annotated = _annotation_names(tree, "Annotated")
    forward_refs = _annotation_names(tree, "ForwardRef")
    proven = _proven_names(tree)
    hits: list[tuple[int, str]] = []
    for root, type_only in _annotations(tree):
        if type_only:
            continue
        nodes, _unparsable, unchecked = _annotation_nodes(
            root, annotated, forward_refs, proven
        )
        hits.extend((line, _UNCHECKED_STRING) for line in unchecked)
        for inner, line in nodes:
            if line is None:
                continue
            names = {
                n
                for n in _resolved_names(inner, aliases)
                if _is_serving(n) or _is_helper(n)
            }
            if names:
                hits.append(
                    (
                        line,
                        f"reference to {_display(names)} quoted in an "
                        "evaluated annotation (FastAPI resolves it)",
                    )
                )
    return hits


def _allowlisted_rebindings(
    tree: ast.AST, aliases: dict[str, set[str]], callees: set[int]
) -> list[tuple[int, str]]:
    """Non-call references to serving names in an allowlisted file.

    An allowlisted file may reference primitives freely, but must not
    hand a primitive, wrapper or helper on under a name the fence does
    not recognise: ``Download = WorkerCleanupStreamingResponse``,
    ``return FileResponse``, ``partial(static_file_response, ...)``, or
    ``class Download(FileResponse)`` would each give importers an
    unfenced serving callable. Exempt: callees, type-only annotations
    (see ``_annotations``: ``= Depends()`` makes FastAPI call the
    annotated class with request-supplied arguments, so such an
    annotation is a reference), and the
    bases of a class whose own name is a serving name (a new
    ``...StreamingResponse`` / ``...FileResponse`` subclass is a
    wrapper, so the wrapper rules follow it into every importer).
    """
    exempt = _annotation_ids(tree) | callees
    hits: list[tuple[int, str]] = []
    for node in ast.walk(tree):
        if not isinstance(node, ast.ClassDef):
            continue
        for base in node.bases:
            names = {
                n
                for n in _resolved_names(base, aliases)
                if _is_serving(n) or _is_helper(n)
            }
            if not names:
                continue
            exempt.add(id(base))
            if not _is_serving(node.name):
                hits.append(
                    (
                        node.lineno,
                        f"class {node.name} subclasses {_display(names)} "
                        "under a name that does not end in StreamingResponse "
                        "/ FileResponse",
                    )
                )
    for node in ast.walk(tree):
        if not isinstance(node, (ast.Name, ast.Attribute)):
            continue
        if not isinstance(node.ctx, ast.Load) or id(node) in exempt:
            continue
        names = {
            n
            for n in _resolved_names(node, aliases)
            if _is_serving(n) or _is_helper(n)
        }
        if names:
            hits.append(
                (
                    node.lineno,
                    f"non-call reference to {_display(names)} in an "
                    "allowlisted file (serving callables must not be "
                    "re-bound or handed on)",
                )
            )
    return hits


def _parse(path: Path) -> tuple[ast.AST | None, str]:
    """Parse ``path``, or return ``(None, offender line)``.

    Fail closed: an unreadable or unparsable file is reported, never
    skipped, and a parser failure (too-deep nesting raises RecursionError
    or MemoryError) never crashes the run.
    """
    try:
        source = path.read_bytes()
    except OSError as exc:
        return None, f"{path}:1: could not read ({type(exc).__name__}: {exc})"
    try:
        # Bytes, not text: ast.parse then honours a UTF-8 BOM and a PEP
        # 263 coding cookie exactly as the interpreter does.
        return ast.parse(source, filename=str(path)), ""
    except (SyntaxError, ValueError, RecursionError, MemoryError) as exc:
        lineno = getattr(exc, "lineno", None) or 1
        return (
            None,
            f"{path}:{lineno}: could not parse ({type(exc).__name__}: {exc})",
        )


def scan_file(path: Path, rel: str | None = None) -> list[str]:
    offenders: list[str] = []
    tree, error = _parse(path)
    if tree is None:
        return [error]
    allowlisted = rel in ALLOWED_FILES
    if not allowlisted:
        for lineno, what in _primitive_references(tree):
            offenders.append(
                f"{path}:{lineno}: {what} (file-serving primitive outside "
                "the ALLOWED_FILES allowlist)"
            )
    aliases = _import_aliases(tree)
    outputs_names = _outputs_names(tree)
    local_generators = _local_generators(tree)
    for lineno, what in _renaming_imports(tree):
        offenders.append(f"{path}:{lineno}: {what}")
    for lineno, what in _string_annotation_hits(tree, aliases):
        offenders.append(f"{path}:{lineno}: {what}")
    callees: set[int] = set()
    for node in ast.walk(tree):
        if not isinstance(node, ast.Call):
            continue
        callees.add(id(node.func))
        names = _resolved_names(node.func, aliases)
        serving = {n for n in names if _is_serving(n) or _is_helper(n)}
        if not serving:
            continue
        if _references_outputs(node, outputs_names):
            offenders.append(
                f"{path}:{node.lineno}: {_display(serving)}(...) serves from "
                "the shared research outputs directory"
            )
        wrappers = {n for n in names if _is_wrapper(n)}
        if wrappers and not _is_local_generator_call(
            _content_argument(node), local_generators
        ):
            offenders.append(
                f"{path}:{node.lineno}: {_display(wrappers)}(...) content is "
                "not a bare-name call to a yielding generator function "
                "defined in this module"
            )
    if not allowlisted:
        for node in ast.walk(tree):
            if not isinstance(node, (ast.Name, ast.Attribute)):
                continue
            wrappers = {
                n
                for n in _resolved_names(node, aliases)
                if _is_wrapper(n) or _is_helper(n)
            }
            if id(node) not in callees and wrappers:
                offenders.append(
                    f"{path}:{node.lineno}: non-call reference to "
                    f"{_display(wrappers)} (serving wrappers and helpers "
                    "must be called directly)"
                )
    else:
        for lineno, what in _allowlisted_rebindings(tree, aliases, callees):
            offenders.append(f"{path}:{lineno}: {what}")
    return offenders


def _repo_relative(path: Path) -> tuple[str | None, str | None]:
    """``(lexical, resolved)`` repo-relative posix paths of ``path``;
    each is None if that form of the path lies outside the repo.

    Both are absolute-ised against the cwd, so ``..`` segments and a cwd
    other than the repo root still yield the true repo-relative path.
    The lexical form only normalises ``..`` textually (symlinks are not
    followed); the resolved form follows symlinks. The caller scans a
    file if EITHER form is in scope, so a symlink inside the fenced
    tree whose target lies elsewhere is still scanned (fail closed).
    """

    def rel(candidate: Path) -> str | None:
        try:
            return candidate.relative_to(REPO_ROOT).as_posix()
        except ValueError:
            return None

    # Collapse ``..`` textually (pathlib keeps it; resolve() would
    # follow symlinks): ``a/link/../b`` -> ``a/b``.
    parts: list[str] = []
    for part in path.absolute().parts:
        if part == "..":
            if len(parts) > 1:
                parts.pop()
        elif part != ".":
            parts.append(part)
    lexical = rel(Path(*parts))
    return lexical, rel(path.resolve())


def _safe_print(line: str) -> None:
    """Print ``line`` without crashing on undecodable path bytes.

    Argv path arguments are decoded with surrogateescape, so an
    offender line built from a non-UTF-8 filename can carry lone
    surrogates that a strict stdout encoder refuses to write
    (``UnicodeEncodeError``). Round-tripping through the
    ``backslashreplace`` error handler turns any such surrogate into a
    literal, printable escape instead of crashing the hook.
    """
    encoding = sys.stdout.encoding or "utf-8"
    print(
        line.encode(encoding, "backslashreplace").decode(
            encoding, "backslashreplace"
        )
    )


def main(argv: list[str]) -> int:
    offenders: list[str] = []
    for arg in argv:
        path = Path(arg)
        if not path.suffix == ".py" or not path.is_file():
            continue
        lexical, resolved = _repo_relative(path)
        in_repo = [r for r in (lexical, resolved) if r is not None]
        if in_repo and not any(r.startswith(FENCED_PREFIX) for r in in_repo):
            continue
        # The allowlist goes by the file whose CONTENT is scanned (the
        # resolved path): a symlink named like an allowlisted file but
        # pointing elsewhere is not exempt.
        offenders.extend(scan_file(path, resolved))
    if offenders:
        _safe_print("Reports-dir serving fence: new HTTP file-serving site:")
        for offender in offenders:
            _safe_print(f"  {offender}")
        _safe_print(
            "Report content is served from the per-user encrypted DB "
            "(research.py get_research_report); the shared artifacts "
            "directory must not gain an HTTP serving route. A new "
            "primitive reference needs an ALLOWED_FILES entry with a "
            "reason; a serving wrapper takes a direct call to a yielding "
            "generator function defined in the same module."
        )
        return 1
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))