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.

46.7 KB
Raw Blame
"""Census of pagination clamping across every paginated FastAPI router.

An unclamped ``?limit=`` is a cheap denial of service on a single-worker
uvicorn deployment: the request forces an unbounded ``SELECT`` and an
unbounded JSON response body while the one worker is blocked. The same
holds for ``?offset=``, which SQLite refuses outright once it exceeds a
signed 64-bit integer (``OverflowError: Python int too large to convert
to SQLite INTEGER``) — a 500 from a query-string typo.

The routers were ported from Flask by different hands, and Flask's
``request.args.get(name, default, type=int)`` swallowed conversion errors
while ``int(request.query_params.get(...))`` raises. Each porter re-derived
a clamp, so the same parameter is bounded differently (or not at all) from
one router to the next. This file is a CENSUS: it pins the clamp actually
in force on each paginated route so a future edit that widens or drops one
fails here by name. Every user-controlled offset now has a ceiling.

Observation strategy (no large datasets are seeded anywhere):

* helper contracts are asserted directly on the shared parse/clamp
  helpers each router calls;
* route-level clamps are read off the *arguments the route hands its
  service*, captured by a recording stand-in, or off the values the route
  echoes back in its own JSON envelope;
* SQL-level clamps are read off a probe wrapped around
  ``sqlalchemy.orm.Query.limit`` / ``.offset``, so the assertion is on the
  LIMIT that reached the database rather than on a materialised response.

Every probe in this file carries a self-test that fails if the probe stops
observing anything (see ``test_probe_observes_a_known_limit`` and
``test_recorder_captures_service_kwargs``).
"""

import ast
import functools
import uuid
from contextlib import contextmanager
from datetime import datetime, timedelta, timezone
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch

import pytest
import sqlalchemy.orm as sa_orm
from starlette.datastructures import QueryParams

from local_deep_research.chat.service import ChatCursorError, ChatService
from local_deep_research.database.models import (
    ChatMessage,
    ChatRole,
    ChatSession,
    ChatSessionStatus,
)
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentCollection,
    SourceType,
)
from local_deep_research.database.session_context import get_user_db_session
from local_deep_research.research_library.services.library_service import (
    LibraryService,
)
from local_deep_research.web import routers as routers_pkg
from local_deep_research.web.routers import chat as chat_router
from local_deep_research.web.routers import notes as notes_router
from local_deep_research.web.routers import unified_search as search_router

# A value above 2**63-1: SQLite's integer binding raises OverflowError on
# anything larger, so this is the exact input that turns an unbounded
# offset into a 500 rather than merely a slow query.
OVER_INT64 = 10**19


@pytest.fixture
def client(authenticated_client):
    return authenticated_client


# ===========================================================================
# 1. Shared parse/clamp helper contracts
# ===========================================================================


class TestClampHelperContracts:
    """Parsing contracts for helpers defined within individual routers.

    The shared history/library offset helper is exercised through all three
    live routes below, including the values that actually reach the database.
    """

    @pytest.mark.parametrize(
        ("raw", "expected"),
        [
            (None, 20),  # absent -> default
            ("", 20),  # empty string -> ValueError -> default
            ("abc", 20),  # non-numeric -> default
            ("1.5", 20),  # float literal -> int() raises -> default
            ("0", 1),  # below min -> min
            ("-5", 1),  # negative -> min
            ("50", 50),  # in range -> unchanged
            ("100", 100),  # exactly max -> unchanged
            ("101", 100),  # above max -> max
            (str(10**40), 100),  # astronomical -> max, never reaches SQL
        ],
    )
    def test_chat_parse_int_param_clamps_to_bounds(self, raw, expected):
        """``chat._parse_int_param`` is total: every input yields [min,max]."""
        assert (
            chat_router._parse_int_param(raw, 20, min_val=1, max_val=100)
            == expected
        )

    def test_chat_parse_int_param_clamps_negative_to_zero_floor(self):
        """The offset call site uses min_val=0, not 1 — a distinct floor."""
        assert (
            chat_router._parse_int_param(
                "-1", 0, min_val=0, max_val=chat_router.MAX_OFFSET
            )
            == 0
        )
        assert (
            chat_router._parse_int_param(
                str(OVER_INT64), 0, min_val=0, max_val=chat_router.MAX_OFFSET
            )
            == chat_router.MAX_OFFSET
        )

    @pytest.mark.parametrize(
        ("raw", "expected"),
        [
            (None, 100),
            ("0", 1),
            ("-3", 1),
            ("42", 42),
            ("999999", notes_router.MAX_LIMIT),
        ],
    )
    def test_notes_clamp_limit_bounds(self, raw, expected):
        query = "" if raw is None else f"limit={raw}"
        request = SimpleNamespace(query_params=QueryParams(query))
        assert notes_router._clamp_limit(request, 100) == expected

    @pytest.mark.parametrize("raw", ["abc", "", "1.5", "1e9"])
    def test_notes_clamp_limit_raises_on_non_integer(self, raw):
        """Notes deliberately RAISES so its callers return 400.

        This is the router-family split the census exists to record: notes,
        unified-search and the journals endpoint reject a malformed limit
        with 400, while history, library, chat, news, rag, benchmark and
        context-overflow silently fall back to their default.
        """
        request = SimpleNamespace(query_params=QueryParams(f"limit={raw}"))
        with pytest.raises((ValueError, TypeError)):
            notes_router._clamp_limit(request, 100)

    @pytest.mark.parametrize(
        ("raw", "expected"),
        [
            (None, search_router.DEFAULT_LIMIT),
            ("0", 1),
            ("-9", 1),
            ("10", 10),
            ("999999", search_router.MAX_LIMIT),
        ],
    )
    def test_unified_search_clamps_limit(self, raw, expected):
        query = "q=quantum" if raw is None else f"q=quantum&limit={raw}"
        request = SimpleNamespace(query_params=QueryParams(query))
        q, limit, error = search_router._validated_query_and_limit(request)
        assert error is None
        assert q == "quantum"
        assert limit == expected

    def test_unified_search_rejects_non_integer_limit(self):
        request = SimpleNamespace(
            query_params=QueryParams("q=quantum&limit=abc")
        )
        q, limit, error = search_router._validated_query_and_limit(request)
        assert q is None and limit is None
        assert error is not None
        assert error.status_code == 400


