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.

43.5 KB
Raw Blame
"""Whole-app census of the slowapi rate-limit wiring.

The FastAPI port replaced Flask-Limiter with slowapi. Individual routers
have their own bucket tests (``tests/web/routers/test_notes_rate_limit_
keys.py``, ``test_auth_rate_limits.py``, ...) and ``tests/web/
test_rate_limit_coverage.py`` checks that a handful of endpoints are
registered at all. Nothing pins the app-wide picture: for EVERY
rate-limited endpoint, what value, which bucket (shared vs its own) and
which key function (per-IP vs per-user).

That gap is where this migration's rate-limit bugs have lived. Two of
them are already fixed and documented in-tree:

  * ``metrics.py``: three journal read endpoints each got their own
    60/min bucket via ``limiter.limit()`` — 180/min combined against a
    60/min intent. Fixed with ``shared_limit(scope="journals_read")``.
  * ``benchmark.py``: ``/api/start`` and ``/api/start-simple`` each got
    their own 3/min bucket — 6 expensive benchmark runs a minute against
    a documented cap of 3. Fixed with
    ``shared_limit(scope="benchmark_start")``.

Both are the same defect: routes that should share one bucket did not.
This file censuses the assembled app so the next instance is caught by a
diff instead of by an incident, and pins the two mechanisms that decide
bucket identity in slowapi:

  ``Limiter.__evaluate_limits`` computes ``limit_scope = lim.scope or
  endpoint``, and ``_check_request_limit`` passes ``endpoint`` =
  ``request["path"]`` because this Limiter runs with slowapi's default
  ``key_style="url"``. So:

    * ``shared_limit(scope="X")``  -> one bucket named "X", app-wide.
    * ``limiter.limit(...)``        -> scope is "", and the bucket falls
      back to the CONCRETE REQUEST URL — not the route template, and not
      the endpoint function.

The second half of that is a live defect for every ``limiter.limit()``
route whose path carries a parameter; see ``TestUnscopedLimitsAreKeyed
ByUrl`` below, whose three failing expectations are marked
``xfail(strict=True)``.

Enforcement tests drive slowapi's real ``_check_request_limit`` — the
exact call the decorator wrapper makes — against the real registered
limits, real key funcs and real storage, so nothing here re-declares a
limit. Usernames/IPs are uuid-unique so the process-wide in-memory
counters never collide with other tests.
"""

import uuid
from collections import defaultdict, namedtuple

import pytest
from slowapi.errors import RateLimitExceeded
from starlette.requests import Request

# Importing the app registers every router's limits with the limiter.
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.dependencies.rate_limit import (
    API_RATE_LIMIT_DEFAULT,
    DEFAULT_RATE_LIMIT,
    _INVALID_VALUE_LOG_CHARS,
    _NUMERIC_VALUE_MAX_CHARS,
    _api_rate_limit_ctx,
    _reset_invalid_value_warnings,
    limiter,
    set_request_api_rate_limit,
)
from local_deep_research.web.routers import (
    api_v1,
    auth,
    benchmark,
    chat,
    metrics,
    rag,
    research,
)

ROUTER_PREFIX = "local_deep_research.web.routers."

# One bucket a route draws from. ``scope`` is slowapi's bucket namespace;
# "" means the decorator was ``limiter.limit()`` (no shared scope), which
# under key_style="url" makes the bucket the request URL.
Bucket = namedtuple("Bucket", "scope key exempt limits")

PER_URL = ""  # sentinel: no shared scope -> bucket is the request path

# --- shared buckets (limiter.shared_limit(scope=...)) ----------------------
# The static default half of the api_v1 pair; the dynamic custom-value
# limit registered alongside it (#5988) lives in _dynamic_route_limits,
# which _observed() deliberately does not read - the census pins the
# static registrations that keep these routes middleware-exempt.
API_V1 = Bucket(
    "api_v1", "_api_user_key", "_api_default_exempt", ("60 per 1 minute",)
)
LOG_EXPORT = Bucket(
    "log_export", "_api_user_key", "_log_export_exempt", ("10 per 1 minute",)
)
UPLOAD_USER = Bucket(
    "upload_user", "_user_key", None, ("1000 per 1 hour", "60 per 1 minute")
)
UPLOAD_IP = Bucket(
    "upload_ip",
    "_get_client_ip",
    None,
    ("1000 per 1 hour", "60 per 1 minute"),
)
SETTINGS = Bucket("settings", "_user_key", None, ("30 per 1 minute",))
# Own bucket so the notification test-url endpoint's stored-URL fallback --
# a zero-argument send trigger -- is capped without spending the quota a
# user needs for saving settings. Exempt when the caller names their own
# destination, which is the case the endpoint exists to serve (#5958).
NOTIFICATION_TEST = Bucket(
    "notification_test",
    "_user_key",
    "_caller_supplied_notification_url",
    ("30 per 1 minute",),
)
BENCH_START = Bucket(
    "benchmark_start", "_get_client_ip", None, ("3 per 1 minute",)
)
JOURNAL_DATA = Bucket("journal_data", "_user_key", None, ("2 per 1 hour",))
JOURNALS_READ = Bucket("journals_read", "_user_key", None, ("60 per 1 minute",))
NEWS_CREATE = Bucket(
    "news_create", "_get_client_ip", None, ("10 per 1 minute",)
)
NEWS_RESEARCH = Bucket(
    "news_research", "_get_client_ip", None, ("5 per 1 minute",)
)
NEWS_FEEDBACK = Bucket(
    "news_feedback", "_get_client_ip", None, ("30 per 1 minute",)
)
NEWS_PREFS = Bucket(
    "news_preferences", "_get_client_ip", None, ("10 per 1 minute",)
)
NOTES_WRITE = Bucket("notes_write", "_user_key", None, ("60 per 1 minute",))
NOTES_SEARCH = Bucket("notes_search", "_user_key", None, ("60 per 1 minute",))
NOTES_AI = Bucket("notes_ai", "_user_key", None, ("10 per 1 minute",))
NOTES_SYNTH = Bucket("notes_synthesize", "_user_key", None, ("5 per 1 minute",))
NOTES_FACT = Bucket("notes_factcheck", "_user_key", None, ("3 per 1 minute",))
NOTES_SAVE = Bucket(
    "notes_save_as_note", "_user_key", None, ("10 per 1 minute",)
)
UNIFIED = Bucket("unified_search", "_user_key", None, ("60 per 1 minute",))

