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.

149 KB
Raw Blame
"""Extraction-bound contracts for ``DownloadService._extract_text_from_pdf``.

Downloaded PDFs are external content: the storage layer caps their size
(default 3 GB), but extraction complexity is a different axis — a PDF
of millions of cheap pages, or one whose pages decompress into huge
text, burns unbounded CPU and memory inside the server process while
``pdfplumber`` walks every page. Extraction must stop at a page ceiling
and cap the total extracted characters.
"""

from __future__ import annotations

from types import SimpleNamespace
from unittest.mock import MagicMock, Mock, patch

import pytest

from local_deep_research.utilities.pdf_extraction_limits import (
    MAX_PDF_EXTRACTED_CHARS,
    MAX_PDF_EXTRACTION_CPU_SECONDS,
    MAX_PDF_EXTRACTION_PAGES,
)


@pytest.fixture
def service(mocker):
    """DownloadService with its constructor dependencies mocked."""
    mock_settings = Mock()
    mock_settings.get_setting.return_value = "/tmp/test_library"
    mocker.patch(
        "local_deep_research.research_library.services.download_service."
        "get_settings_manager",
        return_value=mock_settings,
    )
    mocker.patch("pathlib.Path.mkdir")
    mocker.patch(
        "local_deep_research.research_library.services.download_service."
        "RetryManager"
    )
    from local_deep_research.research_library.services.download_service import (
        DownloadService,
    )

    return DownloadService(username="test_user")


def _pdf_with_pages(mocker, pages):
    """Patch pdfplumber.open and the lazy page walker with mock 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.research_library.services.download_service."
        "pdfplumber.open",
        return_value=mock_pdf,
    )

    def walk(pdf, releaser):
        for page in pages:
            releaser.before_page()
            yield page

    mocker.patch(
        "local_deep_research.utilities.pdf_page_walk.iter_pdfplumber_pages",
        side_effect=walk,
    )
    return mock_pdf


class TestPageCeiling:
    def test_pathological_page_count_is_not_fully_walked(self, service, mocker):
        """A 600-page document must not have every page extracted."""
        pages = []
        for _ in range(600):
            page = MagicMock()
            page.extract_text.return_value = "page text"
            pages.append(page)
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        # Extraction still produced text ...
        assert text
        # ... and stopped exactly AT the page ceiling, not merely
        # "somewhere before 600": a loop that broke early (at page 1, or
        # one page short) would also satisfy `walked < len(pages)`, and
        # would silently cut honest documents shorter than the ceiling.
        walked = sum(1 for page in pages if page.extract_text.called)
        assert walked == MAX_PDF_EXTRACTION_PAGES, (
            f"extraction walked {walked} of {len(pages)} pages — expected "
            f"exactly the page ceiling ({MAX_PDF_EXTRACTION_PAGES})"
        )


class TestCharacterCeiling:
    def test_decompression_sized_text_is_capped(self, service, mocker):
        """20 MB of extracted text must not be returned in full."""
        per_page = "x" * 200_000
        pages = []
        for _ in range(100):
            page = MagicMock()
            page.extract_text.return_value = per_page
            pages.append(page)
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        total = len(per_page) * len(pages)
        assert text
        assert len(text) < total, (
            f"extraction returned all {len(text)} chars ({total} expected "
            "total) — no extracted-character ceiling is in force"
        )


class TestPyPDFFallbackPageCeiling:
    def test_fallback_page_count_is_not_fully_walked(self, service, mocker):
        """The PyPDF fallback loop must honor the page ceiling too.

        pdfplumber yields no text, so extraction falls through to the
        ``PdfReader`` path; a 600-page mock document must not have every
        page extracted there.
        """
        empty_pages = [MagicMock()]
        empty_pages[0].extract_text.return_value = ""
        _pdf_with_pages(mocker, empty_pages)

        pypdf_pages = []
        for _ in range(600):
            page = MagicMock()
            page.extract_text.return_value = "page text"
            pypdf_pages.append(page)
        mock_reader = MagicMock()
        mock_reader.pages = pypdf_pages
        mocker.patch(
            "local_deep_research.research_library.services.download_service."
            "PdfReader",
            return_value=mock_reader,
        )

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text
        walked = sum(1 for page in pypdf_pages if page.extract_text.called)
        assert walked == MAX_PDF_EXTRACTION_PAGES, (
            f"fallback extraction walked {walked} of {len(pypdf_pages)} "
            f"pages — expected exactly the page ceiling "
            f"({MAX_PDF_EXTRACTION_PAGES}) on the PyPDF fallback"
        )


class TestPyPDFFallbackCharacterCeiling:
    def test_fallback_decompression_sized_text_is_capped(self, service, mocker):
        """The PyPDF fallback loop must honor the character ceiling too.

        pdfplumber yields no text, so extraction falls through to the
        ``PdfReader`` path; three pages that decompress to ~4 M chars
        each trip the 10 M character ceiling mid-document, so the
        trailing short page must never be walked at all.
        """
        empty_pages = [MagicMock()]
        empty_pages[0].extract_text.return_value = ""
        _pdf_with_pages(mocker, empty_pages)

        per_page = "x" * 4_000_000
        pypdf_pages = []
        for _ in range(3):
            page = MagicMock()
            page.extract_text.return_value = per_page
            pypdf_pages.append(page)
        trailing_page = MagicMock()
        trailing_page.extract_text.return_value = "trailing short page"
        pypdf_pages.append(trailing_page)
        mock_reader = MagicMock()
        mock_reader.pages = pypdf_pages
        mocker.patch(
            "local_deep_research.research_library.services.download_service."
            "PdfReader",
            return_value=mock_reader,
        )

        text = service._extract_text_from_pdf(b"%PDF-fake")

        total = len(per_page) * 3 + len("trailing short page")
        assert text
        assert len(text) < total, (
            f"fallback extraction returned all {len(text)} chars "
            f"({total} expected total) — no extracted-character ceiling "
            "is in force on the PyPDF fallback"
        )
        walked = sum(1 for page in pypdf_pages if page.extract_text.called)
        assert 0 < walked < len(pypdf_pages), (
            f"fallback extraction walked {walked} of {len(pypdf_pages)} "
            "pages — no extracted-character ceiling is in force on the "
            "PyPDF fallback"
        )
        assert not trailing_page.extract_text.called, (
            "the trailing short page was walked despite the character "
            "ceiling tripping earlier"
        )


class TestBaseDownloaderBounds:
    """The shared base-downloader choke point must enforce both ceilings.

    Every downloader text branch and the search-time PDF fetcher route
    through ``BaseDownloader.extract_text_from_pdf``, so its loop is a
    bound on its own — not merely a mirror of the download service's.
    """

    def test_page_count_is_not_fully_walked(self, mocker):
        """A 600-page document must not have every page extracted."""
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        pages = []
        for _ in range(600):
            page = MagicMock()
            page.extract_text.return_value = "page text"
            pages.append(page)
        mock_reader = MagicMock()
        mock_reader.pages = pages

        # Patch pypdf.PdfReader since it's imported inside the function
        mocker.patch("pypdf.PdfReader", return_value=mock_reader)

        text = BaseDownloader.extract_text_from_pdf(b"%PDF-fake")

        assert text
        walked = sum(1 for page in pages if page.extract_text.called)
        assert walked == MAX_PDF_EXTRACTION_PAGES, (
            f"base-downloader extraction walked {walked} of {len(pages)} "
            f"pages — expected exactly the page ceiling "
            f"({MAX_PDF_EXTRACTION_PAGES}) on the base downloader"
        )

    def test_decompression_sized_text_is_capped(self, mocker):
        """Pages that decompress to ~4 M chars each must be capped."""
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        per_page = "x" * 4_000_000
        pages = []
        for _ in range(3):
            page = MagicMock()
            page.extract_text.return_value = per_page
            pages.append(page)
        trailing_page = MagicMock()
        trailing_page.extract_text.return_value = "trailing short page"
        pages.append(trailing_page)
        mock_reader = MagicMock()
        mock_reader.pages = pages

        # Patch pypdf.PdfReader since it's imported inside the function
        mocker.patch("pypdf.PdfReader", return_value=mock_reader)

        text = BaseDownloader.extract_text_from_pdf(b"%PDF-fake")

        total = len(per_page) * 3 + len("trailing short page")
        assert text
        assert len(text) < total, (
            f"base-downloader extraction returned all {len(text)} chars "
            f"({total} expected total) — no extracted-character ceiling "
            "is in force on the base downloader"
        )
        walked = sum(1 for page in pages if page.extract_text.called)
        assert 0 < walked < len(pages), (
            f"base-downloader extraction walked {walked} of {len(pages)} "
            "pages — no extracted-character ceiling is in force on the "
            "base downloader"
        )
        assert not trailing_page.extract_text.called, (
            "the trailing short page was walked despite the character "
            "ceiling tripping earlier"
        )


def _pages_of(texts):
    """Mock pages yielding *texts*, one string per page."""
    pages = []
    for text in texts:
        page = MagicMock()
        page.extract_text.return_value = text
        pages.append(page)
    return pages


class TestTruncationRatherThanDropping:
    """The page that crosses the character ceiling must be truncated.

    Dropping it whole loses up to a full page of text that fitted the
    budget, and — when the very first page is over the ceiling — turns a
    bounded extraction into a reported extraction FAILURE, which the
    download service records as an error and which sends
    ``_extract_text_from_pdf`` on to the PyPDF branch to repeat the whole
    unbounded parse a second time.
    """

    def test_tripping_page_is_sliced_to_the_remaining_budget(
        self, service, mocker
    ):
        pages = _pages_of(
            [
                "a" * (MAX_PDF_EXTRACTED_CHARS - 10),
                "b" * 1_000,
                "c" * 10,
            ]
        )
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        # The "\n\n" separator counts against the budget: 10 left after
        # the first page is 2 for the separator and 8 for the tripping
        # page's prefix.
        # Compared by length and tail: a failing ``==`` on 10 M-character
        # strings would make pytest diff them.
        assert len(text) == MAX_PDF_EXTRACTED_CHARS, (
            f"{len(text)} characters returned — the page that crossed the "
            "ceiling was not sliced to the remaining budget"
        )
        assert text[-12:] == "aa\n\n" + "b" * 8, (
            "the tripping page did not contribute its prefix"
        )
        assert not pages[2].extract_text.called

    def test_single_over_ceiling_page_yields_the_budget_not_none(
        self, service, mocker
    ):
        pages = _pages_of(["x" * (MAX_PDF_EXTRACTED_CHARS + 1)])
        _pdf_with_pages(mocker, pages)
        pypdf = mocker.patch(
            "local_deep_research.research_library.services.download_service."
            "PdfReader"
        )

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None, (
            "a first page over the character ceiling returned None — "
            "downstream records this as an extraction failure"
        )
        assert len(text) == MAX_PDF_EXTRACTED_CHARS
        assert not pypdf.called, (
            "extraction fell through to the PyPDF branch and repeated the "
            "whole parse a second time"
        )

    def test_exactly_the_ceiling_is_kept_in_full(self, service, mocker):
        pages = _pages_of(["x" * MAX_PDF_EXTRACTED_CHARS])
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        assert len(text) == MAX_PDF_EXTRACTED_CHARS

    def test_one_over_the_ceiling_is_truncated_to_the_ceiling(
        self, service, mocker
    ):
        pages = _pages_of(["x" * (MAX_PDF_EXTRACTED_CHARS + 1)])
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        assert len(text) == MAX_PDF_EXTRACTED_CHARS

    def test_base_downloader_truncates_rather_than_drops(self, mocker):
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        pages = _pages_of(["x" * (MAX_PDF_EXTRACTED_CHARS + 1)])
        mock_reader = MagicMock()
        mock_reader.pages = pages
        mocker.patch("pypdf.PdfReader", return_value=mock_reader)

        text = BaseDownloader.extract_text_from_pdf(b"%PDF-fake")

        assert text is not None, (
            "a first page over the character ceiling returned None from "
            "the base-downloader choke point"
        )
        assert len(text) == MAX_PDF_EXTRACTED_CHARS


class TestCpuTimeBudget:
    """Extraction stops when the CPU-time budget is spent between pages.

    Page and character ceilings do not bind on a document whose pages are
    individually slow but few and terse; the deadline does. It can only
    be checked BETWEEN pages, so a single page already inside
    pdfplumber/pypdf still runs to completion.
    """

    def test_deadline_stops_the_walk_and_keeps_what_was_extracted(
        self, service, mocker
    ):
        clock = {"now": 0.0}
        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])

        def slow_first_page():
            clock["now"] += MAX_PDF_EXTRACTION_CPU_SECONDS + 1.0
            return "first page text"

        pages = _pages_of([None, "second page text"])
        pages[0].extract_text.side_effect = slow_first_page
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text == "first page text", (
            "the deadline either never fired or discarded the text "
            "already extracted"
        )
        assert not pages[1].extract_text.called, (
            "the walk continued past the CPU-time budget"
        )

    def test_base_downloader_honours_the_deadline(self, mocker):
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        clock = {"now": 0.0}
        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])

        def slow_first_page():
            clock["now"] += MAX_PDF_EXTRACTION_CPU_SECONDS + 1.0
            return "first page text"

        pages = _pages_of([None, "second page text"])
        pages[0].extract_text.side_effect = slow_first_page
        mock_reader = MagicMock()
        mock_reader.pages = pages
        mocker.patch("pypdf.PdfReader", return_value=mock_reader)

        text = BaseDownloader.extract_text_from_pdf(b"%PDF-fake")

        assert text == "first page text"
        assert not pages[1].extract_text.called, (
            "the base-downloader walk continued past the CPU-time budget"
        )

    def test_base_downloader_deadline_covers_pdfreader_construction(
        self, mocker
    ):
        """A slow ``PdfReader()`` parse must also count against the
        deadline — not just a slow page inside the loop."""
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        clock = {"now": 0.0}
        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])

        pages = _pages_of(["first page text", "second page text"])
        mock_reader = MagicMock()
        mock_reader.pages = pages

        def slow_construction(_file):
            clock["now"] += MAX_PDF_EXTRACTION_CPU_SECONDS + 1.0
            return mock_reader

        mocker.patch("pypdf.PdfReader", side_effect=slow_construction)

        text = BaseDownloader.extract_text_from_pdf(b"%PDF-fake")

        assert not any(page.extract_text.called for page in pages), (
            "a page was walked even though PdfReader() construction alone "
            "spent the whole deadline"
        )
        assert text is None, (
            "the deadline did not cover PdfReader() construction — a slow "
            "parse still returned extracted text"
        )

    def test_download_service_deadline_covers_pdfplumber_open(
        self, service, mocker
    ):
        """A slow ``pdfplumber.open()`` parse must also count against the
        shared deadline — not just a slow page inside the loop."""
        clock = {"now": 0.0}
        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])

        pages = _pages_of(["first page text", "second page text"])
        mock_pdf = MagicMock()
        mock_pdf.pages = pages
        mock_pdf.__enter__ = Mock(return_value=mock_pdf)
        mock_pdf.__exit__ = Mock(return_value=False)

        def slow_open(_file):
            clock["now"] += MAX_PDF_EXTRACTION_CPU_SECONDS + 1.0
            return mock_pdf

        mocker.patch(
            "local_deep_research.research_library.services."
            "download_service.pdfplumber.open",
            side_effect=slow_open,
        )
        reader = _reader_with_pages(mocker, _pages_of(["fallback text"]))
        mocker.patch(
            "local_deep_research.utilities.pdf_page_walk.iter_pdfplumber_pages",
            return_value=iter(pages),
        )

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert not any(page.extract_text.called for page in pages), (
            "a page was extracted even though pdfplumber.open() construction "
            "alone spent the whole deadline"
        )
        assert not reader.called, (
            "pdfplumber.open() construction alone spent the deadline, yet "
            "the PyPDF fallback was started"
        )
        assert text is None, (
            "the deadline did not cover pdfplumber.open() construction — "
            "a slow parse still returned extracted text"
        )


def _reader_with_pages(mocker, pages):
    """Patch the download service's PdfReader to yield *pages*."""
    mock_reader = MagicMock()
    mock_reader.pages = pages
    return mocker.patch(
        "local_deep_research.research_library.services.download_service."
        "PdfReader",
        return_value=mock_reader,
    )


