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.

94.3 KB
Raw Blame
"""Contracts for the library download service: where remote bytes land,
how big they are allowed to be, what they are trusted to be, and what a
failed download leaves behind.

Scope note: deletion cascades and the download-queue claim protocol are
covered by ``test_deletion_cascade_contracts.py`` and
``tests/web/routers/test_download_queue_claim_4691.py`` and are
deliberately NOT duplicated here.

Everything below drives ``DownloadService._download_pdf`` /
``PDFStorageManager`` DIRECTLY against a REAL on-disk SQLite file and a
real ``tmp_path`` store. No FastAPI app is booted and **no HTTP request
is ever made** -- the downloader and the ``requests`` session are
stubbed at the object boundary, so a stray real fetch would raise rather
than silently pass.

Why on-disk rather than ``:memory:``: in-memory SQLite is
per-connection, so a "the row is there" assertion read back through the
same session that wrote it can pass on uncommitted state. Every
row assertion here is read through a FRESH connection to the same file.

Why ``DownloadService`` is built with ``__new__`` instead of its
constructor: ``__init__`` builds a settings manager, a RetryManager and
seven live downloaders (each opening a ``SafeSession`` and an adaptive
rate-limit tracker backed by the user database). ``_download_pdf`` needs
none of that -- it needs ``username``/``password``, a settings reader,
the two library roots and the downloader list. Constructing exactly
those keeps the test offline and DB-free apart from the session we
control. The egress-policy attributes are read through ``getattr``
defaults by ``_check_url_against_policy``, so an unset context means
"no policy" (the documented back-compat path), which is what we want:
these tests are about storage, not about egress gating.

Covered:
  * the storage path is derived from SERVER-controlled values -- the
    resource row id, or an id regex-extracted from a host-checked URL --
    never from the remote server's ``Content-Disposition`` (which this
    code path never reads) or from the URL's own path segments;
  * a traversal-shaped or wrong-extension filename reaching
    ``save_pdf`` is refused with a specific ``ValueError`` AND leaves the
    filesystem byte-identical;
  * the response-size cap is asserted at the check, never by allocating
    a large body: a too-large ``Content-Length`` is rejected before the
    body is read at all, and a chunked-forever body with no
    ``Content-Length`` is cut off by the installed body guard;
  * a PDF the storage size limit refuses keeps its extracted text in a
    COMPLETED document labelled as storing no PDF and awaiting one (see
    ``document_awaits_pdf``), with the refused size recorded: it is not
    downloaded again while the limit refuses that size, sets no retry
    cooldown for the text routes, and is stored once the limit is raised.
    A refill of a document that awaits its PDF stays awaiting it, while a
    "none"-mode refill still settles as a text-only download;
  * a ``.pdf`` URL alone does not make a response a PDF, but a
    ``Content-Type: application/pdf`` header alone DOES: the body is
    never magic-byte checked when the header claims PDF, and the
    resulting Document is labelled ``file_type="pdf"`` /
    ``mime_type="application/pdf"`` regardless of its bytes (DEFECT --
    xfail);
  * a write that fails part-way leaves no COMPLETED row and no truncated
    file behind (issue #5868), whether it retries a failed document,
    creates a new one, refills a document awaiting its PDF, stores the
    PDF in a matching document that has none (content-hash dedup, also
    without a canonical retry) or persists a PDF at fetch time, and the
    next download stores the PDF; a failed rewrite keeps a PDF already
    stored under the same name;
  * retrying a failed resource updates in place -- one Document, one
    blob, one attempt row per attempt;
  * HTML-only responses are distinguished from actual access failures in
    the skip reasons that ``GenericDownloader`` emits, with
    the producer and the classifier wired together so drift in either
    half fails;
  * an exception message surfaced to the client is credential-scrubbed,
    the download's call sites log only the redacted URL (checked with the
    redaction patcher removed), and configured application logging also
    scrubs the same URL before sinks receive it (holds).
"""

import hashlib
import uuid
from contextlib import contextmanager, nullcontext
from datetime import timedelta
from pathlib import Path
from types import SimpleNamespace

import pytest
from loguru import logger
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.download_tracker import (
    DownloadAttempt,
    DownloadTracker,
)
from local_deep_research.database.models.library import (
    Document,
    DocumentBlob,
    DocumentStatus,
    SourceType,
)
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)
from local_deep_research.library.download_management.failure_classifier import (
    FailureClassifier,
)
from local_deep_research.research_library.downloaders.base import (
    ContentType,
    DownloadResult,
)
from local_deep_research.research_library.downloaders.direct_pdf import (
    DirectPDFDownloader,
)
from local_deep_research.research_library.downloaders.generic import (
    GenericDownloader,
)
from local_deep_research.research_library.services.download_service import (
    DownloadService,
)
from local_deep_research.research_library.services.pdf_storage_manager import (
    PDFStorageManager,
    resolve_pdf_storage_mode,
)
from local_deep_research.research_library.utils import get_url_hash
from local_deep_research.security import file_write_verifier, safe_requests

USERNAME = "download_contract_user"

# A tiny, structurally-valid-enough PDF header. Deliberately small: these
# tests assert *where* bytes land and *whether* a bound is checked, never
# by moving a large payload around.
PDF_BYTES = (
    b"%PDF-1.4\n1 0 obj<</Type/Catalog>>endobj\ntrailer<</Root 1 0 R>>\n%%EOF\n"
)
HTML_BYTES = b"<!DOCTYPE html><html><body>Sign in to continue</body></html>"

# The operator gate that unlocks unencrypted filesystem PDF storage.
# Hard-coded rather than derived from the registry so a rename of the
# setting key fails this file loudly instead of silently downgrading
# every filesystem test into a database-mode test. The
# ``resolve_pdf_storage_mode`` assertion in ``filesystem_mode`` is the
# belt-and-braces check that the gate really opened.
FILESYSTEM_GATE_ENV = "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE"


# ---------------------------------------------------------------------------
# Stubs. Nothing here reaches the network.
# ---------------------------------------------------------------------------


class StubSettings:
    """Minimal stand-in for SettingsManager.get_setting."""

    def __init__(self, values):
        self._values = values

    def get_setting(self, key, default=None):
        return self._values.get(key, default)


class StubDownloader:
    """A downloader that returns a canned result and records its calls.

    Deliberately NOT a ``GenericDownloader`` subclass: ``_download_pdf``
    special-cases ``isinstance(downloader, GenericDownloader)`` when
    deciding whether to keep trying other downloaders, and we do not want
    that branch to change what these tests exercise.
    """

    def __init__(self, result=None, raises=None):
        self._result = result
        self._raises = raises
        self.urls = []

    def can_handle(self, url):
        return True

    def download_with_result(self, url, content_type=ContentType.PDF):
        self.urls.append(url)
        if self._raises is not None:
            raise self._raises
        return self._result


class StubResponse:
    """Enough of ``requests.Response`` for the downloader code paths."""

    def __init__(self, status_code=200, content=b"", headers=None):
        self.status_code = status_code
        self.content = content
        self.headers = headers or {}
        self.closed = False

    def close(self):
        self.closed = True

    def __enter__(self):
        return self

    def __exit__(self, *exc):
        self.close()
        return False


class StubHTTPSession:
    """A ``requests.Session`` stand-in that hands back one canned response.

    ``get``/``head`` never touch a socket; if the code under test ever
    grows a call this stub does not model, it raises rather than
    reaching the network.
    """

    def __init__(self, response):
        self.response = response
        self.headers = {"User-Agent": "test-agent"}
        self.calls = []

    def get(self, url, **kwargs):
        self.calls.append(("get", url, kwargs))
        return self.response

    def head(self, url, **kwargs):
        self.calls.append(("head", url, kwargs))
        return self.response


class StubRateTracker:
    def apply_rate_limit(self, engine_type):
        return 0.0

    def record_outcome(self, **kwargs):
        return None


def _make_downloader(cls, response):
    """Build a real downloader class with its I/O collaborators stubbed.

    ``__new__`` skips ``BaseDownloader.__init__``, which would open a
    ``SafeSession`` and an ``AdaptiveRateLimitTracker`` (the latter reads
    the user database). The methods under test are the real ones.
    """
    downloader = cls.__new__(cls)
    downloader.timeout = 5
    downloader.session = StubHTTPSession(response)
    downloader.rate_tracker = StubRateTracker()
    return downloader


# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------


@pytest.fixture
def store(tmp_path):
    """A real library store plus a sibling directory that must stay empty.

    ``sandbox`` holds everything a write is allowed (or forbidden) to
    touch; the SQLite file lives OUTSIDE it so journal/WAL churn cannot
    pollute a filesystem census.
    """
    sandbox = tmp_path / "fs"
    base = sandbox / "library"
    root = base / USERNAME
    (root / "pdfs").mkdir(parents=True)
    outside = sandbox / "outside"
    outside.mkdir()
    return SimpleNamespace(
        sandbox=sandbox, base=base, root=root, outside=outside
    )


def _tree(root: Path):
    """Every file under ``root``, as posix-relative strings."""
    return {
        p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()
    }


@pytest.fixture
def db(tmp_path):
    """Real on-disk SQLite, FK enforcement on, plus a fresh-connection reader.

    ``download_session`` is a separate session that ``_one_download_session``
    hands the download service for every scope, the way production reuses
    one thread-local session; the fixture closes it at teardown.
    """
    db_dir = tmp_path / "db"
    db_dir.mkdir()
    engine = create_engine(f"sqlite:///{db_dir / 'library.db'}")

    @event.listens_for(engine, "connect")
    def _fk_on(dbapi_connection, _record):
        cur = dbapi_connection.cursor()
        cur.execute("PRAGMA foreign_keys = ON")
        cur.close()

    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    @contextmanager
    def fresh():
        """A brand-new session on the same file -- never the mutated one."""
        with Session() as other:
            yield other

    # `with Session() as session` rather than try/finally + session.close():
    # same teardown, and it satisfies the check-session-context-manager hook.
    with Session() as session, Session() as download_session:
        try:
            yield SimpleNamespace(
                engine=engine,
                session=session,
                fresh=fresh,
                download_session=download_session,
            )
        finally:
            engine.dispose()