# --- unscoped buckets (limiter.limit(...)); bucket == request URL ---------
AUTH_LOGIN = Bucket(PER_URL, "_get_client_ip", None, ("5 per 15 minute",))
# PR 7329 raised the shipped registration default from 3/hour to a
# dual 10/min + 300/hour budget per client IP (both counting failed
# attempts) so a typo no longer locks the signup form out for an hour;
# premise changed by changelog.d/+registration-rate-limit.bugfix.md.
AUTH_REGISTER = Bucket(
    PER_URL, "_get_client_ip", None, ("10 per 1 minute", "300 per 1 hour")
)
AUTH_VALIDATE = Bucket(PER_URL, "_get_client_ip", None, ("30 per 1 minute",))
# /auth/integrity-check runs SQLite's full integrity_check scan, so it gets
# its own tight per-IP budget rather than sharing a brute-force bucket.
AUTH_INTEGRITY = Bucket(PER_URL, "_get_client_ip", None, ("10 per 1 minute",))
CHAT_10 = Bucket(PER_URL, "_chat_user_key", None, ("10 per 1 minute",))
CHAT_20 = Bucket(PER_URL, "_chat_user_key", None, ("20 per 1 minute",))
CHAT_30 = Bucket(PER_URL, "_chat_user_key", None, ("30 per 1 minute",))

