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.

39.8 KB
Raw Blame
"""SIBLING-CONSISTENCY tests across the FastAPI routers.

The migration's dominant, hardest-to-catch defect class is not "this one
handler is wrong" -- it's "this behaviour is applied to N of M equivalent
call sites" (a resource-cleanup ``with`` block present at 4 of 5 identical
sites; auth declared differently in 1 router than the other 19). Per-file
review cannot see this shape: nothing is wrong when you read any single
route in isolation. It only shows up when a route is compared against its
functional peers.

Every test below therefore (1) surveys the LIVE app -- ``app.routes``, each
route's ``dependant``, and the router source ASTs -- to find the actual
convention, then (2) pins conformance to it with an explicit, justified
allowlist for the genuine, verified exceptions. An unjustified drift (a new
mutating route with no auth, a new upload route missing a rate limit, a new
integer-id path param left untyped) fails loudly and names the offending
route, its siblings, and the convention it broke.

Covers:
  1. Auth uniformity on state-changing routes.
  2. Error-response shape ("bare dict from except" => accidental HTTP 200)
     -- SKIPPED here. Already covered end-to-end by
     ``tests/web/routers/test_migration_antipattern_guards.py::
     test_no_bare_dict_return_in_except_block``, whose scanner (verified by
     reading it in full) applies the identical rule -- same AST shape, same
     "only @router-decorated endpoints, nested-def-scoped" semantics, and
     the same three verified-safe status-probe exceptions (api.py's
     check_ollama_status/check_ollama_model, settings.py's
     check_ollama_status) that an independent survey for this file turned
     up too. Duplicating it here would just be two copies of the same
     assertion to keep in sync. ``test_bare_dict_in_except_coverage_is_not_
     orphaned`` below is a thin tripwire so this file fails loudly (rather
     than silently losing coverage) if that sibling file/test is ever
     renamed or removed.
  3. Rate-limit decorator coverage across functional route families
     (uploads, settings-value mutation, chat messaging).
  4. Path params that back an integer database primary key are typed
     ``int`` (a 422 at the routing layer beats a 500 from the service
     layer on a non-numeric id).
"""

from __future__ import annotations

import ast
import functools
import importlib
import inspect
import typing
from pathlib import Path

from fastapi import Depends, FastAPI
from fastapi.routing import APIRoute
from fastapi.testclient import TestClient

from local_deep_research.web.dependencies.auth import require_auth
from local_deep_research.web.fastapi_app import app

ROUTERS_DIR = Path(__file__).resolve().parents[3] / (
    "src/local_deep_research/web/routers"
)
MUTATING_METHODS = frozenset({"POST", "PUT", "PATCH", "DELETE"})


def _api_routes(routes) -> list[APIRoute]:
    return [r for r in routes if isinstance(r, APIRoute)]


def _mutating_routes() -> list[APIRoute]:
    return [r for r in _api_routes(app.routes) if r.methods & MUTATING_METHODS]


def _methods(route: APIRoute) -> tuple[str, ...]:
    return tuple(sorted(route.methods - {"HEAD", "OPTIONS"}))


def _route_label(route: APIRoute) -> str:
    return f"{'/'.join(_methods(route))} {route.path}"


# ---------------------------------------------------------------------------
# Shared AST plumbing: parse each router module once, find the top-level
# function backing a live route so its decorators can be inspected. Source
# static analysis is only used for *decorators* (invisible on the live
# ``Dependant`` for slowapi, which wraps rather than injects); everything
# that FastAPI itself resolves (auth deps, path-param types) is read off
# the live app instead, per the task's introspection preference.
# ---------------------------------------------------------------------------


@functools.lru_cache(maxsize=None)
def _module_ast(module_stem: str) -> ast.Module:
    path = ROUTERS_DIR / f"{module_stem}.py"
    return ast.parse(path.read_text(encoding="utf-8"), filename=str(path))


def _module_stem(route: APIRoute) -> str:
    # route.endpoint.__module__ is e.g.
    # "local_deep_research.web.routers.settings" -> "settings"
    return route.endpoint.__module__.rsplit(".", 1)[-1]


def _decorator_name(dec: ast.expr) -> str:
    target = dec.func if isinstance(dec, ast.Call) else dec
    if isinstance(target, ast.Attribute):
        return target.attr
    if isinstance(target, ast.Name):
        return target.id
    return ast.unparse(target)


def _endpoint_function_node(
    route: APIRoute,
) -> ast.FunctionDef | ast.AsyncFunctionDef | None:
    """The top-level ``@router.<verb>``-decorated function backing *route*.

    Only searches module top level (every route handler in this codebase
    is declared there, never nested), so a same-named nested helper in an
    unrelated function can't be mismatched onto a route.
    """
    tree = _module_ast(_module_stem(route))
    for node in tree.body:
        if (
            isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
            and node.name == route.endpoint.__name__
        ):
            return node
    return None