class TestOneDeadlinePerExtraction:
    """``_extract_text_from_pdf`` has ONE budget for both attempts.

    A per-attempt deadline let a document that exhausted the pdfplumber
    budget without text buy a second full budget — and a second full
    page-tree parse — in the PyPDF fallback.
    """

    def test_pdfplumber_deadline_stop_does_not_start_the_fallback(
        self, service, mocker
    ):
        clock = {"now": 0.0}
        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])

        def slow_empty_page():
            clock["now"] += MAX_PDF_EXTRACTION_CPU_SECONDS + 1.0
            return ""

        pages = _pages_of([None, "second page text"])
        pages[0].extract_text.side_effect = slow_empty_page
        _pdf_with_pages(mocker, pages)
        reader = _reader_with_pages(mocker, _pages_of(["fallback text"]))

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is None
        assert not pages[1].extract_text.called
        assert not reader.called, (
            "pdfplumber stopped on the deadline, yet the PyPDF fallback "
            "was started — a second parse on a spent budget"
        )

    def test_fallback_honours_the_deadline(self, service, mocker):
        clock = {"now": 0.0}
        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])
        _pdf_with_pages(mocker, _pages_of([""]))

        def slow_first_page():
            clock["now"] += MAX_PDF_EXTRACTION_CPU_SECONDS + 1.0
            return "first fallback page"

        fallback_pages = _pages_of([None, "second fallback page"])
        fallback_pages[0].extract_text.side_effect = slow_first_page
        _reader_with_pages(mocker, fallback_pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text == "first fallback page"
        assert not fallback_pages[1].extract_text.called, (
            "the PyPDF fallback walk continued past the CPU-time budget"
        )

    def test_fallback_shares_the_budget_pdfplumber_spent(self, service, mocker):
        """Each attempt alone stays under the budget; together they don't."""
        clock = {"now": 0.0}
        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])
        step = MAX_PDF_EXTRACTION_CPU_SECONDS * 0.6

        def slow(result):
            def page_text():
                clock["now"] += step
                return result

            return page_text

        plumber_pages = _pages_of([None])
        plumber_pages[0].extract_text.side_effect = slow("")
        _pdf_with_pages(mocker, plumber_pages)
        fallback_pages = _pages_of([None, "second fallback page"])
        fallback_pages[0].extract_text.side_effect = slow("first fallback page")
        _reader_with_pages(mocker, fallback_pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text == "first fallback page"
        assert not fallback_pages[1].extract_text.called, (
            "the PyPDF fallback started a fresh budget instead of sharing "
            "the one pdfplumber had already spent"
        )


class TestPyPDFFallbackTruncation:
    def test_fallback_slices_the_tripping_page(self, service, mocker):
        _pdf_with_pages(mocker, _pages_of([""]))
        fallback_pages = _pages_of(
            [
                "a" * (MAX_PDF_EXTRACTED_CHARS - 10),
                "b" * 1_000,
                "c" * 10,
            ]
        )
        _reader_with_pages(mocker, fallback_pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        assert len(text) == MAX_PDF_EXTRACTED_CHARS, (
            f"{len(text)} characters returned — the PyPDF fallback did not "
            "slice the page that crossed the character ceiling to the "
            "remaining budget"
        )
        assert text[-12:] == "aa\n\n" + "b" * 8, (
            "the PyPDF fallback's tripping page did not contribute its prefix"
        )
        assert not fallback_pages[2].extract_text.called

    def test_fallback_single_over_ceiling_page_yields_the_budget(
        self, service, mocker
    ):
        _pdf_with_pages(mocker, _pages_of([""]))
        _reader_with_pages(
            mocker, _pages_of(["x" * (MAX_PDF_EXTRACTED_CHARS + 1)])
        )

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None, (
            "a first fallback page over the character ceiling returned None"
        )
        assert len(text) == MAX_PDF_EXTRACTED_CHARS


class TestSeparatorsCountAgainstTheCeiling:
    """The returned string, join separators included, fits the ceiling."""

    def test_download_service_output_fits_the_ceiling(self, service, mocker):
        half = MAX_PDF_EXTRACTED_CHARS // 2
        pages = _pages_of(["a" * half, "b" * half, "c" * 10])
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        assert len(text) == MAX_PDF_EXTRACTED_CHARS, (
            f"{len(text)} characters returned for a "
            f"{MAX_PDF_EXTRACTED_CHARS}-character ceiling"
        )

    def test_base_downloader_output_fits_the_ceiling(self, mocker):
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        half = MAX_PDF_EXTRACTED_CHARS // 2
        mock_reader = MagicMock()
        mock_reader.pages = _pages_of(["a" * half, "b" * half, "c" * 10])
        mocker.patch("pypdf.PdfReader", return_value=mock_reader)

        text = BaseDownloader.extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        assert len(text) == MAX_PDF_EXTRACTED_CHARS, (
            f"{len(text)} characters returned for a "
            f"{MAX_PDF_EXTRACTED_CHARS}-character ceiling"
        )


class TestExactFitThenOneMorePage:
    """A page that fills the budget exactly, then one more page.

    After an exact-fit page the running count includes that page's join
    separator, so the budget left for the next page is NEGATIVE (-2 in the
    download service, -1 in the base helper). Slicing with a negative
    ``remaining`` would keep all but the last one or two characters of
    the next page, overshooting the ceiling; the ``remaining > 0`` guard
    must append nothing instead.
    """

    def test_download_service_pdfplumber_loop(self, service, mocker):
        pages = _pages_of(["x" * MAX_PDF_EXTRACTED_CHARS, "y" * 100])
        _pdf_with_pages(mocker, pages)
        TestReleaserWiring._patch_page_walk(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        assert len(text) == MAX_PDF_EXTRACTED_CHARS, (
            f"{len(text)} characters returned — the page after an exact "
            "fit was sliced with a negative remaining budget"
        )
        assert "y" not in text[-200:]

    def test_download_service_pypdf_fallback_loop(self, service, mocker):
        _pdf_with_pages(mocker, _pages_of([""]))
        _reader_with_pages(
            mocker, _pages_of(["x" * MAX_PDF_EXTRACTED_CHARS, "y" * 100])
        )

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        assert len(text) == MAX_PDF_EXTRACTED_CHARS, (
            f"{len(text)} characters returned — the PyPDF fallback sliced "
            "the page after an exact fit with a negative remaining budget"
        )
        assert "y" not in text[-200:]

    def test_base_downloader_loop(self, mocker):
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        mock_reader = MagicMock()
        mock_reader.pages = _pages_of(
            ["x" * MAX_PDF_EXTRACTED_CHARS, "y" * 100]
        )
        mocker.patch("pypdf.PdfReader", return_value=mock_reader)

        text = BaseDownloader.extract_text_from_pdf(b"%PDF-fake")

        assert text is not None
        assert len(text) == MAX_PDF_EXTRACTED_CHARS, (
            f"{len(text)} characters returned — the base downloader sliced "
            "the page after an exact fit with a negative remaining budget"
        )
        assert "y" not in text[-200:]


def _wall_clock_racing_ahead(mocker):
    """Make every wall clock jump far past the CPU budget on each read,
    while the thread's own CPU time stands still.

    This is what a heavily loaded server looks like to one extraction:
    wall time passes (GIL waits, other requests' extractions) while this
    thread does no work of its own.
    """
    wall = {"now": 0.0}

    def racing():
        wall["now"] += MAX_PDF_EXTRACTION_CPU_SECONDS * 10
        return wall["now"]

    for name in ("monotonic", "perf_counter"):
        mocker.patch(f"time.{name}", side_effect=racing)
    mocker.patch("time.thread_time", return_value=0.0)


class TestServerLoadDoesNotCutHonestDocuments:
    """Wall-clock time spent waiting is not extraction work.

    A 60 s wall-clock deadline cut an honest 480-page document at page 416
    on an idle host, and at roughly page 65 with four extractions running
    at once; the partial text was saved as the complete document and
    never re-extracted. The budget must count only this thread's CPU.
    """

    def test_download_service_walks_every_page_under_load(
        self, service, mocker
    ):
        pages = _pages_of([f"page {i}" for i in range(20)])
        _pdf_with_pages(mocker, pages)
        _wall_clock_racing_ahead(mocker)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert all(page.extract_text.called for page in pages), (
            "extraction stopped early because WALL-clock time passed — "
            "server load, not this document's cost, cut it short"
        )
        assert text == "\n\n".join(f"page {i}" for i in range(20))

    def test_download_service_fallback_walks_every_page_under_load(
        self, service, mocker
    ):
        _pdf_with_pages(mocker, _pages_of([""]))
        fallback_pages = _pages_of([f"page {i}" for i in range(20)])
        reader = _reader_with_pages(mocker, fallback_pages)
        _wall_clock_racing_ahead(mocker)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert reader.called, (
            "the PyPDF fallback was skipped because WALL-clock time passed"
        )
        assert all(page.extract_text.called for page in fallback_pages)
        assert text == "\n\n".join(f"page {i}" for i in range(20))

    def test_base_downloader_walks_every_page_under_load(self, mocker):
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        pages = _pages_of([f"page {i}" for i in range(20)])
        mock_reader = MagicMock()
        mock_reader.pages = pages
        mocker.patch("pypdf.PdfReader", return_value=mock_reader)
        _wall_clock_racing_ahead(mocker)

        text = BaseDownloader.extract_text_from_pdf(b"%PDF-fake")

        assert all(page.extract_text.called for page in pages), (
            "the base downloader stopped early because WALL-clock time passed"
        )
        assert text == "\n".join(f"page {i}" for i in range(20))

    @pytest.mark.parametrize(
        ("contending_extractions", "dense_page_cpu_seconds", "margin"),
        [(4, 0.5, 2.0), (16, 0.66, 1.8)],
    )
    def test_budget_leaves_the_documented_margin_under_contention(
        self, contending_extractions, dense_page_cpu_seconds, margin
    ):
        """The budget must leave the margin the constant's comment claims
        over an honest document at the page ceiling.

        Measured with pdfplumber 0.11.10 on CPython 3.14: a synthetic
        page of ~12,700 characters of dense text costs ~0.29 s of the
        thread's CPU alone, ~0.5 s with four extractions contending for
        the GIL and ~0.66 s with sixteen. The comment claims 2x margin at
        four and ~1.8x at sixteen; a smaller budget would cut honest
        dense books short on a busy server.
        """
        assert MAX_PDF_EXTRACTION_CPU_SECONDS >= (
            margin * MAX_PDF_EXTRACTION_PAGES * dense_page_cpu_seconds
        ), (
            f"a {MAX_PDF_EXTRACTION_CPU_SECONDS}s budget does not fit a "
            f"dense {MAX_PDF_EXTRACTION_PAGES}-page document with "
            f"{margin}x margin at {contending_extractions} contending "
            "extractions"
        )

    def test_budget_is_still_a_bound(self):
        """The budget must stay small enough to stop pathological pages.

        A budget raised far past the honest-document sizing (say 1e9 s)
        would pass the lower bound above while bounding nothing; pin it
        to at most 15 minutes of CPU per extraction.
        """
        assert 0 < MAX_PDF_EXTRACTION_CPU_SECONDS <= 900, (
            f"a {MAX_PDF_EXTRACTION_CPU_SECONDS}s budget no longer bounds "
            "an extraction whose pages are pathologically expensive"
        )


def _recording_pages(texts, events):
    """Mock pages that log ``("extract", i)``/``("close", i)`` to *events*."""
    pages = _pages_of(texts)
    for index, page in enumerate(pages):
        text = page.extract_text.return_value

        def extract(index=index, text=text):
            events.append(("extract", index))
            return text

        page.extract_text.side_effect = extract
        page.close.side_effect = lambda index=index: events.append(
            ("close", index)
        )
    return pages


class TestPdfplumberPagesAreClosed:
    """Every pdfplumber page the walk reaches is closed before the next.

    pdfplumber caches each page's parsed layout objects and text map on
    the ``Page`` until ``close()``; ``PDF.close()`` on leaving the
    ``with`` block releases them only after the whole walk. Measured on a
    dense PDF, the walk retained ~25 MB per page without a per-page
    close (1 GB at 40 pages, ~12 GB at the page ceiling) and stayed flat
    with it. Closing frees only these caches; what the libraries decode
    and parse is released by the walk releasers (see
    ``TestDecodedStreamsAreReleased``). Closing all pages after the loop would not help, so the
    order is asserted, not just the count.
    """

    @staticmethod
    def _assert_each_extracted_page_closed_before_the_next(pages, events):
        extracted = [i for kind, i in events if kind == "extract"]
        assert extracted, "no page was extracted"
        for i in extracted:
            assert ("close", i) in events, (
                f"page {i} was extracted but never closed — its layout "
                "cache stays alive for the rest of the walk"
            )
            if ("extract", i + 1) in events:
                assert events.index(("close", i)) < events.index(
                    ("extract", i + 1)
                ), f"page {i} was not closed before page {i + 1} was walked"

    def test_full_walk_closes_every_page(self, service, mocker):
        events = []
        pages = _recording_pages([f"page {i}" for i in range(5)], events)
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text == "\n\n".join(f"page {i}" for i in range(5))
        assert all(page.close.called for page in pages)
        self._assert_each_extracted_page_closed_before_the_next(pages, events)

    def test_page_ceiling_break_closes_every_walked_page(self, service, mocker):
        events = []
        pages = _recording_pages(
            ["page text"] * (MAX_PDF_EXTRACTION_PAGES + 5), events
        )
        _pdf_with_pages(mocker, pages)

        assert service._extract_text_from_pdf(b"%PDF-fake")

        assert all(
            page.close.called for page in pages[:MAX_PDF_EXTRACTION_PAGES]
        ), "a page walked before the page ceiling was left open"
        self._assert_each_extracted_page_closed_before_the_next(pages, events)

    def test_character_ceiling_break_closes_the_tripping_page(
        self, service, mocker
    ):
        events = []
        pages = _recording_pages(
            ["x" * 10, "y" * MAX_PDF_EXTRACTED_CHARS, "never reached"], events
        )
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert len(text) == MAX_PDF_EXTRACTED_CHARS
        assert pages[1].close.called, (
            "the page that tripped the character ceiling was left open"
        )
        assert not pages[2].extract_text.called
        self._assert_each_extracted_page_closed_before_the_next(pages, events)

    def test_cpu_budget_break_closes_every_walked_page(self, service, mocker):
        clock = {"now": 0.0}
        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])
        events = []
        pages = _recording_pages(["first", "second", "third"], events)

        def slow_second_page():
            events.append(("extract", 1))
            clock["now"] += MAX_PDF_EXTRACTION_CPU_SECONDS + 1.0
            return "second"

        pages[1].extract_text.side_effect = slow_second_page
        _pdf_with_pages(mocker, pages)

        text = service._extract_text_from_pdf(b"%PDF-fake")

        assert text == "first\n\nsecond"
        assert not pages[2].extract_text.called
        assert pages[0].close.called and pages[1].close.called, (
            "a page walked before the CPU budget ran out was left open"
        )
        self._assert_each_extracted_page_closed_before_the_next(pages, events)

    def test_failing_page_is_closed(self, service, mocker):
        events = []
        pages = _recording_pages(["first", "second"], events)
        pages[1].extract_text.side_effect = ValueError("broken content stream")
        _pdf_with_pages(mocker, pages)

        assert service._extract_text_from_pdf(b"%PDF-fake") is None

        assert pages[0].close.called and pages[1].close.called, (
            "a page whose extraction raised was left open"
        )

    def test_failing_close_does_not_discard_the_text(self, service, mocker):
        pages = _pages_of(["first", "second"])
        pages[0].close.side_effect = RuntimeError("cache flush failed")
        _pdf_with_pages(mocker, pages)

        assert service._extract_text_from_pdf(b"%PDF-fake") == (
            "first\n\nsecond"
        ), "a close() failure aborted the extraction"
        assert pages[1].close.called


# --- What the PDF libraries cache between pages ---------------------------
#
# The tests below use the real pdfplumber/pdfminer and pypdf on PDFs built
# here with the standard library, and inspect the libraries' caches
# directly: RSS is too noisy to assert on. The releasers also measure the
# process's resident memory; that is off here (_no_resident_memory), and
# the tests of it feed it values of their own.


@pytest.fixture(autouse=True)
def _no_resident_memory(monkeypatch):
    """Keep the releasers' resident-memory bound out of tests that pin
    which page a release falls on: the process's memory moves with
    whatever else the test run allocates."""
    from local_deep_research.utilities import pdf_stream_release

    monkeypatch.setattr(
        pdf_stream_release, "_resident_bytes", lambda: None, raising=False
    )


_SERVICE_MODULE = (
    "local_deep_research.research_library.services.download_service"
)
_RETAINED_LIMIT = (
    "local_deep_research.utilities.pdf_extraction_limits."
    "MAX_PDF_RETAINED_DECODED_BYTES"
)
_SPACING = "local_deep_research.utilities.pdf_stream_release._COLLECT_SPACING"
_PAD = 2 * 1024 * 1024  # decoded bytes of whitespace per content stream
_TOUNICODE_LAST = 0x0FFF  # last code of _crafted_pdf's shared /ToUnicode


def _crafted_pdf(
    pages,
    pad=0,
    *,
    late_refs=0,
    widths=0,
    objstm=False,
    unicode_fonts=0,
    inline=False,
    form_uses=0,
    ops=b"",
    simple_fonts=0,
    shared_widths=0,
    font_file=0,
    length1=None,
    cid_fonts=0,
    registry=0,
    registry_inline=False,
    unicode_dest=b"0000",
    descriptor_extra=b"",
    matrix=None,
    form_extra=b"",
):
    """A PDF whose page ``i`` shows ``Page i``.

    Each page has its own FlateDecode content stream: the text operator
    plus *pad* spaces, which compress to almost nothing and yield no
    characters. ``late_refs`` appends that many extra pages that reuse
    the content streams of the first pages, in turn. ``widths`` gives every page
    its own non-standard font whose ``/Widths`` is a separate array
    object of that many entries.

    ``objstm`` writes a PDF 1.5 file with a cross-reference stream and
    object streams instead: one holding the catalog, the page tree and
    the pages (parsed before a walk's first page) and, with ``widths``,
    one per page holding that page's font and its ``/Widths`` array
    (parsed while the page is extracted). Streams stay top-level objects.

    ``unicode_fonts`` gives every page that many fonts of its own (the
    first shows the text), all pointing at one shared compressed
    ``/ToUnicode`` CMap whose single ``bfrange`` maps codes 0 to
    ``_TOUNICODE_LAST`` to themselves. With ``inline`` those font
    dictionaries are written inline in the resource dictionary instead
    of as objects of their own, and with ``form_uses`` they are the
    resources of a page's own Form XObject (which draws nothing) rather
    than the page's: the page shows its text in Helvetica and invokes
    the form that many times. ``ops`` is appended to every page's
    text operator.

    ``simple_fonts`` gives every page that many non-standard Type 1
    fonts of its own (objects; the page shows its text in Helvetica):
    with ``shared_widths`` they all point at one ``/Widths`` array
    object of that many entries, and with ``font_file`` at one
    FlateDecode ``/FontFile`` of that many spaces, whose ``/Length1`` is
    *length1* (by default one less), and have no ``/Encoding``. With
    ``inline`` they are written inline, and with ``form_uses`` they are
    the form's resources, as above.

    ``cid_fonts`` gives every page that many CIDFontType2 fonts of its
    own (objects; the page shows its text in Helvetica) whose
    ``/CIDSystemInfo /Registry`` is one string object of *registry*
    bytes that they all share, or with ``registry_inline`` a string of
    that size written in each font's /CIDSystemInfo dictionary.

    ``unicode_dest`` is the hex destination of the /ToUnicode
    ``bfrange`` (each code maps to it with its last four bytes counting
    up). ``descriptor_extra`` is appended to the simple fonts' font
    descriptor, and with ``matrix`` they are Type 3 fonts with that
    /FontMatrix instead. ``form_extra`` is written into the Form
    XObject's dictionary (with ``form_uses``), whose content is then
    compressed.
    """
    import struct
    import zlib

    bodies = []
    # Object number -> key of the object stream it is written into.
    packed = {}

    def add(body, group=None):
        bodies.append(body)
        if objstm and group is not None:
            packed[len(bodies)] = group
        return len(bodies)

    catalog = add(None, "pre-walk")
    root = add(None, "pre-walk")
    helvetica = add(
        b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", "pre-walk"
    )

    def font_resources(fonts):
        return b" ".join(
            b"/F%d %s" % (k, f if isinstance(f, bytes) else b"%d 0 R" % f)
            for k, f in enumerate(fonts, 1)
        )

    def page(font, stream, xobject=b""):
        fonts = font if isinstance(font, list) else [font]
        return add(
            b"<< /Type /Page /Parent %d 0 R /MediaBox [0 0 612 792] "
            b"/Resources << /Font << %s >> %s>> /Contents %d 0 R >>"
            % (root, font_resources(fonts), xobject, stream),
            "pre-walk",
        )

    if unicode_fonts:
        cmap = zlib.compress(
            b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap "
            b"1 begincodespacerange <0000> <FFFF> endcodespacerange "
            b"1 beginbfrange <0000> <%04X> <%s> endbfrange "
            b"endcmap end end" % (_TOUNICODE_LAST, unicode_dest)
        )
        to_unicode = add(
            b"<< /Length %d /Filter /FlateDecode >>\nstream\n" % len(cmap)
            + cmap
            + b"\nendstream"
        )
    simple_spec = b"<< /Type /Font /Subtype /Type1 /BaseFont /Crafted"
    descriptor = (
        b"<< /Type /FontDescriptor /FontName /Crafted /FontBBox [0 0 1 1]"
    )
    if shared_widths:
        simple_spec += b" /FirstChar 0 /Widths %d 0 R" % add(
            b"[" + b"500 " * shared_widths + b"]"
        )
    if font_file:
        program = zlib.compress(b" " * font_file)
        descriptor += b" /FontFile %d 0 R" % add(
            b"<< /Length %d /Length1 %d /Length2 0 /Length3 0"
            b" /Filter /FlateDecode >>\nstream\n"
            % (len(program), font_file - 1 if length1 is None else length1)
            + program
            + b"\nendstream"
        )
    simple_spec += b" /FontDescriptor " + descriptor + descriptor_extra
    simple_spec += b" >> >>"
    if matrix is not None:
        simple_spec = (
            b"<< /Type /Font /Subtype /Type3 /FontBBox [0 0 1 1] "
            b"/FontMatrix %s /CharProcs << >> /Encoding << /Differences [] "
            b">> /FirstChar 0 /LastChar 0 /Widths [0] /Resources << >> >>"
            % matrix
        )
    if cid_fonts:
        string = b"(" + b"R" * registry + b")"
        cid_spec = (
            b"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /CraftedCID "
            b"/CIDSystemInfo << /Registry %s /Ordering (Identity) "
            b"/Supplement 0 >> /FontDescriptor << /Type /FontDescriptor "
            b"/FontName /CraftedCID /FontBBox [0 0 1 1] >> /W [0 [500]] >>"
            % (string if registry_inline else b"%d 0 R" % add(string))
        )
    kids, streams = [], []
    for i in range(pages):
        font, xobject = helvetica, b""
        if simple_fonts:
            font = [helvetica] + [
                simple_spec if inline else add(simple_spec, i)
                for _ in range(simple_fonts)
            ]
        if cid_fonts:
            font = [helvetica] + [add(cid_spec, i) for _ in range(cid_fonts)]
        if unicode_fonts:
            spec = (
                b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica "
                b"/ToUnicode %d 0 R >>" % to_unicode
            )
            font = [
                spec if inline else add(spec, i) for _ in range(unicode_fonts)
            ]
        if form_uses:
            # With form_extra the form's content is compressed, so that
            # pypdf keeps it as an encoded stream.
            content = zlib.compress(b"q Q") if form_extra else b"q Q"
            form = add(
                b"<< /Type /XObject /Subtype /Form /BBox [0 0 612 792] %s"
                b"/Resources << /Font << %s >> >> /Length %d %s>>\nstream\n"
                % (
                    form_extra,
                    font_resources(font if isinstance(font, list) else [font]),
                    len(content),
                    b"/Filter /FlateDecode " if form_extra else b"",
                )
                + content
                + b"\nendstream"
            )
            font, xobject = helvetica, b"/XObject << /X1 %d 0 R >> " % form
        elif widths:
            array = add(b"[" + b"500 " * widths + b"]", i)
            font = add(
                b"<< /Type /Font /Subtype /Type1 /BaseFont /Crafted%d "
                b"/FirstChar 0 /LastChar 255 /Widths %d 0 R >>" % (i, array),
                i,
            )
        data = zlib.compress(
            b"BT /F1 12 Tf 72 720 Td (Page %d) Tj ET\n" % i
            + ops
            + b"/X1 Do\n" * form_uses
            + b" " * pad
        )
        streams.append(
            add(
                b"<< /Length %d /Filter /FlateDecode >>\nstream\n" % len(data)
                + data
                + b"\nendstream"
            )
        )
        kids.append(page(font, streams[-1], xobject))
    for i in range(late_refs):
        kids.append(page(helvetica, streams[i % pages]))
    bodies[catalog - 1] = b"<< /Type /Catalog /Pages %d 0 R >>" % root
    bodies[root - 1] = b"<< /Type /Pages /Count %d /Kids [%s] >>" % (
        len(kids),
        b" ".join(b"%d 0 R" % kid for kid in kids),
    )
    if objstm:
        return _with_object_streams(bodies, packed, catalog, struct)
    out = bytearray(b"%PDF-1.4\n")
    offsets = []
    for number, body in enumerate(bodies, start=1):
        offsets.append(len(out))
        out += b"%d 0 obj\n" % number + body + b"\nendobj\n"
    xref = len(out)
    out += b"xref\n0 %d\n0000000000 65535 f \n" % (len(bodies) + 1)
    out += b"".join(b"%010d 00000 n \n" % offset for offset in offsets)
    out += b"trailer\n<< /Size %d /Root %d 0 R >>\nstartxref\n%d\n%%%%EOF\n" % (
        len(bodies) + 1,
        catalog,
        xref,
    )
    return bytes(out)


def _with_object_streams(bodies, packed, catalog, struct):
    """Serialise *bodies* as PDF 1.5: the objects in *packed* go into one
    object stream per group (in first-member order), the rest stay
    top-level, and a cross-reference stream indexes both."""
    groups = {}
    for number, group in packed.items():
        groups.setdefault(group, []).append(number)
    # Object number -> cross-reference stream row (type, field 2, field 3).
    rows = {0: (0, 0, 65535)}
    out = bytearray(b"%PDF-1.5\n")
    for number, body in enumerate(bodies, start=1):
        if number not in packed:
            rows[number] = (1, len(out), 0)
            out += b"%d 0 obj\n" % number + body + b"\nendobj\n"
    number = len(bodies)
    for members in groups.values():
        number += 1
        header, data = [], bytearray()
        for index, member in enumerate(members):
            header.append(b"%d %d" % (member, len(data)))
            data += bodies[member - 1] + b"\n"
            rows[member] = (2, number, index)
        head = b" ".join(header) + b"\n"
        rows[number] = (1, len(out), 0)
        out += (
            b"%d 0 obj\n<< /Type /ObjStm /N %d /First %d /Length %d >>\n"
            b"stream\n"
            % (number, len(members), len(head), len(head + data))
            + head
            + data
            + b"\nendstream\nendobj\n"
        )
    number += 1
    rows[number] = (1, len(out), 0)
    table = b"".join(struct.pack(">BIH", *rows[n]) for n in range(number + 1))
    xref = len(out)
    out += (
        b"%d 0 obj\n<< /Type /XRef /Size %d /W [1 4 2] /Root %d 0 R "
        b"/Length %d >>\nstream\n"
        % (number, number + 1, catalog, len(table))
        + table
        + b"\nendstream\nendobj\n"
    )
    out += b"startxref\n%d\n%%%%EOF\n" % xref
    return bytes(out)


def _page_texts(count, sep):
    return sep.join(f"Page {i}" for i in range(count))


def _capture(mocker, target, real):
    """Patch *target* with a pass-through to *real* that records results."""
    made = []

    def passthrough(*args, **kwargs):
        made.append(real(*args, **kwargs))
        return made[-1]

    mocker.patch(target, side_effect=passthrough)
    return made


class _FakeFont(SimpleNamespace):
    """A stand-in for a pdfminer font: attributes as given, and weakly
    referenceable like the real ones."""


def _fake_rsrcmgr(cached=None):
    """A real pdfminer resource manager whose ``get_font()`` hands back
    the *spec* it is given as the font built, so a test can stand any
    object in for a font; *cached* seeds its font cache."""
    from pdfminer.pdfinterp import PDFResourceManager

    class Manager(PDFResourceManager):
        def get_font(self, objid, spec):
            return spec

    manager = Manager()
    manager._cached_fonts.update(cached or {})
    return manager


def _kept_elements(releaser):
    """Elements of the font tables *releaser* still holds. Right after a
    release these are the tables something else holds (pdfminer's
    built-in encodings and metrics), which the release cannot free: they
    were counted once, and are not counted again."""
    from pdfminer.pdftypes import PDFStream

    from local_deep_research.utilities.pdf_stream_release import (
        _count_elements,
    )

    return sum(
        max(1, _count_elements(table, (PDFStream,), []))
        for table in releaser._tables
    )


def _pdfminer_decoded(pdf, pages):
    """Decoded bytes still held by cached objects and walked pages."""
    from pdfminer.pdftypes import PDFStream

    streams = [obj for obj, _ in pdf.doc._cached_objs.values()]
    for page in pages:
        streams.extend(page.page_obj.contents)
    return sum(
        len(obj.data)
        for obj in streams
        if isinstance(obj, PDFStream) and obj.data is not None
    )


def _capture_pdfplumber_walk(mocker):
    """Capture pages yielded by the lazy PDF page walker."""
    from local_deep_research.utilities.pdf_page_walk import (
        iter_pdfplumber_pages,
    )

    walked_pages = []

    def capture_walk(pdf, releaser):
        for page in iter_pdfplumber_pages(pdf, releaser):
            walked_pages.append(page)
            yield page

    mocker.patch(
        "local_deep_research.utilities.pdf_page_walk.iter_pdfplumber_pages",
        side_effect=capture_walk,
    )
    return walked_pages


def _pypdf_decoded(reader):
    from pypdf.generic import EncodedStreamObject

    return sum(
        len(obj.decoded_self.get_data())
        for obj in reader.resolved_objects.values()
        if isinstance(obj, EncodedStreamObject) and obj.decoded_self is not None
    )


class TestDecodedStreamsAreReleased:
    """A walk must not keep every page's decoded streams until it returns.

    ``page.close()`` frees only pdfplumber's per-page caches. pdfminer
    keeps each decoded stream on its ``PDFStream`` (``.data``) and pypdf
    on its ``EncodedStreamObject`` (``.decoded_self``), both reachable
    from the document's object cache. Measured before the release: a
    page of 20 MB of spaces (~20 KB compressed, no characters) added
    ~20 MB per page -- ~10 GB at the page ceiling from a ~10 MB file.
    """

    def test_pdfplumber_walk_releases_decoded_streams(self, service, mocker):
        import pdfplumber

        mocker.patch(_RETAINED_LIMIT, 1024 * 1024)
        walked_pages = _capture_pdfplumber_walk(mocker)
        opened = _capture(
            mocker, f"{_SERVICE_MODULE}.pdfplumber.open", pdfplumber.open
        )

        text = service._extract_text_from_pdf(_crafted_pdf(3, _PAD))

        assert text == _page_texts(3, "\n\n")
        assert not hasattr(opened[0], "_pages"), (
            "service extraction eagerly materialized pdfplumber's page list"
        )
        assert _pdfminer_decoded(opened[0], walked_pages) == 0, (
            "pdfminer still holds decoded content streams after the walk"
        )

    def test_decoded_streams_below_the_threshold_are_left_alone(
        self, service, mocker
    ):
        """Control: without a release the streams stay -- so the test
        above can see retention -- and honest documents, far below the
        threshold, are not touched."""
        import pdfplumber

        walked_pages = _capture_pdfplumber_walk(mocker)
        opened = _capture(
            mocker, f"{_SERVICE_MODULE}.pdfplumber.open", pdfplumber.open
        )

        text = service._extract_text_from_pdf(_crafted_pdf(3, _PAD))

        assert text == _page_texts(3, "\n\n")
        assert _pdfminer_decoded(opened[0], walked_pages) >= 3 * _PAD

    def test_streams_a_later_page_reuses_still_extract(self, service, mocker):
        """Rewound streams decode again when a later page reads them:
        pages 3-5 reuse the content streams of pages 0-2, and a release
        runs after every page."""
        import pdfplumber

        mocker.patch(_RETAINED_LIMIT, 1)
        walked_pages = _capture_pdfplumber_walk(mocker)
        opened = _capture(
            mocker, f"{_SERVICE_MODULE}.pdfplumber.open", pdfplumber.open
        )

        text = service._extract_text_from_pdf(
            _crafted_pdf(3, _PAD, late_refs=3)
        )

        assert text == "\n\n".join([_page_texts(3, "\n\n")] * 2)
        assert _pdfminer_decoded(opened[0], walked_pages) == 0

    def test_pypdf_fallback_releases_decoded_streams(self, service, mocker):
        from pypdf import PdfReader

        mocker.patch(_RETAINED_LIMIT, 1024 * 1024)
        _pdf_with_pages(mocker, _pages_of([""]))
        readers = _capture(mocker, f"{_SERVICE_MODULE}.PdfReader", PdfReader)

        text = service._extract_text_from_pdf(
            _crafted_pdf(3, _PAD, late_refs=3)
        )

        assert text == "\n\n".join([_page_texts(3, "\n\n")] * 2)
        assert _pypdf_decoded(readers[0]) == 0, (
            "pypdf still holds decoded content streams after the fallback"
        )

    def test_base_downloader_releases_decoded_streams(self, mocker):
        from pypdf import PdfReader

        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        mocker.patch(_RETAINED_LIMIT, 1024 * 1024)
        readers = _capture(mocker, "pypdf.PdfReader", PdfReader)

        text = BaseDownloader.extract_text_from_pdf(_crafted_pdf(3, _PAD))

        assert text == _page_texts(3, "\n")
        assert _pypdf_decoded(readers[0]) == 0

    def test_pypdf_below_the_threshold_is_left_alone(self, mocker):
        from pypdf import PdfReader

        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        readers = _capture(mocker, "pypdf.PdfReader", PdfReader)

        assert BaseDownloader.extract_text_from_pdf(
            _crafted_pdf(3, _PAD)
        ) == _page_texts(3, "\n")
        assert _pypdf_decoded(readers[0]) >= 3 * _PAD


class TestParsedObjectsAreReleased:
    """Parsed objects a page resolves count against the same threshold.

    pdfminer's fixture has 20,000 font widths (~2.6 MB at the releaser's
    per-element estimate) and trips a 1 MiB threshold. pypdf 6.19 rejects
    width arrays above 256 entries, so its valid fixture uses that limit
    and a 16 KiB threshold. Both have tiny content streams, so only the
    parsed-object estimate can trip the threshold.
    """

    _ENTRIES = 20_000
    _PYPDF_ENTRIES = 256

    def test_pdfplumber_walk_drops_parsed_objects(self, service, mocker):
        import pdfplumber

        mocker.patch(_RETAINED_LIMIT, 1024 * 1024)
        opened = _capture(
            mocker, f"{_SERVICE_MODULE}.pdfplumber.open", pdfplumber.open
        )

        text = service._extract_text_from_pdf(
            _crafted_pdf(3, widths=self._ENTRIES)
        )

        pdf = opened[0]
        assert text is not None and "Page 2" in text
        assert not [
            obj
            for obj, _ in pdf.doc._cached_objs.values()
            if isinstance(obj, list) and len(obj) >= self._ENTRIES
        ], "a parsed /Widths array outlived the release"
        assert pdf.rsrcmgr._cached_fonts == {}, (
            "font objects (and their widths) outlived the release"
        )

    def test_pypdf_walk_drops_parsed_objects(self, mocker):
        from pypdf import PdfReader

        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        mocker.patch(_RETAINED_LIMIT, 16 * 1024)
        readers = _capture(mocker, "pypdf.PdfReader", PdfReader)

        text = BaseDownloader.extract_text_from_pdf(
            _crafted_pdf(3, widths=self._PYPDF_ENTRIES)
        )

        assert text is not None and "Page 2" in text
        assert not [
            obj
            for obj in readers[0].resolved_objects.values()
            if isinstance(obj, list) and len(obj) >= self._PYPDF_ENTRIES
        ], "a parsed /Widths array outlived the release"

    def test_parsed_objects_below_the_threshold_are_left_alone(self, mocker):
        """Control for the two tests above."""
        from pypdf import PdfReader

        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        readers = _capture(mocker, "pypdf.PdfReader", PdfReader)

        text = BaseDownloader.extract_text_from_pdf(
            _crafted_pdf(3, widths=self._PYPDF_ENTRIES)
        )

        assert text is not None and "Page 2" in text
        assert [
            obj
            for obj in readers[0].resolved_objects.values()
            if isinstance(obj, list) and len(obj) >= self._PYPDF_ENTRIES
        ]


class TestWalkReleasers:
    """The releasers directly, on the real libraries."""

    def test_pdfminer_page_tree_is_kept_and_streams_keep_identity(self, mocker):
        import io

        import pdfplumber
        from pdfminer.pdftypes import PDFStream

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        mocker.patch(_RETAINED_LIMIT, 1)
        with pdfplumber.open(io.BytesIO(_crafted_pdf(2, _PAD))) as pdf:
            pages = pdf.pages
            pre_walk = dict(pdf.doc._cached_objs)
            releaser = PdfplumberWalkReleaser(pdf)
            for page in pages:
                releaser.before_page()
                assert page.extract_text() == f"Page {page.page_number - 1}"
                page.close()
                assert releaser.after_page() is True
            # after_page() without a before_page() has nothing to count.
            assert releaser.after_page() is False
            assert _pdfminer_decoded(pdf, pages) == 0
            for objid, (obj, _) in pre_walk.items():
                assert pdf.doc._cached_objs[objid][0] is obj, (
                    "an object cached before the walk was dropped or replaced"
                )
            for page in pages:
                for stream in page.page_obj.contents:
                    assert isinstance(stream, PDFStream)
                    assert pdf.doc._cached_objs[stream.objid][0] is stream, (
                        "a page's content stream was replaced in the cache"
                    )

    def test_pypdf_page_tree_is_kept_and_pre_walk_streams_are_released(
        self, mocker
    ):
        import io

        from pypdf import PdfReader

        from local_deep_research.utilities.pdf_stream_release import (
            PypdfWalkReleaser,
        )

        mocker.patch(_RETAINED_LIMIT, 1)
        reader = PdfReader(io.BytesIO(_crafted_pdf(2, _PAD)))
        pages = list(reader.pages)
        # Decode page 0's content stream before the walk begins, so the
        # stream is part of the pre-walk cache.
        pages[0].get_contents().get_data()
        pre_walk = dict(reader.resolved_objects)
        assert _pypdf_decoded(reader) >= _PAD

        releaser = PypdfWalkReleaser(reader)
        for page in pages:
            releaser.before_page()
            assert page.extract_text().strip().startswith("Page")
            assert releaser.after_page() is True

        assert _pypdf_decoded(reader) == 0
        for key, obj in pre_walk.items():
            assert reader.resolved_objects.get(key) is obj, (
                "an object cached before the walk was dropped or replaced"
            )

    def test_failed_rewind_leaves_the_stream_intact(self, mocker):
        """If the raw bytes cannot be read back, the decoded stream stays
        as it was -- same object, still cached, still decodable."""
        import io

        import pdfplumber
        from pdfminer.pdftypes import PDFStream

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        mocker.patch(_RETAINED_LIMIT, 1)
        with pdfplumber.open(io.BytesIO(_crafted_pdf(1, _PAD))) as pdf:
            page = pdf.pages[0]
            releaser = PdfplumberWalkReleaser(pdf)
            releaser.before_page()
            page.extract_text()
            page.close()
            cached = {
                objid: obj
                for objid, (obj, _) in pdf.doc._cached_objs.items()
                if isinstance(obj, PDFStream) and obj.data is not None
            }
            assert cached
            # Scoped: PDF.close() on leaving the with block walks the page
            # tree again through getobj().
            with patch.object(
                pdf.doc, "getobj", side_effect=RuntimeError("unreadable")
            ):
                assert releaser.after_page() is True

            for objid, obj in cached.items():
                assert pdf.doc._cached_objs[objid][0] is obj
                assert obj.get_data().startswith(b"BT")

    def test_releasers_never_raise_on_unexpected_objects(self):
        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
            PypdfWalkReleaser,
        )

        class Exploding:
            def __getattr__(self, name):
                raise RuntimeError(name)

        for target in (MagicMock(), Exploding(), object()):
            for cls in (PdfplumberWalkReleaser, PypdfWalkReleaser):
                releaser = cls(target)
                releaser.before_page()
                assert releaser.after_page() is False

    def test_parsed_object_stream_cache_is_cleared(self, mocker):
        """pdfminer caches the objects parsed out of each object stream
        apart from the object cache; a release must drop those too."""
        from pdfminer.pdftypes import PDFStream

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        mocker.patch(_RETAINED_LIMIT, 1)
        stream = PDFStream({}, b"raw")
        stream.decode()
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {7: ([[0] * 10], 1)}
        pdf.rsrcmgr = _fake_rsrcmgr({"F1": _FakeFont()})
        releaser = PdfplumberWalkReleaser(pdf)
        releaser.before_page()
        pdf.doc._cached_objs[1] = (stream, 0)

        assert releaser.after_page() is True

        assert pdf.doc._parsed_objs == {}
        assert pdf.rsrcmgr._cached_fonts == {}

    def test_element_count_descends_containers_but_not_streams(self):
        from local_deep_research.utilities.pdf_stream_release import (
            _count_elements,
        )

        class Stream(dict):
            pass

        nested = {"a": [1, 2, [3, 4]], "b": Stream(x=list(range(100)))}
        # 2 dict entries + 3 list items + 2 nested list items; the
        # stream is skipped.
        assert _count_elements(nested, (Stream,)) == 7

    def test_long_dictionary_keys_count_by_size(self):
        """Keys count by their size like values: pdfminer turns a name
        that is not UTF-8 into the text of its repr, about four
        characters a byte, so a dictionary of long names held about four
        times what its entries counted."""
        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            _count_elements,
        )

        key = "k" * (64 * _BYTES_PER_PARSED_ELEMENT)
        assert _count_elements({key: 1}, ()) >= 1 + 64
        assert _count_elements({1: key}, ()) >= 1 + 64
        assert _count_elements({"Type": 1, 2: 3}, ()) == 2

    def test_containers_something_else_holds_are_told_apart(self):
        """With a *shared* list, ``_count_elements()`` hands back, rather
        than descends into, a nested container that something besides
        its parent holds (``sys.getrefcount()``, calibrated once per
        interpreter), so that a font build's new container holding
        parts of a parsed object does not make the walk cost more than
        the build."""
        from local_deep_research.utilities.pdf_stream_release import (
            _count_elements,
        )

        held_elsewhere = [0] * 1000
        table = (held_elsewhere, [1, 2], {"a": [3]})
        shared = []
        assert _count_elements(table, (), shared) == 3 + 2 + 1 + 1
        assert len(shared) == 1
        assert shared[0] is held_elsewhere
        # Without a list to hand them back in, everything is descended.
        assert _count_elements(table, ()) == 3 + 1000 + 2 + 1 + 1

    def test_empty_and_scalar_objects_are_not_free(self, mocker):
        """Each object the walk caches counts at least one element, so a
        page that caches many empty arrays or plain numbers still adds
        to the estimate."""
        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
            PypdfWalkReleaser,
        )

        objects = 100
        mocker.patch(_RETAINED_LIMIT, objects * _BYTES_PER_PARSED_ELEMENT)

        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = _fake_rsrcmgr()
        releaser = PdfplumberWalkReleaser(pdf)
        releaser.before_page()
        for objid in range(objects):
            pdf.doc._cached_objs[objid] = ([] if objid % 2 else 1000 + objid, 0)
        assert releaser.after_page() is True
        assert pdf.doc._cached_objs == {}

        reader = MagicMock()
        reader.resolved_objects = {}
        releaser = PypdfWalkReleaser(reader)
        releaser.before_page()
        for key in range(objects):
            reader.resolved_objects[(key, 0)] = [] if key % 2 else 1000 + key
        assert releaser.after_page() is True
        assert reader.resolved_objects == {}

    def test_stream_dictionaries_count(self, mocker):
        """A stream's dictionary is parsed like any other, and counts
        like one, without the stream being decoded: a Form XObject whose
        dictionary held hundreds of thousands of empty arrays cost ~30
        times its file bytes and counted one element, under pdfminer
        and pypdf alike."""
        import io

        import pdfplumber
        from pypdf import PdfReader

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
            PypdfWalkReleaser,
        )

        arrays = 20_000
        mocker.patch(_RETAINED_LIMIT, arrays * _BYTES_PER_PARSED_ELEMENT)
        data = _crafted_pdf(
            1, form_uses=1, form_extra=b"/Junk [" + b"[] " * arrays + b"] "
        )
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            (page,) = pdf.pages
            releaser = PdfplumberWalkReleaser(pdf)
            releaser.before_page()
            assert page.extract_text() == "Page 0"
            page.close()
            assert releaser.after_page() is True
            assert releaser._enabled
        reader = PdfReader(io.BytesIO(data))
        (page,) = reader.pages
        releaser = PypdfWalkReleaser(reader)
        releaser.before_page()
        assert "Page 0" in page.extract_text()
        assert releaser.after_page() is True
        assert releaser._enabled

    def test_parsed_object_streams_count_against_the_threshold(self, mocker):
        """pdfminer parses every object of an object stream into one list
        kept in ``_parsed_objs``, apart from the object cache; those
        objects count against the threshold even when no page resolved
        them through the object cache."""
        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        tokens = 1000
        mocker.patch(_RETAINED_LIMIT, tokens * _BYTES_PER_PARSED_ELEMENT)
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        # A pre-walk object stream big enough to cross the threshold on
        # its own: only before_page()'s _objstm_mark keeps it out of the
        # first page's estimate.
        pdf.doc._parsed_objs = {3: ([[] for _ in range(tokens)], 0)}
        pdf.rsrcmgr = _fake_rsrcmgr()
        releaser = PdfplumberWalkReleaser(pdf)

        # Parsed before the walk's page: not this page's cost.
        releaser.before_page()
        assert releaser.after_page() is False
        assert 3 in pdf.doc._parsed_objs

        releaser.before_page()
        pdf.doc._parsed_objs[9] = ([[] for _ in range(tokens)], 0)
        assert releaser.after_page() is True
        assert pdf.doc._parsed_objs == {}

    def test_object_stream_objects_are_counted_once(self, mocker):
        """An object resolved out of an object stream is the very object
        in that stream's list: it is counted there, not again when it
        reaches the object cache, on the same page or a later one."""
        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        mocker.patch(_RETAINED_LIMIT, 1 << 40)
        widths, font = [0] * 1000, {"Widths": None}
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = _fake_rsrcmgr()
        releaser = PdfplumberWalkReleaser(pdf)

        releaser.before_page()
        pdf.doc._parsed_objs[7] = ([4, 0, 5, 9, widths, font], 2)
        pdf.doc._cached_objs[4] = (widths, 0)
        assert releaser.after_page() is False
        # 6 list slots + 1000 widths + 1 font entry + 1 cache entry.
        assert releaser._parsed_elements == 1008

        releaser.before_page()
        pdf.doc._cached_objs[5] = (font, 0)
        assert releaser.after_page() is False
        assert releaser._parsed_elements == 1009

    def test_unexpected_object_stream_entries_are_never_under_counted(
        self, mocker
    ):
        """after_page() reads pdfminer's ``(objects, count)`` pairs. An
        entry of another shape is counted whole when it is a container,
        and switches the releaser off (with the usual warning) when it
        is not, rather than letting it go uncounted."""
        from local_deep_research.utilities import pdf_stream_release

        mocker.patch(_RETAINED_LIMIT, 1 << 40)
        disabled = mocker.patch.object(pdf_stream_release, "_log_disabled")

        def walk(entry):
            pdf = MagicMock()
            pdf.doc._cached_objs = {}
            pdf.doc._parsed_objs = {}
            pdf.rsrcmgr = _fake_rsrcmgr()
            releaser = pdf_stream_release.PdfplumberWalkReleaser(pdf)
            releaser.before_page()
            pdf.doc._parsed_objs[7] = entry
            releaser.after_page()
            return releaser

        reshaped = walk({"objects": [[0] * 1000], "count": 1})
        assert reshaped._enabled
        assert reshaped._parsed_elements >= 1000
        disabled.assert_not_called()

        opaque = walk(object())
        assert not opaque._enabled
        disabled.assert_called_once()

    def test_object_streams_of_a_real_file_count_once_and_release(self, mocker):
        """End to end on a real PDF 1.5 file with object streams: each
        page counts the object stream it parsed (its font and /Widths)
        once, the large pre-walk object stream holding the page tree is
        not charged to the first page, and the release fires once two
        pages' worth is held -- not after the first page, as it did when
        each resolved object counted twice."""
        import io

        import pdfplumber
        from pdfminer.encodingdb import EncodingDB

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
            _count_elements,
        )

        entries = 2000
        # Per page: the object stream list (2 objects -> 6 slots), the
        # /Widths entries, the font's 6 dictionary entries, 3 cache
        # entries (the object stream, the array, the font), the object
        # stream's 4 dictionary entries, the pdfminer font and its own
        # widths table, a copy of the /Widths entries.
        # The fonts share the standard encoding's table and the
        # (0, 0, 0, 0) bounding box pdfminer gives a font without a
        # descriptor, counted once: pdfminer holds them, so a release
        # cannot free them and they are not counted again.
        per_page = 6 + entries + 6 + 3 + 4 + 1 + entries
        shared = len(EncodingDB.get_encoding("StandardEncoding")) + 4
        assert shared < entries // 2
        mocker.patch(
            _RETAINED_LIMIT,
            (per_page + entries // 2) * _BYTES_PER_PARSED_ELEMENT,
        )
        # 500 extra pages make the pre-walk object stream large: charged
        # to the first page it alone would cross the threshold.
        data = _crafted_pdf(3, widths=entries, late_refs=500, objstm=True)
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            pages = pdf.pages
            (pre_walk,) = pdf.doc._parsed_objs
            assert (
                _count_elements(pdf.doc._parsed_objs[pre_walk], ())
                > per_page + entries // 2
            )
            releaser = PdfplumberWalkReleaser(pdf)
            released, texts = [], []
            for page in pages[:3]:
                releaser.before_page()
                texts.append(page.extract_text())
                page.close()
                released.append(releaser.after_page())
                if len(released) == 1:
                    assert releaser._parsed_elements == per_page + shared
                    assert len(pdf.doc._parsed_objs) == 2

            assert texts == ["Page 0", "Page 1", "Page 2"]
            assert released == [False, True, False]
            assert releaser._enabled
            assert releaser._parsed_elements == per_page

    def test_a_release_forgets_the_ids_it_counted(self, mocker):
        """A release clears the ids of the objects and font tables it
        has counted: once they are dropped, CPython can hand their ids
        to new objects, which must then be counted in full. Here the
        very same objects come back after the release, standing in for
        new objects that reuse the freed ids."""
        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        entries = 1000
        mocker.patch(_RETAINED_LIMIT, entries * _BYTES_PER_PARSED_ELEMENT)
        array = [0] * entries
        font = _FakeFont(cid2unicode=dict.fromkeys(range(entries), "x"))
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = _fake_rsrcmgr()
        releaser = PdfplumberWalkReleaser(pdf)

        releaser.before_page()
        pdf.doc._parsed_objs[7] = ([4, 0, array], 1)
        pdf.rsrcmgr.get_font(9, font)
        assert releaser.after_page() is True

        # Top level in the object cache this time, not in an object
        # stream: counted in full, not as one already-counted entry.
        releaser.before_page()
        pdf.doc._cached_objs[4] = (array, 0)
        assert releaser.after_page() is True

        # A font table is let go of only once nothing else holds it, so
        # one that comes back is the same object, not a new one reusing
        # its id: the release could not free it, and it does not count
        # again (only the font, rebuilt, does).
        releaser.before_page()
        pdf.rsrcmgr.get_font(9, font)
        assert releaser.after_page() is False
        assert releaser._parsed_elements == 1
        table = id(font.cid2unicode)
        assert table in releaser._table_ids
        # Once nothing else holds it, the next release lets go of it,
        # and of its id.
        del font
        releaser.before_page()
        pdf.doc._cached_objs[5] = (array, 0)
        assert releaser.after_page() is True
        assert table not in releaser._table_ids
        assert releaser._tables == []


class TestFontTablesAreReleased:
    """Fonts pdfminer builds count against the same threshold.

    pdfminer builds every font in a page's resources, with tables it
    expands from short ranges: each font parses its ``/ToUnicode`` CMap
    into its own map, so one ``<0000> <FFFF> bfrange`` in a ~100-byte
    compressed stream that many fonts share becomes 65,536 entries per
    font. It caches the fonts that are objects of their own; a font
    written inline in a resource dictionary is built again on every use
    and cached nowhere, its map left in a reference cycle. Before these
    were counted, 20 pages of five such fonts (an 18 KB file, or a
    13 KB one with the fonts inline) held ~840 MB with the estimate at
    64 KB and no release. Here each font's map has 4,096 entries.
    """

    _FONTS = 3

    def test_shared_tounicode_fonts_count_and_release(self, mocker):
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        per_page = self._FONTS * (_TOUNICODE_LAST + 1)
        mocker.patch(
            _RETAINED_LIMIT, per_page * 3 // 2 * _BYTES_PER_PARSED_ELEMENT
        )
        # Collect at every release: a deferred collection leaves the
        # dropped fonts' maps held (by their parsers' cycles) until it
        # runs (test_estimated_releases_space_their_collections).
        mocker.patch(_SPACING, 0)
        data = _crafted_pdf(4, unicode_fonts=self._FONTS)
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            pages = pdf.pages
            releaser = PdfplumberWalkReleaser(pdf)
            released, texts, counted, kept = [], [], [], []
            for page in pages:
                releaser.before_page()
                texts.append(page.extract_text())
                page.close()
                released.append(releaser.after_page())
                counted.append(releaser._parsed_elements)
                kept.append(_kept_elements(releaser))

            assert texts == [f"Page {i}" for i in range(4)]
            assert counted[0] >= per_page
            assert released == [False, True, False, True]
            assert pdf.rsrcmgr._cached_fonts == {}
            # Fonts built again after the release count again, all but
            # pdfminer's built-in tables.
            assert counted[2] == counted[0] - kept[1]
            assert releaser._enabled

    def test_released_tounicode_maps_are_freed(self, mocker):
        """Dropping the fonts does not free their /ToUnicode maps: the
        CMapParser that built each map keeps it (and the decoded CMap
        bytes) in a reference cycle. The release collects those cycles
        itself; automatic collection is off here so that only the
        release can. Without it, a walk that released every second page
        still grew ~840 MB over 20 pages."""
        import gc
        import io
        import weakref

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        mocker.patch(_RETAINED_LIMIT, 1)
        # A release's collection is spaced like the others
        # (test_estimated_releases_space_their_collections).
        mocker.patch(_SPACING, 0)
        data = _crafted_pdf(2, unicode_fonts=self._FONTS)
        was_enabled = gc.isenabled()
        gc.disable()
        try:
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                pages = pdf.pages
                releaser = PdfplumberWalkReleaser(pdf)
                for page in pages:
                    releaser.before_page()
                    page.extract_text()
                    page.close()
                    maps = [
                        weakref.ref(font.unicode_map)
                        for font in pdf.rsrcmgr._cached_fonts.values()
                    ]
                    assert len(maps) == self._FONTS
                    assert releaser.after_page() is True
                    assert [ref() for ref in maps] == [None] * self._FONTS
        finally:
            if was_enabled:
                gc.enable()

    def test_released_tounicode_maps_are_collected_at_once(self, mocker):
        """A release spaces its collection like the others, except when
        what it lets go of is the threshold's worth of garbage that only
        a collection frees: the /ToUnicode maps of the fonts it drops,
        which their parsers' cycles hold. Then it collects at once,
        however long the last collection took. Deferring it held the
        maps for as long as nine full collections of the process's heap
        took: 40 pages of five fonts sharing a 65,536-entry map, with 8
        million objects held, peaked ~400 MB above the start (~70 MB
        now). Here the last collection seems to have taken a second,
        and automatic collection is off."""
        import gc
        import io
        import weakref

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        per_page = self._FONTS * (_TOUNICODE_LAST + 1)
        mocker.patch(
            _RETAINED_LIMIT, per_page * 3 // 2 * _BYTES_PER_PARSED_ELEMENT
        )
        data = _crafted_pdf(4, unicode_fonts=self._FONTS)
        was_enabled = gc.isenabled()
        gc.disable()
        try:
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                pages = pdf.pages
                releaser = PdfplumberWalkReleaser(pdf)
                released, maps = [], []
                for page in pages:
                    releaser.before_page()
                    releaser._collect_cost = 1.0
                    page.extract_text()
                    page.close()
                    maps += [
                        weakref.ref(font.unicode_map)
                        for font in pdf.rsrcmgr._cached_fonts.values()
                    ]
                    released.append(releaser.after_page())
                    if released[-1]:
                        assert [ref() for ref in maps] == [None] * len(maps)
                        maps = []
                assert released == [False, True, False, True]
                assert releaser._enabled
        finally:
            if was_enabled:
                gc.enable()

    def test_a_deferred_collection_runs_when_the_walk_ends(self, mocker):
        """A release that the decoded streams ask for, with less than the
        threshold's worth of /ToUnicode maps to free, defers its
        collection; if the walk ends first, the collection runs then (on
        ``close()``, or once the releaser is garbage), so that the walk
        does not end holding them. A walk that deferred nothing (an
        honest document's) collects nothing when it ends."""
        import gc
        import io
        import weakref

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        mocker.patch(_RETAINED_LIMIT, _PAD // 2)
        collect = mocker.patch.object(gc, "collect", wraps=gc.collect)
        was_enabled = gc.isenabled()
        gc.disable()
        try:
            # (pad, whether the release defers a collection, walk end)
            for pad, deferred, close in (
                (_PAD, True, False),
                (_PAD, True, True),
                (0, False, True),
            ):
                collect.reset_mock()
                data = _crafted_pdf(1, pad=pad, unicode_fonts=1)
                with pdfplumber.open(io.BytesIO(data)) as pdf:
                    (page,) = pdf.pages
                    releaser = PdfplumberWalkReleaser(pdf)
                    releaser.before_page()
                    releaser._collect_cost = 1.0
                    page.extract_text()
                    page.close()
                    (font,) = pdf.rsrcmgr._cached_fonts.values()
                    ref = weakref.ref(font.unicode_map)
                    font = None
                    assert releaser.after_page() is deferred
                    assert collect.call_count == 0
                    assert ref() is not None
                    if close:
                        releaser.close()
                    else:
                        del releaser
                    assert collect.call_count == int(deferred)
                    assert (ref() is None) is deferred
                    if close:
                        releaser.close()
                        del releaser
                    assert collect.call_count == int(deferred)
        finally:
            if was_enabled:
                gc.enable()

    def test_font_tables_count_once_and_only_for_new_fonts(self, mocker):
        """Each font counts one element plus every element nested in its
        tables (widths, simple or CID, displacements, maps); a table
        several fonts share counts once, and fonts cached before the
        page are not its cost."""
        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        mocker.patch(_RETAINED_LIMIT, 1 << 40)
        shared = dict.fromkeys(range(100), "x")

        def font(entries):
            return _FakeFont(
                widths=dict.fromkeys(range(entries), 500),
                cid2unicode=shared,
                # A new tuple per entry, as pdfminer builds them.
                disps={cid: (0, -cid) for cid in range(entries)},
                unicode_map=SimpleNamespace(
                    cid2unichr=dict.fromkeys(range(entries), "y")
                ),
            )

        pre = font(10_000)
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = _fake_rsrcmgr({"pre": pre})
        releaser = PdfplumberWalkReleaser(pdf)
        releaser.before_page()
        get_font = pdf.rsrcmgr.get_font
        # Handed out again from the cache: built before the walk.
        get_font("pre", pre)
        a = get_font("a", font(10))
        get_font(None, font(20))  # inline: built, never cached
        get_font("a", a)  # the same font again counts once
        # A font without tables (or a /ToUnicode map) counts one element.
        get_font(None, _FakeFont(unicode_map=None))
        # A simple font (no disps) builds its own widths too.
        get_font(
            "d",
            _FakeFont(
                widths=dict.fromkeys(range(1000), 500), cid2unicode=shared
            ),
        )
        # widths, disps (each entry a 2-tuple) and cid2unichr per font.
        expected = 4 + 100 + (10 + 30 + 10) + (20 + 60 + 20) + 1000
        assert releaser.after_page() is False
        assert releaser._parsed_elements == expected

        # Counted fonts are not counted again on a later page.
        releaser.before_page()
        get_font("a", a)
        assert releaser.after_page() is False
        assert releaser._parsed_elements == expected

    def test_fonts_sharing_one_widths_array_count_per_font(self, mocker):
        """Every simple font builds its own widths table from its
        /Widths array, so fonts sharing one array object each hold a
        copy of it; each copy counts. Before they did, 10 pages of six
        fonts sharing one 200,000-entry array grew ~97 MB a page with
        the estimate at 24 MB and no release."""
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        entries = 5000
        # A page's fonts hold 3 copies; the array counts once more when
        # first parsed. Two pages cross the threshold, one does not.
        mocker.patch(
            _RETAINED_LIMIT, entries * 11 // 2 * _BYTES_PER_PARSED_ELEMENT
        )
        data = _crafted_pdf(4, simple_fonts=3, shared_widths=entries)
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            pages = pdf.pages
            releaser = PdfplumberWalkReleaser(pdf)
            released, texts, counted, kept = [], [], [], []
            for page in pages:
                releaser.before_page()
                texts.append(page.extract_text())
                page.close()
                released.append(releaser.after_page())
                counted.append(releaser._parsed_elements)
                kept.append(_kept_elements(releaser))

            assert texts == [f"Page {i}" for i in range(4)]
            assert counted[0] >= 4 * entries
            assert released == [False, True, False, True]
            # All but pdfminer's built-in tables count again.
            assert counted[2] == counted[0] - kept[1]
            assert releaser._enabled

    def test_type1_header_copies_count_per_build(self, mocker):
        """A Type 1 font without /Encoding parses a fresh copy of its
        /FontFile's first /Length1 bytes, which the header parser keeps
        in a reference cycle: each build's copy counts, even when every
        font shares one /FontFile. Before it did, three fonts a page
        sharing a 20 MB /FontFile of spaces grew ~57 MB a page with no
        release."""
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        size = 256 * 1024
        # The decoded /FontFile counts once, a page's copies three times.
        mocker.patch(_RETAINED_LIMIT, size * 11 // 2)
        data = _crafted_pdf(4, simple_fonts=3, font_file=size)
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            pages = pdf.pages
            releaser = PdfplumberWalkReleaser(pdf)
            released, texts = [], []
            for page in pages:
                releaser.before_page()
                texts.append(page.extract_text())
                page.close()
                released.append(releaser.after_page())

            assert texts == [f"Page {i}" for i in range(4)]
            assert released == [False, True, False, True]
            assert releaser._enabled

    def test_font_parser_cycles_are_collected_between_releases(self, mocker):
        """The parsers a font build leaves in reference cycles (with the
        copy of the /FontFile header they read) are garbage once the
        build returns; the releaser collects them once what they were
        fed could hold the threshold's worth, before any release and in
        the middle of a page. Automatic collection is off here so that
        only the releaser can."""
        import gc
        import io
        import weakref

        import pdfminer.pdffont
        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _PARSER_BYTES_PER_INPUT_BYTE,
            PdfplumberWalkReleaser,
        )

        size = 64 * 1024
        parsers = []
        real = pdfminer.pdffont.Type1FontHeaderParser

        def recording(*args, **kwargs):
            parser = real(*args, **kwargs)
            parsers.append(weakref.ref(parser))
            return parser

        mocker.patch.object(
            pdfminer.pdffont, "Type1FontHeaderParser", side_effect=recording
        )
        # A collection every second build (collections are not spaced
        # here; see test_mid_page_collections_are_spaced); no release.
        mocker.patch(_RETAINED_LIMIT, 2 * size * _PARSER_BYTES_PER_INPUT_BYTE)
        mocker.patch(_SPACING, 0)
        data = _crafted_pdf(2, simple_fonts=3, font_file=size)
        was_enabled = gc.isenabled()
        gc.disable()
        try:
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                pages = pdf.pages
                releaser = PdfplumberWalkReleaser(pdf)
                for page in pages:
                    releaser.before_page()
                    page.extract_text()
                    page.close()
                    assert releaser.after_page() is False
                assert len(parsers) == 6
                alive = [ref for ref in parsers if ref() is not None]
                assert len(alive) <= 1
        finally:
            if was_enabled:
                gc.enable()

    def test_font_tables_let_go_of_are_collected_mid_page(self, mocker):
        """When the tables held reach the threshold's worth and are let
        go of, the cycles holding the maps of fonts pdfminer has dropped
        (an inline font built on every ``Do`` of a form) are collected
        there, in the middle of the page. Before they were, a page
        invoking a form of five such fonts 20 times peaked at ~880 MB.
        Automatic collection is off here so that only the releaser can
        free them."""
        import gc
        import io
        import weakref

        import pdfminer.pdffont
        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        built = []
        real = pdfminer.pdffont.FileUnicodeMap

        def recording(*args, **kwargs):
            unicode_map = real(*args, **kwargs)
            built.append(weakref.ref(unicode_map))
            return unicode_map

        mocker.patch.object(
            pdfminer.pdffont, "FileUnicodeMap", side_effect=recording
        )
        uses = 6
        mocker.patch(_SPACING, 0)  # see test_mid_page_collections_are_spaced
        # The tables are let go of at every third build.
        mocker.patch(
            _RETAINED_LIMIT,
            (_TOUNICODE_LAST + 1) * 5 // 2 * _BYTES_PER_PARSED_ELEMENT,
        )
        data = _crafted_pdf(1, unicode_fonts=1, inline=True, form_uses=uses)
        was_enabled = gc.isenabled()
        gc.disable()
        try:
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                (page,) = pdf.pages
                releaser = PdfplumberWalkReleaser(pdf)
                releaser.before_page()
                assert page.extract_text() == "Page 0"
                page.close()
                assert len(built) == uses
                alive = [ref for ref in built if ref() is not None]
                assert len(alive) <= 2
                assert releaser.after_page() is True
        finally:
            if was_enabled:
                gc.enable()

    def test_inline_tounicode_maps_are_collected_at_once(self, mocker):
        """The /ToUnicode map of a font pdfminer does not cache (written
        inline, here in a form invoked on every ``Do``) is garbage that
        only a collection frees once its build returns and the font is
        let go of, and once the threshold's worth of those has been
        built the releaser collects them at once, in the middle of the
        page, however long the last collection took. Here it seems to
        have taken a second, and automatic collection is off."""
        import gc
        import io
        import weakref

        import pdfminer.pdffont
        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        built = []
        real = pdfminer.pdffont.FileUnicodeMap

        def recording(*args, **kwargs):
            unicode_map = real(*args, **kwargs)
            built.append(weakref.ref(unicode_map))
            return unicode_map

        mocker.patch.object(
            pdfminer.pdffont, "FileUnicodeMap", side_effect=recording
        )
        uses = 6
        # Two and a half maps' worth: collected at every third build.
        mocker.patch(
            _RETAINED_LIMIT,
            (_TOUNICODE_LAST + 1) * 5 // 2 * _BYTES_PER_PARSED_ELEMENT,
        )
        data = _crafted_pdf(1, unicode_fonts=1, inline=True, form_uses=uses)
        was_enabled = gc.isenabled()
        gc.disable()
        try:
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                (page,) = pdf.pages
                releaser = PdfplumberWalkReleaser(pdf)
                releaser.before_page()
                releaser._collect_cost = 1.0
                assert page.extract_text() == "Page 0"
                page.close()
                assert len(built) == uses
                alive = [ref for ref in built if ref() is not None]
                assert len(alive) <= 2
                assert releaser._enabled
        finally:
            if was_enabled:
                gc.enable()

    def test_long_strings_a_font_build_makes_count(self, mocker):
        """A CID font joins its /CIDSystemInfo /Registry and /Ordering
        into a new string (``cidcoding``) on every build, however long
        they are and however many fonts share them; strings count by
        their size. Before they did, ten pages of five fonts sharing a
        20 MB /Registry string (a 25 KB file) grew ~95 MB a page with the
        estimate at 0.1 MB and no release."""
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        size = 256 * 1024
        # The shared string counts once, a page's three fonts' strings
        # three times: two pages cross the threshold, one does not.
        mocker.patch(_RETAINED_LIMIT, size * 11 // 2)
        data = _crafted_pdf(4, cid_fonts=3, registry=size)
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            pages = pdf.pages
            releaser = PdfplumberWalkReleaser(pdf)
            released, texts = [], []
            for page in pages:
                before = set(pdf.rsrcmgr._cached_fonts)
                releaser.before_page()
                texts.append(page.extract_text())
                page.close()
                codings = [
                    font.cidcoding
                    for objid, font in pdf.rsrcmgr._cached_fonts.items()
                    if objid not in before and hasattr(font, "cidcoding")
                ]
                assert len(codings) == 3
                assert all(len(c) > size for c in codings)
                assert len({id(c) for c in codings}) == 3, "one per build"
                released.append(releaser.after_page())

            assert texts == [f"Page {i}" for i in range(4)]
            assert released == [False, True, False, True]
            assert releaser._enabled

    def test_long_strings_in_a_tounicode_map_count(self, mocker):
        """A flat table of strings counts every long string in it by its
        size: a ``bfrange`` with a long destination maps every code to a
        long string of its own. Here they cross the threshold; counted
        as entries only, they do not."""
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        # 4,096 codes, each mapped to a 255-character string.
        mocker.patch(
            _RETAINED_LIMIT,
            3 * (_TOUNICODE_LAST + 1) * _BYTES_PER_PARSED_ELEMENT,
        )
        data = _crafted_pdf(
            1, unicode_fonts=1, unicode_dest=b"0041" * 253 + b"00000000"
        )
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            (page,) = pdf.pages
            releaser = PdfplumberWalkReleaser(pdf)
            releaser.before_page()
            page.extract_text()
            page.close()
            (font,) = pdf.rsrcmgr._cached_fonts.values()
            strings = font.unicode_map.cid2unichr.values()
            assert len(strings) == _TOUNICODE_LAST + 1
            assert min(map(len, strings)) == 255
            assert releaser.after_page() is True
            assert releaser._enabled

    def test_long_strings_in_parsed_dictionaries_count(self, mocker):
        """A /Registry string written in the /CIDSystemInfo dictionary
        of a font in an object stream counts by its size there, and in
        the font's ``cidsysteminfo`` (that same dictionary): both are
        flat tables of strings. Counted as entries only, the first page
        does not cross the threshold."""
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        size = 256 * 1024
        # A page's three fonts: each string counts in the parsed object
        # stream and in the font's cidsysteminfo, and the decoded object
        # stream and each font's cidcoding (a new string) hold another
        # copy: 12 copies a page, 6 of them without the first two.
        mocker.patch(_RETAINED_LIMIT, size * 9)
        data = _crafted_pdf(
            2, cid_fonts=3, registry=size, registry_inline=True, objstm=True
        )
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            releaser = PdfplumberWalkReleaser(pdf)
            released = []
            for page in pdf.pages:
                releaser.before_page()
                page.extract_text()
                page.close()
                released.append(releaser.after_page())
            assert released == [True, True]
            assert releaser._enabled

    def test_parts_of_the_parsed_font_are_walked_once(self, mocker):
        """A font build stores some parts of the parsed font as they are
        (its descriptor; the /FontMatrix items of a Type 3 font, in a new
        tuple), and an inline font in a form is built again on every
        ``Do``. Those parts are walked once, not on every build: before,
        once they reached the threshold's worth the held tables were let
        go of and they were walked again on every ``Do`` (100 ``Do``s of
        a form whose font's descriptor holds 600,000 empty arrays took
        ~66 s of CPU against ~3 s without the releaser)."""
        import io

        import pdfplumber

        from local_deep_research.utilities import pdf_stream_release

        entries = 20_000
        uses = 20
        mocker.patch(
            _RETAINED_LIMIT,
            entries // 2 * pdf_stream_release._BYTES_PER_PARSED_ELEMENT,
        )
        mocker.patch(_SPACING, 0)
        walked = _capture(
            mocker,
            "local_deep_research.utilities.pdf_stream_release._count_elements",
            pdf_stream_release._count_elements,
        )
        arrays = b"[" + b"[]" * entries + b"]"
        for options in (
            {"descriptor_extra": b" /X " + arrays},
            {"matrix": b"[[%s] 0 [[]] 0.001 0 0]" % arrays},
        ):
            del walked[:]
            data = _crafted_pdf(
                1, simple_fonts=1, inline=True, form_uses=uses, **options
            )
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                (page,) = pdf.pages
                releaser = pdf_stream_release.PdfplumberWalkReleaser(pdf)
                releaser.before_page()
                page.extract_text()
                page.close()
                # Counted once: the parts' entries, plus per build the
                # font and what the build made.
                assert releaser._parsed_elements < entries + 300 * uses
                assert releaser.after_page() is True
                assert releaser._enabled
            # And once more, after the page, in the form's dictionary,
            # which holds the inline font.
            assert sum(walked) < 2 * entries + 300 * uses

    def test_a_failed_build_parses_nothing(self, mocker):
        """After a build that raised, the streams it may have parsed are
        looked up in the object cache only: the build may have failed
        before resolving them, so following their references could parse
        objects it never reached, or loop forever on a reference to
        itself (``8 0 obj 8 0 R``). What it did resolve is cached, and
        counts."""
        import pdfminer.pdfinterp
        from pdfminer.pdfinterp import PDFResourceManager
        from pdfminer.pdftypes import PDFObjRef, PDFStream
        from pdfminer.psparser import LIT

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        class Looped(BaseException):
            """Raised once the cyclic reference has been followed."""

        mocker.patch(_RETAINED_LIMIT, 1 << 40)
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = PDFResourceManager()
        followed = []

        def getobj(objid):
            followed.append(objid)
            if len(followed) > 100:
                raise Looped
            return PDFObjRef(pdf.doc, objid)

        pdf.doc.getobj.side_effect = getobj
        cmap = PDFStream({}, b"")
        cmap.data = b"x" * 100
        pdf.doc._cached_objs[9] = (cmap, 0)
        spec = {
            "Type": LIT("Font"),
            "Subtype": LIT("Type1"),
            "BaseFont": LIT("Crafted"),
            "ToUnicode": PDFObjRef(pdf.doc, 9),
            "FontDescriptor": {"FontFile": PDFObjRef(pdf.doc, 8)},
        }
        mocker.patch.object(
            pdfminer.pdfinterp, "PDFType1Font", side_effect=RuntimeError
        )
        releaser = PdfplumberWalkReleaser(pdf)
        releaser.before_page()
        with pytest.raises(RuntimeError):
            pdf.rsrcmgr.get_font(None, spec)
        assert followed == []
        assert releaser._font_bytes == len(cmap.data)
        assert releaser._enabled

    def test_estimated_releases_space_their_collections(self, mocker):
        """A release the estimate asks for drops the caches at once but
        spaces its collection like the others: what triggers it can be
        cheap (a shared 64 MB stream of zeros that a font build decodes
        and hardly parses makes every page release), and a collection
        costs time in proportion to every object the process holds.
        Here every page releases, a page costs 10 ms of the thread's CPU
        and a collection 100 ms: the collections take about a tenth of
        the time, and the deferred ones still run. Before, every release
        collected: 100 such pages with a million objects held took ~25 s
        of CPU, now ~20 s."""
        import gc

        from pdfminer.pdftypes import PDFStream

        from local_deep_research.utilities import pdf_stream_release

        clock = {"now": 0.0}

        def collect(*args):
            clock["now"] += 0.1
            return 0

        mocker.patch("time.thread_time", side_effect=lambda: clock["now"])
        mocker.patch.object(gc, "collect", side_effect=collect)
        mocker.patch(_RETAINED_LIMIT, 1)
        stream = PDFStream({}, b"raw")
        stream.data = b"decoded"
        pdf = MagicMock()
        pdf.doc._cached_objs = {1: (stream, 0)}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = _fake_rsrcmgr()
        releaser = pdf_stream_release.PdfplumberWalkReleaser(pdf)
        released = []
        for _ in range(500):
            releaser.before_page()
            clock["now"] += 0.01
            released.append(releaser.after_page())
        collections = gc.collect.call_count
        assert all(released)
        assert collections >= 2, "deferred collections still run"
        assert collections * 0.1 <= 0.15 * clock["now"]
        assert releaser._enabled

    def test_font_files_pdfminer_does_not_parse_do_not_count(self, mocker):
        """Only what a build parses counts: a Type 1 font's /FontFile
        header is read from its first /Length1 bytes (none here), and not
        at all for a standard font, whose built-in metrics replace the
        descriptor. Counting the whole decoded /FontFile on every build
        made every ``Do`` of a form with such an inline font run a full
        collection: 2,000 of them took ~141 s of CPU instead of ~0.45 s,
        with a million objects held."""
        import gc
        import io
        import re

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _PARSER_BYTES_PER_INPUT_BYTE,
            PdfplumberWalkReleaser,
        )

        size = 64 * 1024
        uses = 20
        mocker.patch(_RETAINED_LIMIT, size * _PARSER_BYTES_PER_INPUT_BYTE)
        collect = mocker.patch.object(gc, "collect", wraps=gc.collect)

        def crafted(length1):
            return _crafted_pdf(
                1,
                simple_fonts=1,
                font_file=size,
                length1=length1,
                inline=True,
                form_uses=uses,
            )

        # /Length1 0; and a standard font with the whole /FontFile as its
        # /Length1, decoded before the walk.
        standard = crafted(size).replace(
            b"/BaseFont /Crafted", b"/BaseFont /Courier"
        )
        for pdf_bytes in (crafted(0), standard):
            collect.reset_mock()
            with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
                (page,) = pdf.pages
                if pdf_bytes is standard:
                    (objid,) = re.findall(
                        rb"(\d+) 0 obj\n<< /Length \d+ /Length1", pdf_bytes
                    )
                    assert len(pdf.doc.getobj(int(objid)).get_data()) == size
                releaser = PdfplumberWalkReleaser(pdf)
                releaser.before_page()
                assert page.extract_text() == "Page 0"
                page.close()
                assert releaser.after_page() is False
                assert collect.call_count == 0
                assert releaser._font_bytes == 0
                assert releaser._enabled

    def test_mid_page_collections_are_spaced(self, mocker):
        """A collection costs time in proportion to every object the
        process holds, whatever it frees, and the builds that ask for one
        can be cheap. One the threshold asks for in the middle of a page
        waits until the thread has spent ``_COLLECT_SPACING`` times the
        last collection's duration since it ended. Here every build asks
        for one, a build costs 1 ms of the thread's CPU and a collection
        100 ms: the collections take about a tenth of the time."""
        import gc

        from local_deep_research.utilities import pdf_stream_release

        clock = {"now": 0.0}

        def thread_time():
            return clock["now"]

        def collect(*args):
            clock["now"] += 0.1
            return 0

        def parsed(spec, built=True, inputs=None):
            clock["now"] += 0.001
            return 1 << 20  # the threshold's worth, at the factor below

        # The worst-case factor asks for a collection on every build;
        # what the builds fed is far below the threshold, so none of it
        # is garbage that must be collected at once.
        mocker.patch.object(
            pdf_stream_release, "_PARSER_BYTES_PER_INPUT_BYTE", 1 << 16
        )
        mocker.patch("time.thread_time", side_effect=thread_time)
        mocker.patch.object(gc, "collect", side_effect=collect)
        mocker.patch.object(
            pdf_stream_release, "_parser_input_bytes", side_effect=parsed
        )
        mocker.patch(_RETAINED_LIMIT, 1 << 36)
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = _fake_rsrcmgr()
        releaser = pdf_stream_release.PdfplumberWalkReleaser(pdf)
        releaser.before_page()
        for _ in range(2000):
            pdf.rsrcmgr.get_font(None, _FakeFont())
        collections = gc.collect.call_count
        # Unspaced, every one of the 2,000 builds collected.
        assert collections >= 2, "still collects"
        assert collections * 0.1 <= 0.15 * clock["now"]
        assert releaser._enabled

    def test_inline_fonts_count_and_release(self, mocker):
        """pdfminer caches only fonts that are objects of their own; a
        font dictionary written inline in a page's resources is built
        on every page that uses it and cached nowhere, so the estimate
        cannot be read off the font cache. Before it counted the fonts
        get_font() builds, 20 pages of five such fonts (a 13 KB file)
        held ~840 MB with no release."""
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        per_page = self._FONTS * (_TOUNICODE_LAST + 1)
        mocker.patch(
            _RETAINED_LIMIT, per_page * 3 // 2 * _BYTES_PER_PARSED_ELEMENT
        )
        # Collect at every release: a deferred collection leaves the
        # dropped fonts' maps held (by their parsers' cycles) until it
        # runs (test_estimated_releases_space_their_collections).
        mocker.patch(_SPACING, 0)
        data = _crafted_pdf(4, unicode_fonts=self._FONTS, inline=True)
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            pages = pdf.pages
            releaser = PdfplumberWalkReleaser(pdf)
            released, texts, counted, kept = [], [], [], []
            for page in pages:
                releaser.before_page()
                texts.append(page.extract_text())
                page.close()
                assert pdf.rsrcmgr._cached_fonts == {}
                released.append(releaser.after_page())
                counted.append(releaser._parsed_elements)
                kept.append(_kept_elements(releaser))

            assert texts == [f"Page {i}" for i in range(4)]
            assert counted[0] >= per_page
            assert released == [False, True, False, True]
            # All but pdfminer's built-in tables count again.
            assert counted[2] == counted[0] - kept[1]
            assert releaser._enabled

    def test_form_xobject_fonts_count_per_use(self, mocker):
        """A Form XObject's resources are initialised on every ``Do``:
        its inline fonts are built again each time the page invokes it,
        and each build counts."""
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        uses = 3
        per_use = _TOUNICODE_LAST + 1
        # One page's builds cross the threshold; a single use does not.
        mocker.patch(
            _RETAINED_LIMIT, per_use * 5 // 2 * _BYTES_PER_PARSED_ELEMENT
        )
        data = _crafted_pdf(2, unicode_fonts=1, inline=True, form_uses=uses)
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            pages = pdf.pages
            releaser = PdfplumberWalkReleaser(pdf)
            released, texts = [], []
            for page in pages:
                releaser.before_page()
                texts.append(page.extract_text())
                page.close()
                # Only the page's own Helvetica, an object, is cached.
                assert len(pdf.rsrcmgr._cached_fonts) == 1
                released.append(releaser.after_page())

            assert texts == ["Page 0", "Page 1"]
            assert released == [True, True]
            assert releaser._enabled

    def test_released_inline_fonts_are_freed(self, mocker):
        """The releaser holds the font tables it has counted until the
        release, inline fonts' maps included (pdfminer holds those only
        in reference cycles); the release must let go of them. Automatic
        collection is off here so that only the release can free them.
        The threshold is above what the tables count, so the releaser
        holds them all, and the pages' padding crosses it."""
        import gc
        import io
        import weakref

        import pdfminer.pdffont
        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        class Table(dict):
            """A /ToUnicode map's table that can be weakly referenced."""

        built = []
        real = pdfminer.pdffont.FileUnicodeMap

        def recording(*args, **kwargs):
            unicode_map = real(*args, **kwargs)
            unicode_map.cid2unichr = Table()
            built.append(weakref.ref(unicode_map))
            built.append(weakref.ref(unicode_map.cid2unichr))
            return unicode_map

        mocker.patch.object(
            pdfminer.pdffont, "FileUnicodeMap", side_effect=recording
        )
        tables = 2 * self._FONTS * (_TOUNICODE_LAST + 1) + 1000
        mocker.patch(_RETAINED_LIMIT, tables * _BYTES_PER_PARSED_ELEMENT)
        # A release's collection is spaced like the others
        # (test_estimated_releases_space_their_collections).
        mocker.patch(_SPACING, 0)
        data = _crafted_pdf(
            2, _PAD * 2, unicode_fonts=self._FONTS, inline=True, form_uses=2
        )
        was_enabled = gc.isenabled()
        gc.disable()
        try:
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                pages = pdf.pages
                releaser = PdfplumberWalkReleaser(pdf)
                for page in pages:
                    del built[:]
                    releaser.before_page()
                    page.extract_text()
                    page.close()
                    assert len(built) == 2 * 2 * self._FONTS
                    assert all(ref() is not None for ref in built)
                    assert len(releaser._tables) > 2 * self._FONTS
                    assert releaser.after_page() is True
                    assert [ref() for ref in built] == [None] * len(built)
        finally:
            if was_enabled:
                gc.enable()

    def test_disabled_releaser_stops_counting_fonts(self, mocker):
        """A releaser that switches itself off must not keep holding the
        font tables it counted, nor go on counting fonts for the rest of
        the walk."""
        from local_deep_research.utilities import pdf_stream_release

        mocker.patch.object(pdf_stream_release, "_log_disabled")
        mocker.patch(_RETAINED_LIMIT, 1 << 40)
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        manager = _fake_rsrcmgr()
        pdf.rsrcmgr = manager
        releaser = pdf_stream_release.PdfplumberWalkReleaser(pdf)
        releaser.before_page()
        assert "get_font" in vars(manager)
        manager.get_font(None, _FakeFont(cid2unicode={1: "x"}))
        assert len(releaser._tables) == 1

        pdf.doc._parsed_objs = None  # internals changed: disables
        assert releaser.after_page() is False
        assert not releaser._enabled
        assert releaser._tables == []
        assert "get_font" not in vars(manager)
        manager.get_font(None, _FakeFont(cid2unicode={2: "y"}))
        assert releaser._tables == []

    def test_fonts_pdfminer_lets_go_of_are_not_held(self, mocker):
        """Counting a font must not keep it: every unknown font name in
        a ``Tf`` builds a new font that pdfminer caches nowhere and
        frees at once, so holding them turned a 2 KB page of 100,000
        such operators into ~920 MB held. Automatic collection is off,
        so only a held reference keeps a font."""
        import gc
        import io
        import weakref

        import pdfminer.pdfinterp
        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        built = []
        real = pdfminer.pdfinterp.PDFType1Font

        def recording(*args, **kwargs):
            font = real(*args, **kwargs)
            built.append(weakref.ref(font))
            return font

        mocker.patch.object(
            pdfminer.pdfinterp, "PDFType1Font", side_effect=recording
        )
        mocker.patch(_RETAINED_LIMIT, 1 << 40)
        uses = 200
        data = _crafted_pdf(1, ops=b"BT /Unknown 1 Tf ET\n" * uses)
        was_enabled = gc.isenabled()
        gc.disable()
        try:
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                (page,) = pdf.pages
                releaser = PdfplumberWalkReleaser(pdf)
                releaser.before_page()
                assert page.extract_text() == "Page 0"
                page.close()
                assert releaser.after_page() is False
                assert len(built) >= uses
                alive = [ref for ref in built if ref() is not None]
                # The page's own Helvetica (cached) and at most the last
                # font a finished interpreter's cycle still holds.
                assert len(alive) <= 2
                assert releaser._parsed_elements >= uses
        finally:
            if was_enabled:
                gc.enable()

    def test_held_font_tables_are_bounded(self, mocker):
        """The tables counted are held so that their ids stay theirs,
        but no more of them than the threshold's worth that nothing else
        holds: past it those are let go of, while every font still
        counts. A table something else still holds then (here the last
        font's, which the build that crossed the threshold is still
        handing out) is kept until the next time."""
        from local_deep_research.utilities.pdf_stream_release import (
            _BYTES_PER_PARSED_ELEMENT,
            PdfplumberWalkReleaser,
        )

        entries = 1000
        mocker.patch(_RETAINED_LIMIT, 3 * entries * _BYTES_PER_PARSED_ELEMENT)
        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = _fake_rsrcmgr()
        releaser = PdfplumberWalkReleaser(pdf)
        releaser.before_page()
        for _ in range(10):
            pdf.rsrcmgr.get_font(
                None, _FakeFont(cid2unicode=dict.fromkeys(range(entries)))
            )
            assert releaser._held_entries < 3 * entries
            assert len(releaser._tables) <= 3
        assert releaser._parsed_elements == 10 * (1 + entries)

    def test_font_recording_holds_no_cycle(self):
        """The get_font() wrapper holds the manager and the releaser
        weakly: dropping the releaser leaves a working pass-through, and
        the document's objects stay free of new reference cycles."""
        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        manager = _fake_rsrcmgr()
        pdf.rsrcmgr = manager
        releaser = PdfplumberWalkReleaser(pdf)
        releaser.before_page()
        wrapper = vars(manager)["get_font"]
        held = [cell.cell_contents for cell in wrapper.__closure__]
        assert all(obj is not manager for obj in held)
        assert all(obj is not releaser for obj in held)
        del releaser
        font = _FakeFont()
        assert manager.get_font(None, font) is font


class TestResidentMemoryBound:
    """What the estimate does not count, the process's resident memory
    shows: a release also runs once it has grown by the threshold since
    the last one, and a collection in the middle of a page once it has
    grown by the threshold since the last collection. Each case of the
    estimate missing an allocation (inline fonts, shared widths, font
    header copies, the strings a CID font joins) went unbounded until it
    was found; this bounds the next one. Fed values of its own here."""

    @pytest.fixture
    def rss(self, monkeypatch):
        from local_deep_research.utilities import pdf_stream_release

        memory = {"now": 1 << 30}
        monkeypatch.setattr(
            pdf_stream_release, "_resident_bytes", lambda: memory["now"]
        )
        return memory

    def _pdfminer(self):
        from local_deep_research.utilities.pdf_stream_release import (
            PdfplumberWalkReleaser,
        )

        pdf = MagicMock()
        pdf.doc._cached_objs = {}
        pdf.doc._parsed_objs = {}
        pdf.rsrcmgr = _fake_rsrcmgr()
        return pdf, PdfplumberWalkReleaser(pdf)

    def test_pdfminer_releases_once_resident_memory_grows(self, mocker, rss):
        import gc

        limit = 1 << 20
        mocker.patch(_RETAINED_LIMIT, limit)
        mocker.patch(_SPACING, 0)  # see test_mid_page_collections_are_spaced
        collect = mocker.patch.object(gc, "collect")
        pdf, releaser = self._pdfminer()
        released = []
        for growth in (limit - 1, 1, limit, limit // 2):
            releaser.before_page()
            rss["now"] += growth
            released.append(releaser.after_page())
        # The second page reaches the threshold since the walk began; the
        # third and fourth are measured from the second's release.
        assert released == [False, True, True, False]
        assert collect.call_count == 2
        assert releaser._enabled

    def test_pdfminer_collects_mid_page_once_resident_memory_grows(
        self, mocker, rss
    ):
        import gc

        from local_deep_research.utilities import pdf_stream_release

        limit = 1 << 20
        mocker.patch(_RETAINED_LIMIT, limit)
        mocker.patch.object(pdf_stream_release, "_RSS_CHECK_INTERVAL", 0)
        collect = mocker.patch.object(gc, "collect")
        pdf, releaser = self._pdfminer()
        releaser.before_page()
        pdf.rsrcmgr.get_font(None, _FakeFont())
        assert collect.call_count == 0
        rss["now"] += limit
        pdf.rsrcmgr.get_font(None, _FakeFont())
        assert collect.call_count == 1
        # Measured from that collection, spaced like the others: without
        # the spacing met, only a growth of _FORCED_GROWTH times the
        # threshold collects.
        mocker.patch.object(pdf_stream_release, "_COLLECT_SPACING", 1e12)
        releaser._collect_cost = 1.0
        rss["now"] += limit
        pdf.rsrcmgr.get_font(None, _FakeFont())
        assert collect.call_count == 1
        rss["now"] += (pdf_stream_release._FORCED_GROWTH - 1) * limit
        pdf.rsrcmgr.get_font(None, _FakeFont())
        assert collect.call_count == 2
        # The page grew by more than the threshold: it releases too.
        assert releaser.after_page() is True
        assert releaser._enabled

    def test_a_failed_read_keeps_the_baseline(self, mocker, monkeypatch):
        """A read of the resident memory that fails once (an OSError
        reading /proc/self/statm) keeps the baseline it would have
        replaced: before, the baseline became unknown and the measured
        trigger was off for the rest of the walk. Here the read that
        follows the first release fails."""
        import gc

        from local_deep_research.utilities import pdf_stream_release

        limit = 1 << 20
        mocker.patch(_RETAINED_LIMIT, limit)
        mocker.patch(_SPACING, 0)
        mocker.patch.object(gc, "collect")
        for library in ("pdfminer", "pypdf"):
            memory = {"now": 1 << 30, "reads": 0}

            def read(memory=memory):
                memory["reads"] += 1
                return None if memory["reads"] == 3 else memory["now"]

            monkeypatch.setattr(pdf_stream_release, "_resident_bytes", read)
            if library == "pdfminer":
                _pdf, releaser = self._pdfminer()
            else:
                reader = MagicMock()
                reader.resolved_objects = {}
                releaser = pdf_stream_release.PypdfWalkReleaser(reader)
            released = []
            for growth in (limit, limit // 2, limit):
                releaser.before_page()
                memory["now"] += growth
                released.append(releaser.after_page())
            # The second page is measured from the baseline before the
            # first release (the read after it failed), the third from
            # the second's release.
            assert released == [True, True, True], library
            assert memory["reads"] > 3
            assert releaser._enabled

    def test_pypdf_releases_once_resident_memory_grows(self, mocker, rss):
        from local_deep_research.utilities.pdf_stream_release import (
            PypdfWalkReleaser,
        )

        limit = 1 << 20
        mocker.patch(_RETAINED_LIMIT, limit)
        reader = MagicMock()
        reader.resolved_objects = {}
        releaser = PypdfWalkReleaser(reader)
        released = []
        for growth in (limit - 1, 1, limit // 2):
            releaser.before_page()
            rss["now"] += growth
            released.append(releaser.after_page())
        assert released == [False, True, False]
        assert releaser._enabled


class TestDisabledReleaserIsLoggedOnce:
    """A releaser that disables itself (library internals changed) turns
    the memory bound off, so the first one of each library in a process
    warns; the rest log at DEBUG so a broken upgrade does not flood the
    log."""

    def test_first_disable_warns_then_debug(self, monkeypatch):
        from loguru import logger

        from local_deep_research.utilities import pdf_stream_release

        monkeypatch.setattr(pdf_stream_release, "_warned_disabled", set())
        records = []
        sink = logger.add(
            lambda message: records.append(message.record),
            level="DEBUG",
            filter="local_deep_research.utilities.pdf_stream_release",
        )
        # The package disables its loguru logger on import; enable it for
        # this block only and put it back afterwards.
        logger.enable("local_deep_research")
        try:
            for _ in range(3):
                for cls in (
                    pdf_stream_release.PdfplumberWalkReleaser,
                    pdf_stream_release.PypdfWalkReleaser,
                ):
                    releaser = cls(object())
                    releaser.before_page()
                    assert releaser.after_page() is False
        finally:
            logger.disable("local_deep_research")
            logger.remove(sink)

        warnings = [r for r in records if r["level"].name == "WARNING"]
        debugs = [r for r in records if r["level"].name == "DEBUG"]
        assert len(warnings) == 2, "one WARNING per library per process"
        assert {w["message"].split()[0] for w in warnings} == {
            "pdfminer",
            "pypdf",
        }
        assert "no longer bounded" in warnings[0]["message"]
        assert len(debugs) == 4


class TestLibraryInternalsTheReleaseReliesOn:
    """Canaries: fail when a pdfplumber/pdfminer or pypdf upgrade removes
    the internals ``utilities/pdf_stream_release`` relies on. The
    releasers swallow such failures by design, so without these an
    upgrade would silently bring the unbounded retention back.
    Written against the pdfplumber, pdfminer.six and pypdf versions
    ``pdm.lock`` pins."""

    def test_pdfminer_internals(self):
        import io

        import pdfplumber
        from pdfminer.pdftypes import PDFStream

        with pdfplumber.open(io.BytesIO(_crafted_pdf(1, 1024))) as pdf:
            page = pdf.pages[0]
            page.extract_text()
            assert isinstance(pdf.doc._cached_objs, dict)
            assert isinstance(pdf.doc._parsed_objs, dict)
            assert isinstance(pdf.rsrcmgr._cached_fonts, dict)
            (stream,) = page.page_obj.contents
            assert isinstance(stream, PDFStream)
            # decode() keeps the decoded bytes and drops the raw ones.
            assert stream.data is not None and stream.rawdata is None
            objid = stream.objid
            assert pdf.doc._cached_objs[objid][0] is stream
            # Parsing the object again yields its raw bytes.
            entry = pdf.doc._cached_objs.pop(objid)
            fresh = pdf.doc.getobj(objid)
            pdf.doc._cached_objs[objid] = entry
            assert isinstance(fresh, PDFStream) and fresh is not stream
            assert fresh.data is None and fresh.rawdata

    def test_pypdf_internals(self):
        import io

        from pypdf import PdfReader
        from pypdf.generic import EncodedStreamObject

        reader = PdfReader(io.BytesIO(_crafted_pdf(1, 1024)))
        page = reader.pages[0]
        page.extract_text()
        assert isinstance(reader.resolved_objects, dict)
        stream = page["/Contents"].get_object()
        assert isinstance(stream, EncodedStreamObject)
        assert stream.decoded_self is not None
        data = stream.get_data()
        # Dropping the cached copy is safe: it decodes again.
        stream.decoded_self = None
        assert stream.get_data() == data

    def test_reference_counts_tell_held_tables_apart(self, monkeypatch):
        """``_let_go_of_tables()`` keeps a table something besides the
        releaser holds and lets go of one only it holds, reading
        ``sys.getrefcount()`` against a count calibrated on this
        interpreter; if the reading of a container only its list holds
        ever exceeds the calibration, it keeps none (an over-count, never
        an under-count)."""
        from local_deep_research.utilities import pdf_stream_release

        releaser = pdf_stream_release.PdfplumberWalkReleaser(MagicMock())
        held = {"a": 1}
        releaser._tables = [held, {"b": 2}]
        releaser._table_ids = {id(t) for t in releaser._tables}
        releaser._let_go_of_tables()
        assert releaser._tables == [held]
        assert releaser._table_ids == {id(held)}

        monkeypatch.setattr(pdf_stream_release, "_SOLE_HOLDER", 0)
        releaser._let_go_of_tables()
        assert releaser._tables == []
        assert releaser._table_ids == set()

    def test_resident_memory_is_measured_on_linux(self, monkeypatch):
        """The releasers read the process's resident memory from
        ``/proc/self/statm`` (Linux); elsewhere they rely on the estimate
        alone."""
        import sys

        from local_deep_research.utilities import pdf_stream_release

        monkeypatch.undo()  # the module's _no_resident_memory
        if not sys.platform.startswith("linux"):
            pytest.skip("resident memory is measured on Linux only")
        before = pdf_stream_release._resident_bytes()
        held = bytearray(64 * 1024 * 1024)
        after = pdf_stream_release._resident_bytes()
        assert before and after - before >= 32 * 1024 * 1024
        del held

    def test_pdfminer_font_internals(self):
        """pdfminer builds every font in a page's resources that is an
        object of its own into ``rsrcmgr._cached_fonts`` (keys added in
        build order; the release clears it), and each
        font expands a shared ``/ToUnicode`` bfrange into its own
        ``unicode_map.cid2unichr``; CID fonts expand /W and /W2 ranges
        into ``widths`` and ``disps``. ``_font_tables()`` finds every one
        of them among the font's attributes; on real pdfminer
        objects."""
        import io

        import pdfplumber
        from pdfminer.pdffont import PDFCIDFont
        from pdfminer.psparser import LIT

        from local_deep_research.utilities.pdf_stream_release import (
            _font_tables,
        )

        with pdfplumber.open(
            io.BytesIO(_crafted_pdf(2, unicode_fonts=2))
        ) as pdf:
            pages = pdf.pages
            fonts = pdf.rsrcmgr._cached_fonts
            assert isinstance(fonts, dict) and not fonts
            seen = []
            for page in pages:
                page.extract_text()
                assert list(fonts)[: len(seen)] == seen, "not in build order"
                seen = list(fonts)
            assert len(fonts) == 4, "every font in /Resources is built"
            maps = [font.unicode_map.cid2unichr for font in fonts.values()]
            for cid2unichr in maps:
                assert type(cid2unichr) is dict
                assert len(cid2unichr) == _TOUNICODE_LAST + 1
            assert len({id(m) for m in maps}) == 4, "maps are per font"
            for font in fonts.values():
                tables = _font_tables(font)
                assert any(t is font.unicode_map.cid2unichr for t in tables)
                assert type(font.cid2unicode) is dict
                assert any(t is font.cid2unicode for t in tables)

        vertical = PDFCIDFont(
            None,
            {
                "BaseFont": LIT("Crafted"),
                "Encoding": LIT("Identity-V"),
                "W2": [0, 9, 1000, 500, 880],
            },
            strict=False,
        )
        assert len(vertical.widths) == len(vertical.disps) == 10
        horizontal = PDFCIDFont(
            None,
            {
                "BaseFont": LIT("Crafted"),
                "Encoding": LIT("Identity-H"),
                "W": [0, 99, 500],
            },
            strict=False,
        )
        assert len(horizontal.widths) == 100
        for font in (vertical, horizontal):
            tables = _font_tables(font)
            for table in (font.widths, font.disps):
                assert type(table) is dict
                assert any(t is table for t in tables)

    def test_pdfminer_font_builds_copy_what_fonts_share(self):
        """What the releaser counts per font build rather than once per
        shared object, on real pdfminer objects: every simple font builds
        its own ``widths`` dict from a /Widths array that many fonts
        share (``_font_tables()`` counts each); and a Type 1 font without
        /Encoding hands ``Type1FontHeaderParser`` a fresh copy of its
        /FontFile's first /Length1 bytes, which the parser keeps in a
        reference cycle (its bound-method state), so only the cycle
        collector frees it (``_parser_input_bytes()`` counts that copy's
        length per build)."""
        import io
        from unittest import mock

        import pdfminer.pdffont
        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _font_tables,
            _parser_input_bytes,
        )

        entries = 300
        data = _crafted_pdf(1, simple_fonts=2, shared_widths=entries)
        with pdfplumber.open(io.BytesIO(data)) as pdf:
            pdf.pages[0].extract_text()
            fonts = [
                font
                for font in pdf.rsrcmgr._cached_fonts.values()
                if font.basefont == "Crafted"
            ]
            assert len(fonts) == 2
            first, second = (font.widths for font in fonts)
            assert type(first) is dict and len(first) == entries
            assert first == second and first is not second, "one per font"
            for font in fonts:
                assert any(t is font.widths for t in _font_tables(font))

        size = 4096
        parsers = []
        real = pdfminer.pdffont.Type1FontHeaderParser

        def recording(*args, **kwargs):
            parsers.append(real(*args, **kwargs))
            return parsers[-1]

        data = _crafted_pdf(1, simple_fonts=1, font_file=size)
        with (
            mock.patch.object(
                pdfminer.pdffont,
                "Type1FontHeaderParser",
                side_effect=recording,
            ),
            pdfplumber.open(io.BytesIO(data)) as pdf,
        ):
            spec = None
            build = pdf.rsrcmgr.get_font

            def get_font(objid, font_spec):
                nonlocal spec
                if getattr(font_spec.get("BaseFont"), "name", 0) == "Crafted":
                    spec = font_spec
                return build(objid, font_spec)

            pdf.rsrcmgr.get_font = get_font
            pdf.pages[0].extract_text()
            (parser,) = parsers
            font_file = spec["FontDescriptor"]["FontFile"].resolve()
            assert len(font_file.data) == size
            header = parser.fp.getvalue()
            assert len(header) == size - 1
            assert header is not font_file.data, "a copy per build"
            assert vars(parser)["_parse1"].__self__ is parser, "a cycle"
            assert _parser_input_bytes(spec) == len(header)

    def test_pdfminer_builds_every_font_through_get_font(self):
        """pdfminer caches only fonts that are objects of their own: a
        font dictionary written inline in a resource dictionary is built
        on every use (each page, each ``Do`` of a Form XObject) and
        cached nowhere. Every build goes through ``get_font()`` looked
        up on the resource manager instance (a Type0 font's descendant
        too), which ``_count_fonts()`` wraps; it relies on all of it."""
        import io
        import weakref

        import pdfplumber
        from pdfminer.pdfinterp import PDFResourceManager
        from pdfminer.psparser import LIT

        def recording(manager):
            assert "get_font" not in vars(manager)
            built = []
            real = manager.get_font

            def get_font(objid, spec):
                built.append((objid, real(objid, spec)))
                return built[-1][1]

            manager.get_font = get_font
            return built

        for form_uses, per_page in ((0, 2), (3, 2 * 3)):
            data = _crafted_pdf(
                2, unicode_fonts=2, inline=True, form_uses=form_uses
            )
            with pdfplumber.open(io.BytesIO(data)) as pdf:
                pages = pdf.pages
                assert isinstance(pdf.rsrcmgr, PDFResourceManager)
                built = recording(pdf.rsrcmgr)
                for page in pages:
                    del built[:]
                    assert page.extract_text().startswith("Page")
                    inline = [font for objid, font in built if objid is None]
                    assert len(inline) == per_page
                    assert len({id(font) for font in inline}) == per_page
                    for font in inline:
                        assert len(font.unicode_map.cid2unichr) == (
                            _TOUNICODE_LAST + 1
                        )
                        # The releaser remembers the fonts it counted
                        # weakly.
                        assert weakref.ref(font)() is font
                assert all(
                    font not in pdf.rsrcmgr._cached_fonts.values()
                    for font in inline
                )
                if form_uses:
                    # The page's own Helvetica is an object: cached.
                    assert len(pdf.rsrcmgr._cached_fonts) == 1
                else:
                    assert pdf.rsrcmgr._cached_fonts == {}

        manager = PDFResourceManager()
        built = recording(manager)
        font = manager.get_font(
            None,
            {
                "Type": LIT("Font"),
                "Subtype": LIT("Type0"),
                "BaseFont": LIT("Crafted"),
                "Encoding": LIT("Identity-H"),
                "DescendantFonts": [
                    {
                        "Type": LIT("Font"),
                        "Subtype": LIT("CIDFontType2"),
                        "BaseFont": LIT("Crafted"),
                    }
                ],
            },
        )
        assert [built_font for _, built_font in built] == [font, font]

    def test_pdfminer_object_stream_internals(self):
        """``_parsed_objs`` maps an object stream's id to ``(objects,
        count)``: the list of its header numbers then its objects, and
        the number of objects. Keys are added in parse order, and the
        object getobj() caches is the one in that list. after_page()
        relies on all of it; on a real PDF 1.5 file."""
        import io

        import pdfplumber

        from local_deep_research.utilities.pdf_stream_release import (
            _is_objstm_entry,
        )

        with pdfplumber.open(
            io.BytesIO(_crafted_pdf(2, widths=10, objstm=True))
        ) as pdf:
            pages = pdf.pages
            parsed = pdf.doc._parsed_objs
            assert isinstance(parsed, dict)
            (pre_walk,) = parsed  # catalog, page tree and pages
            for page in pages:
                page.extract_text()
            keys = list(parsed)
            assert keys[0] == pre_walk and len(keys) == 3
            assert keys == sorted(keys), "not in parse order"
            for key, value in parsed.items():
                assert _is_objstm_entry(value)
                objs, count = value
                assert type(value) is tuple and type(objs) is list
                assert type(count) is int and count > 0
                stream = pdf.doc.getobj(key)
                assert stream.get("Type").name == "ObjStm"
                assert stream.get("N") == count
                assert len(objs) == 3 * count
                for index in range(count):
                    objid = objs[2 * index]
                    cached = pdf.doc._cached_objs.get(objid)
                    if cached is not None:
                        assert cached[0] is objs[2 * count + index]
            # Each page's font and its /Widths array came out of the
            # page's own object stream and are the objects in its list.
            for key in keys[1:]:
                objs, count = parsed[key]
                assert count == 2
                assert [type(obj) for obj in objs[2 * count :]] == [list, dict]
                assert pdf.doc._cached_objs[objs[0]][0] is objs[4]
                assert pdf.doc._cached_objs[objs[2]][0] is objs[5]


class TestReleaserWiring:
    """Every loop hands each page to its releaser in the right order."""

    @staticmethod
    def _recording_releaser(mocker, target, events):
        releaser = MagicMock()
        releaser.before_page.side_effect = lambda: events.append("before")
        releaser.after_page.side_effect = lambda: events.append("after")
        return mocker.patch(target, return_value=releaser)

    @staticmethod
    def _patch_page_walk(mocker, pages):
        def walk(pdf, releaser):
            for page in pages:
                releaser.before_page()
                yield page

        return mocker.patch(
            "local_deep_research.utilities.pdf_page_walk.iter_pdfplumber_pages",
            side_effect=walk,
        )

    def test_pdfplumber_loop(self, service, mocker):
        events = []
        pages = _recording_pages(["first", "second"], events)
        pdf = _pdf_with_pages(mocker, pages)
        cls = self._recording_releaser(
            mocker,
            "local_deep_research.utilities.pdf_stream_release."
            "PdfplumberWalkReleaser",
            events,
        )
        self._patch_page_walk(mocker, pages)

        assert service._extract_text_from_pdf(b"%PDF-fake") == (
            "first\n\nsecond"
        )

        cls.assert_called_once_with(pdf)
        assert events == [
            "before",
            ("extract", 0),
            ("close", 0),
            "after",
            "before",
            ("extract", 1),
            ("close", 1),
            "after",
        ]

    def test_pdfplumber_loop_releases_after_a_failing_page(
        self, service, mocker
    ):
        events = []
        pages = _recording_pages(["first", "second"], events)
        pages[1].extract_text.side_effect = ValueError("broken")
        _pdf_with_pages(mocker, pages)
        self._recording_releaser(
            mocker,
            "local_deep_research.utilities.pdf_stream_release."
            "PdfplumberWalkReleaser",
            events,
        )
        self._patch_page_walk(mocker, pages)

        assert service._extract_text_from_pdf(b"%PDF-fake") is None

        assert events[-2:] == [("close", 1), "after"]

    def test_pypdf_fallback_loop(self, service, mocker):
        events = []
        pdf_pages = _pages_of([""])
        _pdf_with_pages(mocker, pdf_pages)
        fallback = _recording_pages(["first", "second"], events)
        reader = _reader_with_pages(mocker, fallback)

        self._recording_releaser(
            mocker,
            "local_deep_research.utilities.pdf_stream_release."
            "PdfplumberWalkReleaser",
            [],
        )
        self._patch_page_walk(mocker, pdf_pages)

        cls = self._recording_releaser(
            mocker,
            "local_deep_research.utilities.pdf_stream_release."
            "PypdfWalkReleaser",
            events,
        )

        assert service._extract_text_from_pdf(b"%PDF-fake") == (
            "first\n\nsecond"
        )
        cls.assert_called_once_with(reader.return_value)
        assert events == [
            "before",
            ("extract", 0),
            "after",
            "before",
            ("extract", 1),
            "after",
        ]

    def test_base_downloader_loop(self, mocker):
        from local_deep_research.research_library.downloaders.base import (
            BaseDownloader,
        )

        events = []
        pages = _recording_pages(["first", "second"], events)
        mock_reader = MagicMock()
        mock_reader.pages = pages
        mocker.patch("pypdf.PdfReader", return_value=mock_reader)
        cls = self._recording_releaser(
            mocker,
            "local_deep_research.research_library.downloaders.base."
            "PypdfWalkReleaser",
            events,
        )

        assert BaseDownloader.extract_text_from_pdf(b"%PDF-fake") == (
            "first\nsecond"
        )

        cls.assert_called_once_with(mock_reader)
        assert events == [
            "before",
            ("extract", 0),
            "after",
            "before",
            ("extract", 1),
            "after",
        ]