# ===========================================================================
# 2. Route-level clamps, read off the arguments handed to the service
# ===========================================================================


@pytest.fixture
def recorder():
    """Build recording stand-ins for service methods.

    ``recorder.patch(cls, "method", result)`` replaces the bound method with
    one that appends its kwargs to ``recorder.calls`` and returns *result*.
    The route's clamped values are then read straight off the recorded call,
    with no dataset seeded and no response body materialised.
    """

    class _Recorder:
        def __init__(self):
            self.calls = []

        def patch(self, monkeypatch, cls, name, result):
            def _stub(_self, *args, **kwargs):
                self.calls.append((name, args, kwargs))
                return result

            monkeypatch.setattr(cls, name, _stub)

        def kwargs_for(self, name):
            matches = [c[2] for c in self.calls if c[0] == name]
            assert matches, (
                f"{name} was never called — the route returned before "
                f"reaching it, so nothing about its clamp was observed. "
                f"Recorded calls: {[c[0] for c in self.calls]}"
            )
            return matches[-1]

    return _Recorder()


class TestLibraryDocumentsClamp:
    """``GET /library/api/documents`` — limit clamped to [1, 1000].

    SQLite treats ``LIMIT -1`` as "no limit", so an unclamped negative
    limit would stream every Document row (including the large
    ``text_content`` bodies) into memory.
    """

    def test_recorder_captures_service_kwargs(
        self, client, recorder, monkeypatch
    ):
        """Negative control for the recorder used by this whole class.

        Without this, a route that 404s or errors before touching the
        service would make every clamp assertion below vacuously pass.
        """
        recorder.patch(monkeypatch, LibraryService, "get_documents", [])
        resp = client.get("/library/api/documents?limit=7&offset=3")
        assert resp.status_code == 200
        kwargs = recorder.kwargs_for("get_documents")
        assert kwargs["limit"] == 7
        assert kwargs["offset"] == 3

    @pytest.mark.parametrize(
        ("query", "expected_limit"),
        [
            ("", 100),
            ("?limit=999999", 1000),
            ("?limit=-1", 1),
            ("?limit=0", 1),
            ("?limit=abc", 100),
            (f"?limit={OVER_INT64}", 1000),
        ],
    )
    def test_limit_is_clamped_before_sql(
        self, client, recorder, monkeypatch, query, expected_limit
    ):
        recorder.patch(monkeypatch, LibraryService, "get_documents", [])
        client.get(f"/library/api/documents{query}")
        assert recorder.kwargs_for("get_documents")["limit"] == expected_limit

    @pytest.mark.parametrize(
        ("query", "expected_offset"),
        [("?offset=-5", 0), ("?offset=abc", 0), ("?offset=25", 25)],
    )
    def test_offset_lower_bound(
        self, client, recorder, monkeypatch, query, expected_offset
    ):
        recorder.patch(monkeypatch, LibraryService, "get_documents", [])
        client.get(f"/library/api/documents{query}")
        assert recorder.kwargs_for("get_documents")["offset"] == expected_offset


class TestChatSessionListClamp:
    """``GET /api/chat/sessions`` — limit [1,100], offset [0, MAX_OFFSET]."""

    @pytest.mark.parametrize(
        ("query", "expected"),
        [
            ("", {"limit": 20, "offset": 0}),
            ("?limit=999999", {"limit": 100, "offset": 0}),
            ("?limit=0&offset=-4", {"limit": 1, "offset": 0}),
            ("?limit=abc&offset=abc", {"limit": 20, "offset": 0}),
            (
                f"?offset={OVER_INT64}",
                {"limit": 20, "offset": chat_router.MAX_OFFSET},
            ),
        ],
    )
    def test_list_sessions_receives_clamped_paging(
        self, client, recorder, monkeypatch, query, expected
    ):
        recorder.patch(monkeypatch, ChatService, "list_sessions", [])
        resp = client.get(f"/api/chat/sessions{query}")
        assert resp.status_code == 200
        kwargs = recorder.kwargs_for("list_sessions")
        assert {
            "limit": kwargs["limit"],
            "offset": kwargs["offset"],
        } == expected