@pytest.fixture
def seeded(db):
    """A research resource with an already-FAILED Document and a tracker.

    Seeding the Document makes ``_download_pdf`` take its
    existing-document branch, which is the branch a retry hits. It also
    keeps the test off ``get_source_type_id`` /
    ``get_default_library_id``, which open their own user-database
    sessions.
    """
    session = db.session
    source_type = SourceType(
        id=str(uuid.uuid4()),
        name="research_download",
        display_name="Research Download",
    )
    research = ResearchHistory(
        id=str(uuid.uuid4()),
        query="contract query",
        mode="quick",
        status="completed",
        created_at="2026-08-01T00:00:00",
    )
    session.add_all([source_type, research])
    session.commit()

    def _make(url):
        resource = ResearchResource(
            research_id=research.id,
            title="Contract Paper",
            url=url,
            source_type="academic",
            created_at="2026-08-01T00:00:00",
        )
        session.add(resource)
        session.commit()

        document = Document(
            id=str(uuid.uuid4()),
            source_type_id=source_type.id,
            resource_id=resource.id,
            research_id=research.id,
            # The placeholder shape _record_failed_text_extraction writes.
            document_hash=f"failed:placeholder:{uuid.uuid4().hex}",
            original_url=url,
            file_size=0,
            file_type="pdf",
            title=resource.title,
            status=DocumentStatus.FAILED,
            # Explicit: the column defaults to "database", and a
            # previously-failed row has nothing stored anywhere.
            storage_mode="none",
        )
        tracker = DownloadTracker(
            url=url,
            # Seed with the production hash (post-B2 #6758 scheme), not a
            # hand-rolled copy of its old implementation.
            url_hash=get_url_hash(url),
            first_resource_id=resource.id,
            is_downloaded=False,
        )
        session.add_all([document, tracker])
        session.commit()
        return SimpleNamespace(
            resource=resource,
            document=document,
            tracker=tracker,
            source_type=source_type,
        )

    return _make


def make_service(store, downloader, *, storage_mode="database", max_mb=3072):
    service = DownloadService.__new__(DownloadService)
    service.username = USERNAME
    service.password = None
    service._closed = False
    service.settings = StubSettings(
        {
            "research_library.pdf_storage_mode": storage_mode,
            "research_library.max_pdf_size_mb": max_mb,
        }
    )
    service.library_root = str(store.root)
    service.legacy_library_root = str(store.base)
    service.downloaders = [downloader]
    service.retry_manager = None
    service._egress_context = None
    return service


@pytest.fixture
def log_sink(monkeypatch):
    """Capture messages and extras after real application logger setup.

    Configure the production patcher explicitly so this contract does not
    depend on another test having started the application first. Restore
    process-wide state so this fixture cannot affect later tests either.
    """
    from local_deep_research.utilities.log_utils import config_logger
    from tests.test_utils import restored_loguru_state

    captured = []

    def _sink(message):
        captured.append(f"{message}{message.record['extra']!r}")

    monkeypatch.setenv("LDR_ENABLE_FILE_LOGGING", "false")
    with restored_loguru_state():
        config_logger("download_contract")
        # Exercise the installed production patcher with an isolated sink;
        # this service-level contract needs no DB or frontend log delivery.
        logger.remove()
        logger.add(
            _sink,
            level="TRACE",
            format="{level} | {name}:{function} | {message}",
            diagnose=False,
            backtrace=True,
        )
        yield captured


@pytest.fixture
def raw_log_sink():
    """Capture what the call sites format, with NO redaction patcher.

    ``log_sink`` installs the production ``_sanitize_record`` patcher, so
    a call site that logs a raw URL still passes there. This sink runs
    with the core patcher replaced by a no-op, so it sees each record
    exactly as the call site built it -- as a process that never calls
    ``config_logger`` (benchmark CLI, programmatic API use) would. Restores
    process-wide loguru state afterwards.
    """
    from tests.test_utils import restored_loguru_state

    captured = []

    def _sink(message):
        captured.append(f"{message}{message.record['extra']!r}")

    with restored_loguru_state():
        logger.configure(patcher=lambda record: None)
        logger.enable("local_deep_research")
        logger.remove()
        logger.add(
            _sink,
            level="TRACE",
            format="{level} | {name}:{function} | {message}",
            diagnose=False,
            backtrace=True,
        )
        yield captured


@pytest.fixture
def filesystem_mode(monkeypatch):
    """Open the operator gate for unencrypted filesystem PDF storage.

    Asserts the gate actually opened: without this, a renamed env var
    would quietly leave every "filesystem" test running in database mode
    and writing no file at all -- the assertions would then be about
    nothing.
    """
    monkeypatch.setenv(FILESYSTEM_GATE_ENV, "true")
    assert resolve_pdf_storage_mode("filesystem") == "filesystem", (
        "the filesystem-storage operator gate did not open; the rest of "
        "this test would silently exercise database mode"
    )


# ===========================================================================
# 1. Where the downloaded bytes land
# ===========================================================================


@pytest.mark.parametrize(
    "url, expected_name",
    [
        # Traversal in the path, a filename= query param, and a trailing
        # ".pdf" that a naive "use the last path segment" would honour.
        (
            "https://evil.example.com/a/../../../../etc/cron.d/pwn.pdf"
            "?filename=../../../../outside/pwn.pdf",
            "{resource_id}_{content_hash}.pdf",
        ),
        # arXiv takes the regex-extracted id branch; the hostile query is
        # not part of the name.
        (
            "https://arxiv.org/pdf/2401.12345v1?f=../../../outside/pwn.pdf",
            "arxiv_2401.12345_{content_hash}.pdf",
        ),
    ],
)
def test_storage_path_comes_from_server_side_values_only(
    db, store, seeded, filesystem_mode, url, expected_name
):
    """The written path is <root>/pdfs/<server-derived name>, and nothing
    is created anywhere else under the sandbox.

    The remote server also offers a hostile ``Content-Disposition``; this
    code path never reads that header (grep: the library download path has
    no Content-Disposition reader at all), and the assertion below is that
    the header changed nothing about where the bytes landed.
    """
    fixture = seeded(url)
    before = _tree(store.sandbox)

    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="filesystem")

    success, reason, status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()

    assert success is True, reason
    expected = expected_name.format(
        resource_id=fixture.resource.id,
        content_hash=hashlib.sha256(PDF_BYTES).hexdigest(),
    )
    written = store.root / "pdfs" / expected

    # The exact whole-sandbox diff: one new file, at the expected path.
    # "the expected file exists" alone would pass just as happily for a
    # run that ALSO wrote /fs/outside/pwn.pdf.
    assert _tree(store.sandbox) - before == {
        written.relative_to(store.sandbox).as_posix()
    }
    assert written.read_bytes() == PDF_BYTES

    with db.fresh() as other:
        doc = other.get(Document, fixture.document.id)
        tracker = other.get(DownloadTracker, fixture.tracker.id)
    assert doc.file_path == f"pdfs/{expected}"
    assert doc.storage_mode == "filesystem"
    assert tracker.file_path == f"pdfs/{expected}"
    assert tracker.file_name == expected
    # Relative, and contained: no separator games survived into the DB.
    assert not Path(doc.file_path).is_absolute()
    assert ".." not in doc.file_path


@pytest.mark.parametrize(
    "url, expected",
    [
        (
            "https://evil.example.com/../../../../etc/passwd.pdf",
            "77.pdf",
        ),
        ("https://evil.example.com/x?name=../../pwn.pdf", "77.pdf"),
        # Host is checked exactly, so a look-alike host cannot reach the
        # arXiv/PMC id branches.
        ("https://arxiv.org.evil.example.com/pdf/2401.12345", "77.pdf"),
        ("https://arxiv.org/pdf/2401.12345v2", "arxiv_2401.12345.pdf"),
        # The id now comes from the same parser the downloader and the
        # citation label use, and that parser reads the PATH rather than
        # searching the whole URL for four-dot-five digits. `/pdf/../../…`
        # normalises to `arxiv.org/2401.12345`, which names no paper — so the
        # file keeps the server-side name instead of claiming a provenance
        # nothing else in the pipeline agrees with (#6413).
        ("https://arxiv.org/pdf/../../2401.12345", "77.pdf"),
        (
            "https://ncbi.nlm.nih.gov/pmc/articles/PMC777/../../x",
            "pmc_PMC777.pdf",
        ),
    ],
)
def test_generated_filename_is_always_a_bare_name(store, url, expected):
    """``_generate_filename`` never emits a path separator or a ``..``.

    This is the single function that lets any part of a remote-influenced
    URL into the stored filename, so it is pinned directly as well as
    end-to-end.
    """
    manager = PDFStorageManager(
        library_root=store.root, storage_mode="filesystem"
    )
    name = manager._generate_filename(url, 77, "77.pdf")

    assert name == expected
    assert "/" not in name and "\\" not in name
    assert ".." not in name
    assert name.endswith(".pdf")


@pytest.mark.parametrize(
    "filename, match",
    [
        ("../../outside/pwn.pdf", "traversal"),
        ("..%2f..%2foutside%2fpwn.pdf", "encoded traversal"),
        # A remote payload must not be able to land under an executable
        # or config extension even inside the store.
        ("pwn.sh", "Invalid file type"),
    ],
)
def test_save_pdf_refuses_an_escaping_filename_and_writes_nothing(
    db, store, seeded, filename, match
):
    """The refusal is specific AND the filesystem is byte-identical after.

    ``_download_pdf`` itself only ever passes ``f"{resource.id}.pdf"``, so
    this is the contract that keeps that true if a future caller forwards
    a remote-supplied name (the sibling collection-upload path does).
    """
    fixture = seeded("https://evil.example.com/paper")
    before = _tree(store.sandbox)

    manager = PDFStorageManager(
        library_root=store.root, storage_mode="filesystem"
    )
    with pytest.raises(ValueError, match=match):
        manager.save_pdf(
            pdf_content=PDF_BYTES,
            document=fixture.document,
            session=db.session,
            filename=filename,
            url=None,
            resource_id=fixture.resource.id,
        )

    assert _tree(store.sandbox) == before
    assert not (store.outside / "pwn.pdf").exists()
    assert list(store.outside.iterdir()) == []
    # The refusal happened before the row was touched, too.
    assert fixture.document.file_path is None


def test_an_absolute_filename_is_contained_inside_the_store(db, store, seeded):
    """An absolute-looking filename is normalized INTO the store, not out.

    ``pdfs/`` + ``/etc/cron.d/pwn.pdf`` normalizes to
    ``pdfs/etc/cron.d/pwn.pdf``, which ``safe_join`` accepts because it no
    longer escapes the base. Containment therefore holds -- nothing lands
    in the real ``/etc`` -- but the write then dies on the missing
    intermediate directory with a raw ``FileNotFoundError`` rather than a
    clean refusal, and the caller only survives it because
    ``_download_pdf`` wraps everything in ``except Exception``.
    """
    fixture = seeded("https://evil.example.com/paper")
    before = _tree(store.sandbox)

    manager = PDFStorageManager(
        library_root=store.root, storage_mode="filesystem"
    )
    with pytest.raises(FileNotFoundError):
        manager.save_pdf(
            pdf_content=PDF_BYTES,
            document=fixture.document,
            session=db.session,
            filename="/etc/cron.d/pwn.pdf",
            url=None,
            resource_id=fixture.resource.id,
        )

    assert _tree(store.sandbox) == before
    assert not Path("/etc/cron.d/pwn.pdf").exists()
    assert fixture.document.file_path is None