def _decorator_names(route: APIRoute) -> set[str]:
    node = _endpoint_function_node(route)
    if node is None:
        return set()
    return {_decorator_name(d) for d in node.decorator_list}


def _references_name_anywhere(node: ast.AST, name: str) -> bool:
    """Whether *name* appears as a bare identifier or attribute anywhere
    in *node*'s subtree (including nested closures -- unlike the
    except/bare-dict check, scope doesn't matter here: a nested ``_impl``
    that touches ``UploadFile`` still means the route handles uploads)."""
    for child in ast.walk(node):
        if isinstance(child, ast.Name) and child.id == name:
            return True
        if isinstance(child, ast.Attribute) and child.attr == name:
            return True
    return False


# ===========================================================================
# 1. Auth uniformity on state-changing routes
# ===========================================================================
#
# Survey (live dependant-tree walk over all 137 POST/PUT/PATCH/DELETE
# routes across all 18 routers): 130 declare `username: str =
# Depends(require_auth)` as a DIRECT dependency. The remaining 7 split into
# two verified, narrow families:
#
#   - auth.py: login, logout, register, validate-password (4 routes) have
#     no `require_auth` anywhere in their dependency tree at all -- read in
#     full, each is deliberately pre-authentication (you can't require a
#     session to create one) or, for validate-password, a stateless
#     strength-check with its own rate limit as its actual defense.
#     change-password, by contrast, DOES depend on require_auth directly
#     (verified) -- it is not "auth.py is exempt", only these four routes.
#
#   - api_v1.py: api_quick_summary, api_generate_report,
#     api_analyze_documents (3 routes) depend directly on
#     `require_api_access`, not `require_auth`. Read in full:
#     `require_api_access`'s own first parameter is
#     `username: str = Depends(require_auth)` -- it wraps the same gate and
#     additionally enforces the user's `app.enable_api` setting plus caches
#     the per-user API rate limit. So `require_auth` still appears in every
#     one of these routes' dependency trees; only the *direct* declaration
#     differs, and it differs in the stricter direction.


def _dependant_calls(dependant) -> set:
    """Every dependency callable reachable from *dependant*, including
    itself, walked recursively (cycle-safe via identity dedup)."""
    seen: set[int] = set()
    calls: set = set()

    def _walk(d):
        if id(d) in seen:
            return
        seen.add(id(d))
        if d.call is not None:
            calls.add(d.call)
        for sub in d.dependencies:
            _walk(sub)

    _walk(dependant)
    return calls


def _requires_auth_transitively(route: APIRoute) -> bool:
    return require_auth in _dependant_calls(route.dependant)


def _direct_require_auth_param_name(route: APIRoute) -> str | None:
    """The injection name of a DIRECT (first-level) ``Depends(require_auth)``
    on this route, or ``None`` if require_auth isn't a direct dependency."""
    for sub in route.dependant.dependencies:
        if sub.call is require_auth:
            return sub.name
    return None


# (method, path) -> justification. Every entry was read in full in
# routers/auth.py; each is a route that must work WITHOUT an existing
# session, so it cannot itself require one.
PUBLIC_MUTATING_ROUTES: dict[tuple[str, str], str] = {
    ("POST", "/auth/login"): (
        "creates the session being authenticated against; requiring "
        "require_auth here would make login impossible."
    ),
    ("POST", "/auth/logout"): (
        "reads request.session directly (no-op if already logged out) "
        "and is POST-only specifically to block CSRF-triggered logout via "
        "GET -- there is no session to require before clearing one."
    ),
    ("POST", "/auth/register"): (
        "creates the account the session will belong to; same "
        "chicken-and-egg as login."
    ),
    ("POST", "/auth/validate-password"): (
        "stateless password-strength check used by the register/"
        "change-password forms on every keystroke; guarded by its own "
        "VALIDATE_PASSWORD_RATE_LIMIT bucket rather than a session, "
        "documented in its docstring as 'used to prevent using this "
        "endpoint as a complexity oracle'."
    ),
}

# (method, path) -> justification. Verified by reading api_v1.py's
# require_api_access in full: its first parameter is
# `username: str = Depends(require_auth)`, so require_auth is still in
# these routes' dependency trees -- only the direct declaration differs,
# and only to add strictly MORE gating (app.enable_api + per-user API rate
# limit), not less.
INDIRECT_AUTH_VIA_STRONGER_GATE: dict[tuple[str, str], str] = {
    ("POST", "/api/v1/quick_summary"): (
        "depends on require_api_access, which itself Depends(require_auth) "
        "and additionally enforces app.enable_api + api_rate_limit."
    ),
    ("POST", "/api/v1/generate_report"): (
        "same require_api_access wrapper as quick_summary."
    ),
    ("POST", "/api/v1/analyze_documents"): (
        "same require_api_access wrapper as quick_summary."
    ),
}