# Every rate-limited endpoint in the assembled app, module tail + function
# name -> the buckets it draws from. Adding a rate-limited route without
# updating this map fails ``test_no_unlisted_rate_limited_endpoint``.
CENSUS = {
    # --- /api/v1: one per-user bucket, kill-switch via app.api_rate_limit
    "api_v1.api_documentation": (API_V1,),
    "api_v1.api_quick_summary": (API_V1,),
    "api_v1.api_generate_report": (API_V1,),
    "api_v1.api_analyze_documents": (API_V1,),
    # start_research shares the /api/v1 bucket (parity with main, where
    # research_routes.start_research also carried @api_rate_limit).
    "research.start_research": (API_V1,),
    # --- auth: per-IP brute-force budgets, one route each ----------------
    "auth.login": (AUTH_LOGIN,),
    "auth.change_password": (AUTH_LOGIN,),
    "auth.register": (AUTH_REGISTER,),
    "auth.validate_password": (AUTH_VALIDATE,),
    "auth.integrity_check": (AUTH_INTEGRITY,),
    # --- benchmark: both start endpoints in ONE bucket -------------------
    "benchmark.start_benchmark": (BENCH_START,),
    "benchmark.start_benchmark_simple": (BENCH_START,),
    # --- chat: per-user, but each route unscoped (see the xfails) --------
    "chat.create_session": (CHAT_20,),
    "chat.send_message": (CHAT_10,),
    "chat.generate_session_title": (CHAT_10,),
    "chat.retry_attempt": (CHAT_10,),
    "chat.update_session": (CHAT_30,),
    "chat.delete_session": (CHAT_30,),
    "chat.delete_attempt": (CHAT_30,),
    # --- journal quality -------------------------------------------------
    "metrics.api_journal_data_download": (JOURNAL_DATA,),
    "metrics.api_journal_quality": (JOURNALS_READ,),
    "metrics.api_user_research_journals": (JOURNALS_READ,),
    "metrics.api_research_journals": (JOURNALS_READ,),
    # --- news POSTs (per-IP, matching main) ------------------------------
    "news_flask_api.create_subscription": (NEWS_CREATE,),
    "news_flask_api.research_news_item": (NEWS_RESEARCH,),
    "news_flask_api.submit_feedback": (NEWS_FEEDBACK,),
    "news_flask_api.save_preferences": (NEWS_PREFS,),
    # --- notes: six cost-tiered per-user buckets -------------------------
    "notes.create_note": (NOTES_WRITE,),
    "notes.update_note": (NOTES_WRITE,),
    "notes.delete_note": (NOTES_WRITE,),
    "notes.add_note_to_collection": (NOTES_WRITE,),
    "notes.remove_note_from_collection": (NOTES_WRITE,),
    "notes.link_research_to_note": (NOTES_WRITE,),
    "notes.patch_note_research": (NOTES_WRITE,),
    "notes.reorder_note_research": (NOTES_WRITE,),
    "notes.create_research_note": (NOTES_WRITE,),
    "notes.create_research_annotation": (NOTES_WRITE,),
    "notes.delete_research_annotation": (NOTES_WRITE,),
    "notes.create_document_note": (NOTES_WRITE,),
    "notes.create_document_annotation": (NOTES_WRITE,),
    "notes.delete_document_annotation": (NOTES_WRITE,),
    "notes.index_note_to_collection": (NOTES_WRITE,),
    "notes.accept_suggested_link": (NOTES_WRITE,),
    "notes.restore_note_version": (NOTES_WRITE,),
    "notes.list_notes": (NOTES_SEARCH,),
    "notes.semantic_search_notes": (NOTES_SEARCH,),
    "notes.ask_context": (NOTES_SEARCH,),
    "notes.search_notes_for_linking": (NOTES_SEARCH,),
    "notes.get_research_notes": (NOTES_SEARCH,),
    "notes.get_research_annotations": (NOTES_SEARCH,),
    "notes.get_document_notes": (NOTES_SEARCH,),
    "notes.get_document_annotations": (NOTES_SEARCH,),
    "notes.get_similar_notes": (NOTES_SEARCH,),
    "notes.get_backlinks": (NOTES_SEARCH,),
    "notes.get_outgoing_links": (NOTES_SEARCH,),
    "notes.get_suggested_links": (NOTES_SEARCH,),
    "notes.get_unlinked_mentions": (NOTES_SEARCH,),
    "notes.similar_passages": (NOTES_SEARCH,),
    "notes.resolve_link": (NOTES_SEARCH,),
    "notes.summarize_note": (NOTES_AI,),
    "notes.extract_research_questions": (NOTES_AI,),
    "notes.suggest_tags": (NOTES_AI,),
    "notes.extract_key_concepts": (NOTES_AI,),
    "notes.get_semantic_diff": (NOTES_AI,),
    "notes.preview_synthesis": (NOTES_SYNTH,),
    "notes.synthesize_notes": (NOTES_SYNTH,),
    "notes.fact_check_note": (NOTES_FACT,),
    "notes.grade_note_fact_check": (NOTES_FACT,),
    "notes.save_research_as_note": (NOTES_SAVE,),
    # --- uploads: dual per-user AND per-IP buckets, both routers ---------
    "research.upload_pdf": (UPLOAD_IP, UPLOAD_USER),
    "rag.upload_to_collection": (UPLOAD_IP, UPLOAD_USER),
    # --- misc ------------------------------------------------------------
    "research.export_research_logs": (LOG_EXPORT,),
    "settings.save_all_settings": (SETTINGS,),
    "settings.save_settings": (SETTINGS,),
    "settings.reset_to_defaults": (SETTINGS,),
    "settings.api_import_settings": (SETTINGS,),
    "settings.api_update_setting": (SETTINGS,),
    "settings.api_delete_setting": (SETTINGS,),
    "settings.api_toggle_search_favorite": (SETTINGS,),
    "settings.api_update_search_favorites": (SETTINGS,),
    "settings.api_test_notification_url": (NOTIFICATION_TEST,),
    "unified_search.keyword_search": (UNIFIED,),
    "unified_search.semantic_search": (UNIFIED,),
}

# Routes with NO limit at all — not even the global default: slowapi's
# ``_check_request_limit`` returns immediately for an exempt endpoint.
# Same eight sites main marked ``@limiter.exempt`` (app_factory x2,
# benchmark x3, rag, history, research), so this is migration parity.
# Every one is a cheap status/poll endpoint the UI hits on a timer.
EXPECTED_EXEMPT = {
    "local_deep_research.web.fastapi_app.favicon",
    "local_deep_research.web.fastapi_app.serve_static",
    "local_deep_research.web.routers.benchmark.get_benchmark_results",
    "local_deep_research.web.routers.benchmark.get_benchmark_status",
    "local_deep_research.web.routers.benchmark.get_search_quality",
    "local_deep_research.web.routers.history.get_research_status",
    "local_deep_research.web.routers.rag.get_index_status",
    "local_deep_research.web.routers.research.get_research_status",
}

# Shared scopes main declared in security/rate_limiter.py, mapped to the
# branch scope that carries them. main's three auth scopes ("login",
# "registration", "password_change") were single-route buckets; the port
# expresses them as plain ``limiter.limit()`` decorators, which is only
# equivalent while each stays alone on one static path — see
# ``TestUnscopedLimitsAreKeyedByUrl``.
MAIN_SHARED_SCOPES = {
    "settings": "settings",
    "api_v1": "api_v1",
    "upload_user": "upload_user",
    "upload_ip": "upload_ip",
    "journal_data": "journal_data",
    "journals_read": "journals_read",
    "log_export": "log_export",
    "unified_search": "unified_search",
    "news_create": "news_create",
    "news_research": "news_research",
    "news_feedback": "news_feedback",
    "news_preferences": "news_preferences",
}


def _short(qualified):
    if qualified.startswith(ROUTER_PREFIX):
        return qualified[len(ROUTER_PREFIX) :]
    return qualified


