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.

24.8 KB
Raw Blame
"""Extraction-bound contracts for the PDF upload service.

The upload service walks pages lazily and stops at the shared page,
character, and thread-CPU ceilings in ``utilities/pdf_extraction_limits``.
It closes each page and hands it to ``utilities/pdf_stream_release`` so
cached decoded objects do not accumulate across a long document. The
arXiv engine now uses the shared downloader (#6691), whose bounds are
covered by its own tests.

Most tests hand the walkers MagicMock PDFs carrying a ``.pages`` list;
the lazy walk needs a real pdfminer document, so those tests patch
``iter_pdfplumber_pages`` to walk the list. The tests that pin the lazy
walk itself use small synthetic PDFs built in memory.
"""

from __future__ import annotations

import io
from itertools import chain, repeat
from unittest.mock import MagicMock, Mock

import pytest

TOTAL_PAGES = 600


def _pages(n: int, text: str = "page text"):
    pages = []
    for _ in range(n):
        page = MagicMock()
        page.extract_text.return_value = text
        pages.append(page)
    return pages


SERVICE_MODULE = "local_deep_research.web.services.pdf_extraction_service"


def _walk_mock_pages(mocker, module: str):
    """Make *module*'s lazy page walk iterate a MagicMock PDF's
    ``.pages`` list (the real walk needs a pdfminer document)."""
    mocker.patch(
        f"{module}.iter_pdfplumber_pages",
        side_effect=lambda pdf, releaser=None: iter(pdf.pages),
    )


def _text_pdf(
    n_pages: int,
    text: str = "Hello",
    *,
    count: str | None = None,
    extra_objects: dict[int, bytes] | None = None,
    resources_entries: int = 0,
    compressed: dict[int, tuple[int, int]] | None = None,
) -> bytes:
    """A valid PDF of *n_pages* pages that all show *text* (one shared
    content stream and font), with a flat page tree declaring
    /Count n_pages.

    *count* replaces the /Count value written into the page-tree root
    (e.g. an indirect reference into *extra_objects*, which are added
    as numbered objects). With *resources_entries*, every page gets its
    own indirect /Resources dictionary carrying a /ProcSet array of that
    many entries, which pdfminer resolves while building the page.

    With *compressed* (object number -> (object-stream number, index)),
    the cross-reference is written as an uncompressed xref stream in
    which those objects are type-2 entries (inside an object stream)
    rather than as a classic xref table."""
    content = f"BT /F1 12 Tf 72 700 Td ({text}) Tj ET".encode()
    objects = {
        1: b"<< /Type /Catalog /Pages 2 0 R >>",
        3: b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
        4: b"<< /Length %d >>\nstream\n" % len(content)
        + content
        + b"\nendstream",
    }
    first_page = 5
    kids = " ".join(f"{first_page + i} 0 R" for i in range(n_pages))
    count_value = str(n_pages) if count is None else count
    objects[2] = (
        f"<< /Type /Pages /Kids [{kids}] /Count {count_value} >>".encode()
    )
    first_resources = first_page + n_pages
    for i in range(n_pages):
        if resources_entries:
            procset = b" ".join([b"/PDF"] * resources_entries)
            objects[first_resources + i] = (
                b"<< /Font << /F1 3 0 R >> /ProcSet [" + procset + b"] >>"
            )
            resources = b"%d 0 R" % (first_resources + i)
        else:
            resources = b"<< /Font << /F1 3 0 R >> >>"
        objects[first_page + i] = (
            b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] "
            b"/Contents 4 0 R /Resources " + resources + b" >>"
        )
    objects.update(extra_objects or {})
    out = io.BytesIO()
    out.write(b"%PDF-1.4\n")
    offsets = {}
    for num in sorted(objects):
        offsets[num] = out.tell()
        out.write(b"%d 0 obj\n" % num + objects[num] + b"\nendobj\n")
    if compressed:
        xref_id = max([*objects, *compressed]) + 1
        size = xref_id + 1
        xref = out.tell()
        offsets[xref_id] = xref
        rows = []
        for num in range(size):
            if num in compressed:
                strm, index = compressed[num]
                rows.append(
                    b"\x02" + strm.to_bytes(4, "big") + index.to_bytes(2, "big")
                )
            elif num in offsets:
                rows.append(
                    b"\x01" + offsets[num].to_bytes(4, "big") + b"\x00\x00"
                )
            else:
                rows.append(b"\x00" * 7)
        data = b"".join(rows)
        out.write(
            b"%d 0 obj\n<< /Type /XRef /Size %d /W [1 4 2] /Root 1 0 R "
            b"/Length %d >>\nstream\n"
            % (xref_id, size, len(data))
            + data
            + b"\nendstream\nendobj\nstartxref\n%d\n%%%%EOF\n" % xref
        )
        return out.getvalue()
    size = max(objects) + 1
    xref = out.tell()
    out.write(b"xref\n0 %d\n0000000000 65535 f \n" % size)
    for num in range(1, size):
        if num in offsets:
            out.write(b"%010d 00000 n \n" % offsets[num])
        else:
            out.write(b"0000000000 65535 f \n")
    out.write(
        b"trailer\n<< /Size %d /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n"
        % (size, xref)
    )
    return out.getvalue()