class TestAuthDetectorSelfTest:
    """Proves the dependant-tree walk actually distinguishes "has
    require_auth somewhere" from "doesn't", against real FastAPI
    ``Dependant`` objects (not source text) -- self-test guards the
    detector the live-app assertions below rely on."""

    def _build(self, use_auth: bool) -> APIRoute:
        probe = FastAPI()

        def _other_dep() -> str:
            return "unauthenticated"

        if use_auth:

            def handler(username: str = Depends(require_auth)) -> dict:
                return {"username": username}
        else:

            def handler(username: str = Depends(_other_dep)) -> dict:
                return {"username": username}

        probe.post("/probe")(handler)
        return _api_routes(probe.routes)[0]

    def test_detects_direct_require_auth_dependency(self):
        route = self._build(use_auth=True)
        assert _requires_auth_transitively(route) is True
        assert _direct_require_auth_param_name(route) == "username"

    def test_flags_route_with_unrelated_dependency_as_unauthenticated(self):
        route = self._build(use_auth=False)
        assert _requires_auth_transitively(route) is False
        assert _direct_require_auth_param_name(route) is None


def test_every_mutating_route_requires_auth_or_is_allowlisted():
    """Every POST/PUT/PATCH/DELETE route must have require_auth somewhere
    in its dependency tree, UNLESS it is one of the two verified-public
    families above. A brand-new unauthenticated mutating route -- the
    exact defect class this file targets -- fails here by name."""
    allowlist = {**PUBLIC_MUTATING_ROUTES, **INDIRECT_AUTH_VIA_STRONGER_GATE}
    violations = []
    for route in _mutating_routes():
        allowlisted = any((m, route.path) in allowlist for m in _methods(route))
        if allowlisted:
            continue
        if not _requires_auth_transitively(route):
            violations.append(
                f"  {_route_label(route)} ({route.endpoint.__module__}."
                f"{route.endpoint.__name__}) has no require_auth anywhere "
                "in its dependency tree, and is not in "
                "PUBLIC_MUTATING_ROUTES. Every other state-changing route "
                "in this router family declares "
                "`username: str = Depends(require_auth)` (directly or via "
                "a wrapper that itself depends on it) -- add the "
                "dependency, or add this route to PUBLIC_MUTATING_ROUTES "
                "with a justification if it is genuinely meant to be "
                "public."
            )
    assert not violations, (
        "Unauthenticated state-changing route(s) found:\n"
        + "\n".join(violations)
    )


def test_direct_require_auth_dependencies_use_uniform_param_name():
    """Of the 137 mutating routes, every one that declares require_auth
    DIRECTLY (130 of them, verified) names the injected value `username` --
    not `user`, `current_user`, or any other spelling. This is the
    "auth declared differently in one router than the other nineteen"
    shape: a route that authenticates via a same-purpose dependency named
    or typed differently from its 130 siblings is exactly the kind of
    drift per-file review misses."""
    exceptions = {**PUBLIC_MUTATING_ROUTES, **INDIRECT_AUTH_VIA_STRONGER_GATE}
    violations = []
    checked = 0
    for route in _mutating_routes():
        if any((m, route.path) in exceptions for m in _methods(route)):
            continue
        name = _direct_require_auth_param_name(route)
        if name is None:
            # Already reported by the presence test above; don't double-count.
            continue
        checked += 1
        if name != "username":
            violations.append(
                f"  {_route_label(route)} injects require_auth as "
                f"`{name}`, not `username` like its siblings "
                f"(e.g. every other route in "
                f"{route.endpoint.__module__.rsplit('.', 1)[-1]}.py)."
            )
    assert checked >= 100, (
        f"Only {checked} direct require_auth declarations were checked -- "
        "expected >=100 based on the current survey; the route table or "
        "dependency-injection shape likely changed and this test's "
        "baseline needs re-deriving, not silently passing on a shrunk set."
    )
    assert not violations, "Non-uniform auth parameter naming:\n" + "\n".join(
        violations
    )


# ===========================================================================
# 2. Error-response shape ("bare dict from except") -- SKIPPED, see module
#    docstring. Tripwire only: fail loudly if the sibling coverage
#    disappears instead of silently losing the invariant.
# ===========================================================================