class TestChatMessagesClamp:
    """``GET /api/chat/sessions/{id}/messages`` — the cursor-paginated leg.

    ``ChatService.get_session_messages`` fetches ``limit + offset`` rows from
    BOTH ``chat_messages`` and ``chat_progress_steps``, so an unbounded
    offset means an unbounded SQL LIMIT on two tables. ``MAX_OFFSET`` is
    what keeps that bounded; the route also asks for ``limit + 1`` rows to
    compute ``has_more`` without a second round trip.
    """

    @pytest.fixture
    def stubbed(self, recorder, monkeypatch):
        recorder.patch(monkeypatch, ChatService, "get_session", {"id": "s"})
        recorder.patch(monkeypatch, ChatService, "get_session_messages", [])
        recorder.patch(
            monkeypatch, ChatService, "get_in_progress_research_id", None
        )
        return recorder

    @pytest.mark.parametrize(
        ("query", "expected_peek_limit", "expected_offset"),
        [
            ("", 51, 0),
            ("?limit=999999", 101, 0),
            ("?limit=0", 2, 0),
            ("?limit=-7", 2, 0),
            ("?limit=abc", 51, 0),
            (f"?offset={OVER_INT64}", 51, chat_router.MAX_OFFSET),
            ("?offset=-1", 51, 0),
        ],
    )
    def test_paging_is_clamped_before_the_service(
        self, client, stubbed, query, expected_peek_limit, expected_offset
    ):
        resp = client.get(f"/api/chat/sessions/abc/messages{query}")
        assert resp.status_code == 200
        kwargs = stubbed.kwargs_for("get_session_messages")
        assert kwargs["limit"] == expected_peek_limit
        assert kwargs["offset"] == expected_offset

    def test_worst_case_row_fetch_stays_bounded(self, client, stubbed):
        """The product of both caps bounds the per-table SQL LIMIT.

        ``get_session_messages`` issues ``LIMIT limit + offset`` per table,
        so the caps must multiply out to something a single worker can
        serve. Pinning the arithmetic keeps a future cap increase from
        silently restoring the unbounded fetch.
        """
        resp = client.get(
            f"/api/chat/sessions/abc/messages?limit=999999&offset={OVER_INT64}"
        )
        assert resp.status_code == 200
        kwargs = stubbed.kwargs_for("get_session_messages")
        assert kwargs["limit"] + kwargs["offset"] == 101 + 1000

    @pytest.mark.parametrize(
        "cursor", ["not-a-timestamp", "2026-13-45", "", "0", "'; DROP TABLE"]
    )
    def test_cursor_is_forwarded_verbatim_and_validated_downstream(
        self, client, stubbed, cursor
    ):
        """The route does NOT validate ``before_created_at`` itself.

        It forwards the raw string; ``ChatService.get_session_messages``
        parses it with ``datetime.fromisoformat`` and rejects a bad one
        (pinned in TestCursorPagination below). This recording service
        deliberately only observes forwarding; real-service HTTP tests
        verify the resulting 400 response.
        """
        resp = client.get(
            f"/api/chat/sessions/abc/messages?before_created_at={cursor}"
        )
        assert resp.status_code == 200
        kwargs = stubbed.kwargs_for("get_session_messages")
        assert kwargs["before_created_at"] == (cursor or None)


class TestJournalsPageClamp:
    """``GET /metrics/api/journals`` — the one route that 400s on a bad page.

    Backed by the bundled read-only reference DB, which may be absent in a
    checkout, so the reference handle is stubbed to keep the test about the
    clamp rather than about bundle availability.
    """

    @pytest.fixture
    def journal_ref(self, monkeypatch):
        calls = []

        class _Ref:
            available = True

            def get_journals_page(self, **kwargs):
                calls.append(kwargs)
                return [], 0

        monkeypatch.setattr(
            "local_deep_research.journal_quality.db.get_journal_reference_db",
            lambda *a, **k: _Ref(),
        )
        return calls

    def test_stub_is_actually_reached(self, client, journal_ref):
        """Negative control: without this the 400 cases below could be
        passing because the reference DB was unavailable (503), not
        because validation rejected the input."""
        resp = client.get("/metrics/api/journals?page=2&per_page=25")
        assert resp.status_code == 200
        assert journal_ref, "get_journals_page was never called"
        assert journal_ref[-1]["page"] == 2
        assert journal_ref[-1]["per_page"] == 25

    @pytest.mark.parametrize(
        ("query", "expected_per_page"),
        [("?per_page=999999", 200), ("?per_page=0", 1), ("?per_page=-5", 1)],
    )
    def test_per_page_clamped(
        self, client, journal_ref, query, expected_per_page
    ):
        resp = client.get(f"/metrics/api/journals{query}")
        assert resp.status_code == 200
        assert journal_ref[-1]["per_page"] == expected_per_page

    @pytest.mark.parametrize(
        "query", ["?page=abc", "?per_page=abc", "?page=1.5"]
    )
    def test_non_integer_paging_is_rejected_with_400(
        self, client, journal_ref, query
    ):
        resp = client.get(f"/metrics/api/journals{query}")
        assert resp.status_code == 400
        assert not journal_ref, (
            "rejected input still reached the reference DB query"
        )

    def test_page_above_max_page_is_rejected_not_silently_clamped(
        self, client, journal_ref
    ):
        """An astronomical page must never reach ``.offset()``."""
        resp = client.get(f"/metrics/api/journals?page={OVER_INT64}")
        assert resp.status_code == 400
        assert not journal_ref