# ===========================================================================
# 2. Size bounds on the response -- asserted at the check, never allocated
# ===========================================================================


def test_oversized_content_length_is_rejected_before_the_body_is_read():
    """A declared body over the cap is refused without reading a byte.

    ``raw.read`` is booby-trapped: if the guard ever consumed the body to
    find out how big it was, this test fails instead of allocating.
    """

    def _explode(*args, **kwargs):
        raise AssertionError("body was read despite an over-cap Content-Length")

    response = StubResponse(
        headers={"Content-Length": str(safe_requests.MAX_RESPONSE_SIZE + 1)}
    )
    response.raw = SimpleNamespace(read=_explode)

    with pytest.raises(ValueError, match="Response too large"):
        safe_requests._check_response_size(response)

    assert response.closed is True, "the connection was leaked on rejection"


def test_chunked_forever_body_is_cut_off_by_the_body_guard(monkeypatch):
    """A body with no ``Content-Length`` that never ends is bounded.

    The cap is lowered to 4 KiB for the duration so the bound is proven
    with kilobytes rather than by streaming a real gigabyte. ``bounded_read``
    reads ``MAX_RESPONSE_SIZE`` from the module at call time, so this
    exercises the production guard, not a copy of it.
    """
    monkeypatch.setattr(safe_requests, "MAX_RESPONSE_SIZE", 4096)

    served = SimpleNamespace(total=0)

    def _endless(amt=None, *args, **kwargs):
        chunk = b"\x00" * 512
        served.total += len(chunk)
        return chunk

    response = StubResponse(headers={})  # no Content-Length at all
    response.raw = SimpleNamespace(read=_endless)

    safe_requests._check_response_size(response)  # installs the guard

    with pytest.raises(ValueError, match="Response body too large"):
        for _ in range(10_000):  # would be unbounded without the guard
            response.raw.read(512)

    assert response.closed is True
    # One chunk of slack past the cap, and nowhere near the 5 MB that
    # 10_000 unguarded iterations would have produced.
    assert served.total <= 4096 + 512


SEARCHABLE_TEXT = "Searchable text extracted from the oversized PDF."
# 1 MiB of padding against a 1 MB limit: the same
# ``file_size > self.max_pdf_size_bytes`` branch a multi-gigabyte PDF takes,
# without moving multiple gigabytes.
OVERSIZED_PDF = PDF_BYTES + b"\x00" * (1024 * 1024)
REFUSAL = "pdf_storage_size_limit"


def _one_download_session(db, monkeypatch):
    """Give the download service one reused session, as production does.

    ``get_user_db_session`` hands every scope on a thread the same
    thread-local session and does not close it at scope exit.
    ``_download_pdf`` saves the extracted text in a later scope than the one
    that stored the PDF, through the resource instance that scope loaded.
    With a fresh session per scope (``db.fresh()``) that instance is
    detached and expired by then, so ``_save_text_with_db`` fails on it, the
    failure is logged and swallowed as best-effort, and the text is never
    saved: the tests that patched ``get_user_db_session`` that way read
    ``text_content`` as None. ``test_library_sync_contracts`` documents the
    same trap on its ``one_download_session`` fixture.
    """
    from local_deep_research.research_library.services import download_service

    monkeypatch.setattr(
        download_service,
        "get_user_db_session",
        lambda *a, **k: nullcontext(db.download_session),
    )