def test_bare_dict_in_except_coverage_is_not_orphaned():
    """``test_migration_antipattern_guards.py`` already pins "no bare
    ``return {...}`` inside an ``except`` block of a ``@router`` endpoint"
    with the identical rule this file would otherwise duplicate (verified
    by reading it in full: same AST shape, same nested-def scoping, same
    three verified status-probe exceptions). Skipping it here is only
    correct as long as that coverage keeps existing -- if the module or
    the specific test function is ever renamed/removed, this fails instead
    of silently leaving the invariant unchecked anywhere."""
    module = importlib.import_module(
        "tests.web.routers.test_migration_antipattern_guards"
    )
    assert hasattr(module, "test_no_bare_dict_return_in_except_block"), (
        "test_migration_antipattern_guards.py no longer defines "
        "test_no_bare_dict_return_in_except_block -- the 'bare dict "
        "return from an except block' invariant (item 2 of the "
        "sibling-consistency review) has lost its only coverage. Either "
        "restore that test or add an equivalent check to this file."
    )


# ===========================================================================
# 3. Rate-limit decorator coverage across functional route families
# ===========================================================================
#
# Survey method: parse every router module's top-level @router-decorated
# functions, collect their decorator names, and group by DECORATOR (not by
# router file -- rate limiting in this codebase is applied by cost/abuse
# category, not per-file, so "siblings" means "same category of action",
# per the task's own example: upload endpoints as a class, wherever they
# live).

RATE_LIMIT_DECORATOR_NAMES = frozenset(
    {
        "limit",  # @limiter.limit(...)
        "settings_limit",
        "upload_rate_limit_user",
        "upload_rate_limit_ip",
        "api_rate_limit",
    }
)


def _is_rate_limit_decorator(name: str) -> bool:
    """Whether a decorator name denotes a rate limiter.

    Deliberately NOT a fixed whitelist. Routers define their own
    ``limiter.shared_limit(...)`` handles with module-private names
    (``_notes_ai_limit``, ``_notes_synthesize_limit``,
    ``_log_export_limit``, ...), and a hardcoded list silently fails to
    see them — which reads as "this route is unlimited" and manufactures
    a phantom rate-limit gap. Match the naming convention instead, so a
    newly-added ``_x_limit`` handle is recognised without editing this
    file.
    """
    return name in RATE_LIMIT_DECORATOR_NAMES or name.endswith("_limit")


def _has_rate_limit(route: APIRoute) -> bool:
    return any(_is_rate_limit_decorator(n) for n in _decorator_names(route))


# --- 3a. Upload endpoints: both the per-user AND per-IP bucket -------------
#
# This is the task's own named example ("one upload endpoint unlimited
# while its siblings are limited"). Structural, not a hardcoded list: any
# mutating route whose handler references `UploadFile` anywhere in its body
# is, by definition, a file-upload endpoint and must carry BOTH
# upload_rate_limit_user and upload_rate_limit_ip -- so a THIRD upload
# route added later is caught automatically, not just the two known today
# (research.py: POST /api/upload/pdf, rag.py: POST
# /api/collections/{collection_id}/upload).


def _is_upload_route(route: APIRoute) -> bool:
    node = _endpoint_function_node(route)
    return node is not None and _references_name_anywhere(node, "UploadFile")


class TestUploadDetectorSelfTest:
    def test_detects_uploadfile_reference_in_nested_closure(self):
        tree = ast.parse(
            "@router.post('/x')\n"
            "async def h(request):\n"
            "    form = await request.form()\n"
            "    def _impl():\n"
            "        files = [f for f in form.getlist('files') "
            "if isinstance(f, UploadFile)]\n"
            "        return files\n"
            "    return _impl()\n"
        )
        node = tree.body[0]
        assert _references_name_anywhere(node, "UploadFile") is True

    def test_ignores_routes_without_uploadfile(self):
        tree = ast.parse(
            "@router.post('/x')\n"
            "async def h(request):\n"
            "    data = await request.json()\n"
            "    return data\n"
        )
        node = tree.body[0]
        assert _references_name_anywhere(node, "UploadFile") is False


def test_upload_endpoints_have_paired_rate_limits():
    upload_routes = [r for r in _mutating_routes() if _is_upload_route(r)]
    assert len(upload_routes) >= 2, (
        f"Expected to find at least the 2 known upload routes "
        f"(research.upload_pdf, rag.upload_to_collection); found "
        f"{len(upload_routes)}. The UploadFile-reference detector or the "
        "route table likely changed -- re-verify before trusting this "
        "test's coverage."
    )
    violations = []
    for route in upload_routes:
        decos = _decorator_names(route)
        missing = {
            "upload_rate_limit_user",
            "upload_rate_limit_ip",
        } - decos
        if missing:
            violations.append(
                f"  {_route_label(route)} ({route.endpoint.__module__}."
                f"{route.endpoint.__name__}) handles UploadFile but is "
                f"missing {sorted(missing)}. Its sibling upload routes "
                "carry BOTH @upload_rate_limit_user and "
                "@upload_rate_limit_ip (research.py's upload_pdf, "
                "rag.py's upload_to_collection) -- an upload endpoint "
                "missing either bucket is unbounded on that axis."
            )
    assert not violations, "Upload rate-limit gap(s):\n" + "\n".join(violations)


