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.

31.7 KB
Raw Blame
"""Executable invariants for defect classes the Flask->FastAPI migration
branch keeps re-producing.

Each class below has caused a REAL, shipped bug on this branch — some more
than once — so a one-off fix at the call site is not enough; the point of
this file is that the *next* occurrence fails CI instead of shipping again.

1. ``StreamingResponse(BytesIO(...))`` — ``StreamingResponse`` iterates its
   ``content`` argument to produce ASGI ``http.response.body`` sends.
   Iterating a ``BytesIO`` yields *lines* (splits on every ``0x0A`` byte),
   so a binary payload (PDF, ODT, ...) turns into thousands of one-line
   ASGI sends instead of one body, and Starlette can no longer compute a
   ``Content-Length`` header up front. Fixed twice on this branch:
   ``library.py``'s PDF-serving route (see the comment at
   ``routers/library.py`` around the ``return Response(content=pdf_bytes,
   ...)`` call) and ``research.py``'s report-export route (see the comment
   above ``return Response(content=export_content, ...)``). Fully
   materialised bytes must use ``Response(content=...)``, not
   ``StreamingResponse``.

2. Flask idioms on a Starlette ``Request`` — ``request.args``,
   ``request.form`` used as attribute access (not ``await
   request.form()``), ``request.get_json``, ``flask.g``, ``jsonify``,
   ``has_app_context``. None of these exist on a Starlette ``Request``;
   ``request.args`` raises ``AttributeError`` and FastAPI turns that into
   an unhandled 500. This shipped once and 500'd every call to the
   endpoint the log panel polls.

3. A bare ``return {...}`` inside an ``except`` block of an
   ``@router``-decorated endpoint. FastAPI serialises a returned dict as
   HTTP 200 regardless of what it contains, so an error path that
   ``return``s a dict inside ``except`` reports success. This shipped
   once in ``history.py``'s ``get_history`` (a failed history load
   rendered as "you have no history" because the client's
   ``response.ok`` check passed on the 200). ``history.py`` now has an
   explicit comment about it and uses ``JSONResponse(..., status_code=...)``
   — this test pins that the fix doesn't quietly regress, and that no
   sibling endpoint reintroduces the pattern.

Scope: per the migration, these classes are specific to the web
layer's request/response plumbing, so the scan covers
``src/local_deep_research/web/routers/*.py`` (every router module), the
top-level files directly under ``src/local_deep_research/web/*.py``
(``app.py``, ``fastapi_app.py``, ``exceptions.py``, ``research_state.py``,
``server_config.py``, ``template_config.py``, ...), and the top level of
the request/response subpackages ``auth/``, ``dependencies/``, ``queue/``,
``routes/``, and ``services/`` (middleware, auth decorators, queue
processors, and the service layer all build or inspect responses and
handle ``Request`` objects). Class 3 additionally requires an
``@router``-decorated endpoint, so it only ever scans the router
modules regardless of SCANNED_FILES. Deliberately still excluded:
``database/``, ``models/``, ``utils/``, ``warning_checks/``, ``themes/``
(no Request/Response plumbing) and ``static/``/``templates/`` (not
Python). The scanned directories are located by *importing*
``local_deep_research.web`` and ``local_deep_research.web.routers`` (not a
hand-maintained relative path), so this test exercises the real package
layout rather than a guess frozen at write time.

Detection is AST-based throughout — a regex over ``request\\.args`` would
also match an unrelated ``argparse`` ``request.args`` in a docstring, a
comment explaining the fix (this file's own module docstring, above, would
false-positive!), or a string literal, none of which are executable code.
"""

import ast
from pathlib import Path

import local_deep_research.web as web_pkg
import local_deep_research.web.routers as routers_pkg

ROUTERS_DIR = Path(routers_pkg.__file__).resolve().parent
WEB_DIR = Path(web_pkg.__file__).resolve().parent