def _count_built_pages(mocker):
    """Count the pdfminer PDFPage objects built from here on (what
    pdfplumber's eager ``PDF.pages`` builds for every page)."""
    from pdfminer.pdfpage import PDFPage

    return mocker.spy(PDFPage, "__init__")


class TestUploadPathExtractionService:
    def _service(self):
        from local_deep_research.web.services.pdf_extraction_service import (
            PDFExtractionService,
        )

        return PDFExtractionService()

    def _extract(self, mocker, pages):
        mock_pdf = MagicMock()
        mock_pdf.pages = pages
        mock_pdf.__enter__ = Mock(return_value=mock_pdf)
        mock_pdf.__exit__ = Mock(return_value=False)
        mocker.patch(
            "local_deep_research.web.services.pdf_extraction_service."
            "pdfplumber.open",
            return_value=mock_pdf,
        )
        _walk_mock_pages(mocker, SERVICE_MODULE)
        return self._service().extract_text_and_metadata(
            b"%PDF-fake", "hostile.pdf"
        )

    @pytest.mark.parametrize("ceiling", [5, 6])
    def test_no_extraction_when_separator_leaves_no_character_budget(
        self, monkeypatch, ceiling
    ):
        monkeypatch.setattr(
            f"{SERVICE_MODULE}.MAX_PDF_EXTRACTED_CHARS", ceiling
        )
        pdf = _text_pdf(
            2,
            "Hello",
            extra_objects={
                6: (
                    b"<< /Type /Page /Parent 2 0 R "
                    b"/MediaBox [0 0 612 792] /Contents 8 0 R "
                    b"/Resources << /Font << /F1 3 0 R >> >> >>"
                ),
                8: (
                    b"<< /Filter /NotAFilter /Length 5 >>\nstream\n"
                    b"hello\nendstream"
                ),
            },
        )

        result = self._service().extract_text_and_metadata(pdf, "paper.pdf")

        assert result["success"] is True
        assert result["text"] == "Hello"
        assert result["truncated"] is True
        assert result["pages"] == 2

    def test_extracts_when_one_character_fits_after_separator(
        self, monkeypatch
    ):
        monkeypatch.setattr(f"{SERVICE_MODULE}.MAX_PDF_EXTRACTED_CHARS", 7)

        result = self._service().extract_text_and_metadata(
            _text_pdf(2, "Hello"), "paper.pdf"
        )

        assert result["success"] is True
        assert result["text"] == "Hello\nH"
        assert result["truncated"] is True

    def test_pathological_page_count_is_not_fully_walked(self, mocker):
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTION_PAGES,
        )

        pages = _pages(TOTAL_PAGES)

        result = self._extract(mocker, pages)

        walked = sum(1 for page in pages if page.extract_text.called)
        assert 0 < walked < TOTAL_PAGES, (
            f"extraction walked {walked} of {TOTAL_PAGES} pages — no page "
            "ceiling is in force on the upload path"
        )
        assert result["success"]
        assert result["truncated"] is True
        # The pages after the ceiling are never built: the count
        # reported is the pages the walk reached, a lower bound.
        assert result["pages"] == MAX_PDF_EXTRACTION_PAGES + 1

    def test_page_ceiling_is_exact(self, mocker):
        """Exactly MAX_PDF_EXTRACTION_PAGES pages are walked; the next
        one is not."""
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTION_PAGES,
        )

        pages = _pages(MAX_PDF_EXTRACTION_PAGES + 1)

        result = self._extract(mocker, pages)

        assert all(
            page.extract_text.called
            for page in pages[:MAX_PDF_EXTRACTION_PAGES]
        )
        assert not pages[MAX_PDF_EXTRACTION_PAGES].extract_text.called
        assert result["truncated"] is True

    def test_document_at_page_ceiling_is_not_truncated(self, mocker):
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTION_PAGES,
        )

        pages = _pages(MAX_PDF_EXTRACTION_PAGES)

        result = self._extract(mocker, pages)

        assert all(page.extract_text.called for page in pages)
        assert result["truncated"] is False
        assert result["text"] == "\n".join(["page text"] * len(pages))

    def test_decompression_sized_text_is_capped(self, mocker):
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTED_CHARS,
        )

        pages = _pages(100, text="x" * 200_000)

        result = self._extract(mocker, pages)

        assert result["success"]
        assert result["truncated"] is True, (
            "character-ceiling trip was not reported on the upload path"
        )
        assert len(result["text"]) == MAX_PDF_EXTRACTED_CHARS, (
            "extraction did not return exactly the character ceiling's "
            "worth of text, separators included"
        )

    def test_first_page_over_the_ceiling_yields_its_prefix(self, mocker):
        """A single page larger than the ceiling is truncated, not
        dropped: the upload succeeds with the budget's worth of text."""
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTED_CHARS,
        )

        pages = _pages(1, text="x" * (MAX_PDF_EXTRACTED_CHARS + 1)) + _pages(1)

        result = self._extract(mocker, pages)

        assert result["success"] is True, result["error"]
        assert result["truncated"] is True
        assert result["text"] == "x" * MAX_PDF_EXTRACTED_CHARS
        assert not pages[1].extract_text.called

    def test_text_of_exactly_the_ceiling_is_kept_whole(self, mocker):
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTED_CHARS,
        )

        pages = _pages(1, text="x" * MAX_PDF_EXTRACTED_CHARS)

        result = self._extract(mocker, pages)

        assert result["success"] is True
        assert result["truncated"] is False
        assert len(result["text"]) == MAX_PDF_EXTRACTED_CHARS

    def test_join_separators_count_against_the_ceiling(self, mocker):
        """Two halves of the ceiling fit as text alone, but not with the
        "\\n" that joins them: the output must still not exceed it."""
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTED_CHARS,
        )

        half = MAX_PDF_EXTRACTED_CHARS // 2
        pages = _pages(2, text="x" * half)

        result = self._extract(mocker, pages)

        assert len(result["text"]) == MAX_PDF_EXTRACTED_CHARS
        assert result["text"] == "x" * half + "\n" + "x" * (half - 1)
        assert result["truncated"] is True

    def test_each_page_is_closed_after_extraction(self, mocker):
        pages = _pages(3)

        self._extract(mocker, pages)

        for page in pages:
            page.close.assert_called_once_with()

    def test_truncated_is_reported_on_every_result(self, mocker):
        no_text = self._extract(mocker, _pages(2, text=""))
        assert no_text["success"] is False
        assert no_text["truncated"] is False

        mocker.patch(
            "local_deep_research.web.services.pdf_extraction_service."
            "pdfplumber.open",
            side_effect=Exception("broken pdf"),
        )
        failed = self._service().extract_text_and_metadata(
            b"%PDF-fake", "hostile.pdf"
        )
        assert failed["success"] is False
        assert failed["truncated"] is False