# --- 3b. Settings-value-mutation family -------------------------------
#
# Survey (routers/settings.py, every POST/PUT/DELETE that writes through
# SettingsManager.set_setting / equivalent onto the `settings` table):
# 7 of 9 siblings carry @settings_limit. The remaining 2 -- both read in
# full -- are genuine gaps, not intentional exceptions (neither carries any
# rate-limit rationale, and both do the exact same "look up current value, call
# settings_manager.set_setting, invalidate cache" shape as
# api_update_setting, which IS limited). Reported as a real finding; pinned
# here (not silently allowlisted as "fine") so a reader of this test sees
# it's a known bug, and so a fix removes it from KNOWN_GAP instead of this
# test just staying green either way.

SETTINGS_VALUE_MUTATION_LIMITED = {
    ("POST", "/settings/save_all_settings"),
    ("POST", "/settings/reset_to_defaults"),
    ("POST", "/settings/save_settings"),
    ("POST", "/settings/api/import"),
    ("PUT", "/settings/api/{key}"),
    ("DELETE", "/settings/api/{key}"),
    # Both search-favorites writers were missing @settings_limit while
    # every sibling on the same settings_manager.set_setting path had it
    # — the N-of-M-siblings class. Found by this survey, fixed, and moved
    # here so a revert fails the test.
    ("PUT", "/settings/api/search-favorites"),
    ("POST", "/settings/api/search-favorites/toggle"),
}

# (method, path) -> why this is a REPORTED GAP, not a justified exception.
# Empty: the two search-favorites writers this survey found unlimited have
# since been given @settings_limit and moved into
# SETTINGS_VALUE_MUTATION_LIMITED above. Kept as a named, empty set so a
# future survey has an obvious place to record a newly-found gap rather
# than quietly allowlisting it as justified.
SETTINGS_VALUE_MUTATION_KNOWN_GAP = {}

# (method, path) -> the rate-limit decorator the route must carry. For
# routes in this family limited by their OWN bucket rather than the shared
# @settings_limit, so the expectation names that handle instead of
# assuming the family default.
SETTINGS_VALUE_MUTATION_OWN_BUCKET_LIMITED = {
    # Previously a justified exception, quoting a docstring that said
    # "Rate limiting is not applied here because users need to test URLs
    # when configuring notifications." That reasoning covers a caller who
    # submits a URL, and that path is still exempt -- via
    # notification_test_limit's exempt_when predicate. It does not cover
    # the stored-URL fallback, a zero-argument send trigger whose
    # destination the caller never has to name, so the route now carries
    # its own "notification_test" scope: testing a notification must not
    # spend the quota that saving settings needs.
    ("POST", "/settings/api/notifications/test-url"): "notification_test_limit",
}

# (method, path) -> justification. Verified by reading each route in full.
SETTINGS_VALUE_MUTATION_JUSTIFIED_EXCEPTIONS = {
    ("POST", "/settings/api/rate-limiting/engines/{engine_type}/reset"): (
        "administrative: deletes the caller's own persisted "
        "RateLimitEstimate rows, not a Setting value -- a different "
        "operation family from the settings-value-mutation group, and "
        "not a resource an attacker profits from spamming."
    ),
    ("POST", "/settings/api/rate-limiting/cleanup"): (
        "same RateLimitEstimate-table maintenance family as the reset "
        "route above, not a settings-value write."
    ),
    ("POST", "/settings/open_file_location"): (
        "opens a local OS file browser on the server; consistent with "
        "the identically-unlimited open_folder (library.py) and "
        "open_file_location (research.py) siblings -- 0 of 3 in this "
        "family carry a rate limit, so this is internally consistent, "
        "not a gap."
    ),
}