# Every router module, the top-level (non-package) files directly in
# web/, and the top level of the request/response subpackages (auth/,
# dependencies/, queue/, routes/, services/). `Path.glob("*.py")` is
# non-recursive, which is deliberate: the subpackages have no nested
# Python packages today, and a stray new subpackage should show up as
# a scope-floor failure below rather than silently scan debris.
# Deliberately excluded: database/, models/, utils/, warning_checks/,
# themes/ (no Request/Response plumbing), static/, templates/ (not
# Python) -- see the module docstring "Scope" note.
_SCANNED_SUBPKGS = ("auth", "dependencies", "queue", "routes", "services")
SCANNED_FILES = sorted(
    {
        *ROUTERS_DIR.glob("*.py"),
        *WEB_DIR.glob("*.py"),
        *(
            path
            for subpkg in _SCANNED_SUBPKGS
            for path in (WEB_DIR / subpkg).glob("*.py")
        ),
    }
)


def _rel(path: Path) -> str:
    # parents[3] of .../src/local_deep_research/web/routers is the repo
    # root, so this yields "src/local_deep_research/web/..." -- matching
    # the keys used in the allowlists below.
    return str(path.relative_to(ROUTERS_DIR.parents[3]))


def _parse(path: Path) -> ast.Module:
    return ast.parse(path.read_text(encoding="utf-8"), filename=str(path))


def _annotate_parents(tree: ast.AST) -> ast.AST:
    """Attach a ``.parent`` link to every node so callers can inspect a
    node's immediate context (used to tell ``request.form`` apart from
    ``request.form()``)."""
    for node in ast.walk(tree):
        for child in ast.iter_child_nodes(node):
            child.parent = node
    return tree


# ===========================================================================
# 1. StreamingResponse(BytesIO(...))
# ===========================================================================


def _is_bytesio_call(node: ast.AST) -> bool:
    if not isinstance(node, ast.Call):
        return False
    func = node.func
    if isinstance(func, ast.Name):
        return func.id == "BytesIO"
    if isinstance(func, ast.Attribute):
        # io.BytesIO(...)
        return func.attr == "BytesIO"
    return False


def _is_streamingresponse_call(node: ast.AST) -> bool:
    if not isinstance(node, ast.Call):
        return False
    func = node.func
    if isinstance(func, ast.Name):
        return func.id in {
            "StreamingResponse",
            "WorkerCleanupStreamingResponse",
        }
    if isinstance(func, ast.Attribute):
        return func.attr in {
            "StreamingResponse",
            "WorkerCleanupStreamingResponse",
        }
    return False


def _streamingresponse_content_arg(node: ast.Call):
    """Return the AST node passed as StreamingResponse's ``content``
    (its first positional param), whether passed positionally or by
    keyword, or ``None`` if not present."""
    if node.args:
        return node.args[0]
    for kw in node.keywords:
        if kw.arg == "content":
            return kw.value
    return None


class _StreamingBytesIOScanner(ast.NodeVisitor):
    """Flags ``StreamingResponse(BytesIO(...))`` and the equally-broken
    indirected form (``buf = BytesIO(...); StreamingResponse(buf)``),
    scoped per enclosing function so a same-named variable in a sibling
    function can't cause a false positive."""

    def __init__(self):
        self.violations = []  # (lineno, message)
        self._scope_stack = [set()]

    def _enter_scope(self):
        self._scope_stack.append(set())

    def _exit_scope(self):
        self._scope_stack.pop()

    def _bind_bytesio(self, name):
        self._scope_stack[-1].add(name)

    def _is_known_bytesio_name(self, name):
        return any(name in scope for scope in self._scope_stack)

    def visit_FunctionDef(self, node):
        self._enter_scope()
        self.generic_visit(node)
        self._exit_scope()

    visit_AsyncFunctionDef = visit_FunctionDef

    def visit_Assign(self, node):
        if (
            len(node.targets) == 1
            and isinstance(node.targets[0], ast.Name)
            and _is_bytesio_call(node.value)
        ):
            self._bind_bytesio(node.targets[0].id)
        self.generic_visit(node)

    def visit_Call(self, node):
        if _is_streamingresponse_call(node):
            arg = _streamingresponse_content_arg(node)
            if arg is not None:
                if _is_bytesio_call(arg):
                    self.violations.append(
                        (
                            node.lineno,
                            "StreamingResponse(BytesIO(...)) constructed inline",
                        )
                    )
                elif isinstance(arg, ast.Name) and self._is_known_bytesio_name(
                    arg.id
                ):
                    self.violations.append(
                        (
                            node.lineno,
                            f"StreamingResponse(...) passed '{arg.id}', "
                            "which was assigned from BytesIO(...)",
                        )
                    )
        self.generic_visit(node)