class TestNotesListEchoesItsClamp:
    """``GET /notes/api/notes`` echoes the effective limit/offset it used.

    No stand-in is needed: the envelope reports the post-clamp values, so
    the assertion is on the route's own arithmetic against an empty
    collection.
    """

    @pytest.mark.parametrize(
        ("query", "expected_limit"),
        [
            ("", 100),
            ("?limit=999999", notes_router.MAX_LIMIT),
            ("?limit=0", 1),
            ("?limit=-4", 1),
            (f"?limit={OVER_INT64}", notes_router.MAX_LIMIT),
        ],
    )
    def test_limit_echo_is_clamped(self, client, query, expected_limit):
        resp = client.get(f"/notes/api/notes{query}")
        assert resp.status_code == 200, resp.text
        assert resp.json()["limit"] == expected_limit

    @pytest.mark.parametrize("query", ["?limit=abc", "?offset=abc"])
    def test_non_integer_paging_is_rejected_with_400(self, client, query):
        resp = client.get(f"/notes/api/notes{query}")
        assert resp.status_code == 400
        assert resp.json()["success"] is False

    @pytest.mark.parametrize("raw_offset", ["1000", str(OVER_INT64)])
    def test_offset_past_end_of_result_set_is_clamped_to_total(
        self, client, raw_offset
    ):
        """Notes is the ONLY router that clamps offset to the row count.

        With an empty collection the total is 0, so any offset past the end
        must come back as 0 — the DB is never asked to scan-and-discard,
        and an over-int64 offset never reaches SQLite's integer binding.
        """
        resp = client.get(f"/notes/api/notes?offset={raw_offset}")
        assert resp.status_code == 200, resp.text
        body = resp.json()
        assert body["total"] == 0
        assert body["offset"] == 0


# ===========================================================================
# 3. SQL-level census: what LIMIT actually reached the database
# ===========================================================================


@pytest.fixture
def sql_probe(monkeypatch):
    """Record every ``Query.limit`` / ``Query.offset`` value in the process.

    Wrapping the ORM rather than the routes means routes with an inline
    query (history) and routes that delegate to a service are measured by
    the same instrument, and the assertion is on the bound that reached the
    database rather than on a materialised response body.
    """
    recorded = {"limit": [], "offset": []}
    real_limit = sa_orm.Query.limit
    real_offset = sa_orm.Query.offset

    def _limit(self, value):
        recorded["limit"].append(value)
        return real_limit(self, value)

    def _offset(self, value):
        recorded["offset"].append(value)
        return real_offset(self, value)

    monkeypatch.setattr(sa_orm.Query, "limit", _limit)
    monkeypatch.setattr(sa_orm.Query, "offset", _offset)
    return recorded


class TestSqlLevelLimitCensus:
    @pytest.fixture
    def rag_collection(self, client):
        """Eleven real rows exercise partial pages as well as exact divisors."""
        username = client.get("/auth/check").json()["username"]
        collection_id = str(uuid.uuid4())
        source_type_id = str(uuid.uuid4())
        with get_user_db_session(username) as db:
            db.add(Collection(id=collection_id, name="Pagination census"))
            db.add(
                SourceType(
                    id=source_type_id,
                    name="pagination_census",
                    display_name="Pagination census",
                )
            )
            db.flush()
            for i in range(11):
                document_id = str(uuid.uuid4())
                db.add(
                    Document(
                        id=document_id,
                        source_type_id=source_type_id,
                        document_hash=f"{i:064x}",
                        title=f"Document {i}",
                        file_size=0,
                        file_type="txt",
                    )
                )
                db.flush()
                db.add(
                    DocumentCollection(
                        document_id=document_id,
                        collection_id=collection_id,
                    )
                )
            db.commit()
        return collection_id

    @pytest.mark.parametrize("page", [2, 3])
    @pytest.mark.parametrize(
        ("raw_per_page", "expected", "expected_pages"),
        [
            ("1", 1, 11),
            ("2", 2, 6),
            ("9", 9, 2),
            ("10", 10, 2),
            ("100", 100, 1),
            ("101", 100, 1),
            (str(OVER_INT64), 100, 1),
            ("0", 1, 11),
            ("-5", 1, 11),
            ("abc", 50, 1),
            ("", 50, 1),
            (None, 50, 1),
        ],
    )
    def test_rag_page_size_controls_sql_limit_and_offset(
        self,
        client,
        rag_collection,
        sql_probe,
        raw_per_page,
        expected,
        expected_pages,
        page,
    ):
        params = {"page": str(page), "collection_id": rag_collection}
        if raw_per_page is not None:
            params["per_page"] = raw_per_page
        response = client.get("/library/api/rag/documents", params=params)

        assert response.status_code == 200, response.text
        pagination = response.json()["pagination"]
        assert pagination["page"] == page
        assert pagination["per_page"] == expected
        assert pagination["total"] == 11
        assert pagination["pages"] == expected_pages
        assert sql_probe["limit"], "The documents query must reach SQL"
        assert max(sql_probe["limit"]) == expected
        assert sql_probe["offset"] == [(page - 1) * expected], sql_probe

    def test_probe_observes_a_known_limit(self, client, sql_probe):
        """Negative control for ``sql_probe``.

        ``/history/api?limit=37`` must be seen as a literal 37 on the ORM
        query. If the probe ever stops firing (an ORM upgrade that routes
        ``.limit()`` elsewhere, a route rewritten to Core ``select()``),
        every census assertion below would pass on an empty list — this
        test fails first and says so.
        """
        resp = client.get("/history/api?limit=37&offset=5")
        assert resp.status_code == 200
        assert 37 in sql_probe["limit"], sql_probe
        assert 5 in sql_probe["offset"], sql_probe

    @pytest.mark.parametrize(
        ("path", "query", "cap"),
        [
            # (route, hostile query, widest LIMIT the handler may emit)
            ("/history/api", "?limit=999999", 500),
            ("/history/api", f"?limit={OVER_INT64}", 500),
            ("/api/history", "?limit=999999", 500),
            ("/api/history", f"?limit={OVER_INT64}", 500),
            ("/library/api/documents", "?limit=999999", 1000),
            ("/library/api/rag/documents", "?per_page=999999", 100),
            # ?per_page clamps to 500, but the SAME handler also runs a
            # time-series query with a hard-coded .limit(1000) that is not
            # driven by any query param. 1000 is therefore the widest LIMIT
            # this route can emit — still far below the 999999 an unclamped
            # per_page would produce, so the guard keeps its teeth.
            ("/api/context-overflow", "?per_page=999999", 1000),
        ],
    )
    def test_no_route_lets_an_oversized_limit_reach_sql(
        self, client, sql_probe, path, query, cap
    ):
        """The DoS question: can a client force an unbounded SELECT?

        No LIMIT above the handler's widest legitimate value may reach the
        database. An unclamped parameter would show up as the raw 999999
        (or the over-int64 value), orders of magnitude above every cap
        here.
        """
        resp = client.get(f"{path}{query}")
        assert resp.status_code < 500, resp.text
        assert sql_probe["limit"], (
            f"{path}{query} issued no LIMIT at all — either it returned "
            f"before querying, or it loads the table unbounded"
        )
        assert max(sql_probe["limit"]) <= cap, (
            f"{path}{query} reached SQL with LIMIT "
            f"{max(sql_probe['limit'])}, above the {cap} ceiling"
        )

    def test_context_overflow_per_page_clamps_to_five_hundred(
        self, client, sql_probe
    ):
        """Pin the clamped ``per_page`` itself, not just the ceiling.

        The handler's unrelated hard-coded ``.limit(1000)`` sets the
        ceiling above this route's own ``per_page`` cap, so the cap needs
        its own assertion: the clamped 500 must be among the LIMITs the
        request issued.
        """
        resp = client.get("/api/context-overflow?per_page=999999")
        assert resp.status_code < 400, resp.text
        assert 500 in sql_probe["limit"], (
            f"?per_page=999999 did not produce the clamped LIMIT 500; "
            f"observed {sql_probe['limit']}"
        )

    #: ``_MAX_PAGE`` (10_000) times the largest ``per_page`` any of these
    #: routes accepts (500) — the widest OFFSET a page-style route can be
    #: talked into. Far below SQLite's 2**63 binding limit, which is the
    #: point of the page cap.
    MAX_DERIVED_OFFSET = 10_000 * 500

    @pytest.mark.parametrize(
        ("path", "query"),
        [
            ("/library/api/rag/documents", f"?page={OVER_INT64}"),
            ("/api/context-overflow", f"?page={OVER_INT64}"),
        ],
    )
    def test_page_style_routes_bound_the_derived_offset(
        self, client, sql_probe, path, query
    ):
        """``page`` is multiplied into an OFFSET, so it needs its own cap.

        Both routes clamp to ``_MAX_PAGE`` = 10_000 exactly because an
        uncapped ``?page=10**19`` reaches ``.offset()`` and raises
        OverflowError from SQLite's 64-bit integer binding.
        """
        resp = client.get(f"{path}{query}")
        assert resp.status_code < 500, resp.text
        assert sql_probe["offset"], f"{path}{query} issued no OFFSET"
        widest = max(sql_probe["offset"])
        assert widest < 2**63, (
            f"{path}{query} reached SQLite with OFFSET {widest}, which its "
            f"64-bit integer binding rejects outright"
        )
        assert widest <= self.MAX_DERIVED_OFFSET