def test_settings_value_mutation_rate_limit_coverage():
    """Pins the current, surveyed rate-limit landscape of settings.py's
    value-mutation routes. Any route in this family not accounted for by
    one of the four registries above is new drift and fails here by
    name; the KNOWN_GAP entries specifically stay reported (not silently
    justified) so this test doesn't quietly launder a real bug into "fine
    by allowlist"."""
    seen_limited = set()
    seen_own_bucket = set()
    unaccounted = []
    for route in _mutating_routes():
        if route.endpoint.__module__.rsplit(".", 1)[-1] != "settings":
            continue
        key = None
        for m in _methods(route):
            candidate = (m, route.path)
            if (
                candidate in SETTINGS_VALUE_MUTATION_LIMITED
                or candidate in SETTINGS_VALUE_MUTATION_OWN_BUCKET_LIMITED
                or candidate in SETTINGS_VALUE_MUTATION_KNOWN_GAP
                or candidate in SETTINGS_VALUE_MUTATION_JUSTIFIED_EXCEPTIONS
            ):
                key = candidate
                break
        if key is None:
            continue  # not a route this survey classified; other tests own it

        has_settings_limit = "settings_limit" in _decorator_names(route)
        if key in SETTINGS_VALUE_MUTATION_LIMITED:
            seen_limited.add(key)
            if not has_settings_limit:
                unaccounted.append(
                    f"  {_route_label(route)} was surveyed as "
                    "@settings_limit-decorated but no longer is -- "
                    "either the decorator was dropped (regression) or "
                    "this route needs moving to "
                    "SETTINGS_VALUE_MUTATION_KNOWN_GAP with a reason."
                )
        elif key in SETTINGS_VALUE_MUTATION_OWN_BUCKET_LIMITED:
            seen_own_bucket.add(key)
            expected = SETTINGS_VALUE_MUTATION_OWN_BUCKET_LIMITED[key]
            if expected not in _decorator_names(route):
                unaccounted.append(
                    f"  {_route_label(route)} was surveyed as carrying "
                    f"@{expected}, its own rate-limit bucket, but no "
                    "longer does -- either the decorator was dropped "
                    "(regression: the stored-URL fallback is a "
                    "zero-argument send trigger) or the handle was "
                    "renamed and this registry needs updating."
                )
        elif key in SETTINGS_VALUE_MUTATION_KNOWN_GAP:
            if has_settings_limit:
                unaccounted.append(
                    f"  {_route_label(route)} now HAS @settings_limit -- "
                    "great, but it's still listed in "
                    "SETTINGS_VALUE_MUTATION_KNOWN_GAP as a reported bug. "
                    "Move it to SETTINGS_VALUE_MUTATION_LIMITED, the gap "
                    "is fixed."
                )
        elif key in SETTINGS_VALUE_MUTATION_JUSTIFIED_EXCEPTIONS:
            pass  # no decorator expectation either way; justified by comment

    missing_from_limited = SETTINGS_VALUE_MUTATION_LIMITED - seen_limited
    assert not missing_from_limited, (
        "Surveyed @settings_limit route(s) no longer found in the live "
        f"app: {sorted(missing_from_limited)} -- route renamed/removed? "
        "Update this test's registries."
    )
    missing_from_own_bucket = (
        set(SETTINGS_VALUE_MUTATION_OWN_BUCKET_LIMITED) - seen_own_bucket
    )
    assert not missing_from_own_bucket, (
        "Surveyed own-bucket rate-limited route(s) no longer found in the "
        f"live app: {sorted(missing_from_own_bucket)} -- route "
        "renamed/removed? Update this test's registries."
    )
    assert not unaccounted, "\n".join(unaccounted)


# --- 3c. Chat messaging family: fully covered, pinned as a positive -------
#
# Survey: all 7 mutating chat.py routes carry @limiter.limit(...,
# key_func=_chat_user_key) -- 7 of 7, no gap. Pinned so a new chat route
# silently skipping the decorator (the exact "one upload endpoint
# unlimited while its siblings are limited" shape, applied to chat) fails
# here.

CHAT_MESSAGE_MUTATION_ROUTES = {
    ("POST", "/api/chat/sessions"),
    ("POST", "/api/chat/sessions/{session_id}/generate-title"),
    ("PATCH", "/api/chat/sessions/{session_id}"),
    ("DELETE", "/api/chat/sessions/{session_id}"),
    ("POST", "/api/chat/sessions/{session_id}/messages"),
    ("DELETE", "/api/chat/sessions/{session_id}/attempts/{research_id}"),
    ("POST", "/api/chat/sessions/{session_id}/attempts/{research_id}/retry"),
}


def test_chat_message_mutation_routes_all_rate_limited():
    found = set()
    violations = []
    for route in _mutating_routes():
        if route.endpoint.__module__.rsplit(".", 1)[-1] != "chat":
            continue
        for m in _methods(route):
            key = (m, route.path)
            if key in CHAT_MESSAGE_MUTATION_ROUTES:
                found.add(key)
                if "limit" not in _decorator_names(route):
                    violations.append(
                        f"  {_route_label(route)} is a chat-mutation "
                        "route without @limiter.limit(...) -- all 7 "
                        "chat.py mutation routes are rate-limited per-"
                        "user via _chat_user_key; this one broke that "
                        "convention."
                    )
    missing = CHAT_MESSAGE_MUTATION_ROUTES - found
    assert not missing, (
        f"Surveyed chat mutation route(s) no longer in the live app: "
        f"{sorted(missing)}"
    )
    assert not violations, "\n".join(violations)