def find_streamingresponse_bytesio(tree: ast.AST):
    scanner = _StreamingBytesIOScanner()
    scanner.visit(tree)
    return scanner.violations


# file::symbol -> justification. Seeded ONLY with verified-safe cases.
# (Currently empty: every live StreamingResponse() call site in the
# scanned scope streams an async generator, not a BytesIO -- see the
# module docstring.)
STREAMING_BYTESIO_ALLOWLIST: dict[str, str] = {}


def test_no_streamingresponse_wraps_bytesio():
    """No route may construct ``StreamingResponse`` over a ``BytesIO`` (or
    a variable assigned from one). Fully materialised bytes belong in
    ``Response(content=...)`` -- see library.py's PDF route and
    research.py's report-export route for the established fix pattern.
    """
    violations = []
    for path in SCANNED_FILES:
        tree = _parse(path)
        for lineno, message in find_streamingresponse_bytesio(tree):
            key = f"{_rel(path)}::L{lineno}"
            if key in STREAMING_BYTESIO_ALLOWLIST:
                continue
            violations.append(f"  {_rel(path)}:{lineno}: {message}")

    assert not violations, (
        "StreamingResponse(BytesIO(...)) detected. Iterating a BytesIO "
        "yields one chunk per 0x0A byte -- for binary payloads (PDF/ODT/"
        "...) that is thousands of tiny ASGI sends and no Content-Length "
        "header. Use `Response(content=<the fully materialised bytes>, "
        "media_type=...)` instead (see library.py's PDF route or "
        "research.py's `/api/report/{research_id}/export` for the fix). "
        "If this is a genuine streaming source (e.g. an async generator), "
        "it is not a BytesIO and should not trip this check -- investigate "
        "rather than allowlist.\n" + "\n".join(violations)
    )


class TestStreamingBytesIOScannerSelfTest:
    """Proves the scanner actually distinguishes the bug from the fix,
    so a silent breakage in the AST walk doesn't make the guard above
    pass vacuously."""

    def test_flags_inline_bytesio(self):
        tree = ast.parse(
            "def route():\n"
            "    return WorkerCleanupStreamingResponse(BytesIO(data), media_type='application/pdf')\n"
        )
        violations = find_streamingresponse_bytesio(tree)
        assert len(violations) == 1
        assert violations[0][0] == 2

    def test_flags_io_bytesio_qualified(self):
        tree = ast.parse(
            "def route():\n    return StreamingResponse(io.BytesIO(data))\n"
        )
        assert len(find_streamingresponse_bytesio(tree)) == 1

    def test_flags_indirected_bytesio_variable(self):
        tree = ast.parse(
            "def route():\n"
            "    buf = BytesIO(data)\n"
            "    return StreamingResponse(buf, media_type='application/pdf')\n"
        )
        violations = find_streamingresponse_bytesio(tree)
        assert len(violations) == 1
        assert violations[0][0] == 3

    def test_flags_bytesio_passed_as_content_kwarg(self):
        tree = ast.parse(
            "def route():\n"
            "    return StreamingResponse(content=BytesIO(data))\n"
        )
        assert len(find_streamingresponse_bytesio(tree)) == 1

    def test_ignores_plain_response_over_bytes(self):
        tree = ast.parse(
            "def route():\n"
            "    return Response(content=pdf_bytes, media_type='application/pdf')\n"
        )
        assert find_streamingresponse_bytesio(tree) == []

    def test_ignores_streamingresponse_over_generator(self):
        tree = ast.parse(
            "def route():\n"
            "    def generate():\n"
            "        yield b'a'\n"
            "    return StreamingResponse(generate(), media_type='text/event-stream')\n"
        )
        assert find_streamingresponse_bytesio(tree) == []

    def test_same_named_bytesio_var_in_sibling_function_is_scoped(self):
        """A BytesIO-bound name in one function must not taint an
        unrelated same-named variable streamed from a generator in
        another function."""
        tree = ast.parse(
            "def other_route():\n"
            "    buf = BytesIO(data)\n"
            "    return Response(content=buf.getvalue())\n"
            "def route():\n"
            "    buf = make_generator()\n"
            "    return StreamingResponse(buf)\n"
        )
        assert find_streamingresponse_bytesio(tree) == []