class TestUploadPathLazyWalkAndBudgets:
    """Behaviour wired in from #6459/#6782: the lazy page walk, the CPU
    budget and the cache releaser on the upload path."""

    def _service(self):
        from local_deep_research.web.services.pdf_extraction_service import (
            PDFExtractionService,
        )

        return PDFExtractionService()

    @pytest.mark.timeout(60)
    def test_pages_past_the_ceiling_are_never_built(self, mocker):
        """A document declaring more pages than the ceiling has only the
        ceiling's pages (plus the one that shows there are more) built.
        pdfplumber's ``PDF.pages`` -- also reached through
        ``PDF.close()`` when leaving a ``with pdfplumber.open(...)``
        block -- would build all of them."""
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTION_PAGES,
        )

        declared = MAX_PDF_EXTRACTION_PAGES + 20
        pdf_bytes = _text_pdf(declared)
        built = _count_built_pages(mocker)

        result = self._service().extract_text_and_metadata(
            pdf_bytes, "many-pages.pdf"
        )

        assert result["success"] is True, result["error"]
        assert result["truncated"] is True
        assert built.call_count == MAX_PDF_EXTRACTION_PAGES + 1, (
            f"{built.call_count} of {declared} pages were built -- the "
            "upload path materialises the whole page tree"
        )
        # The unbuilt rest is not reported: /Count is never read, so
        # pages is the walk's lower bound (the ceiling plus the page
        # that showed there are more), flagged by truncated.
        assert result["pages"] == MAX_PDF_EXTRACTION_PAGES + 1
        assert result["text"] == "\n".join(["Hello"] * MAX_PDF_EXTRACTION_PAGES)

    def test_short_document_reports_the_pages_walked(self):
        result = self._service().extract_text_and_metadata(
            _text_pdf(3), "short.pdf"
        )

        assert result["success"] is True, result["error"]
        assert result["truncated"] is False
        assert result["pages"] == 3
        assert result["text"] == "Hello\nHello\nHello"

    def test_page_ceiling_reports_the_pages_seen(self, mocker):
        """A walk stopped by the page ceiling reports the pages seen
        (the ceiling plus one)."""
        from local_deep_research.utilities.pdf_extraction_limits import (
            MAX_PDF_EXTRACTION_PAGES,
        )

        mock_pdf = MagicMock()
        mock_pdf.pages = _pages(MAX_PDF_EXTRACTION_PAGES + 5)
        mocker.patch(f"{SERVICE_MODULE}.pdfplumber.open", return_value=mock_pdf)
        _walk_mock_pages(mocker, SERVICE_MODULE)

        result = self._service().extract_text_and_metadata(b"%PDF", "x.pdf")

        assert result["truncated"] is True
        assert result["pages"] == MAX_PDF_EXTRACTION_PAGES + 1

    def test_cpu_budget_stops_the_walk_between_pages(self, mocker):
        """Once the thread's CPU budget is spent no new page starts."""
        from local_deep_research.web.services import pdf_extraction_service

        # Entry (deadline base), first page check, then budget exhausted.
        clock = Mock(side_effect=chain([0.0, 0.0], repeat(1e12)))
        mocker.patch.object(
            pdf_extraction_service, "time", Mock(thread_time=clock)
        )
        pages = _pages(5)
        mock_pdf = MagicMock()
        mock_pdf.pages = pages
        mocker.patch(f"{SERVICE_MODULE}.pdfplumber.open", return_value=mock_pdf)
        _walk_mock_pages(mocker, SERVICE_MODULE)

        result = self._service().extract_text_and_metadata(b"%PDF", "x.pdf")

        walked = [page.extract_text.called for page in pages]
        assert walked == [True, False, False, False, False]
        assert result["success"] is True
        assert result["truncated"] is True
        assert result["text"] == "page text"
        # The page that met the spent budget is still closed.
        pages[1].close.assert_called_once_with()

    def test_every_page_is_handed_to_the_cache_releaser(self, mocker):
        """With the release threshold at zero the releaser runs after
        every page, so the decoded streams and parsed objects pdfminer
        caches on the document cannot pile up across the walk."""
        from local_deep_research.utilities import (
            pdf_extraction_limits,
            pdf_stream_release,
        )

        mocker.patch.object(
            pdf_extraction_limits, "MAX_PDF_RETAINED_DECODED_BYTES", 0
        )
        after_page = mocker.spy(
            pdf_stream_release.PdfplumberWalkReleaser, "after_page"
        )

        result = self._service().extract_text_and_metadata(
            _text_pdf(3), "three.pdf"
        )

        assert result["text"] == "Hello\nHello\nHello"
        assert after_page.call_count == 3
        assert after_page.spy_return_list == [True, True, True]

    def test_close_failure_does_not_mask_the_text(self, mocker):
        pages = _pages(2)
        for page in pages:
            page.close.side_effect = RuntimeError("close failed")
        mock_pdf = MagicMock()
        mock_pdf.pages = pages
        mocker.patch(f"{SERVICE_MODULE}.pdfplumber.open", return_value=mock_pdf)
        _walk_mock_pages(mocker, SERVICE_MODULE)

        result = self._service().extract_text_and_metadata(b"%PDF", "x.pdf")

        assert result["success"] is True
        assert result["text"] == "page text\npage text"