def _observed():
    """The live census, read off the limiter the app is wired with."""
    out = {}
    for name, limits in limiter._route_limits.items():
        grouped = defaultdict(list)
        for lim in limits:
            grouped[
                (
                    lim.scope,
                    lim.key_func.__name__,
                    getattr(lim.exempt_when, "__name__", None),
                )
            ].append(str(lim.limit))
        buckets = [
            Bucket(scope, key, exempt, tuple(sorted(values)))
            for (scope, key, exempt), values in grouped.items()
        ]
        buckets.sort(key=lambda b: (b.scope, b.key, b.exempt or "", b.limits))
        out[_short(name)] = tuple(buckets)
    return out


def _route_paths():
    """endpoint short name -> {(method, path template)} in the real app."""
    paths = defaultdict(set)
    for route in app.routes:
        endpoint = getattr(route, "endpoint", None)
        if endpoint is None:
            continue
        name = _short(f"{endpoint.__module__}.{endpoint.__name__}")
        for method in getattr(route, "methods", None) or ():
            paths[name].add((method, route.path))
    return paths


def _storage_keys():
    """Live counter keys in the limiter's storage backend.

    The key embeds the resolved bucket namespace (``LIMITER/<key>/
    <scope-or-url>/<amount>/...``), so it is the ground truth for "did
    these two requests land in the same bucket".
    """
    storage = limiter._storage
    keys = set(getattr(storage, "events", None) or {})
    keys |= set(getattr(storage, "storage", None) or {})
    return keys


def _request(username=None, path="/x", method="POST", ip="10.211.0.1"):
    """Minimal Starlette Request from a raw ASGI scope.

    ``path`` matters: under key_style="url" it IS the bucket namespace for
    any limit registered without a shared scope.
    """
    scope = {
        "type": "http",
        "method": method,
        "path": path,
        "query_string": b"",
        "headers": [],
        "client": (ip, 51234),
    }
    if username is not None:
        scope["session"] = {"username": username}
    return Request(scope)


def _unique_user(tag):
    return f"rlcensus-{tag}-{uuid.uuid4().hex[:12]}"


def _unique_ip():
    raw = uuid.uuid4().int
    return f"10.211.{raw % 250 + 1}.{raw // 250 % 250 + 1}"


@pytest.fixture()
def check(monkeypatch):
    """Drive slowapi's real limit check for one endpoint function.

    ``limiter.enabled`` is forced on (restored by monkeypatch) because
    the test environment may run with LDR_DISABLE_RATE_LIMITING set.
    """
    monkeypatch.setattr(limiter, "enabled", True)

    def _check(
        endpoint_func,
        path,
        username=None,
        ip="10.211.0.1",
        method="POST",
        in_middleware=False,
    ):
        request = _request(username=username, path=path, method=method, ip=ip)
        limiter._check_request_limit(request, endpoint_func, in_middleware)

    return _check


@pytest.fixture(autouse=True)
def _fresh_invalid_value_warnings():
    """``set_request_api_rate_limit`` warns once per invalid value per
    process. The same literals recur across parametrizations here and in
    the HTTP contract tests, so each test starts with nothing warned yet."""
    _reset_invalid_value_warnings()
    yield
    _reset_invalid_value_warnings()


class TestCensusIsComplete:
    """The table above must describe the assembled app exactly."""

    def test_no_unlisted_rate_limited_endpoint(self):
        extra = sorted(set(_observed()) - set(CENSUS))
        assert not extra, (
            f"rate-limited endpoints missing from CENSUS: {extra}. Add each "
            "with the bucket it draws from — an endpoint that quietly gets "
            "its own bucket instead of joining a shared one is the exact "
            "defect metrics.py and benchmark.py were both fixed for."
        )

    def test_no_stale_census_entry(self):
        missing = sorted(set(CENSUS) - set(_observed()))
        assert not missing, (
            f"CENSUS lists endpoints with no registered limit: {missing}. "
            "Their rate limit was dropped, renamed or moved."
        )

    @pytest.mark.parametrize("endpoint", sorted(CENSUS))
    def test_endpoint_buckets_match_the_census(self, endpoint):
        observed = _observed().get(endpoint)
        assert observed == CENSUS[endpoint], (
            f"{endpoint}: rate-limit wiring changed.\n"
            f"  expected {CENSUS[endpoint]}\n"
            f"  observed {observed}"
        )

    def test_every_limited_endpoint_is_reachable(self):
        """A limit on a function FastAPI never routes is decoration only."""
        paths = _route_paths()
        orphans = sorted(name for name in CENSUS if not paths.get(name))
        assert not orphans, (
            f"rate-limited functions with no route in the app: {orphans}"
        )