# ===========================================================================
# 4. Very large offsets are bounded before SQLite sees them
# ===========================================================================


@pytest.mark.parametrize(
    "path", ["/history/api", "/api/history", "/library/api/documents"]
)
def test_offset_above_int64_does_not_500(client, path):
    resp = client.get(f"{path}?offset={OVER_INT64}")
    assert resp.status_code < 500, (
        f"{path} returned {resp.status_code} for an over-int64 offset"
    )


@pytest.mark.parametrize(
    "path", ["/history/api", "/api/history", "/library/api/documents"]
)
@pytest.mark.parametrize(
    ("raw_offset", "expected"),
    [
        (None, 0),
        ("", 0),
        ("1.5", 0),
        ("-1", 0),
        ("0", 0),
        ("37", 37),
        ("10000", 10000),
        ("10001", None),
        (str(OVER_INT64), None),
        ("abc", 0),
    ],
)
def test_offset_bounds_reach_the_database(
    client, sql_probe, path, raw_offset, expected
):
    """Keep ordinary pages and fallback behavior while bounding SQL work."""
    query = "" if raw_offset is None else f"?offset={raw_offset}"
    resp = client.get(f"{path}{query}")
    if expected is None:
        assert resp.status_code == 400, resp.text
        assert not sql_probe["offset"], sql_probe
        assert resp.json()["error"] == "Offset must not exceed 10000"
    else:
        assert resp.status_code == 200, resp.text
        assert sql_probe["offset"], f"{path} issued no offset query"
        assert set(sql_probe["offset"]) == {expected}, sql_probe


# ===========================================================================
# 5. Cursor pagination (chat) — before_created_at / before_id
# ===========================================================================


@contextmanager
def _service_db(SessionLocal):
    """Point ChatService's per-user encrypted DB at the plain test DB."""

    @contextmanager
    def _managed():
        with SessionLocal() as db:
            yield db

    with patch(
        "local_deep_research.chat.service.get_user_db_session",
        side_effect=lambda *a, **k: _managed(),
    ):
        yield