# ===========================================================================
# 2. Flask idioms on a Starlette Request
# ===========================================================================


class _FlaskIdiomScanner(ast.NodeVisitor):
    def __init__(self):
        self.violations = []  # (lineno, message)

    def visit_Import(self, node):
        for alias in node.names:
            if alias.name == "flask" or alias.name.startswith("flask."):
                self.violations.append((node.lineno, f"import {alias.name}"))
        self.generic_visit(node)

    def visit_ImportFrom(self, node):
        if node.module == "flask" or (
            node.module and node.module.startswith("flask.")
        ):
            names = ", ".join(a.name for a in node.names)
            self.violations.append(
                (node.lineno, f"from {node.module} import {names}")
            )
        self.generic_visit(node)

    def visit_Attribute(self, node):
        base = node.value
        if isinstance(base, ast.Name) and base.id == "request":
            if node.attr in ("args", "get_json"):
                self.violations.append((node.lineno, f"request.{node.attr}"))
            elif node.attr == "form":
                parent = getattr(node, "parent", None)
                is_call_target = (
                    isinstance(parent, ast.Call) and parent.func is node
                )
                if not is_call_target:
                    self.violations.append(
                        (
                            node.lineno,
                            "request.form (attribute access, not "
                            "`await request.form()`)",
                        )
                    )
        elif isinstance(base, ast.Name) and base.id == "flask":
            if node.attr in ("g", "has_app_context"):
                self.violations.append((node.lineno, f"flask.{node.attr}"))
        self.generic_visit(node)

    def visit_Call(self, node):
        func = node.func
        if isinstance(func, ast.Name) and func.id in (
            "jsonify",
            "has_app_context",
        ):
            self.violations.append((node.lineno, f"{func.id}(...)"))
        self.generic_visit(node)


def find_flask_idioms(tree: ast.AST):
    scanner = _FlaskIdiomScanner()
    scanner.visit(tree)
    return scanner.violations


# file::symbol -> justification. Seeded ONLY with verified-safe cases.
# (Currently empty: no Flask idiom appears anywhere in the scanned scope
# today -- the only matches before this test was written were in comments
# / docstrings narrating the historical Flask->FastAPI migration, which
# `ast` correctly ignores.)
FLASK_IDIOM_ALLOWLIST: dict[str, str] = {}


def test_no_flask_idioms_on_starlette_request():
    """None of request.args / request.form (attribute access) /
    request.get_json / flask.g / jsonify / has_app_context may appear.
    None exist on a Starlette Request or in a Flask-free codebase;
    request.args in particular raises AttributeError -> unhandled 500.
    """
    violations = []
    for path in SCANNED_FILES:
        tree = _annotate_parents(_parse(path))
        for lineno, message in find_flask_idioms(tree):
            key = f"{_rel(path)}::L{lineno}"
            if key in FLASK_IDIOM_ALLOWLIST:
                continue
            violations.append(f"  {_rel(path)}:{lineno}: {message}")

    assert not violations, (
        "Flask idiom(s) found on what must be a Starlette Request (or a "
        "reintroduced `flask` import) in the FastAPI web layer. These "
        "raise AttributeError at runtime (Starlette's Request has no "
        "`.args`/`.get_json`/Flask has no import here at all), which "
        "FastAPI turns into an unhandled 500 for every caller. Use: "
        "`request.query_params` for `request.args`; `await request.form()` "
        "for `request.form`; `await request.json()` for "
        "`request.get_json()`; a FastAPI dependency for `flask.g`; "
        "`JSONResponse(...)` / a returned dict for `jsonify(...)`; and "
        "delete any `has_app_context()` check outright (FastAPI has no "
        "app-context concept).\n" + "\n".join(violations)
    )