class TestPageWalkHelpers:
    def test_lazy_walk_matches_pdfplumber_pages(self):
        import pdfplumber

        from local_deep_research.utilities.pdf_page_walk import (
            iter_pdfplumber_pages,
        )

        pdf_bytes = _text_pdf(4)
        with pdfplumber.open(io.BytesIO(pdf_bytes)) as eager:
            expected = [
                (p.page_number, p.initial_doctop, p.extract_text())
                for p in eager.pages
            ]
        lazy = pdfplumber.open(io.BytesIO(pdf_bytes))
        got = [
            (p.page_number, p.initial_doctop, p.extract_text())
            for p in iter_pdfplumber_pages(lazy)
        ]

        assert got == expected
        assert not hasattr(lazy, "_pages")

    @pytest.mark.parametrize(
        "pdf_kwargs",
        [
            pytest.param(
                {"extra_objects": {90: b"90 0 R"}}, id="self-reference"
            ),
            pytest.param(
                # /Count is a type-2 xref entry in an object stream whose
                # own object is a self-reference: getobj -> stream_value
                # -> resolve1 spins inside pdfminer.
                {
                    "extra_objects": {91: b"91 0 R"},
                    "compressed": {90: (91, 0)},
                },
                id="object-stream-cycle",
            ),
            pytest.param(
                # /Count is a stream whose /Length is a self-reference:
                # the parser's int_value -> resolve1 spins.
                {
                    "extra_objects": {
                        90: b"<< /Length 91 0 R >>\nstream\nxx\nendstream",
                        91: b"91 0 R",
                    }
                },
                id="length-cycle",
            ),
        ],
    )
    def test_upload_service_survives_a_cyclic_count(self, mocker, pdf_kwargs):
        """A truncated walk must not resolve the page tree's /Count:
        each of these makes pdfminer loop forever on the resolve, after
        the CPU budget has stopped applying. The service never reads
        /Count, so it returns promptly with the pages it saw."""
        import threading

        from local_deep_research.web.services import pdf_extraction_service

        mocker.patch.object(
            pdf_extraction_service, "MAX_PDF_EXTRACTION_PAGES", 1
        )
        pdf_bytes = _text_pdf(2, count="90 0 R", **pdf_kwargs)
        result = []
        worker = threading.Thread(
            target=lambda: result.append(
                pdf_extraction_service.PDFExtractionService.extract_text_and_metadata(
                    pdf_bytes, "cyclic.pdf"
                )
            ),
            daemon=True,
        )
        worker.start()
        worker.join(timeout=10)

        assert not worker.is_alive(), "the upload service looped on /Count"
        assert result[0]["truncated"] is True
        assert result[0]["pages"] == 2
        assert result[0]["text"] == "Hello"