class TestCursorPagination:
    """``before_created_at`` / ``before_id`` on ``get_session_messages``.

    Five rows are seeded — enough to distinguish "older than the cursor"
    from "newest page", which is the whole contract.
    """

    @staticmethod
    def _seed(db, count=5):
        session_id = str(uuid.uuid4())
        db.add(
            ChatSession(
                id=session_id,
                title="cursor test",
                status=ChatSessionStatus.ACTIVE.value,
                message_count=count,
            )
        )
        base = datetime(2026, 1, 1, tzinfo=timezone.utc)
        ids = []
        for i in range(count):
            mid = str(uuid.uuid4())
            ids.append(mid)
            db.add(
                ChatMessage(
                    id=mid,
                    session_id=session_id,
                    role=ChatRole.USER.value,
                    message_type="query",
                    content=f"m{i}",
                    sequence_number=i + 1,
                    created_at=base + timedelta(minutes=i),
                )
            )
        db.commit()
        return session_id, ids, base

    def test_cursor_returns_only_strictly_older_rows(
        self, setup_database_for_all_tests
    ):
        SessionLocal = setup_database_for_all_tests
        service = ChatService(username=f"u_{uuid.uuid4().hex[:8]}")
        with _service_db(SessionLocal):
            with SessionLocal() as db:
                session_id, _ids, base = self._seed(db)
            cutoff = (base + timedelta(minutes=2)).isoformat()
            page = service.get_session_messages(
                session_id, limit=10, before_created_at=cutoff
            )
        assert [m["content"] for m in page] == ["m0", "m1"]

    @pytest.mark.parametrize(
        "cursor", ["not-a-timestamp", "2026-13-45T00:00:00", "0", "banana"]
    )
    def test_malformed_cursor_is_rejected(
        self, setup_database_for_all_tests, cursor
    ):
        """A malformed cursor never silently returns the newest page."""
        SessionLocal = setup_database_for_all_tests
        service = ChatService(username=f"u_{uuid.uuid4().hex[:8]}")
        with _service_db(SessionLocal):
            with SessionLocal() as db:
                session_id, _ids, _base = self._seed(db)
            with pytest.raises(ChatCursorError):
                service.get_session_messages(
                    session_id, limit=10, before_created_at=cursor
                )
            page = service.get_session_messages(session_id, limit=10)
        assert [m["content"] for m in page] == ["m0", "m1", "m2", "m3", "m4"]

    def test_before_id_without_a_timestamp_is_rejected(
        self, setup_database_for_all_tests
    ):
        """The id is a tie-breaker and requires the timestamp half."""
        SessionLocal = setup_database_for_all_tests
        service = ChatService(username=f"u_{uuid.uuid4().hex[:8]}")
        with _service_db(SessionLocal):
            with SessionLocal() as db:
                session_id, ids, _base = self._seed(db)
            with pytest.raises(ChatCursorError):
                service.get_session_messages(
                    session_id, limit=10, before_id=ids[0]
                )
            assert len(service.get_session_messages(session_id, limit=10)) == 5

    def test_composite_cursor_keeps_same_timestamp_rows_from_being_lost(
        self, setup_database_for_all_tests
    ):
        """Rows sharing the cursor timestamp are split by id, not dropped.

        With a bare timestamp cursor the ``created_at < cutoff`` filter
        drops every row at the boundary instant; adding ``before_id``
        resolves its sequence/kind and includes the earlier tied entries so the
        page boundary loses nothing.
        """
        SessionLocal = setup_database_for_all_tests
        service = ChatService(username=f"u_{uuid.uuid4().hex[:8]}")
        stamp = datetime(2026, 1, 1, tzinfo=timezone.utc)
        with _service_db(SessionLocal):
            with SessionLocal() as db:
                session_id = str(uuid.uuid4())
                db.add(
                    ChatSession(
                        id=session_id,
                        title="tie",
                        status=ChatSessionStatus.ACTIVE.value,
                        message_count=3,
                    )
                )
                # Deterministic lexicographic order: a < b < c.
                for i, mid in enumerate(["aaa", "bbb", "ccc"]):
                    db.add(
                        ChatMessage(
                            id=mid,
                            session_id=session_id,
                            role=ChatRole.USER.value,
                            message_type="query",
                            content=mid,
                            sequence_number=i + 1,
                            created_at=stamp,
                        )
                    )
                db.commit()

            bare = service.get_session_messages(
                session_id, limit=10, before_created_at=stamp.isoformat()
            )
            composite = service.get_session_messages(
                session_id,
                limit=10,
                before_created_at=stamp.isoformat(),
                before_id="ccc",
            )
        assert bare == [], "bare timestamp cursor must exclude the boundary"
        assert [m["content"] for m in composite] == ["aaa", "bbb"]


# ===========================================================================
# 6. Static census: every pagination query param in every router
# ===========================================================================
#
# The behavioural sections above sample the routes one HTTP call at a time.
# This section is the exhaustive half: it reads the router sources and
# enumerates EVERY ``request.query_params.get("limit"/"offset"/"page"/
# "per_page")`` in a route handler, together with the kind of upper bound
# in force on it. A new paginated route, or a clamp deleted from an existing
# one, changes the survey and fails here by name — without booting the app
# or seeding a row.

PAGINATION_PARAM_NAMES = frozenset({"limit", "offset", "page", "per_page"})

#: Module-level helpers that parse-and-clamp a ``?limit`` themselves. A call
#: to one of these is a binding of ``limit`` even though the router source
#: never names ``query_params`` at that line.
LIMIT_CLAMP_HELPERS = frozenset({"_clamp_limit"})

#: Helpers with fixed ceilings, verified by the behavioural tests above.
FIXED_BOUND_HELPERS = LIMIT_CLAMP_HELPERS | {"parse_bounded_offset"}