class TestFlaskIdiomScannerSelfTest:
    def test_flags_request_args(self):
        tree = _annotate_parents(
            ast.parse("def h(request):\n    return request.args.get('x')\n")
        )
        assert find_flask_idioms(tree) == [(2, "request.args")]

    def test_flags_request_form_attribute_access(self):
        tree = _annotate_parents(
            ast.parse("def h(request):\n    return request.form.get('x')\n")
        )
        violations = find_flask_idioms(tree)
        assert len(violations) == 1 and violations[0][0] == 2

    def test_ignores_awaited_request_form_call(self):
        tree = _annotate_parents(
            ast.parse(
                "async def h(request):\n    return await request.form()\n"
            )
        )
        assert find_flask_idioms(tree) == []

    def test_ignores_unawaited_request_form_call(self):
        """The call site itself (`request.form()`) is fine even without
        `await` visible in this snippet -- it is a Call, not a bare
        attribute access, which is the actual bug pattern."""
        tree = _annotate_parents(
            ast.parse("def h(request):\n    form = request.form()\n")
        )
        assert find_flask_idioms(tree) == []

    def test_flags_request_get_json(self):
        tree = _annotate_parents(
            ast.parse("def h(request):\n    return request.get_json()\n")
        )
        assert find_flask_idioms(tree) == [(2, "request.get_json")]

    def test_flags_flask_g(self):
        tree = _annotate_parents(ast.parse("def h():\n    flask.g.user = 1\n"))
        assert find_flask_idioms(tree) == [(2, "flask.g")]

    def test_flags_jsonify_call(self):
        tree = _annotate_parents(
            ast.parse("def h():\n    return jsonify({'a': 1}), 200\n")
        )
        assert find_flask_idioms(tree) == [(2, "jsonify(...)")]

    def test_flags_has_app_context_call(self):
        tree = _annotate_parents(
            ast.parse("def h():\n    if has_app_context():\n        pass\n")
        )
        assert find_flask_idioms(tree) == [(2, "has_app_context(...)")]

    def test_flags_flask_import(self):
        tree = _annotate_parents(ast.parse("from flask import jsonify\n"))
        assert find_flask_idioms(tree) == [(1, "from flask import jsonify")]

    def test_ignores_unrelated_dotted_args_attribute(self):
        """A same-named `.args` on something other than a variable
        literally named `request` (e.g. an exception object) is not a
        Starlette-Request mistake and must not be flagged."""
        tree = _annotate_parents(
            ast.parse("def h(exc):\n    return exc.args\n")
        )
        assert find_flask_idioms(tree) == []


# ===========================================================================
# 3. Bare dict returned from an except block of a router endpoint
# ===========================================================================


def _is_router_decorator(dec: ast.AST) -> bool:
    """True for `@router.get(...)`, `@router.post(...)`, etc. -- every
    router module in scope binds its APIRouter to the name `router`."""
    target = dec.func if isinstance(dec, ast.Call) else dec
    return (
        isinstance(target, ast.Attribute)
        and isinstance(target.value, ast.Name)
        and target.value.id == "router"
    )


def _collect_dict_returns_in_except(node, in_except, func_name, out):
    """Single-pass recursive walk of *node*'s children: records every
    `return <dict literal>` reachable while `in_except` is (or becomes)
    True, without double-counting a return that sits under nested
    except blocks, and without crossing into a nested def/lambda (a
    nested helper's return does not return from the route)."""
    for child in ast.iter_child_nodes(node):
        if isinstance(
            child,
            (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda, ast.ClassDef),
        ):
            continue  # separate scope: its `return` doesn't return the route
        if (
            in_except
            and isinstance(child, ast.Return)
            and isinstance(child.value, ast.Dict)
        ):
            out.append((child.lineno, func_name))
        child_in_except = in_except or isinstance(child, ast.ExceptHandler)
        _collect_dict_returns_in_except(child, child_in_except, func_name, out)


def find_bare_dict_returns_in_except(tree: ast.AST):
    """Return (lineno, function_name) for every `return <dict literal>`
    that sits inside an `except` block of an `@router`-decorated endpoint
    function."""
    violations = []
    for node in ast.walk(tree):
        if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        if not any(_is_router_decorator(d) for d in node.decorator_list):
            continue
        _collect_dict_returns_in_except(node, False, node.name, violations)
    return violations