class TestReleaseCoversPageConstruction:
    """Building a page (``next()`` on pdfminer's page generator) resolves
    its /Resources and /Contents. The lazy walks open the releaser's
    window before that, so a page whose indirect /Resources carries a
    large array is counted and released. With the window opened only
    before ``extract_text()``, those objects were cached outside every
    window and never counted: ~8M cached array elements at 40 pages."""

    # Each page's /Resources array alone reaches the release threshold
    # below; nothing else a page caches comes near it.
    ENTRIES = 200
    LIMIT = 128 * 150

    def test_upload_service_releases_what_building_a_page_cached(self, mocker):
        from local_deep_research.utilities import (
            pdf_extraction_limits,
            pdf_stream_release,
        )
        from local_deep_research.web.services.pdf_extraction_service import (
            PDFExtractionService,
        )

        mocker.patch.object(
            pdf_extraction_limits, "MAX_PDF_RETAINED_DECODED_BYTES", self.LIMIT
        )
        after_page = mocker.spy(
            pdf_stream_release.PdfplumberWalkReleaser, "after_page"
        )

        result = PDFExtractionService.extract_text_and_metadata(
            _text_pdf(3, resources_entries=self.ENTRIES), "res.pdf"
        )

        assert result["text"] == "Hello\nHello\nHello"
        assert after_page.spy_return_list == [True, True, True]