#: Helper whose ``max_val=`` keyword supplies the ceiling.
BOUNDED_HELPERS = frozenset({"_parse_int_param"})

_FUNC_NODES = (ast.FunctionDef, ast.AsyncFunctionDef)


def _query_param_name(node):
    """``request.query_params.get("x", ...)`` -> ``"x"``, else None."""
    if not isinstance(node, ast.Call):
        return None
    func = node.func
    if not (isinstance(func, ast.Attribute) and func.attr == "get"):
        return None
    owner = func.value
    if not (isinstance(owner, ast.Attribute) and owner.attr == "query_params"):
        return None
    if node.args and isinstance(node.args[0], ast.Constant):
        return node.args[0].value
    return None


def _find_call(node, name):
    """First ``name(...)`` call anywhere under *node* (bare or attribute)."""
    for sub in ast.walk(node):
        if isinstance(sub, ast.Call):
            func = sub.func
            if isinstance(func, ast.Name) and func.id == name:
                return sub
            if isinstance(func, ast.Attribute) and func.attr == name:
                return sub
    return None


def _binding_param(value_node):
    """The pagination param *value_node* reads, or None."""
    for helper in LIMIT_CLAMP_HELPERS:
        if _find_call(value_node, helper) is not None:
            return "limit"
    for sub in ast.walk(value_node):
        name = _query_param_name(sub)
        if name in PAGINATION_PARAM_NAMES:
            return name
    return None


def _upper_bound_kind(func_node, binding, variables):
    """Classify the ceiling on *variables* inside *func_node*.

    ``"helper"``  delegated to a helper with a fixed ceiling or an explicit
                  ``max_val`` argument;
    ``"min"``     an explicit ``min(...)`` in an assignment to the variable
                  or to something derived from it;
    ``"guard"``   an ``if <var> > ...:`` statement (reject or clamp);
    ``"none"``    no upper bound anywhere in the handler.
    """
    for helper in FIXED_BOUND_HELPERS:
        if _find_call(binding.value, helper) is not None:
            return "helper"
    for helper in BOUNDED_HELPERS:
        call = _find_call(binding.value, helper)
        if call is not None and any(
            kw.arg == "max_val" for kw in call.keywords
        ):
            return "helper"
    for stmt in ast.walk(func_node):
        if (
            isinstance(stmt, ast.Assign)
            and any(
                isinstance(t, ast.Name) and t.id in variables
                for t in stmt.targets
            )
            and _find_call(stmt.value, "min") is not None
        ):
            return "min"
    for stmt in ast.walk(func_node):
        if isinstance(stmt, ast.If) and isinstance(stmt.test, ast.Compare):
            test = stmt.test
            if (
                isinstance(test.left, ast.Name)
                and test.left.id in variables
                and any(isinstance(op, ast.Gt) for op in test.ops)
            ):
                return "guard"
    return "none"


def _derived_names(func_node, seed):
    """Fixpoint closure of *seed* over ``NAME = <expr mentioning a member>``.

    ``research.get_research_logs`` binds the raw string to ``_limit_raw`` and
    clamps a *different* name (``limit = max(1, min(limit, CAP))``), so the
    ceiling is only visible if the alias is followed.
    """
    names = set(seed)
    changed = True
    while changed:
        changed = False
        for stmt in ast.walk(func_node):
            if not isinstance(stmt, ast.Assign):
                continue
            mentioned = {
                n.id for n in ast.walk(stmt.value) if isinstance(n, ast.Name)
            }
            if not (mentioned & names):
                continue
            for target in stmt.targets:
                if isinstance(target, ast.Name) and target.id not in names:
                    names.add(target.id)
                    changed = True
    return names


def survey_module(source, module_name):
    """Map ``(module, handler, param)`` -> upper-bound kind for one router."""
    tree = ast.parse(source, module_name)
    found = {}
    for func in tree.body:
        if not isinstance(func, _FUNC_NODES):
            continue
        for stmt in ast.walk(func):
            if not isinstance(stmt, ast.Assign):
                continue
            param = _binding_param(stmt.value)
            if param is None:
                continue
            target = stmt.targets[0]
            if not isinstance(target, ast.Name):
                continue
            variables = _derived_names(func, {target.id})
            found[(module_name, func.name, param)] = _upper_bound_kind(
                func, stmt, variables
            )
    return found


@functools.lru_cache(maxsize=1)
def survey_routers():
    """Survey every module in ``local_deep_research.web.routers``."""
    routers_dir = Path(routers_pkg.__file__).parent
    result = {}
    for path in sorted(routers_dir.glob("*.py")):
        result.update(
            survey_module(path.read_text(encoding="utf-8"), path.name)
        )
    return result