class TestBucketConsistency:
    """Scope collisions and split budgets, read off the live registry."""

    def test_each_scope_has_one_agreed_definition(self):
        """slowapi keeps ONE counter per (key, scope). Two endpoints in the
        same scope with different rates or key funcs silently merge into
        whichever definition the first request evaluates."""
        by_scope = defaultdict(set)
        for endpoint, buckets in _observed().items():
            for bucket in buckets:
                if bucket.scope == PER_URL:
                    continue
                by_scope[bucket.scope].add((bucket.key, bucket.limits))
        conflicting = {s: v for s, v in by_scope.items() if len(v) > 1}
        assert not conflicting, (
            f"scopes with conflicting definitions: {conflicting}"
        )

    def test_main_shared_scopes_all_survived_the_migration(self):
        live = {
            bucket.scope
            for buckets in _observed().values()
            for bucket in buckets
        }
        lost = sorted(
            main
            for main, branch in MAIN_SHARED_SCOPES.items()
            if branch not in live
        )
        assert not lost, (
            f"shared buckets main declared are gone: {lost}. Splitting one "
            "of these back into per-route limits multiplies its cap by the "
            "number of routes."
        )

    @pytest.mark.parametrize(
        "scope,members",
        [
            # The two groups that were shipped broken and fixed in-tree.
            (
                "benchmark_start",
                {
                    "benchmark.start_benchmark",
                    "benchmark.start_benchmark_simple",
                },
            ),
            (
                "journals_read",
                {
                    "metrics.api_journal_quality",
                    "metrics.api_user_research_journals",
                    "metrics.api_research_journals",
                },
            ),
            # Cross-router groups: one budget must cover both uploaders and
            # both /api/v1-class research entry points.
            (
                "upload_user",
                {"research.upload_pdf", "rag.upload_to_collection"},
            ),
            (
                "upload_ip",
                {"research.upload_pdf", "rag.upload_to_collection"},
            ),
            (
                "api_v1",
                {
                    "api_v1.api_documentation",
                    "api_v1.api_quick_summary",
                    "api_v1.api_generate_report",
                    "api_v1.api_analyze_documents",
                    "research.start_research",
                },
            ),
            (
                "unified_search",
                {
                    "unified_search.keyword_search",
                    "unified_search.semantic_search",
                },
            ),
        ],
    )
    def test_scope_membership(self, scope, members):
        live = {
            endpoint
            for endpoint, buckets in _observed().items()
            if any(b.scope == scope for b in buckets)
        }
        assert live == members, (
            f"membership of the '{scope}' bucket changed: {sorted(live)}. A "
            "route leaving the bucket gets its own counter, multiplying the "
            "effective cap."
        )