@pytest.fixture
def upload_client(monkeypatch):
    from fastapi import FastAPI
    from fastapi.testclient import TestClient

    from local_deep_research.web.dependencies.rate_limit import limiter
    from local_deep_research.web.routers import research

    monkeypatch.setattr(limiter, "enabled", False)
    app = FastAPI()
    app.include_router(research.router)
    app.dependency_overrides[research.require_auth] = lambda: "test_pdf_upload"
    with TestClient(app) as client:
        yield client


@pytest.mark.parametrize(
    ("pages", "extracted_pages", "reported_pages", "truncated"),
    [(520, 500, 501, True), (3, 3, 3, False)],
)
def test_upload_endpoint_reports_partial_extraction(
    upload_client, pages, extracted_pages, reported_pages, truncated
):
    """Exercise real multipart validation and extraction, not a mock result."""
    response = upload_client.post(
        "/api/upload/pdf",
        files={"files": ("paper.pdf", _text_pdf(pages), "application/pdf")},
    )

    assert response.status_code == 200, response.text
    body = response.json()
    item = body["extracted_texts"][0]
    assert item["truncated"] is truncated
    assert item["pages"] == reported_pages
    assert item["text"] == "\n".join(["Hello"] * extracted_pages)
    assert ("[Partial PDF extraction:" in body["combined_text"]) is truncated
    assert body["errors"] == []


@pytest.mark.parametrize("ceiling", ["characters", "cpu"])
def test_upload_endpoint_reports_other_extraction_limits(
    upload_client, monkeypatch, ceiling
):
    from local_deep_research.web.services import pdf_extraction_service

    if ceiling == "characters":
        monkeypatch.setattr(
            pdf_extraction_service, "MAX_PDF_EXTRACTED_CHARS", 10
        )
        expected = "Hello\nHell"
    else:
        clock = Mock(side_effect=chain([0.0, 0.0], repeat(1e12)))
        monkeypatch.setattr(
            pdf_extraction_service, "time", Mock(thread_time=clock)
        )
        expected = "Hello"

    response = upload_client.post(
        "/api/upload/pdf",
        files={"files": ("paper.pdf", _text_pdf(3), "application/pdf")},
    )

    assert response.status_code == 200, response.text
    body = response.json()
    item = body["extracted_texts"][0]
    assert item["truncated"] is True
    assert item["pages"] == 2
    assert item["text"] == expected
    assert "[Partial PDF extraction:" in body["combined_text"]


def test_ceiling_values_are_pinned():
    """The constants are shared with the download-service bounds (#6459);
    this pins them across merges."""
    from local_deep_research.utilities.pdf_extraction_limits import (
        MAX_PDF_EXTRACTED_CHARS,
        MAX_PDF_EXTRACTION_PAGES,
    )

    assert MAX_PDF_EXTRACTION_PAGES == 500
    assert MAX_PDF_EXTRACTED_CHARS == 10_000_000