# --- 3d. Notes AI family: reported gap relative to chat.py's convention ---
#
# Finding: chat.py rate-limits every LLM-invoking mutation route per-user
# (10-30/min via @limiter.limit(..., key_func=_chat_user_key)). notes.py
# has an equally LLM-invoking mutation family -- summarize_note,
# extract_research_questions, suggest_tags, extract_key_concepts,
# fact_check_note, synthesize_notes all call NoteAIService (an LLM call per
# request, verified by reading each handler). All six ARE rate-limited, by
# purpose-built handles tiered to their cost: _notes_ai_limit (10/min),
# _notes_synthesize_limit (5/min) and the stricter _notes_factcheck_limit.
# An earlier survey reported this family as unlimited; that was a detector
# artifact, not a real gap -- the check matched a hardcoded list of limiter
# names that omitted every module-private ``_x_limit`` handle. See
# ``_is_rate_limit_decorator``, which matches the convention instead.

NOTES_AI_MUTATION_ROUTES = {
    (
        "POST",
        "/notes/api/notes/{note_id}/summarize",
    ): "NoteAIService.summarize_note",
    ("POST", "/notes/api/notes/{note_id}/research-questions"): (
        "NoteAIService.extract_research_questions"
    ),
    ("POST", "/notes/api/notes/suggest-tags"): "NoteAIService.suggest_tags",
    ("POST", "/notes/api/notes/{note_id}/key-concepts"): (
        "NoteAIService.extract_key_concepts"
    ),
    ("POST", "/notes/api/notes/{note_id}/fact-check"): (
        "NoteAIService (claim extraction) via fact_check_note"
    ),
    (
        "POST",
        "/notes/api/notes/synthesize",
    ): "NoteAIService via synthesize_notes",
}


def test_notes_ai_routes_are_rate_limited():
    """Every LLM-invoking notes route carries a dedicated rate limiter.

    Each of these calls ``NoteAIService``, i.e. one LLM round-trip per
    request, so an unlimited peer is both a cost and a denial-of-service
    vector. They are limited today, and tiered by cost rather than sharing
    one bucket: ``_notes_ai_limit`` 10/min, ``_notes_synthesize_limit``
    5/min, and a stricter ``_notes_factcheck_limit`` because fact-check
    kicks off a full research run downstream.

    This asserts the property, not a fixed decorator name, so adding a
    seventh AI route without a limiter fails here — which is the
    N-of-M-siblings class this whole file exists to catch.
    """
    unlimited = []
    found = set()
    for route in _mutating_routes():
        if route.endpoint.__module__.rsplit(".", 1)[-1] != "notes":
            continue
        for m in _methods(route):
            key = (m, route.path)
            if key in NOTES_AI_MUTATION_ROUTES:
                found.add(key)
                if not _has_rate_limit(route):
                    unlimited.append(_route_label(route))
    missing = set(NOTES_AI_MUTATION_ROUTES) - found
    assert not missing, (
        f"Surveyed notes-AI route(s) no longer in the live app: "
        f"{sorted(missing)} — update NOTES_AI_MUTATION_ROUTES."
    )
    assert not unlimited, (
        "These notes routes invoke an LLM per request but carry NO "
        "rate-limit decorator, unlike their siblings. Each unlimited "
        f"peer is a cost/DoS vector: {unlimited}"
    )


# ===========================================================================
# 4. Path params backing an integer primary key must be typed `int`
# ===========================================================================
#
# Survey method: for every path param name in the live route table, the
# backing DB column was looked up in database/models/*.py. Only two names
# resolve to an Integer primary key anywhere in the schema:
#   - resource_id  -> research_resources.id (Integer, autoincrement) --
#     documents.resource_id is `Column(Integer, ForeignKey(
#     "research_resources.id"))` (database/models/library.py).
#   - benchmark_run_id -> benchmark_runs.id (Integer)
#     (database/models/benchmark.py).
# Every other *_id path param name in the app (research_id, session_id,
# note_id/document_id, collection_id, subscription_id, folder_id, card_id,
# version_id) resolves to a String(36)/String(50) UUID-shaped primary key,
# so `str` is the correct typing there, not a gap -- confirmed per-name
# against the model definitions, not assumed from the name pattern (a
# pre-commit hook, check-research-id-type.py, separately pins research_id
# specifically to str/UUID app-wide).
#
# ALL live usages of both integer-PK names are typed `int`: library.py's
# download_single_resource and download_text_single take `resource_id: int`
# (pinned separately by
# tests/web/routers/test_library_download_resource_id_typing.py), and
# api.py's api_delete_resource plus all 5 of benchmark.py's
# benchmark_run_id routes were already `int`. So this test has ZERO
# failures today. It is written to the CORRECT end state regardless: if
# the library.py annotation regresses back to untyped, this test fails and
# names exactly those two routes.