class TestSharedBucketsAreEnforced:
    """Behavioural proof the shared scopes really are ONE counter."""

    def test_benchmark_start_endpoints_drain_one_bucket(self, check):
        """Regression fence for the fix in benchmark.py: /api/start and
        /api/start-simple kick off the same expensive LLM+search run, so
        3/min is a combined cap, not 3 each."""
        ip = _unique_ip()
        user = _unique_user("bench")
        for _ in range(3):
            check(
                benchmark.start_benchmark,
                "/benchmark/api/start",
                username=user,
                ip=ip,
            )
        with pytest.raises(RateLimitExceeded):
            check(
                benchmark.start_benchmark,
                "/benchmark/api/start",
                username=user,
                ip=ip,
            )
        # The sibling start endpoint must be blocked by the SAME counter.
        with pytest.raises(RateLimitExceeded):
            check(
                benchmark.start_benchmark_simple,
                "/benchmark/api/start-simple",
                username=user,
                ip=ip,
            )

    def test_uploads_share_one_per_user_bucket_across_routers(self, check):
        """research.upload_pdf and rag.upload_to_collection both write user
        files to disk; main gave them one shared upload_user budget."""
        user = _unique_user("upload")
        ip = _unique_ip()
        for _ in range(60):
            check(research.upload_pdf, "/api/upload/pdf", username=user, ip=ip)
        with pytest.raises(RateLimitExceeded):
            check(
                rag.upload_to_collection,
                "/library/api/collections/7/upload",
                username=user,
                ip=ip,
            )

    def test_api_v1_custom_values_are_enforced_per_user(self, check):
        """#5988: a custom nonzero ``app.api_rate_limit`` caps each user
        at THEIR value. Alice at 2/min and Bob at 5/min (same IP) drain
        independent counters - Alice's exhaustion does not touch Bob, and
        Bob's cap is his five, not Alice's two or the static default's
        sixty. The ContextVar stands in for ``require_api_access``, which
        caches the setting there before the decorator's check runs."""
        alice = _unique_user("apiv1-two")
        bob = _unique_user("apiv1-five")
        ip = _unique_ip()

        token = _api_rate_limit_ctx.set(2)
        try:
            for _ in range(2):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=alice,
                    ip=ip,
                    method="GET",
                )
            with pytest.raises(RateLimitExceeded):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=alice,
                    ip=ip,
                    method="GET",
                )

            _api_rate_limit_ctx.set(5)
            for _ in range(5):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=bob,
                    ip=ip,
                    method="GET",
                )
            with pytest.raises(RateLimitExceeded):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=bob,
                    ip=ip,
                    method="GET",
                )
        finally:
            _api_rate_limit_ctx.reset(token)

    @pytest.mark.parametrize(
        "raw,limit",
        [("120", 120), (" 2 ", 2), ("2".rjust(_NUMERIC_VALUE_MAX_CHARS), 2)],
        ids=["env-override-string", "padded-string", "padded-to-the-bound"],
    )
    def test_api_v1_numeric_strings_are_honoured(
        self, check, loguru_caplog, raw, limit
    ):
        """``app.api_rate_limit`` has no defaults-JSON entry, so an env
        override (``LDR_APP_API_RATE_LIMIT=120``) or a text-typed settings
        row reaches ``set_request_api_rate_limit`` as the raw string. Main
        honoured it (``f"{value} per minute"``), so the string is cached as
        the int it spells and enforced at THAT value, with no warning. The
        length bound on that conversion is inclusive: a string padded out
        to exactly ``_NUMERIC_VALUE_MAX_CHARS`` is still read."""
        user = _unique_user("apiv1-str")
        ip = _unique_ip()

        token = _api_rate_limit_ctx.set(API_RATE_LIMIT_DEFAULT)
        try:
            set_request_api_rate_limit(raw)
            assert _api_rate_limit_ctx.get() == limit
            for _ in range(limit):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=user,
                    ip=ip,
                    method="GET",
                )
            with pytest.raises(RateLimitExceeded):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=user,
                    ip=ip,
                    method="GET",
                )
        finally:
            _api_rate_limit_ctx.reset(token)

        assert "invalid app.api_rate_limit" not in loguru_caplog.text

    @pytest.mark.parametrize(
        "bad_value",
        [
            -5,
            2.5,
            "abc",
            "12a",
            None,
            True,
            False,
            "1" * (_NUMERIC_VALUE_MAX_CHARS + 1),
            "\u0661\u0662\u0660",
        ],
        ids=[
            "negative",
            "fractional",
            "non-numeric",
            "mixed-string",
            "none",
            "true",
            "false",
            "digits-over-length-bound",
            "non-ascii-digits",
        ],
    )
    def test_api_v1_invalid_values_fall_back_to_the_default(
        self, check, loguru_caplog, bad_value
    ):
        """``app.api_rate_limit`` has no settings-schema entry, so a
        negative, fractional, non-numeric, null or boolean value can reach
        ``set_request_api_rate_limit``. It must be logged and the user held
        at the static default, NOT treated as a custom value: that would
        exempt the static limit and hand the dynamic one a limit string
        slowapi cannot parse, which it logs and skips - no limit at all.
        ``False`` is the sneaky one (an ``int`` equal to 0): taken at face
        value it would engage the kill switch. A digit string past
        ``_NUMERIC_VALUE_MAX_CHARS`` is refused too, even though ``int()``
        could convert it: the length bound runs before the digit scan. So
        are the Arabic-Indic digits for 120: ``str.isdecimal()`` passes
        them and ``int()`` reads them, but the setting is a number typed
        into an env var or a text column, so only ASCII digits count."""
        user = _unique_user("apiv1-bad")
        ip = _unique_ip()

        token = _api_rate_limit_ctx.set(API_RATE_LIMIT_DEFAULT)
        try:
            set_request_api_rate_limit(bad_value)
            for _ in range(API_RATE_LIMIT_DEFAULT):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=user,
                    ip=ip,
                    method="GET",
                )
            with pytest.raises(RateLimitExceeded):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=user,
                    ip=ip,
                    method="GET",
                )
            assert _api_rate_limit_ctx.get() == API_RATE_LIMIT_DEFAULT
        finally:
            _api_rate_limit_ctx.reset(token)

        assert f"invalid app.api_rate_limit {bad_value!r}" in loguru_caplog.text

    @pytest.mark.parametrize(
        "huge",
        ["x" * 4096, "9" * 5000, "9" * 80 + "x"],
        ids=["text", "digits", "mixed"],
    )
    def test_api_v1_invalid_value_warning_is_bounded(self, loguru_caplog, huge):
        """The ``app.*`` namespace is client-writable, so the warning must
        not echo a multi-KB value whole: ``repr`` is cut at
        ``_INVALID_VALUE_LOG_CHARS`` characters and the default is still
        what gets cached. The digit string is also past ``int()``'s
        4300-digit conversion limit, so it must be refused like any other
        garbage rather than raise out of the request. All three are past
        ``_NUMERIC_VALUE_MAX_CHARS`` as well, so none of them is scanned
        for digits before being refused."""
        token = _api_rate_limit_ctx.set(API_RATE_LIMIT_DEFAULT)
        try:
            set_request_api_rate_limit(huge)
            assert _api_rate_limit_ctx.get() == API_RATE_LIMIT_DEFAULT
        finally:
            _api_rate_limit_ctx.reset(token)

        echo = repr(huge)[:_INVALID_VALUE_LOG_CHARS] + "..."
        assert f"invalid app.api_rate_limit {echo}" in loguru_caplog.text
        assert huge not in loguru_caplog.text

    def test_api_v1_invalid_value_is_sliced_before_repr(self, loguru_caplog):
        """The cut above has to happen BEFORE ``repr``: a multi-MB stored
        value would otherwise be copied in full on every request just to
        show the head of it. A ``str`` subclass that records the lengths
        ``repr`` is asked for is the probe: slicing hands ``repr`` a plain
        ``str`` of at most ``_INVALID_VALUE_LOG_CHARS`` characters, so the
        recorder never sees the whole value."""
        seen = []

        class Recorded(str):
            def __repr__(self):
                seen.append(len(self))
                return super().__repr__()

        huge = Recorded("x" * 4096)
        token = _api_rate_limit_ctx.set(API_RATE_LIMIT_DEFAULT)
        try:
            set_request_api_rate_limit(huge)
            assert _api_rate_limit_ctx.get() == API_RATE_LIMIT_DEFAULT
        finally:
            _api_rate_limit_ctx.reset(token)

        assert all(n <= _INVALID_VALUE_LOG_CHARS for n in seen), seen
        echo = repr("x" * 4096)[:_INVALID_VALUE_LOG_CHARS] + "..."
        assert f"invalid app.api_rate_limit {echo}" in loguru_caplog.text
        assert "x" * 4096 not in loguru_caplog.text

    def test_api_v1_invalid_value_warns_once_per_value(self, loguru_caplog):
        """A stored invalid value is re-read on every request, so the
        warning fires once per distinct value per process: a repeat of the
        same value is logged at debug, a different value warns again, and
        the default is cached every time regardless."""
        token = _api_rate_limit_ctx.set(API_RATE_LIMIT_DEFAULT)
        try:
            for value in ("abc", "abc", "12a"):
                set_request_api_rate_limit(value)
                assert _api_rate_limit_ctx.get() == API_RATE_LIMIT_DEFAULT
        finally:
            _api_rate_limit_ctx.reset(token)

        logged = [
            (record.levelname, record.getMessage())
            for record in loguru_caplog.records
            if "invalid app.api_rate_limit" in record.getMessage()
        ]
        assert [level for level, _ in logged] == ["WARNING", "DEBUG", "WARNING"]
        assert "invalid app.api_rate_limit 'abc'" in logged[0][1]
        assert "invalid app.api_rate_limit 'abc'" in logged[1][1]
        assert "invalid app.api_rate_limit '12a'" in logged[2][1]

    @pytest.mark.parametrize("zero", [0, "0"], ids=["int", "numeric-string"])
    def test_api_v1_zero_still_disables_the_limit(self, check, zero):
        """The kill switch survives the validation above: ``0`` is cached
        as-is (and ``"0"`` as 0, like any digit string) and exempts both
        halves of the api_v1 pair, so the user is never counted, let alone
        cut off past the static default."""
        user = _unique_user("apiv1-zero")
        ip = _unique_ip()

        token = _api_rate_limit_ctx.set(API_RATE_LIMIT_DEFAULT)
        try:
            set_request_api_rate_limit(zero)
            for _ in range(API_RATE_LIMIT_DEFAULT + 1):
                check(
                    api_v1.api_documentation,
                    "/api/v1/",
                    username=user,
                    ip=ip,
                    method="GET",
                )
        finally:
            _api_rate_limit_ctx.reset(token)

    def test_journal_read_endpoints_drain_one_bucket(self, check):
        """Regression fence for the fix documented in metrics.py."""
        user = _unique_user("journals")
        ip = _unique_ip()
        for _ in range(60):
            check(
                metrics.api_journal_quality,
                "/metrics/api/journals",
                username=user,
                ip=ip,
                method="GET",
            )
        with pytest.raises(RateLimitExceeded):
            check(
                metrics.api_research_journals,
                "/metrics/api/journals/research/abc",
                username=user,
                ip=ip,
                method="GET",
            )