#: The census, frozen. Keys are (router module, handler, query param);
#: values are the kind of ceiling in force (see ``_upper_bound_kind``).
#: All user-controlled page sizes and offsets must have an upper bound.
EXPECTED_PAGINATION_CENSUS = {
    ("benchmark.py", "get_benchmark_results", "limit"): "min",
    ("chat.py", "list_sessions", "limit"): "helper",
    ("chat.py", "list_sessions", "offset"): "helper",
    ("chat.py", "get_messages", "limit"): "helper",
    ("chat.py", "get_messages", "offset"): "helper",
    (
        "context_overflow_api.py",
        "get_context_overflow_metrics",
        "page",
    ): "min",
    (
        "context_overflow_api.py",
        "get_context_overflow_metrics",
        "per_page",
    ): "min",
    ("history.py", "get_history", "limit"): "min",
    ("history.py", "get_history", "offset"): "helper",
    ("history.py", "get_research_logs", "limit"): "min",
    ("library.py", "library_page", "page"): "min",
    ("library.py", "download_manager_page", "page"): "min",
    ("library.py", "get_documents", "limit"): "min",
    ("library.py", "get_documents", "offset"): "helper",
    ("metrics.py", "api_journal_quality", "page"): "min",
    ("metrics.py", "api_journal_quality", "per_page"): "min",
    ("news_flask_api.py", "get_news_feed", "limit"): "min",
    ("news_flask_api.py", "get_subscription_history", "limit"): "min",
    ("notes.py", "list_notes", "limit"): "helper",
    ("notes.py", "list_notes", "offset"): "guard",
    ("notes.py", "get_note_versions", "limit"): "helper",
    ("notes.py", "get_note_versions", "offset"): "guard",
    ("notes.py", "get_similar_notes", "limit"): "helper",
    ("notes.py", "get_suggested_links", "limit"): "helper",
    ("notes.py", "search_notes_for_linking", "limit"): "helper",
    ("notes.py", "semantic_search_notes", "limit"): "helper",
    ("rag.py", "get_documents", "page"): "min",
    ("rag.py", "get_documents", "per_page"): "min",
    ("research.py", "get_history", "limit"): "min",
    ("research.py", "get_history", "offset"): "helper",
    ("research.py", "get_research_logs", "limit"): "min",
    ("unified_search.py", "_validated_query_and_limit", "limit"): "min",
}


class TestSurveyorSelfTest:
    """Negative controls for the survey. Without these the census tests
    below could pass on an analyser that classifies everything as bounded
    (or that finds nothing at all)."""

    CLAMPED = (
        "def handler(request):\n"
        "    limit = int(request.query_params.get('limit', 50))\n"
        "    limit = max(1, min(limit, 200))\n"
    )
    UNCLAMPED = (
        "def handler(request):\n"
        "    limit = int(request.query_params.get('limit', 50))\n"
        "    limit = max(1, limit)\n"
    )
    ALIASED = (
        "def handler(request):\n"
        "    raw = request.query_params.get('limit')\n"
        "    n = int(raw)\n"
        "    n = min(n, 500)\n"
    )
    GUARDED = (
        "def handler(request):\n"
        "    page = int(request.query_params.get('page', 1))\n"
        "    if page > 10000:\n"
        "        return 400\n"
    )

    def test_detects_a_min_clamp(self):
        assert survey_module(self.CLAMPED, "m.py") == {
            ("m.py", "handler", "limit"): "min"
        }

    def test_detects_a_missing_clamp(self):
        assert survey_module(self.UNCLAMPED, "m.py") == {
            ("m.py", "handler", "limit"): "none"
        }

    def test_follows_an_alias_to_the_clamp(self):
        assert survey_module(self.ALIASED, "m.py") == {
            ("m.py", "handler", "limit"): "min"
        }

    def test_detects_a_reject_guard(self):
        assert survey_module(self.GUARDED, "m.py") == {
            ("m.py", "handler", "page"): "guard"
        }

    @pytest.mark.parametrize(
        ("parser", "expected"),
        [
            ("parse_bounded_offset", "helper"),
            ("parse_offset", "none"),
            ("int", "none"),
        ],
    )
    def test_only_known_offset_helper_is_bounded(self, parser, expected):
        source = (
            "def handler(request):\n"
            f"    offset = {parser}(request.query_params.get('offset'))\n"
        )
        assert survey_module(source, "m.py") == {
            ("m.py", "handler", "offset"): expected
        }

    def test_ignores_non_pagination_query_params(self):
        source = (
            "def handler(request):\n"
            "    q = request.query_params.get('search', '')\n"
        )
        assert survey_module(source, "m.py") == {}


class TestPaginationCensus:
    def test_inventory_matches_the_frozen_census(self):
        """Every paginated query param in every router, and its ceiling.

        A route added without a clamp review, a clamp deleted, or a handler
        renamed all show up here as a set difference naming the exact
        (module, handler, param) that moved.
        """
        actual = survey_routers()
        assert actual == EXPECTED_PAGINATION_CENSUS

    @pytest.mark.parametrize(
        "key",
        [
            k
            for k, v in sorted(EXPECTED_PAGINATION_CENSUS.items())
            if k[2] in {"limit", "page", "per_page"}
        ],
    )
    def test_every_page_size_param_is_upper_bounded(self, key):
        """No route may let a client choose an unbounded page size.

        On a single-worker uvicorn deployment an unclamped ``?limit=`` is a
        one-request denial of service: an unbounded SELECT plus an
        unbounded JSON body while the only worker is blocked.
        """
        actual = survey_routers()
        assert actual.get(key) != "none", (
            f"{key[0]}::{key[1]} reads ?{key[2]} with no upper bound"
        )

    @pytest.mark.parametrize(
        "key",
        [
            pytest.param(("chat.py", "list_sessions", "offset")),
            pytest.param(("chat.py", "get_messages", "offset")),
            pytest.param(("notes.py", "list_notes", "offset")),
            pytest.param(("notes.py", "get_note_versions", "offset")),
            pytest.param(
                ("history.py", "get_history", "offset"),
            ),
            pytest.param(
                ("library.py", "get_documents", "offset"),
            ),
            pytest.param(
                ("research.py", "get_history", "offset"),
            ),
        ],
    )
    def test_every_offset_param_is_upper_bounded(self, key):
        actual = survey_routers()
        assert actual.get(key) != "none", (
            f"{key[0]}::{key[1]} reads ?offset with no upper bound"
        )