def _refusing_service(db, store, monkeypatch, fixture, mode, *, existing):
    """A service whose 1 MB limit refuses ``OVERSIZED_PDF`` in ``mode``.

    ``existing`` keeps the seeded FAILED document (the retry branch);
    otherwise it is deleted so the download creates a new document. Text
    extraction returns ``SEARCHABLE_TEXT``, so a test can tell whether the
    text survived the refusal. Every ``record_attempt`` call is collected.
    The service gets one reused session (see ``_one_download_session``).
    """
    from local_deep_research.database.models.library import Collection
    from local_deep_research.research_library.services import download_service

    collection_id = "retry-library"
    db.session.add(Collection(id=collection_id, name="Retry library"))
    if not existing:
        db.session.delete(fixture.document)
    db.session.commit()
    monkeypatch.setattr(
        download_service,
        "get_source_type_id",
        lambda *a: fixture.source_type.id,
    )
    monkeypatch.setattr(
        download_service, "get_default_library_id", lambda *a: collection_id
    )
    _one_download_session(db, monkeypatch)
    downloader = StubDownloader(
        DownloadResult(content=OVERSIZED_PDF, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode=mode, max_mb=1)
    service._extract_text_from_pdf = lambda content: SEARCHABLE_TEXT
    recorded = []
    service.retry_manager = SimpleNamespace(
        record_attempt=lambda **kwargs: recorded.append(kwargs)
    )
    return service, downloader, recorded


@pytest.mark.parametrize("mode", ["database", "filesystem"])
@pytest.mark.parametrize(
    "existing", [False, True], ids=["new-document", "failed-document"]
)
def test_a_refused_pdf_keeps_its_text_in_a_document_awaiting_the_pdf(
    db, store, seeded, filesystem_mode, monkeypatch, mode, existing
):
    """The size limit refuses the PDF, not the download.

    The document is COMPLETED with the extracted text saved, so it stays
    searchable, and it is labelled as storing no PDF and awaiting one
    (``document_awaits_pdf``), with the refused PDF's size recorded. No
    bytes are stored anywhere, and the retry manager records a success.
    """
    from local_deep_research.constants import FILE_PATH_TEXT_ONLY
    from local_deep_research.research_library.utils import document_awaits_pdf

    fixture = seeded("https://example.com/huge.pdf")
    resource_id = fixture.resource.id
    tracker_id = fixture.tracker.id
    service, downloader, recorded = _refusing_service(
        db, store, monkeypatch, fixture, mode, existing=existing
    )

    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == 1
    with db.fresh() as other:
        doc = other.query(Document).filter_by(resource_id=resource_id).one()
        assert doc.status is DocumentStatus.COMPLETED
        assert doc.text_content == SEARCHABLE_TEXT
        assert (doc.file_type, doc.storage_mode, doc.file_path) == (
            "pdf",
            "none",
            FILE_PATH_TEXT_ONLY,
        )
        assert doc.file_size == len(OVERSIZED_PDF)
        assert document_awaits_pdf(doc)
        assert other.query(DocumentBlob).count() == 0
        tracker = other.get(DownloadTracker, tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (True, None)
        assert other.query(DownloadAttempt).one().succeeded is True
    assert _tree(store.sandbox) == set()
    assert [call["result"] for call in recorded] == [(True, None)]


@pytest.mark.parametrize("mode", ["database", "filesystem"])
def test_a_refused_pdf_is_not_downloaded_again_while_the_limit_refuses_it(
    db, store, seeded, filesystem_mode, monkeypatch, mode
):
    """No daily (or per-run) re-download of a PDF the limit still refuses.

    ``download_resource`` answers with the refusal before fetching anything,
    and records nothing with the retry manager, as long as the limit is
    below the recorded size. The document and its text are left as they are.
    """
    from local_deep_research.research_library.utils import document_awaits_pdf

    fixture = seeded("https://example.com/huge.pdf")
    resource_id = fixture.resource.id
    service, downloader, recorded = _refusing_service(
        db, store, monkeypatch, fixture, mode, existing=False
    )
    assert service.download_resource(resource_id) == (True, None)

    for _ in range(3):
        assert service.download_resource(resource_id) == (False, REFUSAL)
    assert len(downloader.urls) == 1
    assert len(recorded) == 1
    with db.fresh() as other:
        doc = other.query(Document).filter_by(resource_id=resource_id).one()
        assert document_awaits_pdf(doc)
        assert doc.text_content == SEARCHABLE_TEXT
        assert other.query(DownloadAttempt).count() == 1
    assert _tree(store.sandbox) == set()


@pytest.mark.parametrize("mode", ["database", "filesystem"])
@pytest.mark.parametrize(
    "existing", [False, True], ids=["new-document", "failed-document"]
)
def test_a_refused_pdf_is_stored_once_the_limit_is_raised(
    db, store, seeded, filesystem_mode, monkeypatch, mode, existing
):
    """Raising the limit above the PDF's size makes the next download store it.

    The PDF goes into the same document, which keeps its text and no
    longer awaits its PDF.
    """
    from local_deep_research.research_library.utils import document_awaits_pdf

    fixture = seeded("https://example.com/huge.pdf")
    resource_id = fixture.resource.id
    service, downloader, _recorded = _refusing_service(
        db, store, monkeypatch, fixture, mode, existing=existing
    )
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        doc_id = (
            other.query(Document).filter_by(resource_id=resource_id).one().id
        )

    service.settings._values["research_library.max_pdf_size_mb"] = 2
    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == 2
    with db.fresh() as other:
        doc = other.query(Document).filter_by(resource_id=resource_id).one()
        assert doc.id == doc_id
        assert doc.status is DocumentStatus.COMPLETED
        assert not document_awaits_pdf(doc)
        assert doc.storage_mode == mode
        assert doc.text_content == SEARCHABLE_TEXT
        manager = PDFStorageManager(library_root=store.root, storage_mode=mode)
        assert manager.load_pdf(doc, other) == OVERSIZED_PDF


def test_a_refusal_sets_no_cooldown_for_the_text_routes(
    db, store, seeded, monkeypatch
):
    """The refusal must not hold back the text routes through the retry layer.

    ``download_as_text`` and ``_try_arxiv_text_extraction`` both ask
    ``RetryManager.should_retry_resource`` before any network access. With
    a real ``RetryManager`` recording every ``download_resource`` call, the
    first (refused) download records a success, and the answers while the
    limit refuses the PDF record nothing, however often they are asked, so
    the resource stays retryable for those routes.
    """
    from local_deep_research.library.download_management import (
        retry_manager as retry_manager_module,
    )
    from local_deep_research.library.download_management.models import (
        Base as RetryBase,
    )
    from local_deep_research.library.download_management.status_tracker import (
        MAX_TOTAL_RETRIES,
        ResourceStatusTracker,
    )

    RetryBase.metadata.create_all(db.engine)
    tracker = ResourceStatusTracker.__new__(ResourceStatusTracker)
    tracker.Session = sessionmaker(bind=db.engine)
    monkeypatch.setattr(
        retry_manager_module,
        "ResourceStatusTracker",
        lambda *args, **kwargs: tracker,
    )
    retry_manager = retry_manager_module.RetryManager(USERNAME)

    fixture = seeded("https://example.com/huge.pdf")
    resource_id = fixture.resource.id
    service, downloader, _recorded = _refusing_service(
        db, store, monkeypatch, fixture, "database", existing=False
    )
    service.retry_manager = retry_manager

    assert service.download_resource(resource_id) == (True, None)
    for _ in range(MAX_TOTAL_RETRIES + 2):
        assert service.download_resource(resource_id) == (False, REFUSAL)
    assert len(downloader.urls) == 1
    assert retry_manager.should_retry_resource(resource_id).can_retry is True
    status = retry_manager.status_tracker.get_resource_status(resource_id)
    assert status is None or status["status"] == "completed"


@pytest.mark.parametrize(
    "size,refused",
    [(1024 * 1024, False), (1024 * 1024 + 1, True)],
    ids=["at-the-limit", "one-byte-over"],
)
def test_the_limit_check_refuses_only_a_pdf_over_the_limit(
    store, size, refused
):
    """The check refuses exactly what ``save_pdf`` refuses: more than the
    limit (``file_size > max_pdf_size_bytes``), not a PDF of exactly the
    limit's size."""
    from local_deep_research.constants import FILE_PATH_TEXT_ONLY

    document = Document(
        status=DocumentStatus.COMPLETED,
        file_type="pdf",
        storage_mode="none",
        file_path=FILE_PATH_TEXT_ONLY,
        resource_id=1,
        file_size=size,
    )
    service = make_service(
        store, StubDownloader(), storage_mode="database", max_mb=1
    )
    assert service._pdf_refused_at_current_limit(document) is refused
    manager = PDFStorageManager(
        library_root=store.root, storage_mode="database", max_pdf_size_mb=1
    )
    assert (size > manager.max_pdf_size_bytes) is refused


def _second_resource_on(db, fixture, url):
    """Another research resource citing ``url``, with no tracker of its own."""
    resource = ResearchResource(
        research_id=fixture.resource.research_id,
        title="Same Paper, Cited Again",
        url=url,
        source_type="academic",
        created_at="2026-08-01T00:00:00",
    )
    db.session.add(resource)
    db.session.commit()
    return resource.id


def test_auto_persist_links_a_second_resource_to_a_refused_pdf_without_fetching(
    db, store, seeded, monkeypatch
):
    """Auto-persist tracker dedup in ``download_resource``.

    A second resource on the same URL finds the document whose PDF the
    limit refused. It is linked to it without downloading the PDF again,
    and once the limit is raised, its download stores the PDF there.
    """
    from local_deep_research.research_library.utils import document_awaits_pdf

    url = "https://example.com/huge.pdf"
    fixture = seeded(url)
    first_id = fixture.resource.id
    service, downloader, _recorded = _refusing_service(
        db, store, monkeypatch, fixture, "database", existing=False
    )
    service._settings_snapshot = {
        "research_library.auto_persist_research_sources": True
    }
    assert service.download_resource(first_id) == (True, None)
    second_id = _second_resource_on(db, fixture, url)

    assert service.download_resource(
        second_id, collection_id="retry-library"
    ) == (True, None)
    assert len(downloader.urls) == 1
    with db.fresh() as other:
        doc = other.query(Document).filter_by(resource_id=first_id).one()
        assert other.get(ResearchResource, second_id).document_id == doc.id
        assert document_awaits_pdf(doc)
        assert other.query(Document).count() == 1

    service.settings._values["research_library.max_pdf_size_mb"] = 2
    assert service.download_resource(
        second_id, collection_id="retry-library"
    ) == (True, None)
    assert len(downloader.urls) == 2
    with db.fresh() as other:
        doc = other.query(Document).filter_by(resource_id=first_id).one()
        assert not document_awaits_pdf(doc)
        manager = PDFStorageManager(
            library_root=store.root, storage_mode="database"
        )
        assert manager.load_pdf(doc, other) == OVERSIZED_PDF


@pytest.mark.parametrize("link", ["content-hash", "url"])
def test_persist_research_sources_links_to_a_refused_pdf_without_fetching(
    db, store, seeded, monkeypatch, link
):
    """The two dedup links of ``persist_research_sources``.

    ``content-hash``: the URL's tracker names the refused PDF's hash, and
    the document's own URL is another one, so only the hash link can find
    it. ``url``: the tracker has no hash, so only the URL link can. Either
    way the second resource is linked to the document without downloading
    the PDF again, and the first resource's own document, which the limit
    still refuses, is not downloaded again either.
    """
    url = "https://example.com/huge.pdf"
    fixture = seeded(url)
    first_id = fixture.resource.id
    tracker_id = fixture.tracker.id
    service, downloader, _recorded = _refusing_service(
        db, store, monkeypatch, fixture, "database", existing=False
    )
    service.settings._values["document_scheduler.download_pdfs"] = True
    assert service.download_resource(first_id) == (True, None)
    second_id = _second_resource_on(db, fixture, url)
    with db.fresh() as other:
        doc = other.query(Document).filter_by(resource_id=first_id).one()
        doc_id = doc.id
        if link == "content-hash":
            doc.original_url = "https://mirror.example.org/huge.pdf"
        else:
            other.get(DownloadTracker, tracker_id).file_hash = None
        other.commit()

    summary = service.persist_research_sources(fixture.resource.research_id)

    assert len(downloader.urls) == 1
    assert summary["failed"] == 0
    with db.fresh() as other:
        assert other.get(ResearchResource, second_id).document_id == doc_id
        assert other.query(Document).count() == 1


@pytest.mark.parametrize("mode", ["database", "filesystem"])
def test_dedup_storage_refusal_links_keeps_the_text_and_stores_after_raise(
    db, store, seeded, filesystem_mode, monkeypatch, mode
):
    """A duplicate whose bytes match a text-only research download.

    The limit refuses the upgrade of the canonical document to a stored
    PDF. The duplicate is linked to it as before (collection, queue,
    tracker), the canonical keeps its text and now awaits its PDF, neither
    resource downloads it again while the limit refuses it, and once the
    limit is raised the duplicate's download stores the PDF in the
    canonical document.
    """
    from local_deep_research.constants import FILE_PATH_TEXT_ONLY
    from local_deep_research.database.models.library import (
        Collection,
        DocumentCollection,
        DownloadQueue,
    )
    from local_deep_research.research_library.utils import document_awaits_pdf

    canonical = seeded("https://example.com/canonical.pdf")
    duplicate = seeded("https://example.com/duplicate.pdf")
    canonical_id = canonical.document.id
    canonical_resource_id = canonical.resource.id
    duplicate_resource_id = duplicate.resource.id
    canonical_tracker_id = canonical.tracker.id
    duplicate_tracker_id = duplicate.tracker.id
    duplicate_url_hash = duplicate.tracker.url_hash
    db.session.delete(duplicate.document)
    db.session.add_all(
        [
            Collection(id="original-collection", name="Original"),
            Collection(id="target-collection", name="Target"),
        ]
    )
    db.session.flush()
    db.session.add_all(
        [
            DocumentCollection(
                document_id=canonical_id, collection_id="original-collection"
            ),
            DownloadQueue(
                resource_id=duplicate_resource_id,
                research_id=duplicate.resource.research_id,
                collection_id="target-collection",
                status=DocumentStatus.PENDING,
            ),
        ]
    )
    db.session.commit()

    downloader = StubDownloader(
        DownloadResult(content=OVERSIZED_PDF, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="none", max_mb=1)
    service._extract_text_from_pdf = lambda content: SEARCHABLE_TEXT
    service.retry_manager = SimpleNamespace(
        record_attempt=lambda **kwargs: None
    )
    _one_download_session(db, monkeypatch)
    assert service.download_resource(canonical_resource_id) == (True, None)

    service.settings._values["research_library.pdf_storage_mode"] = mode
    assert service.download_resource(duplicate_resource_id) == (True, None)
    with db.fresh() as other:
        doc = other.get(Document, canonical_id)
        assert other.query(Document).count() == 1
        assert doc.status is DocumentStatus.COMPLETED
        assert doc.text_content == SEARCHABLE_TEXT
        assert (doc.storage_mode, doc.file_path) == (
            "none",
            FILE_PATH_TEXT_ONLY,
        )
        assert document_awaits_pdf(doc)
        assert other.query(DocumentBlob).count() == 0
        assert (
            other.get(ResearchResource, duplicate_resource_id).document_id
            == canonical_id
        )
        tracker = other.get(DownloadTracker, duplicate_tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (True, None)
        assert {
            row.collection_id for row in other.query(DocumentCollection).all()
        } == {"original-collection", "target-collection"}
        assert (
            other.query(DownloadQueue).one().status is DocumentStatus.COMPLETED
        )
        attempt = (
            other.query(DownloadAttempt)
            .filter_by(url_hash=duplicate_url_hash)
            .one()
        )
        assert attempt.succeeded is True
    assert _tree(store.sandbox) == set()

    # Neither resource fetches the PDF again while the limit refuses it.
    assert service.download_resource(duplicate_resource_id) == (False, REFUSAL)
    assert service.download_resource(canonical_resource_id) == (False, REFUSAL)
    assert len(downloader.urls) == 2

    service.settings._values["research_library.max_pdf_size_mb"] = 2
    assert service.download_resource(duplicate_resource_id) == (True, None)
    assert len(downloader.urls) == 3
    with db.fresh() as other:
        doc = other.get(Document, canonical_id)
        assert other.query(Document).count() == 1
        assert doc.storage_mode == mode
        assert not document_awaits_pdf(doc)
        assert doc.text_content == SEARCHABLE_TEXT
        trackers = [
            other.get(DownloadTracker, tid)
            for tid in (canonical_tracker_id, duplicate_tracker_id)
        ]
        assert all(tracker.is_downloaded for tracker in trackers)
        assert trackers[0].file_path == trackers[1].file_path
        manager = PDFStorageManager(library_root=store.root, storage_mode=mode)
        assert manager.load_pdf(doc, other) == OVERSIZED_PDF


def _document_awaiting_its_pdf(db, seeded, site):
    """A document "Sync library" left awaiting its PDF, and the refill.

    ``own-document`` refills it through its own resource with changed
    bytes; ``linked-canonical`` through a resource linked to it whose
    bytes the content-hash dedup matches to it.
    """
    from local_deep_research.constants import FILE_PATH_TEXT_ONLY
    from local_deep_research.database.models.library import Collection
    from local_deep_research.research_library.utils import document_awaits_pdf

    owner = seeded("https://example.com/awaiting.pdf")
    awaiting_id = owner.document.id
    awaiting = owner.document
    awaiting.status = DocumentStatus.COMPLETED
    awaiting.storage_mode = "none"
    awaiting.file_path = FILE_PATH_TEXT_ONLY
    awaiting.text_content = "text kept by the sync"
    refill = owner
    if site == "own-document":
        awaiting.document_hash = hashlib.sha256(b"the lost PDF").hexdigest()
    else:
        awaiting.document_hash = hashlib.sha256(OVERSIZED_PDF).hexdigest()
        refill = seeded("https://example.com/linked-awaiting.pdf")
        db.session.delete(refill.document)
        db.session.flush()
        refill.resource.document_id = awaiting_id
    db.session.add(Collection(id="refill-collection", name="Refill"))
    db.session.commit()
    assert document_awaits_pdf(db.session.get(Document, awaiting_id))
    return awaiting_id, refill


@pytest.mark.parametrize("mode", ["database", "filesystem"])
@pytest.mark.parametrize("site", ["own-document", "linked-canonical"])
def test_a_refused_refill_keeps_the_document_awaiting_its_pdf(
    db, store, seeded, filesystem_mode, mode, site
):
    """A refill the size limit refuses leaves the document awaiting its PDF.

    It keeps its text and records the refused size, so ``download_resource``
    stops fetching it while the limit refuses that size and fetches it
    again once the limit is raised above it.
    """
    from local_deep_research.constants import FILE_PATH_TEXT_ONLY
    from local_deep_research.database.models.library import DocumentCollection
    from local_deep_research.research_library.utils import document_awaits_pdf

    awaiting_id, refill = _document_awaiting_its_pdf(db, seeded, site)
    tracker_id = refill.tracker.id
    url_hash = refill.tracker.url_hash

    downloader = StubDownloader(
        DownloadResult(content=OVERSIZED_PDF, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode=mode, max_mb=1)
    success, reason, _ = service._download_pdf(
        refill.resource, refill.tracker, db.session, "refill-collection"
    )
    db.session.commit()

    assert (success, reason) == (True, None)
    with db.fresh() as other:
        doc = other.get(Document, awaiting_id)
        assert other.query(Document).count() == 1
        assert doc.status is DocumentStatus.COMPLETED
        assert doc.text_content == "text kept by the sync"
        assert (doc.storage_mode, doc.file_path) == (
            "none",
            FILE_PATH_TEXT_ONLY,
        )
        assert doc.file_size == len(OVERSIZED_PDF)
        assert document_awaits_pdf(doc)
        assert other.query(DocumentBlob).count() == 0
        assert {
            row.collection_id
            for row in other.query(DocumentCollection).filter_by(
                document_id=awaiting_id
            )
        } == {"refill-collection"}
        tracker = other.get(DownloadTracker, tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (True, None)
        attempt = (
            other.query(DownloadAttempt).filter_by(url_hash=url_hash).one()
        )
        assert attempt.succeeded is True

        assert service._pdf_refused_at_current_limit(doc) is True
        service.settings._values["research_library.max_pdf_size_mb"] = 2
        assert service._pdf_refused_at_current_limit(doc) is False
    assert _tree(store.sandbox) == set()


def test_a_none_mode_refill_still_settles_text_only(db, store, seeded):
    """Storage mode "none" stores no PDF, and that is no refusal.

    A refill there still settles the document as a text-only download
    (``file_path`` None), so the download paths stop fetching it, and the
    limit check does not hold it back either.
    """
    from local_deep_research.research_library.utils import document_awaits_pdf

    awaiting_id, refill = _document_awaiting_its_pdf(db, seeded, "own-document")
    downloader = StubDownloader(
        DownloadResult(content=OVERSIZED_PDF, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="none", max_mb=1)
    success, reason, _ = service._download_pdf(
        refill.resource, refill.tracker, db.session, "refill-collection"
    )
    db.session.commit()

    assert (success, reason) == (True, None)
    with db.fresh() as other:
        doc = other.get(Document, awaiting_id)
        assert (doc.storage_mode, doc.file_path) == ("none", None)
        assert not document_awaits_pdf(doc)
        assert doc.text_content == "text kept by the sync"
        assert service._pdf_refused_at_current_limit(doc) is False
    assert _tree(store.sandbox) == set()


def test_text_only_mode_does_not_require_pdf_storage(db, store, seeded):
    """Storage mode "none" completes the download without treating the
    unstored PDF as refused: the document does not await its PDF."""
    from local_deep_research.research_library.utils import document_awaits_pdf

    fixture = seeded("https://example.com/text-only.pdf")
    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="none")
    success, reason, _ = service._download_pdf(
        fixture.resource, fixture.tracker, db.session
    )
    db.session.commit()
    assert success is True and reason is None
    with db.fresh() as other:
        assert other.query(DocumentBlob).count() == 0
        doc = other.get(Document, fixture.document.id)
        assert doc.file_path is None
        assert not document_awaits_pdf(doc)
    assert _tree(store.sandbox) == set()


@pytest.mark.parametrize("raw_mode", ["NONE", "None", "none ", "Database"])
def test_non_canonical_storage_mode_spellings_stay_text_only(
    db, store, seeded, raw_mode
):
    """A storage mode spelling the manager does not know refuses nothing.

    ``resolve_pdf_storage_mode`` returns these values unchanged and
    ``PDFStorageManager`` coerces each to an effective "none" (with a
    warning), so nothing is stored and nothing counts as refused: the
    download succeeds and the document is not made to await its PDF.

    What this guards: a refusal check that reads the raw setting and
    treats anything other than the exact string "none" as a mode that
    stores PDFs (every spelling here would then count as refused). It does
    not tell the raw setting from the manager's effective mode in general:
    for the canonical "database" and "filesystem" the two are the same,
    and the raw spellings here are not "database" or "filesystem" either,
    so a check against those two raw strings passes it too. The env
    override (LDR_RESEARCH_LIBRARY_PDF_STORAGE_MODE) can carry these
    values, so the premise is reachable.
    """
    from local_deep_research.research_library.utils import document_awaits_pdf

    fixture = seeded("https://example.com/noncanon.pdf")
    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode=raw_mode)
    success, reason, _ = service._download_pdf(
        fixture.resource, fixture.tracker, db.session
    )
    db.session.commit()
    assert success is True and reason is None
    with db.fresh() as other:
        doc = other.get(Document, fixture.document.id)
        assert not document_awaits_pdf(doc)
        assert service._pdf_refused_at_current_limit(doc) is False


def test_a_completed_document_always_has_retrievable_bytes(db, store, seeded):
    """CONTRACT: COMPLETED means the bytes are somewhere, or it says so.

    Either the call reports failure, or the row is not COMPLETED, or the
    content is retrievable, or the document is labelled as storing no PDF
    and awaiting one (``document_awaits_pdf``), as a document whose PDF
    the storage size limit refused is.
    """
    from local_deep_research.research_library.utils import document_awaits_pdf

    fixture = seeded("https://evil.example.com/huge.pdf")
    downloader = StubDownloader(
        DownloadResult(content=OVERSIZED_PDF, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, max_mb=1)

    success, _reason, _status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()

    with db.fresh() as other:
        doc = other.get(Document, fixture.document.id)
        manager = PDFStorageManager(
            library_root=store.root, storage_mode="none"
        )
        retrievable = manager.has_pdf(doc, other)
        completed = doc.status == DocumentStatus.COMPLETED
        says_no_pdf = doc.storage_mode == "none" and document_awaits_pdf(doc)

    assert not (success and completed) or retrievable or says_no_pdf


# ===========================================================================
# 3. Is a downloaded file trusted to be a PDF?
# ===========================================================================


def test_a_pdf_url_serving_html_is_rejected():
    """A ``.pdf`` URL is routing only -- it does not make the reply a PDF.

    ``DirectPDFDownloader.can_handle`` accepts the URL on its extension,
    but the response is still checked, and an HTML body under an HTML
    content-type is refused.
    """
    url = "https://evil.example.com/paper.pdf"
    downloader = _make_downloader(
        DirectPDFDownloader,
        StubResponse(
            status_code=200,
            content=HTML_BYTES,
            headers={"content-type": "text/html; charset=utf-8"},
        ),
    )

    assert downloader.can_handle(url) is True
    assert downloader._download_pdf(url) is None


def test_a_pdf_content_type_header_requires_matching_body():
    """The downloader requires PDF bytes even with a PDF content type."""
    url = "https://example.com/paper"
    downloader = _make_downloader(
        DirectPDFDownloader,
        StubResponse(
            status_code=200,
            content=HTML_BYTES,
            headers={"content-type": "application/pdf"},
        ),
    )

    assert downloader._download_pdf(url) is None
    assert not HTML_BYTES.startswith(b"%PDF")


@pytest.mark.xfail(
    strict=True,
    reason=(
        "DEFECT: nothing between the response header and the stored blob "
        "checks the %PDF magic bytes. _download_pdf hard-codes "
        'file_type="pdf" / mime_type="application/pdf" on whatever bytes '
        "arrive, so an HTML page served as application/pdf is stored, "
        "labelled and later served as a PDF."
    ),
)
def test_stored_pdf_bytes_are_actually_a_pdf(db, store, seeded):
    """CONTRACT: bytes labelled ``application/pdf`` start with ``%PDF``."""
    fixture = seeded("https://evil.example.com/paper.pdf")
    downloader = StubDownloader(
        DownloadResult(content=HTML_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader)

    success, reason, _status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()
    assert success is True, reason

    with db.fresh() as other:
        doc = other.get(Document, fixture.document.id)
        blob = (
            other.query(DocumentBlob)
            .filter_by(document_id=fixture.document.id)
            .first()
        )
        assert doc.status == DocumentStatus.COMPLETED
        assert doc.file_type == "pdf"
        assert blob is not None
        assert blob.pdf_binary.startswith(b"%PDF")


# ===========================================================================
# 4. Failure states
# ===========================================================================


def _explode_mid_write(path, content, *args, **kwargs):
    """Write half the bytes, then fail the way a full disk does."""
    Path(path).write_bytes(content[: len(content) // 2])
    raise OSError(28, "No space left on device")


def test_a_write_that_fails_partway_leaves_no_completed_row(
    db, store, seeded, filesystem_mode, monkeypatch
):
    """The row must not claim success when the bytes never fully landed."""
    fixture = seeded("https://evil.example.com/paper.pdf")
    # _save_to_filesystem imports write_file_verified INSIDE the function,
    # so the source module is the only place a patch takes effect.
    monkeypatch.setattr(
        file_write_verifier, "write_file_verified", _explode_mid_write
    )

    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="filesystem")

    success, reason, _status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()

    assert success is False
    assert "No space left on device" in reason

    with db.fresh() as other:
        doc = other.get(Document, fixture.document.id)
        blob = (
            other.query(DocumentBlob)
            .filter_by(document_id=fixture.document.id)
            .first()
        )
        attempts = (
            other.query(DownloadAttempt)
            .filter_by(url_hash=fixture.tracker.url_hash)
            .all()
        )
    # Rolled back to the seeded state, not left mid-flight as COMPLETED.
    assert doc.status == DocumentStatus.FAILED
    assert doc.file_path is None
    assert doc.storage_mode == "none"
    assert blob is None
    # The attempt is still recorded, as failed -- the rollback did not
    # swallow the audit row.
    assert len(attempts) == 1
    assert attempts[0].succeeded is False


def test_a_write_that_fails_partway_leaves_no_half_written_file(
    db, store, seeded, filesystem_mode, monkeypatch
):
    """CONTRACT: a failed download leaves nothing behind on disk.

    Issue #5868: ``_save_to_filesystem`` wrote in place, so a write that
    died partway left a truncated .pdf in the store that the rolled-back
    database no longer knew about. It now stages the bytes and moves them
    into place only once the write completed.
    """
    fixture = seeded("https://evil.example.com/paper.pdf")
    before = _tree(store.sandbox)
    # _save_to_filesystem imports write_file_verified INSIDE the function,
    # so the source module is the only place a patch takes effect.
    monkeypatch.setattr(
        file_write_verifier, "write_file_verified", _explode_mid_write
    )

    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="filesystem")
    service._download_pdf(fixture.resource, fixture.tracker, db.session, None)
    db.session.commit()

    assert _tree(store.sandbox) == before


def _stored_pdf(store, resource_id, content):
    """The sandbox-relative path ``_save_to_filesystem`` stores ``content`` at
    for a generic URL, and its ``file_path`` relative to the library root."""
    name = f"{resource_id}_{hashlib.sha256(content).hexdigest()}.pdf"
    stored = store.root / "pdfs" / name
    return stored.relative_to(store.sandbox).as_posix(), f"pdfs/{name}"


def test_a_new_document_write_that_fails_partway_is_not_recorded(
    db, store, seeded, filesystem_mode, monkeypatch
):
    """A new document whose PDF write fails partway is not kept (#5868).

    ``_save_pdf_document_record`` adds the document as COMPLETED before
    ``save_pdf`` writes the file. The failed write rolls the document back,
    leaves no truncated file, and records a failed attempt; the next
    download, with the disk writable again, creates the document COMPLETED
    with the stored PDF.
    """
    from local_deep_research.database.models.library import Collection
    from local_deep_research.research_library.services import download_service

    fixture = seeded("https://example.com/new-document.pdf")
    resource_id = fixture.resource.id
    tracker_id = fixture.tracker.id
    url_hash = fixture.tracker.url_hash
    # Without the seeded FAILED document the download takes the
    # new-document branch. The source type and default library lookups
    # open their own user-database sessions, so they answer from the seed.
    db.session.add(Collection(id="new-library", name="New library"))
    db.session.delete(fixture.document)
    db.session.commit()
    monkeypatch.setattr(
        download_service,
        "get_source_type_id",
        lambda *a: fixture.source_type.id,
    )
    monkeypatch.setattr(
        download_service, "get_default_library_id", lambda *a: "new-library"
    )
    write_file = file_write_verifier.write_file_verified
    monkeypatch.setattr(
        file_write_verifier, "write_file_verified", _explode_mid_write
    )
    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="filesystem")
    service._extract_text_from_pdf = lambda content: None

    success, _reason, _ = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()

    assert success is False
    with db.fresh() as other:
        assert (
            other.query(Document).filter_by(resource_id=resource_id).count()
            == 0
        )
        assert other.query(DocumentBlob).count() == 0
        tracker = other.get(DownloadTracker, tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (False, None)
        attempts = (
            other.query(DownloadAttempt).filter_by(url_hash=url_hash).all()
        )
        assert [attempt.succeeded for attempt in attempts] == [False]
    assert _tree(store.sandbox) == set()

    monkeypatch.setattr(file_write_verifier, "write_file_verified", write_file)
    success, reason, _ = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()

    assert success is True, reason
    stored, file_path = _stored_pdf(store, resource_id, PDF_BYTES)
    with db.fresh() as other:
        doc = other.query(Document).filter_by(resource_id=resource_id).one()
        assert doc.status is DocumentStatus.COMPLETED
        assert (doc.storage_mode, doc.file_path) == ("filesystem", file_path)
        attempts = (
            other.query(DownloadAttempt)
            .filter_by(url_hash=url_hash)
            .order_by(DownloadAttempt.id)
            .all()
        )
        assert [attempt.succeeded for attempt in attempts] == [False, True]
    assert _tree(store.sandbox) == {stored}


@pytest.mark.parametrize("site", ["own-document", "linked-canonical"])
def test_a_refill_write_that_fails_partway_keeps_the_document_awaiting_its_pdf(
    db, store, seeded, filesystem_mode, monkeypatch, site
):
    """A refill whose PDF write fails partway still awaits its PDF (#5868).

    A refill of a document that "Sync library" left awaiting its PDF sets
    it COMPLETED and gives it the new bytes' hash before ``save_pdf``
    writes the file. The failed write rolls that back: the document keeps
    its text and still awaits its PDF, so the Download Manager offers it
    again, and no truncated file is left. ``own-document`` takes the
    existing-document branch, ``linked-canonical`` the content-hash dedup
    branch. The next refill stores the PDF in the same document.
    """
    from local_deep_research.constants import FILE_PATH_TEXT_ONLY
    from local_deep_research.research_library.utils import document_awaits_pdf

    awaiting_id, refill = _document_awaiting_its_pdf(db, seeded, site)
    tracker_id = refill.tracker.id
    url_hash = refill.tracker.url_hash
    write_file = file_write_verifier.write_file_verified
    monkeypatch.setattr(
        file_write_verifier, "write_file_verified", _explode_mid_write
    )
    # The linked-canonical document carries OVERSIZED_PDF's hash; the
    # default limit of make_service stores it.
    downloader = StubDownloader(
        DownloadResult(content=OVERSIZED_PDF, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="filesystem")
    service._extract_text_from_pdf = lambda content: None

    success, _reason, _ = service._download_pdf(
        refill.resource, refill.tracker, db.session, "refill-collection"
    )
    db.session.commit()

    assert success is False
    with db.fresh() as other:
        doc = other.get(Document, awaiting_id)
        assert other.query(Document).count() == 1
        assert document_awaits_pdf(doc)
        assert (doc.storage_mode, doc.file_path) == (
            "none",
            FILE_PATH_TEXT_ONLY,
        )
        assert doc.text_content == "text kept by the sync"
        assert other.query(DocumentBlob).count() == 0
        tracker = other.get(DownloadTracker, tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (False, None)
        attempts = (
            other.query(DownloadAttempt).filter_by(url_hash=url_hash).all()
        )
        assert [attempt.succeeded for attempt in attempts] == [False]
    assert _tree(store.sandbox) == set()

    monkeypatch.setattr(file_write_verifier, "write_file_verified", write_file)
    success, reason, _ = service._download_pdf(
        refill.resource, refill.tracker, db.session, "refill-collection"
    )
    db.session.commit()

    assert success is True, reason
    with db.fresh() as other:
        doc = other.get(Document, awaiting_id)
        assert other.query(Document).count() == 1
        assert doc.status is DocumentStatus.COMPLETED
        assert not document_awaits_pdf(doc)
        assert doc.storage_mode == "filesystem"
        assert doc.text_content == "text kept by the sync"
        manager = PDFStorageManager(
            library_root=store.root, storage_mode="filesystem"
        )
        assert manager.load_pdf(doc, other) == OVERSIZED_PDF
    assert len(_tree(store.sandbox)) == 1


def _rolling_back_download_session(db, monkeypatch):
    """Give the download service one reused session, rolled back on error.

    Like ``_one_download_session``, but a scope that raises rolls the
    session back before the error propagates, as ``get_user_db_session``
    does, so a failed write is judged by what production keeps rather
    than by pending changes a bare ``nullcontext`` would leave behind.
    """
    from local_deep_research.research_library.services import download_service

    @contextmanager
    def scope(*args, **kwargs):
        try:
            yield db.download_session
        except Exception:
            db.download_session.rollback()
            raise

    monkeypatch.setattr(download_service, "get_user_db_session", scope)


def test_a_fetch_time_write_that_fails_partway_is_not_recorded(
    db, store, seeded, filesystem_mode, monkeypatch
):
    """Fetch-time persistence whose PDF write fails partway keeps nothing.

    ``_do_persist_downloaded_pdf_on_the_spot`` (a research's own fetch,
    with auto-persistence on) adds the document as COMPLETED before
    ``save_pdf`` writes the file (#5868). The failed write rolls it back
    and leaves no truncated file; the next fetch persists the document
    COMPLETED with the stored PDF and its text.
    """
    from local_deep_research.database.models.library import Collection
    from local_deep_research.research_library.services import download_service

    fixture = seeded("https://example.com/fetched.pdf")
    resource_id = fixture.resource.id
    url = fixture.resource.url
    db.session.add(Collection(id="fetch-library", name="Fetch library"))
    db.session.delete(fixture.document)
    db.session.commit()
    monkeypatch.setattr(
        download_service,
        "get_source_type_id",
        lambda *a: fixture.source_type.id,
    )
    _rolling_back_download_session(db, monkeypatch)
    write_file = file_write_verifier.write_file_verified
    monkeypatch.setattr(
        file_write_verifier, "write_file_verified", _explode_mid_write
    )

    def persist():
        return download_service._do_persist_downloaded_pdf_on_the_spot(
            url=url,
            pdf_content=PDF_BYTES,
            text=SEARCHABLE_TEXT,
            title="Fetched paper",
            username=USERNAME,
            settings_snapshot={
                "document_scheduler.download_pdfs": True,
                "research_library.pdf_storage_mode": "filesystem",
                "research_library.max_pdf_size_mb": 3072,
            },
            library_root=store.root,
            collection_id="fetch-library",
            resource_id=resource_id,
        )

    assert persist() is None
    with db.fresh() as other:
        assert other.query(Document).count() == 0
        assert other.query(DocumentBlob).count() == 0
    assert _tree(store.sandbox) == set()

    monkeypatch.setattr(file_write_verifier, "write_file_verified", write_file)
    doc_id = persist()

    assert doc_id is not None
    stored, file_path = _stored_pdf(store, resource_id, PDF_BYTES)
    with db.fresh() as other:
        doc = other.get(Document, doc_id)
        assert doc.status is DocumentStatus.COMPLETED
        assert (doc.storage_mode, doc.file_path) == ("filesystem", file_path)
        assert doc.text_content == SEARCHABLE_TEXT
    assert _tree(store.sandbox) == {stored}


def test_a_dedup_upgrade_write_that_fails_partway_is_not_recorded(
    db, store, seeded, filesystem_mode, monkeypatch
):
    """A duplicate whose PDF write into the matching document fails (#5868).

    The duplicate's bytes match a document that a download with PDF
    storage off kept without its PDF. The content-hash dedup links the
    duplicate to it and stores the PDF there, with no canonical retry
    involved. A write that fails partway used to be logged as best-effort
    and the download recorded as a success, with the duplicate linked,
    its tracker downloaded and nothing stored, so nothing fetched it
    again. Now the download is recorded as failed and rolled back, and
    the next download links the duplicate and stores the PDF in the
    matching document.
    """
    from local_deep_research.database.models.library import (
        Collection,
        DocumentCollection,
    )

    canonical = seeded("https://example.com/canonical.pdf")
    duplicate = seeded("https://example.com/duplicate.pdf")
    canonical_id = canonical.document.id
    canonical_tracker_id = canonical.tracker.id
    duplicate_resource_id = duplicate.resource.id
    duplicate_tracker_id = duplicate.tracker.id
    duplicate_url_hash = duplicate.tracker.url_hash
    file_hash = hashlib.sha256(PDF_BYTES).hexdigest()
    # The shape a download with PDF storage "none" leaves: COMPLETED, the
    # content hash, the text, and no stored PDF.
    document = canonical.document
    document.status = DocumentStatus.COMPLETED
    document.document_hash = file_hash
    document.file_size = len(PDF_BYTES)
    document.text_content = "text of the canonical download"
    canonical.tracker.file_hash = file_hash
    canonical.tracker.is_downloaded = True
    db.session.delete(duplicate.document)
    db.session.add(Collection(id="dedup-collection", name="Dedup"))
    db.session.commit()
    write_file = file_write_verifier.write_file_verified
    monkeypatch.setattr(
        file_write_verifier, "write_file_verified", _explode_mid_write
    )
    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="filesystem")
    service._extract_text_from_pdf = lambda content: None

    success, _reason, _ = service._download_pdf(
        duplicate.resource, duplicate.tracker, db.session, "dedup-collection"
    )
    db.session.commit()

    assert success is False
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        doc = other.get(Document, canonical_id)
        assert doc.status is DocumentStatus.COMPLETED
        assert (doc.storage_mode, doc.file_path) == ("none", None)
        assert doc.text_content == "text of the canonical download"
        assert other.query(DocumentBlob).count() == 0
        assert other.query(DocumentCollection).count() == 0
        assert (
            other.get(ResearchResource, duplicate_resource_id).document_id
            is None
        )
        tracker = other.get(DownloadTracker, duplicate_tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (False, None)
        assert (
            other.get(DownloadTracker, canonical_tracker_id).file_path is None
        )
        attempts = (
            other.query(DownloadAttempt)
            .filter_by(url_hash=duplicate_url_hash)
            .all()
        )
        assert [attempt.succeeded for attempt in attempts] == [False]
    assert _tree(store.sandbox) == set()

    monkeypatch.setattr(file_write_verifier, "write_file_verified", write_file)
    success, reason, _ = service._download_pdf(
        duplicate.resource, duplicate.tracker, db.session, "dedup-collection"
    )
    db.session.commit()

    assert success is True, reason
    stored, file_path = _stored_pdf(store, duplicate_resource_id, PDF_BYTES)
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        doc = other.get(Document, canonical_id)
        assert doc.status is DocumentStatus.COMPLETED
        assert (doc.storage_mode, doc.file_path) == ("filesystem", file_path)
        assert doc.text_content == "text of the canonical download"
        manager = PDFStorageManager(
            library_root=store.root, storage_mode="filesystem"
        )
        assert manager.load_pdf(doc, other) == PDF_BYTES
        assert (
            other.get(ResearchResource, duplicate_resource_id).document_id
            == canonical_id
        )
        assert {
            row.collection_id for row in other.query(DocumentCollection).all()
        } == {"dedup-collection"}
        tracker = other.get(DownloadTracker, duplicate_tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (True, file_path)
        assert (
            other.get(DownloadTracker, canonical_tracker_id).file_path
            == file_path
        )
        attempts = (
            other.query(DownloadAttempt)
            .filter_by(url_hash=duplicate_url_hash)
            .order_by(DownloadAttempt.id)
            .all()
        )
        assert [attempt.succeeded for attempt in attempts] == [False, True]
    assert _tree(store.sandbox) == {stored}


def test_a_fetch_time_write_into_a_matching_document_that_fails_keeps_it(
    db, store, seeded, filesystem_mode, monkeypatch
):
    """Fetch-time persistence into a document with the same bytes (#5868).

    ``_save_pdf_document_record`` reuses the document whose hash matches
    the fetched bytes. Here it stores no PDF (a download with PDF storage
    off, which found no text), so persistence gives it the text and then
    writes the PDF. A write that fails partway leaves no truncated file
    and leaves the document as it was; the next fetch stores the PDF and
    the text in that same document.
    """
    from local_deep_research.database.models.library import Collection
    from local_deep_research.research_library.services import download_service

    fixture = seeded("https://example.com/fetched-again.pdf")
    resource_id = fixture.resource.id
    document_id = fixture.document.id
    tracker_id = fixture.tracker.id
    url = fixture.resource.url
    document = fixture.document
    document.status = DocumentStatus.COMPLETED
    document.document_hash = hashlib.sha256(PDF_BYTES).hexdigest()
    document.file_size = len(PDF_BYTES)
    db.session.add(Collection(id="fetch-library", name="Fetch library"))
    db.session.commit()
    _rolling_back_download_session(db, monkeypatch)
    write_file = file_write_verifier.write_file_verified
    monkeypatch.setattr(
        file_write_verifier, "write_file_verified", _explode_mid_write
    )

    def persist():
        return download_service._do_persist_downloaded_pdf_on_the_spot(
            url=url,
            pdf_content=PDF_BYTES,
            text=SEARCHABLE_TEXT,
            title="Fetched paper",
            username=USERNAME,
            settings_snapshot={
                "document_scheduler.download_pdfs": True,
                "research_library.pdf_storage_mode": "filesystem",
                "research_library.max_pdf_size_mb": 3072,
            },
            library_root=store.root,
            collection_id="fetch-library",
            resource_id=resource_id,
        )

    assert persist() is None
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        doc = other.get(Document, document_id)
        assert doc.status is DocumentStatus.COMPLETED
        assert (doc.storage_mode, doc.file_path) == ("none", None)
        assert doc.text_content is None
        assert other.query(DocumentBlob).count() == 0
        tracker = other.get(DownloadTracker, tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (False, None)
    assert _tree(store.sandbox) == set()

    monkeypatch.setattr(file_write_verifier, "write_file_verified", write_file)

    assert persist() == document_id
    stored, file_path = _stored_pdf(store, resource_id, PDF_BYTES)
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        doc = other.get(Document, document_id)
        assert (doc.storage_mode, doc.file_path) == ("filesystem", file_path)
        assert doc.text_content == SEARCHABLE_TEXT
        tracker = other.get(DownloadTracker, tracker_id)
        assert (tracker.is_downloaded, tracker.file_path) == (True, file_path)
    assert _tree(store.sandbox) == {stored}


def test_a_failed_rewrite_keeps_the_pdf_a_document_already_stores(
    db, store, seeded, filesystem_mode, monkeypatch
):
    """A retry whose rewrite of a stored PDF fails partway keeps it (#5868).

    A document whose PDF is stored is FAILED when a later text extraction
    fails, and still names its file. Its retry downloads the same bytes and
    writes them under the same file name. The in-place write truncated
    that stored PDF when it failed partway; the staged write leaves it
    whole, and the document still loads it. The next retry completes the
    document with the same file.
    """
    fixture = seeded("https://example.com/stored.pdf")
    resource_id = fixture.resource.id
    document_id = fixture.document.id
    url_hash = fixture.tracker.url_hash
    stored, file_path = _stored_pdf(store, resource_id, PDF_BYTES)
    (store.sandbox / stored).write_bytes(PDF_BYTES)
    document = fixture.document
    document.document_hash = hashlib.sha256(PDF_BYTES).hexdigest()
    document.file_size = len(PDF_BYTES)
    document.storage_mode = "filesystem"
    document.file_path = file_path
    db.session.commit()
    write_file = file_write_verifier.write_file_verified
    monkeypatch.setattr(
        file_write_verifier, "write_file_verified", _explode_mid_write
    )
    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader, storage_mode="filesystem")
    service._extract_text_from_pdf = lambda content: None
    manager = PDFStorageManager(
        library_root=store.root, storage_mode="filesystem"
    )

    success, _reason, _ = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()

    assert success is False
    assert (store.sandbox / stored).read_bytes() == PDF_BYTES
    assert _tree(store.sandbox) == {stored}
    with db.fresh() as other:
        doc = other.get(Document, document_id)
        assert doc.status is DocumentStatus.FAILED
        assert (doc.storage_mode, doc.file_path) == ("filesystem", file_path)
        assert manager.load_pdf(doc, other) == PDF_BYTES
        attempts = (
            other.query(DownloadAttempt).filter_by(url_hash=url_hash).all()
        )
        assert [attempt.succeeded for attempt in attempts] == [False]

    monkeypatch.setattr(file_write_verifier, "write_file_verified", write_file)
    success, reason, _ = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()

    assert success is True, reason
    assert _tree(store.sandbox) == {stored}
    with db.fresh() as other:
        doc = other.get(Document, document_id)
        assert doc.status is DocumentStatus.COMPLETED
        assert (doc.storage_mode, doc.file_path) == ("filesystem", file_path)
        assert manager.load_pdf(doc, other) == PDF_BYTES


def test_retrying_a_failed_resource_does_not_duplicate_rows(db, store, seeded):
    """Attempt 1 fails, attempt 2 succeeds: one Document, one blob.

    Also pins the #3827 rule that the FAILED placeholder hash IS replaced
    on the transition to COMPLETED (and only then).
    """
    fixture = seeded("https://evil.example.com/paper.pdf")
    placeholder_hash = fixture.document.document_hash

    failing = StubDownloader(
        DownloadResult(
            skip_reason="Article not found (404) - may have been removed",
            is_success=False,
            status_code=404,
        )
    )
    service = make_service(store, failing)
    success, reason, status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    assert success is False
    assert status == 404
    assert "404" in reason

    succeeding = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service.downloaders = [succeeding]
    success, reason, _status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()
    assert success is True, reason

    with db.fresh() as other:
        docs = (
            other.query(Document)
            .filter_by(resource_id=fixture.resource.id)
            .all()
        )
        blobs = (
            other.query(DocumentBlob)
            .filter_by(document_id=fixture.document.id)
            .all()
        )
        attempts = (
            other.query(DownloadAttempt)
            .filter_by(url_hash=fixture.tracker.url_hash)
            .order_by(DownloadAttempt.id)
            .all()
        )
        trackers = (
            other.query(DownloadTracker)
            .filter_by(url_hash=fixture.tracker.url_hash)
            .all()
        )

    assert len(docs) == 1
    assert len(blobs) == 1
    assert len(trackers) == 1
    assert docs[0].status == DocumentStatus.COMPLETED
    assert docs[0].document_hash == hashlib.sha256(PDF_BYTES).hexdigest()
    assert docs[0].document_hash != placeholder_hash
    assert blobs[0].pdf_binary == PDF_BYTES
    # One audit row per attempt, in order.
    assert [a.succeeded for a in attempts] == [False, True]


# ===========================================================================
# 5. Paywall / failure keyword classification
# ===========================================================================


@pytest.mark.parametrize(
    "status_code, content_type, expected_type, expect_permanent",
    [
        # An HTML landing page alone is not evidence of a paywall.
        (200, "text/html; charset=utf-8", "html_not_pdf", False),
        (403, "text/html", "forbidden", True),
        (404, "text/html", "not_found", True),
        (500, "text/html", "unknown_error", False),
    ],
)
def test_generic_downloader_reasons_distinguish_html_from_access_failures(
    status_code, content_type, expected_type, expect_permanent
):
    """The producer and the classifier are wired together, not hardcoded.

    ``GenericDownloader.download_with_result`` produces the skip_reason
    string; ``FailureClassifier`` consumes it. Asserting a literal
    skip_reason in this file would let the two drift apart silently, so
    the reason is generated by the real downloader and fed to the real
    classifier in the argument shape ``RetryManager.record_attempt``
    uses (``error_type=type(skip_reason).__name__``, i.e. ``"str"``).
    """
    url = "https://journal.example.com/article/123"
    downloader = _make_downloader(
        GenericDownloader,
        StubResponse(
            status_code=status_code,
            content=HTML_BYTES,
            headers={"content-type": content_type},
        ),
    )

    result = downloader.download_with_result(url, ContentType.PDF)
    assert result.is_success is False
    assert result.skip_reason

    failure = FailureClassifier().classify_failure(
        error_type=type(result.skip_reason).__name__,
        status_code=result.status_code,
        url=url,
        details=result.skip_reason,
    )

    assert failure.error_type == expected_type
    assert failure.is_permanent() is expect_permanent


def test_fallback_pdf_extraction_wraps_html_not_pdf_reason_correctly(
    db, store, seeded
):
    """The ``download_as_text`` fallback path wraps the reason too, and
    the classifier must still recognize it there.

    ``download_resource`` passes ``GenericDownloader``'s skip_reason to
    the classifier verbatim (covered above), but
    ``DownloadService._fallback_pdf_extraction`` -- the path
    ``download_as_text`` takes when no API/text extraction succeeded --
    wraps it first: ``download_service.py`` returns
    ``f"PDF extraction failed: {error_msg}"``, and that wrapped string is
    what ``_record_retry_attempt`` passes to the classifier as
    ``details``. Exercise the real fallback method end to end (real
    downloader, real classifier, real DB session) rather than
    hand-wrapping the string here, so a change to the wrapper's exact
    text is caught too.

    REVERT: undoing the wrapper-prefix stripping in
    ``FailureClassifier.classify_failure`` (i.e. going back to
    ``if details_lower == "source is an html page, not a pdf":`` with no
    unwrapping) makes this test fail: ``failure.error_type`` becomes
    ``"unknown_error"`` instead of ``"html_not_pdf"``.
    """
    url = "https://journal.example.com/article/456"
    fixture = seeded(url)

    generic_response = StubResponse(
        status_code=200,
        content=HTML_BYTES,
        headers={"content-type": "text/html; charset=utf-8"},
    )
    downloader = _make_downloader(GenericDownloader, generic_response)
    service = make_service(store, downloader)

    success, error_message = service._fallback_pdf_extraction(
        db.session, fixture.resource
    )

    assert success is False
    # The producer's exact string survives, just wrapped.
    assert (
        error_message
        == "PDF extraction failed: Source is an HTML page, not a PDF"
    )

    failure = FailureClassifier().classify_failure(
        error_type=type(error_message).__name__,
        # _record_retry_attempt (download_service.py) never passes a
        # status_code on this call path.
        status_code=None,
        url=url,
        details=error_message,
    )

    assert failure.error_type == "html_not_pdf"
    assert failure.is_permanent() is False


def test_a_401_auth_required_page_is_retried_hourly_forever():
    """CHARACTERIZATION: the 401 reason misses every paywall keyword.

    ``GenericDownloader`` emits "Authentication required - please login
    to access this article". The classifier's keyword list is
    ``requires login`` / ``subscription`` / ``paywall`` / ``requires
    authentication`` -- none of which that sentence contains -- and 401
    is absent from its status-code table. The result is a generic
    ``unknown_error`` with a one-hour cooldown, so an
    authentication-walled article is re-fetched every hour indefinitely,
    while its 403 sibling is correctly permanent.

    This branch does modify ``failure_classifier.py``, but only to add an
    exact-match (and wrapped-exact-match) branch for GenericDownloader's
    HTML-format-mismatch string; the 401 reason above matches neither
    form, so this assertion's outcome is unaffected and still matches
    ``main`` byte for byte. It is pinned as current behaviour rather than
    as a port regression.
    """
    url = "https://journal.example.com/article/123"
    downloader = _make_downloader(
        GenericDownloader,
        StubResponse(
            status_code=401,
            content=HTML_BYTES,
            headers={"content-type": "text/html"},
        ),
    )

    result = downloader.download_with_result(url, ContentType.PDF)
    assert "Authentication required" in result.skip_reason

    failure = FailureClassifier().classify_failure(
        error_type=type(result.skip_reason).__name__,
        status_code=result.status_code,
        url=url,
        details=result.skip_reason,
    )

    assert failure.error_type == "unknown_error"
    assert failure.is_permanent() is False
    assert failure.retry_after == timedelta(hours=1)


# ===========================================================================
# 6. Credentials for authenticated sources
# ===========================================================================

# Userinfo credentials and a query-string key on one URL -- both shapes
# the scrubber claims to handle, and both shapes a real authenticated
# academic source produces.
SECRET_PASSWORD = "s3cr3tpassw0rd"
SECRET_KEY = "AKIAIOSFODNN7EXAMPLE"
SECRET_URL = (
    f"https://alice:{SECRET_PASSWORD}@journal.example.com/paper.pdf"
    f"?api_key={SECRET_KEY}"
)


def test_error_returned_to_the_client_is_credential_scrubbed(db, store, seeded):
    """A transport error that echoes the URL must not ship the secret.

    The skip_reason returned here is surfaced to the browser over the
    download SSE stream.
    """
    fixture = seeded(SECRET_URL)
    downloader = StubDownloader(
        raises=RuntimeError(f"connection refused for {SECRET_URL}")
    )
    service = make_service(store, downloader)

    success, reason, _status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()

    assert success is False
    assert reason  # a scrubbed message, not an empty one
    assert SECRET_PASSWORD not in reason
    assert SECRET_KEY not in reason

    with db.fresh() as other:
        attempt = (
            other.query(DownloadAttempt)
            .filter_by(url_hash=fixture.tracker.url_hash)
            .one()
        )
    assert SECRET_PASSWORD not in (attempt.error_message or "")
    assert SECRET_KEY not in (attempt.error_message or "")


def test_download_logs_do_not_leak_url_credentials(db, store, seeded, log_sink):
    """CONTRACT: a credential-bearing URL never reaches a log sink."""
    fixture = seeded(SECRET_URL)
    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader)

    success, reason, _status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()
    assert success is True, reason

    logged = "\n".join(log_sink)
    # Guard against a vacuous pass: the service must actually have logged
    # something during the download.
    assert logged.strip(), "nothing was captured; the sink is not wired up"
    assert SECRET_PASSWORD not in logged
    assert SECRET_KEY not in logged


def test_download_call_sites_redact_url_without_the_log_patcher(
    db, store, seeded, raw_log_sink
):
    """CONTRACT: the download's own log calls never format a raw URL.

    Independent of the global redaction patcher: ``raw_log_sink`` has it
    removed, so this fails if a call site regresses to logging the raw
    ``resource.url`` even while ``_sanitize_record`` would hide it.
    """
    # Canary: prove the patcher really is bypassed, otherwise the
    # assertions below would be vacuous.
    logger.info("canary {}", SECRET_URL)
    assert SECRET_KEY in "\n".join(raw_log_sink), (
        "the redaction patcher is still active; this test cannot see "
        "what the call sites log"
    )
    raw_log_sink.clear()

    fixture = seeded(SECRET_URL)
    downloader = StubDownloader(
        DownloadResult(content=PDF_BYTES, is_success=True, status_code=200)
    )
    service = make_service(store, downloader)

    success, reason, _status = service._download_pdf(
        fixture.resource, fixture.tracker, db.session, None
    )
    db.session.commit()
    assert success is True, reason

    logged = "\n".join(raw_log_sink)
    assert logged.strip(), "nothing was captured; the sink is not wired up"
    # The call sites still identify the resource by its (redacted) host.
    assert "journal.example.com" in logged
    assert SECRET_PASSWORD not in logged
    assert SECRET_KEY not in logged