class TestUnscopedLimitsAreKeyedByUrl:
    """The defect this census exists to surface.

    ``limiter.limit()`` leaves ``Limit.scope`` empty, and slowapi then
    falls back to the ``endpoint`` argument of ``__evaluate_limits``,
    which ``_check_request_limit`` sets to ``request["path"]`` under
    ``key_style="url"`` (slowapi's default; this app does not override
    it). Flask-Limiter on main had no such mode — its fallback scope was
    ``request.endpoint``, the route's name.

    For a static path the two agree. For a path template with a
    parameter they do not: every distinct id value opens a FRESH bucket,
    so the cap is per-URL rather than per-route, and a caller who varies
    the id is effectively unlimited.
    """

    def test_key_style_is_endpoint(self):
        """Mechanism pin: the bucket must be the ROUTE, not the request URL.

        slowapi's default is ``"url"``, which gives every distinct path
        parameter value its own counter — rotating one segment resets the
        limit. Flask-Limiter keyed off ``request.endpoint`` on main, so
        ``"endpoint"`` is parity. If this flips back, every unscoped limit
        on a parameterised path silently stops capping anything.
        """
        assert limiter._key_style == "endpoint"

    def test_unscoped_limits_carry_no_scope(self):
        """The other half of the mechanism: no scope -> URL fallback."""
        unscoped = {
            endpoint
            for endpoint, buckets in _observed().items()
            if any(b.scope == PER_URL for b in buckets)
        }
        assert unscoped, (
            "no unscoped limits left — if every route moved to "
            "shared_limit, delete this class along with the xfails below"
        )

    def test_unscoped_limits_only_sit_on_parameterless_paths(self):
        """An unscoped limit is only a real cap when its path is fixed.

        Either fix satisfies this: give the group a
        ``shared_limit(scope=...)``, or construct the Limiter with
        ``key_style="endpoint"`` so the fallback scope becomes the
        endpoint function (which is what Flask-Limiter did on main).
        """
        paths = _route_paths()
        offenders = {}
        if limiter._key_style == "url":
            for endpoint, buckets in _observed().items():
                if not any(b.scope == PER_URL for b in buckets):
                    continue
                parameterised = sorted(
                    path
                    for _method, path in paths.get(endpoint, ())
                    if "{" in path
                )
                if parameterised:
                    offenders[endpoint] = parameterised
        assert not offenders, (
            "unscoped rate limits on parameterised paths (each id value "
            f"is a separate bucket): {offenders}"
        )

    def test_send_message_budget_is_per_user_not_per_chat_session(self, check):
        user = _unique_user("chat")
        ip = _unique_ip()
        for _ in range(10):
            check(
                chat.send_message,
                "/api/chat/sessions/AAAA/messages",
                username=user,
                ip=ip,
            )
        with pytest.raises(RateLimitExceeded):
            check(
                chat.send_message,
                "/api/chat/sessions/AAAA/messages",
                username=user,
                ip=ip,
            )
        # Same user, same route, a different session id: must still be
        # blocked, because 10/min is a budget for the ROUTE.
        with pytest.raises(RateLimitExceeded):
            check(
                chat.send_message,
                "/api/chat/sessions/BBBB/messages",
                username=user,
                ip=ip,
            )

    def test_global_default_limit_is_scoped_per_route_not_per_url(self, check):
        """SlowAPIMiddleware applies DEFAULT_RATE_LIMIT to undecorated
        routes through the same ``__evaluate_limits`` path, so it inherits
        the URL scoping. Two GETs of one route with different ids must
        share the counter; today they open two.

        Read off the storage backend rather than by exhausting 5000
        requests: the counter key embeds the resolved bucket namespace.
        """
        ip = _unique_ip()
        user = _unique_user("default")
        before = _storage_keys()
        for session_id in ("X1", "X2"):
            check(
                chat.get_session,
                f"/api/chat/sessions/{session_id}",
                username=user,
                ip=ip,
                method="GET",
                in_middleware=True,
            )
        created = sorted(_storage_keys() - before)
        assert created, "no default-limit counter was created at all"
        # One counter per configured default limit ("5000 per hour" and
        # "50000 per day"), NOT one per distinct URL.
        assert len(created) == 2, (
            "the global default limit opened a separate counter per URL: "
            f"{created}"
        )