INTEGER_PK_PATH_PARAM_NAMES: dict[str, str] = {
    "resource_id": (
        "research_resources.id (Integer, autoincrement) -- "
        "database/models/research.py ResearchResource; "
        "documents.resource_id FKs to it as Integer "
        "(database/models/library.py)"
    ),
    "benchmark_run_id": (
        "benchmark_runs.id (Integer) -- database/models/benchmark.py "
        "BenchmarkRun"
    ),
}


def _path_param_annotation(route: APIRoute, param_name: str):
    """The Python type annotation of *param_name* on *route*'s endpoint
    signature, resolving string/forward-ref annotations the same way
    FastAPI does. Returns ``inspect.Parameter.empty`` if unannotated."""
    try:
        hints = typing.get_type_hints(route.endpoint)
    except Exception:
        hints = {}
    if param_name in hints:
        return hints[param_name]
    sig = inspect.signature(route.endpoint)
    if param_name in sig.parameters:
        return sig.parameters[param_name].annotation
    return inspect.Parameter.empty


class TestPathParamTypingSelfTest:
    def test_detects_untyped_path_param(self):
        probe = FastAPI()

        def handler(resource_id) -> dict:
            return {"resource_id": resource_id}

        probe.post("/probe/{resource_id}")(handler)
        route = _api_routes(probe.routes)[0]
        assert (
            _path_param_annotation(route, "resource_id")
            is inspect.Parameter.empty
        )

    def test_detects_typed_int_path_param(self):
        probe = FastAPI()

        def handler(resource_id: int) -> dict:
            return {"resource_id": resource_id}

        probe.post("/probe/{resource_id}")(handler)
        route = _api_routes(probe.routes)[0]
        assert _path_param_annotation(route, "resource_id") is int


def test_integer_primary_key_path_params_are_typed_int():
    violations = []
    checked_names: set[str] = set()
    for route in _api_routes(app.routes):
        if "{" not in route.path:
            continue
        for param_name in route.param_convertors:
            if param_name not in INTEGER_PK_PATH_PARAM_NAMES:
                continue
            checked_names.add(param_name)
            annotation = _path_param_annotation(route, param_name)
            if annotation is not int:
                shown = (
                    "<no annotation, defaults to str>"
                    if annotation is inspect.Parameter.empty
                    else annotation
                )
                violations.append(
                    f"  {_route_label(route)} ({route.endpoint.__module__}."
                    f"{route.endpoint.__name__}): `{param_name}` is "
                    f"{shown}, expected `int`. {param_name} backs "
                    f"{INTEGER_PK_PATH_PARAM_NAMES[param_name]} -- a "
                    "non-numeric value should 422 at the routing layer, "
                    "matching every other route using this id."
                )
    assert checked_names == set(INTEGER_PK_PATH_PARAM_NAMES), (
        "Expected to find both known integer-PK path param names "
        f"{sorted(INTEGER_PK_PATH_PARAM_NAMES)} in the live route table; "
        f"only found {sorted(checked_names)}. Route table changed -- "
        "re-verify before trusting this test's coverage."
    )
    assert not violations, (
        "Integer-PK path param(s) not typed `int`:\n" + "\n".join(violations)
    )


def test_integer_pk_path_params_reject_non_numeric_with_422_not_500():
    """End-to-end confirmation (real app, real routing, no mocking) that
    the `int` annotation actually does its job: a non-numeric id must
    never reach the service layer. This is what an untyped-but-superficially
    -looks-fine annotation (e.g. a stale `# type: int` comment with no
    real annotation) would NOT catch, unlike the signature-level test
    above."""
    client = TestClient(app, raise_server_exceptions=False)
    checked = 0
    failures = []
    for route in _api_routes(app.routes):
        if "{" not in route.path:
            continue
        for param_name in route.param_convertors:
            if param_name not in INTEGER_PK_PATH_PARAM_NAMES:
                continue
            for method in _methods(route) or ("GET",):
                path = route.path
                for p in route.param_convertors:
                    filler = "not-a-number" if p == param_name else "1"
                    path = path.replace(f"{{{p}}}", filler)
                resp = client.request(method, path)
                checked += 1
                # Auth/CSRF middleware may intercept first (401/403); what
                # must never happen is the param reaching the handler and
                # blowing up as an unhandled 500.
                if resp.status_code == 500:
                    failures.append(
                        f"  {method} {path} (param {param_name}) -> 500 "
                        "instead of a routing-level 422/401/403"
                    )
    assert checked > 0, (
        "No integer-PK path param route was exercised -- the route table "
        "or param registry changed; re-verify this test's coverage."
    )
    assert not failures, "\n".join(failures)