# file::symbol -> justification. Seeded ONLY with verified-safe cases:
# each entry below was read in full and confirmed to be an "always-200,
# status-is-the-payload" health-probe endpoint, where the except-block
# dict has the IDENTICAL shape (a `running`/`available` boolean field) as
# every non-exceptional return in the same function -- it is not an error
# path masquerading as success, it *is* the success path reporting that
# the probed external service is unavailable. Contrast with the actual
# history.py bug this test pins: there, the except-block dict had NO
# `status`/error framing at all and the surrounding non-exceptional
# returns were a different, genuinely-successful shape.
DICT_RETURN_ALLOWLIST: dict[str, str] = {
    "src/local_deep_research/web/routers/api.py::check_ollama_status": (
        "GET /research/api/check/ollama_status always answers 200 with "
        "{'running': bool, ...}; the except branch reports 'Ollama probe "
        "raised' using the exact same shape as the outcome=='ok' / "
        "'bad_status' / 'connection_error' / 'timeout' branches earlier "
        "in the same try block (all of which also `return {...}` -- this "
        "is a status report, not error propagation)."
    ),
    "src/local_deep_research/web/routers/api.py::check_ollama_model": (
        "GET /research/api/check/ollama_model always answers 200 with "
        "{'available': bool, ...}; same always-200 status-probe shape as "
        "check_ollama_status above, verified line-by-line against its "
        "sibling non-exceptional returns in the same function."
    ),
    "src/local_deep_research/web/routers/settings.py::check_ollama_status": (
        "GET /settings/api/ollama-status always answers 200 with "
        "{'running': bool, ...}; the `except "
        "requests.exceptions.RequestException` branch returns the same "
        "{'running': False, 'error': ...} shape as the response.status_code "
        "!= 200 branch just above it in the same try block."
    ),
    "src/local_deep_research/web/routers/settings.py::api_get_backup_status": (
        "GET /settings/api/backup-status always answers 200 to "
        "authenticated callers with {'enabled', 'listing_available', "
        "'count', 'backups', 'total_size_*', ...} plus the last-attempt "
        "report; the except branch returns the exact same shape with "
        "listing_available=False / count=None -- a status report on "
        "whether the backup directory could be read, not error "
        "propagation. The 200 matters here (#7199): the last-attempt "
        "failure record is fetched before the try block, and a non-200 "
        "would make the shared JS client's catch() wipe the section to "
        "'Could not load backup status.', discarding that failure report "
        "exactly when storage trouble makes the listing failure and the "
        "backup failure correlated. Verified line-by-line against the "
        "non-exceptional return in the same try block; the paired "
        "Flask-snapshot divergence is recorded in "
        "EXPECTED_STATUS_CODES_LOST (test_route_table_parity.py)."
    ),
}


def test_no_bare_dict_return_in_except_block():
    """Inside an `except` block of an `@router`-decorated endpoint, a bare
    `return {...}` serialises as HTTP 200 no matter what the dict says --
    FastAPI has no idea it came from an error path. Use
    `JSONResponse({...}, status_code=...)` or re-raise. See history.py's
    `get_history` for the established fix (and its explanatory comment).
    """
    violations = []
    for path in ROUTERS_DIR.glob("*.py"):
        tree = _parse(path)
        for lineno, func_name in find_bare_dict_returns_in_except(tree):
            key = f"{_rel(path)}::{func_name}"
            if key in DICT_RETURN_ALLOWLIST:
                continue
            violations.append(
                f"  {_rel(path)}:{lineno}: {func_name}() returns a bare "
                "dict literal from inside an except block"
            )

    assert not violations, (
        "Bare `return {...}` inside an except block of a router endpoint "
        "detected. FastAPI serialises ANY returned dict as HTTP 200, so "
        "this reports success on a failure path (see history.py's "
        "get_history() history -- a failed history load rendered as 'you "
        "have no history' because the client's `response.ok` check passed "
        "on the 200). Replace with `JSONResponse({...}, "
        "status_code=<4xx/5xx>)` or re-raise. If the dict genuinely IS the "
        "correct 200 response (an always-succeeds status-probe endpoint "
        "reporting an external service as unavailable, matching its own "
        "non-exceptional return shape), add it to DICT_RETURN_ALLOWLIST "
        "in this file with a justification -- verify the claim by reading "
        "the whole function first.\n" + "\n".join(violations)
    )