class TestAuthBucketsHaveASingleOccupant:
    """main gave login / registration / password-change their own shared
    scopes. The port dropped the scopes and relies on each limit sitting
    alone on one static path — true today, and silently untrue the moment
    a second route reuses one of the constants (two routes, two buckets,
    double the brute-force budget)."""

    @pytest.mark.parametrize(
        "endpoint,path",
        [
            ("auth.login", "/auth/login"),
            ("auth.register", "/auth/register"),
            ("auth.change_password", "/auth/change-password"),
            ("auth.validate_password", "/auth/validate-password"),
            ("auth.integrity_check", "/auth/integrity-check"),
        ],
    )
    def test_auth_limit_sits_alone_on_one_static_path(self, endpoint, path):
        observed = _observed()
        buckets = observed[endpoint]
        assert all(b.scope == PER_URL for b in buckets)

        methods_paths = _route_paths()[endpoint]
        assert {p for _m, p in methods_paths} == {path}
        assert "{" not in path

        # No OTHER rate-limited endpoint may serve this same path, or the
        # two would silently share (or split) this brute-force budget.
        sharers = {
            other
            for other in observed
            if other != endpoint
            and path in {p for _m, p in _route_paths()[other]}
        }
        assert not sharers, (
            f"{path} is also served by rate-limited {sorted(sharers)}"
        )

    def test_login_and_change_password_do_not_share_a_bucket(self, check):
        """Same 5-per-15-minutes value, deliberately separate budgets on
        main (scopes "login" vs "password_change"): burning login attempts
        must not lock a signed-in user out of rotating their password."""
        ip = _unique_ip()
        for _ in range(5):
            check(auth.login, "/auth/login", ip=ip)
        with pytest.raises(RateLimitExceeded):
            check(auth.login, "/auth/login", ip=ip)
        # Different path -> different bucket, still allowed.
        check(auth.change_password, "/auth/change-password", ip=ip)


class TestExemptions:
    """@limiter.exempt removes a route from limiting ENTIRELY — including
    the global default (``_check_request_limit`` returns before any limit
    is assembled). Keep the list short and deliberate."""

    def test_exempt_route_set_is_pinned(self):
        assert limiter._exempt_routes == EXPECTED_EXEMPT, (
            "the @limiter.exempt set changed. An exempt route has NO cap "
            "at all, not even DEFAULT_RATE_LIMIT — added:"
            f" {sorted(limiter._exempt_routes - EXPECTED_EXEMPT)}, removed:"
            f" {sorted(EXPECTED_EXEMPT - limiter._exempt_routes)}"
        )

    def test_no_route_is_both_exempt_and_limited(self):
        """Exemption wins, so a limit on an exempt route never runs."""
        limited = set(limiter._route_limits)
        assert not (limited & EXPECTED_EXEMPT)

    def test_exempt_route_skips_even_the_default_limit(self, check):
        """Behavioural: an exempt endpoint creates no counter, while a
        plain undecorated one does."""
        ip = _unique_ip()
        user = _unique_user("exempt")

        before = _storage_keys()
        check(
            research.get_research_status,
            "/api/research/abc/status",
            username=user,
            ip=ip,
            method="GET",
            in_middleware=True,
        )
        assert _storage_keys() == before, (
            "an exempt route consumed a rate-limit counter"
        )

        # Control: a non-exempt, undecorated route on the same IP does.
        check(
            chat.get_session,
            "/api/chat/sessions/exempt-control",
            username=user,
            ip=ip,
            method="GET",
            in_middleware=True,
        )
        assert _storage_keys() != before


class TestGlobalDefault:
    def test_default_limits_are_configured_and_pinned(self):
        live = [
            str(lim.limit) for grp in limiter._default_limits for lim in grp
        ]
        assert live == ["5000 per 1 hour", "50000 per 1 day"]

    def test_default_limit_string_comes_from_server_config(self):
        assert DEFAULT_RATE_LIMIT == "5000 per hour;50000 per day"