class TestBareDictReturnScannerSelfTest:
    def test_flags_bare_dict_in_router_endpoint_except(self):
        tree = ast.parse(
            "@router.get('/x')\n"
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception:\n"
            "        return {'status': 'error'}\n"
        )
        assert find_bare_dict_returns_in_except(tree) == [(6, "h")]

    def test_ignores_jsonresponse_in_except(self):
        tree = ast.parse(
            "@router.get('/x')\n"
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception:\n"
            "        return JSONResponse({'status': 'error'}, status_code=500)\n"
        )
        assert find_bare_dict_returns_in_except(tree) == []

    def test_ignores_reraise_in_except(self):
        tree = ast.parse(
            "@router.get('/x')\n"
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception:\n"
            "        raise\n"
        )
        assert find_bare_dict_returns_in_except(tree) == []

    def test_ignores_bare_dict_in_undecorated_helper(self):
        """A plain helper's except-block dict return is a normal Python
        function result (consumed and interpreted by its caller), not
        directly serialised as an HTTP response -- only `@router`
        endpoints are in scope."""
        tree = ast.parse(
            "def _helper():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception:\n"
            "        return {'ok': False}\n"
        )
        assert find_bare_dict_returns_in_except(tree) == []

    def test_ignores_dict_return_in_nested_def_inside_endpoint(self):
        """A dict returned from a nested helper's own except does not
        return from the route -- only the thunk. Not flagged (see the
        `_collect_dict_returns_in_except` docstring)."""
        tree = ast.parse(
            "@router.get('/x')\n"
            "def h():\n"
            "    def _impl():\n"
            "        try:\n"
            "            risky()\n"
            "        except Exception:\n"
            "            return {'ok': False}\n"
            "    return run_db_sync(_impl)\n"
        )
        assert find_bare_dict_returns_in_except(tree) == []

    def test_ignores_dict_return_outside_except(self):
        tree = ast.parse(
            "@router.get('/x')\ndef h():\n    return {'status': 'success'}\n"
        )
        assert find_bare_dict_returns_in_except(tree) == []

    def test_flags_nested_try_within_except_of_endpoint(self):
        """A dict return from a SECOND try/except that is itself nested
        inside the outer except (not a separate function) still returns
        from the route -- must be flagged."""
        tree = ast.parse(
            "@router.get('/x')\n"
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception:\n"
            "        try:\n"
            "            cleanup()\n"
            "        except Exception:\n"
            "            return {'status': 'error'}\n"
        )
        assert find_bare_dict_returns_in_except(tree) == [(9, "h")]


# ===========================================================================
# Scope sanity: make sure the scan is actually looking at something.
# ===========================================================================


def test_scan_covers_the_known_router_modules():
    """If the router/web layout moves, SCANNED_FILES silently shrinking to
    near-nothing would make every test above pass vacuously. Pin a floor
    and spot-check the two files with the documented real-bug history."""
    names = {p.name for p in SCANNED_FILES}
    assert len(SCANNED_FILES) >= 50, (
        f"Only {len(SCANNED_FILES)} files scanned ({sorted(names)}) -- "
        "the web/routers layout moved; update this test's scope."
    )
    for expected in (
        "library.py",
        "research.py",
        "history.py",
        "api.py",
        "settings.py",
    ):
        assert expected in names, (
            f"{expected} is no longer in the scanned scope -- it has "
            "documented history for the defect classes pinned in this "
            "file. Update SCANNED_FILES."
        )
    # The widened request/response scope must stay wired: these are the
    # files where a Request/Response defect class would next surface
    # (middleware handling raw Request objects, the export-building
    # service layer).
    for expected in (
        "csrf.py",  # web/dependencies/ -- CSRFMiddleware
        "research_service.py",  # web/services/ -- export assembly
    ):
        assert expected in names, (
            f"{expected} is no longer in the scanned scope -- the "
            "request/response subpackage coverage regressed. Update "
            "SCANNED_FILES / _SCANNED_SUBPKGS."
        )