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.

241 KB
Raw Blame
"""
PDF Download Service for Research Library

Handles downloading PDFs from various academic sources with:
- Deduplication using download tracker
- Source-specific download strategies (arXiv, PubMed, etc.)
- Progress tracking and error handling
- File organization and storage
"""

from concurrent.futures import ThreadPoolExecutor
from contextlib import nullcontext
import atexit
import threading
import hashlib
import os
import re
import time
import uuid
from datetime import datetime, UTC
from pathlib import Path
from collections.abc import Set as AbstractSet
from typing import (
    Any,
    Dict,
    List,
    NamedTuple,
    Optional,
    Tuple,
    Union,
    assert_never,
)
from urllib.parse import urlparse

import requests
from loguru import logger
from ...security import redact_url_for_log
from sqlalchemy import and_, func, or_
from sqlalchemy.orm import Session, defer, load_only
import pdfplumber
from pypdf import PdfReader

from ...utilities.arxiv import extract_arxiv_id, is_arxiv_paper_url
from ...constants import DEFAULT_SEARCH_TOOL
from ...utilities.url_utils import canonical_url_key
from ...constants import FILE_PATH_SENTINELS, FILE_PATH_TEXT_ONLY
from ...utilities.type_utils import to_bool, unwrap_setting
from ...database.models.download_tracker import (
    DownloadAttempt,
    DownloadTracker,
)

# SSRF chokepoint: every outbound fetch in this module goes through
# `safe_get`, which validates via ssrf_validator (raising on failure,
# re-validating after redirects, always blocking cloud-metadata
# addresses). `requests` is imported above for its exception types only
# — do not add a raw requests.get()/post() here. GHSA-9c54 was exactly
# that bypass, and `is_downloadable_domain` upstream is a relevance
# filter, not a security control.
from ...security import (
    failure_reason_for_log,
    redact_secrets,
    safe_get,
    sanitize_error_for_client,
)
from ...security.client_safe_errors import (
    CLIENT_SAFE_DOWNLOAD_MESSAGES,
    client_safe_download_message,
)
from ...security.secure_logging import log_exception_type
from ...database.models.library import (
    Collection,
    Document as Document,
    DocumentChunk,
    DocumentCollection,
    DocumentStatus,
    DownloadQueue as LibraryDownloadQueue,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)
from .pdf_storage_manager import (
    DEFAULT_MAX_PDF_SIZE_MB,
    PDFStorageManager,
    resolve_pdf_storage_mode,
)
from ...database.models.research import ResearchResource
from ...database.library_init import get_source_type_id, get_default_library_id
from ...database.session_context import get_user_db_session, safe_rollback
from ...utilities.db_utils import get_settings_manager
from ...library.download_management import FailureClassifier, RetryManager
from ...library.download_management.models import (
    DownloadStatus as RetryStatus,
)
from ...config.paths import get_library_directory
from ..utils import (
    _TRUSTED_STORAGE_LEAF,
    apply_user_subdir,
    document_awaits_pdf,
    download_claim_is_live,
    ensure_in_collection,
    find_tracker_by_url,
    get_document_for_resource,
    get_url_hash,
    get_url_hash_candidates,
    get_absolute_path_from_settings,
    is_downloadable_url,
)

# Import our modular downloaders
from ..downloaders import (
    BaseDownloader,
    ContentType,
    DownloadResult,
    ArxivDownloader,
    PubMedDownloader,
    BioRxivDownloader,
    DirectPDFDownloader,
    SemanticScholarDownloader,
    OpenAlexDownloader,
    OpenAccessDOIDownloader,
    GenericDownloader,
)
from ..downloaders.arxiv import ArxivTextSource, attribute_arxiv_text
from ..extraction_catalog import (
    ExtractionMethodExtra,
    ExtractionSourceExtra,
)

# Extraction ceilings live in ``utilities.pdf_extraction_limits`` so that
# the base downloader shares one definition without importing this module.
from ...utilities.pdf_extraction_limits import (
    MAX_PDF_EXTRACTED_CHARS,
    MAX_PDF_EXTRACTION_CPU_SECONDS,
    MAX_PDF_EXTRACTION_PAGES,
)


class RefillRank(NamedTuple):
    """Where a resource stands among the resources that refill a document.

    A document that awaits its PDF (see ``document_awaits_pdf``) is
    refilled by a download of its own resource (``Document.resource_id``)
    or of a resource linked to it by content
    (``ResearchResource.document_id``), and one download refills it. Unless
    one of them is already being downloaded or queued, the queueing paths
    give that download to the resource whose rank sorts first (see
    ``DownloadService._refilled_by_another_resource``). The fields sort in
    order, False before True.
    """

    #: Its URL cannot be queued: it is not downloadable, or the egress
    #: policy refuses it.
    cannot_queue: bool
    #: The retry manager holds it back: a cooldown, a permanent failure or
    #: a retry limit.
    held_back: bool
    #: Its latest download attempt failed.
    failed_last: bool
    #: It is not the document's own resource.
    linked: bool
    resource_id: int

    @property
    def viable(self) -> bool:
        """Whether nothing known stands in the way of its download.

        Its URL can be queued, the retry manager does not hold it back,
        and its latest download attempt did not fail.
        """
        return not (self.cannot_queue or self.held_back or self.failed_last)


def _text_extraction_quality(method: str | None, source: str | None) -> str:
    if method in {"native_api", "html_extraction"}:
        return "high"
    if method == "pdfplumber" or (
        method == "pdf_extraction" and source == "pdfplumber"
    ):
        return "medium"
    return "low"


def _text_extraction_rank(quality: str, method: str, source: str) -> int:
    # The arXiv API fallback is explicitly metadata/abstract only. Its historic
    # "high" quality label describes the API, not full-paper coverage.
    if method == "native_api" and source == "arxiv_api":
        return -1
    return {"low": 0, "medium": 1, "high": 2}[quality]


def _arxiv_text_provenance(source: ArxivTextSource) -> tuple[str, str]:
    match source:
        case ArxivTextSource.ARXIV_HTML:
            return (
                ExtractionMethodExtra.HTML_EXTRACTION.value,
                ExtractionSourceExtra.ARXIV_HTML.value,
            )
        case ArxivTextSource.LOCAL_PDF:
            return "pdf_extraction", "local_pdf"
        case ArxivTextSource.ARXIV_API:
            return "native_api", "arxiv_api"
        case unreachable:
            assert_never(unreachable)


def _link_retry_to_canonical(
    session: Session,
    resource: ResearchResource,
    failed: Document,
    canonical: Document,
) -> None:
    """Reuse content without cascading away the failed record's history.

    A FAILED record can still have annotations, stored content or other
    resource references. Only redirect the resource whose retry established
    the content match; retain the original record and its dependents.
    Collection membership is copied, never its stale indexing state.
    """
    resource.document_id = canonical.id
    collection_ids = {link.collection_id for link in failed.collections}
    library = session.query(Collection).filter_by(name="Library").first()
    if library:
        collection_ids.add(library.id)
    for collection_id in collection_ids:
        ensure_in_collection(session, canonical.id, collection_id)


def _has_reusable_text(document: Document) -> bool:
    """Reuse successful text, including recovered failed-attempt provenance.

    An upload with no extraction method still needs its stored PDF extracted.
    """
    return bool(
        document.text_content
        and (
            (
                document.extraction_method
                and document.extraction_method != "failed"
            )
            or (
                document.extraction_method == "failed"
                and document.status == DocumentStatus.COMPLETED
                and document.error_message is None
            )
        )
    )


# Single source of truth for caller-safe skip-reason tokens lives in
# ``security.client_safe_errors`` (PR #6564 review: the services and HTML
# downloader copies drifted). Aliases below preserve the historic import
# locations for back-compat; new code should import from ``security``.
_CLIENT_SAFE_DOWNLOAD_MESSAGES = CLIENT_SAFE_DOWNLOAD_MESSAGES


def _client_safe_download_message(exc: BaseException) -> str:
    """Back-compat alias for :func:`client_safe_download_message`."""
    return client_safe_download_message(exc)


class PDFStorageLimitError(ValueError):
    """The configured PDF storage size limit refuses a PDF.

    It names the fixed reason ``pdf_storage_size_limit`` (see
    ``CLIENT_SAFE_DOWNLOAD_MESSAGES``) that ``DownloadService.download_resource``
    returns, without downloading anything, for a document whose PDF the
    current limit would refuse again (see
    ``DownloadService._pdf_refused_at_current_limit``).
    """


#: The reason ``download_resource`` returns for a PDF the limit refuses.
PDF_STORAGE_SIZE_LIMIT_REASON = client_safe_download_message(
    PDFStorageLimitError("PDF exceeds the configured storage size limit")
)


def _pdf_storage_refused(file_path: str | None, storage_mode: str) -> bool:
    """Whether ``save_pdf`` refused a PDF that its effective mode stores.

    ``storage_mode`` is the manager's effective mode
    (``PDFStorageManager.storage_mode``, which coerces any spelling it does
    not know to "none"). In "database" and "filesystem" mode ``save_pdf``
    returns no path only for a PDF over the size limit; in "none" mode it
    never stores one, and that is no refusal.
    """
    return file_path is None and storage_mode in ("filesystem", "database")


def _keep_refused_pdf_text_only(
    document: Document,
    session: Session,
    pdf_storage_manager: Any,
    pdf_size: int,
) -> bool:
    """Keep a research download whose PDF the size limit refused, text-only.

    The document stays COMPLETED with the text its download extracts, and
    is labelled the way a text-only document is (``file_type`` "pdf",
    ``storage_mode`` "none", ``file_path`` ``FILE_PATH_TEXT_ONLY``), which
    is also the shape of a document that awaits its PDF (see
    ``document_awaits_pdf``). So the Download Manager offers its PDF for
    download, and ``file_size`` records how large the refused PDF was:
    ``DownloadService._pdf_refused_at_current_limit`` answers a download of
    it without fetching it again until the operator raises the limit above
    that size.

    Only a research download (with a ``resource_id``) that stores no PDF is
    relabelled. A document that already stores a PDF keeps it, a text
    extraction keeps its label, and an upload or a Zotero import (no
    ``resource_id``) has nothing that would download its PDF. Returns
    whether the document was relabelled.
    """
    if document.resource_id is None or document.file_type == "text":
        return False
    if pdf_storage_manager.has_pdf(document, session):
        return False
    document.file_type = "pdf"
    document.mime_type = "application/pdf"
    document.storage_mode = "none"
    document.file_path = FILE_PATH_TEXT_ONLY
    document.file_size = pdf_size
    return True


_PDF_PERSIST_EXECUTOR: Optional[ThreadPoolExecutor] = None
_EXECUTOR_LOCK = threading.Lock()
# Bound the background persistence queue: full PDF payloads park in RAM
# until two workers drain, so cap pending work and bytes. When full the
# fetch path skips background persistence and the completion sweep handles
# it synchronously — never silently retaining unbounded PDFs.
_PDF_PERSIST_SEMAPHORE = threading.Semaphore(4)
_PDF_PERSIST_BYTES = 0
_PDF_PERSIST_BYTES_LOCK = threading.Lock()
_PDF_PERSIST_MAX_PENDING_BYTES = 20 * 1024 * 1024


def _is_pdf_bytes(payload: Optional[bytes]) -> bool:
    """Return True when ``payload`` looks like a PDF.

    Direct ``%PDF`` magic prefix (allowing leading whitespace, NULs, or UTF-8 BOM
    within the first 1024 bytes) is accepted. The PDF spec allows leading bytes
    within the first 1024 bytes for streams with wrappers or leading headers.
    Structural text documents (HTML/XML/JSON) that happen to mention ``%PDF``
    are excluded.
    """
    if not payload or len(payload) < 4:
        return False
    if payload[:4] == b"%PDF":
        return True
    try:
        head = payload[:1024]
        idx = head.find(b"%PDF")
        if idx == -1:
            return False
        # Fast path: HTML, XML, or JSON text payloads are never PDFs
        head_stripped = head.lstrip(b"\xef\xbb\xbf \t\r\n\x00")
        if head_stripped.startswith(
            (
                b"<!DOCTYPE",
                b"<!doctype",
                b"<html",
                b"<HTML",
                b"<?xml",
                b'{"',
                b"[{",
                b"<head",
                b"<body",
            )
        ):
            return False
        return True
    except Exception:  # allow: silent-exception
        return False


def _get_pdf_persistence_executor() -> ThreadPoolExecutor:
    global _PDF_PERSIST_EXECUTOR
    if _PDF_PERSIST_EXECUTOR is None:
        with _EXECUTOR_LOCK:
            if _PDF_PERSIST_EXECUTOR is None:
                _PDF_PERSIST_EXECUTOR = ThreadPoolExecutor(
                    max_workers=2,
                    thread_name_prefix="pdf_persistence_worker",
                )
    return _PDF_PERSIST_EXECUTOR


def _shutdown_pdf_persistence_executor() -> None:
    global _PDF_PERSIST_EXECUTOR
    executor = _PDF_PERSIST_EXECUTOR
    _PDF_PERSIST_EXECUTOR = None
    if executor is not None:
        try:
            executor.shutdown(wait=False, cancel_futures=True)
        except Exception:  # allow: silent-exception
            pass


atexit.register(_shutdown_pdf_persistence_executor)


def _resolve_persist_identity(
    username: Optional[str] = None,
    password: Optional[str] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
) -> tuple[Optional[str], Optional[str], Optional[Dict[str, Any]]]:
    """Resolve on-the-spot persistence identity via the central helper."""
    try:
        from ...utilities.thread_context import resolve_fetch_identity

        return resolve_fetch_identity(
            egress_context=None,
            settings_snapshot=settings_snapshot,
            username=username,
            password=password,
        )
    except Exception:
        return username, password, settings_snapshot


def _is_auto_persist_enabled(
    settings_snapshot: Optional[Dict[str, Any]] = None,
    username: Optional[str] = None,
) -> bool:
    """Return whether research source auto-persistence is enabled.

    Default-off. Strictly requires boolean True. String values (e.g.
    ``"false"``, ``"off"``), numbers, and any other non-bool
    types are treated as disabled/unset so a default-off gate never fails open.
    Settings snapshots with wrapped envelopes (``{"value": True}``) are
    unwrapped before evaluating the boolean gate.
    """
    if isinstance(settings_snapshot, dict) and (
        "research_library.auto_persist_research_sources" in settings_snapshot
    ):
        val = unwrap_setting(
            settings_snapshot.get(
                "research_library.auto_persist_research_sources"
            )
        )
        return val is True

    if username:
        try:
            settings = get_settings_manager(username=username)
            val = unwrap_setting(
                settings.get_setting(
                    "research_library.auto_persist_research_sources", False
                )
            )
            return val is True
        except Exception:  # allow: silent-exception
            pass
    return False


def _resolve_pdf_storage_mode(
    settings_snapshot: Optional[Dict[str, Any]],
    username: Optional[str] = None,
) -> str:
    """Resolve ``pdf_storage_mode`` preferring an explicit snapshot key.

    A snapshot that lacks the key (e.g. ``{"_username": ...}`` built for
    snapshot-less runs) must fall back to live settings instead of
    defaulting to ``"database"`` and writing blobs against a live
    ``"none"`` preference.
    Also honors ``document_scheduler.download_pdfs``: when explicitly
    disabled (default False), PDF bytes are not stored and the mode is
    treated as ``"none"`` (text only).
    """
    scheduler_allows_pdfs = None
    if isinstance(settings_snapshot, dict) and (
        "document_scheduler.download_pdfs" in settings_snapshot
    ):
        raw_sched = unwrap_setting(
            settings_snapshot.get("document_scheduler.download_pdfs")
        )
        scheduler_allows_pdfs = (
            to_bool(raw_sched) if raw_sched is not None else False
        )
    elif username:
        try:
            settings = get_settings_manager(username=username)
            raw_sched = unwrap_setting(
                settings.get_setting("document_scheduler.download_pdfs", False)
            )
            scheduler_allows_pdfs = (
                to_bool(raw_sched) if raw_sched is not None else False
            )
        except Exception:  # allow: silent-exception
            pass

    if scheduler_allows_pdfs is False:
        return "none"

    if isinstance(settings_snapshot, dict) and (
        "research_library.pdf_storage_mode" in settings_snapshot
    ):
        return resolve_pdf_storage_mode(
            unwrap_setting(
                settings_snapshot.get("research_library.pdf_storage_mode")
            )
        )
    try:
        settings = get_settings_manager(username=username)
        return resolve_pdf_storage_mode(
            unwrap_setting(
                settings.get_setting(
                    "research_library.pdf_storage_mode", "database"
                )
            )
        )
    except Exception:
        if isinstance(settings_snapshot, dict):
            return resolve_pdf_storage_mode(
                unwrap_setting(
                    settings_snapshot.get(
                        "research_library.pdf_storage_mode", "database"
                    )
                )
            )
        return resolve_pdf_storage_mode("database")


def _save_pdf_document_record(
    session: Session,
    pdf_storage_manager: Any,
    pdf_content: bytes,
    file_hash: str,
    url: str,
    text: Optional[str] = None,
    title: Optional[str] = None,
    username: Optional[str] = None,
    password: Optional[str] = None,
    collection_id: Optional[str] = None,
    resource_id: Optional[int] = None,
    research_id: Optional[str] = None,
    extra_urls: Optional[List[str]] = None,
    pdf_storage_mode: str = "database",
    require_text: bool = False,
) -> Tuple[Optional[Document], Optional[str]]:
    """Save or update Document, PDF storage, trackers, and collection membership.

    Unified helper shared between DownloadService._download_pdf and
    persist_downloaded_pdf_on_the_spot to prevent logic drift.

    Versioning: content hash wins. Identical bytes reuse the existing
    document; new bytes at a known URL mint a new document (matching main)
    so Document and DownloadTracker hashes never diverge.
    When ``require_text`` is True (fetch-time on-the-spot), garbage
    ``%PDF``-magic payloads without extractable text are rejected
    (return ``(None, None)``) instead of persisting empty COMPLETED rows.
    Library downloads pass False to preserve placeholder/retry behavior.
    """
    # Extraction is fetch-time only: library downloads (require_text=False)
    # leave text production to their own canonical step, as on main.
    if (
        require_text
        and (not text or not text.strip())
        and _is_pdf_bytes(pdf_content)
    ):
        from ..downloaders.base import BaseDownloader

        try:
            extracted = BaseDownloader.extract_text_from_pdf(pdf_content)
            if extracted and extracted.strip():
                text = extracted
        except Exception:  # allow: silent-exception
            pass

    if not text or not text.strip():
        if require_text:
            logger.debug(
                f"pdf_persist skipped no text for {redact_url_for_log(url)}"
            )
            return None, None
        # Keep None (not "") so the RAG sweep's IS NOT NULL check skips
        # documents that have no text yet.
        text = None

    # Resolve the default collection early so callers that fail later on
    # mocked storage still exercise the default-library path (matches main's
    # order where collection defaulting precedes the blob write).
    if not collection_id:
        try:
            collection_id = get_default_library_id(username, password)
        except Exception:  # allow: silent-exception
            pass

    is_pdf_storage_enabled = pdf_storage_mode in ("database", "filesystem")
    char_count = len(text) if text else None
    w_count = len(text.split()) if text else None

    # Hash-first reuse: identical bytes across mirrors share one document.
    existing_by_hash = (
        session.query(Document).filter_by(document_hash=file_hash).first()
    )
    if existing_by_hash is not None:
        target_doc = existing_by_hash
        if text and not target_doc.text_content:
            target_doc.text_content = text
            target_doc.character_count = char_count
            target_doc.word_count = w_count
        if title and target_doc.title in (None, "", "Untitled"):
            target_doc.title = title
        if target_doc.status != DocumentStatus.COMPLETED:
            target_doc.status = DocumentStatus.COMPLETED
            target_doc.error_message = None
        if resource_id is not None and not target_doc.resource_id:
            target_doc.resource_id = resource_id
        if research_id is not None and not target_doc.research_id:
            target_doc.research_id = research_id

        file_path_result: Optional[str] = target_doc.file_path
        has_retrievable_pdf = False
        try:
            if target_doc.file_type == "pdf":
                from sqlalchemy import func
                from ...database.models.library import DocumentBlob

                blob_len = (
                    session.query(func.length(DocumentBlob.pdf_binary))
                    .filter_by(document_id=target_doc.id)
                    .scalar()
                )
                if blob_len is not None and blob_len > 0:
                    has_retrievable_pdf = True
                elif target_doc.file_path:
                    safe_path = None
                    if hasattr(pdf_storage_manager, "_get_safe_file_path"):
                        safe_path = pdf_storage_manager._get_safe_file_path(
                            target_doc.file_path
                        )
                    elif (
                        target_doc.file_path != "database"
                        and target_doc.file_path not in FILE_PATH_SENTINELS
                    ):
                        safe_path = Path(target_doc.file_path)
                    if safe_path and safe_path.is_file():
                        try:
                            if safe_path.stat().st_size > 0:
                                has_retrievable_pdf = True
                        except OSError:
                            pass
        except Exception:  # allow: silent-exception
            has_retrievable_pdf = False
        needs_storage = (
            target_doc.storage_mode in (None, "none") or not has_retrievable_pdf
        ) and is_pdf_storage_enabled

        if needs_storage:
            saved_path, _ = pdf_storage_manager.save_pdf(
                pdf_content=pdf_content,
                document=target_doc,
                session=session,
                filename=f"{resource_id}.pdf"
                if resource_id is not None
                else f"{target_doc.id}.pdf",
                url=url,
                resource_id=resource_id,
            )
            if saved_path:
                file_path_result = saved_path
                target_doc.file_type = "pdf"
                target_doc.mime_type = "application/pdf"
            else:
                file_path_result = None
                target_doc.file_type = "pdf"
                target_doc.mime_type = "application/pdf"
                target_doc.storage_mode = "none"
                if require_text:
                    target_doc.file_path = FILE_PATH_TEXT_ONLY
                else:
                    target_doc.file_path = None
        elif not is_pdf_storage_enabled:
            # None-mode PDF downloads keep the pdf label (awaiting refill
            # shape) as main did; do not flip to text. A document that
            # already stores its PDF (blob or file) keeps its storage
            # metadata: this fetch declined to store new bytes, it did
            # not remove the stored ones, and relabelling would make
            # document_awaits_pdf report a present PDF as missing.
            target_doc.file_type = "pdf"
            target_doc.mime_type = "application/pdf"
            if not has_retrievable_pdf:
                target_doc.storage_mode = "none"
                if require_text:
                    target_doc.file_path = FILE_PATH_TEXT_ONLY
                elif not target_doc.file_path or target_doc.file_path in (
                    "database",
                    FILE_PATH_TEXT_ONLY,
                ):
                    target_doc.file_path = None
            file_path_result = None
    else:
        try:
            # Reuse the caller's session: it may hold a staged tracker update
            # and attempt that a nested scope's rollback would discard.
            source_type_id = get_source_type_id(
                username, "research_download", password, session
            )
        except Exception:
            source_type = (
                session.query(SourceType)
                .filter_by(name="research_download")
                .first()
            )
            if source_type:
                source_type_id = source_type.id
            else:
                raise

        doc_id = str(uuid.uuid4())
        target_doc = Document(
            id=doc_id,
            source_type_id=source_type_id,
            resource_id=resource_id,
            research_id=research_id,
            document_hash=file_hash,
            original_url=url,
            # Fetch-time rows keep the awaiting-refill sentinel in
            # none mode; library downloads match main's None shape.
            file_path=FILE_PATH_TEXT_ONLY
            if (require_text and not is_pdf_storage_enabled)
            else None,
            file_size=len(pdf_content),
            file_type="pdf",
            mime_type="application/pdf",
            title=title or "Untitled",
            status=DocumentStatus.COMPLETED,
            text_content=text,
            character_count=char_count,
            word_count=w_count,
            processed_at=datetime.now(UTC),
            created_at=datetime.now(UTC),
            storage_mode=pdf_storage_mode,
        )
        session.add(target_doc)
        winner = None
        if require_text:
            try:
                with session.begin_nested():
                    session.flush()
            except Exception as exc:
                # Concurrent writers racing SELECT-then-INSERT on the unique
                # hash: reuse the winner instead of dying. Nested savepoint
                # scopes the rollback so outer pending work survives.
                from sqlalchemy.exc import IntegrityError

                if isinstance(exc, IntegrityError):
                    # pysqlite invalidates the whole transaction on a
                    # failed statement (SAVEPOINT cannot scope recovery),
                    # so a full rollback is mandatory before any further
                    # use of this session. Tracker and collection writes
                    # below are re-executed afterwards, so nothing is
                    # lost except already-staged caller state that the
                    # caller re-applies from the returned winner.
                    try:
                        session.rollback()
                    except Exception:  # allow: silent-exception
                        pass
                    winner = (
                        session.query(Document)
                        .filter_by(document_hash=file_hash)
                        .first()
                    )
                    if winner is not None:
                        target_doc = winner
                        if text and not target_doc.text_content:
                            target_doc.text_content = text
                            target_doc.character_count = char_count
                            target_doc.word_count = w_count
                    else:
                        raise
                else:
                    raise
        else:
            session.flush()

        file_path_result = None
        if winner is None:
            saved_path, _ = pdf_storage_manager.save_pdf(
                pdf_content=pdf_content,
                document=target_doc,
                session=session,
                filename=f"{resource_id}.pdf"
                if resource_id is not None
                else f"{doc_id}.pdf",
                url=url,
                resource_id=resource_id,
            )
            if saved_path:
                file_path_result = saved_path
                target_doc.file_type = "pdf"
                target_doc.mime_type = "application/pdf"
            else:
                file_path_result = None
                target_doc.file_type = "pdf"
                target_doc.mime_type = "application/pdf"
                target_doc.storage_mode = "none"
                if require_text:
                    target_doc.file_path = FILE_PATH_TEXT_ONLY
                else:
                    target_doc.file_path = None
        else:
            file_path_result = target_doc.file_path

    if resource_id is None:
        candidate_urls = [url]
        if extra_urls:
            for eu in extra_urls:
                if eu and eu not in candidate_urls:
                    candidate_urls.append(eu)
        res_match = (
            session.query(ResearchResource)
            .filter(ResearchResource.url.in_(candidate_urls))
            .first()
        )
        if res_match is not None:
            resource_id = res_match.id
            if not research_id:
                research_id = res_match.research_id
        if resource_id is not None and not target_doc.resource_id:
            target_doc.resource_id = resource_id
        if research_id is not None and not target_doc.research_id:
            target_doc.research_id = research_id
    all_urls = [url]
    if extra_urls:
        for eu in extra_urls:
            if eu and eu not in all_urls:
                all_urls.append(eu)

    for u in all_urls:
        tracker = find_tracker_by_url(session, u)
        recorded_size = len(pdf_content)
        recorded_path = (
            file_path_result
            if (is_pdf_storage_enabled and file_path_result)
            else None
        )
        recorded_name = (
            Path(file_path_result).name
            if (
                is_pdf_storage_enabled
                and file_path_result
                and file_path_result != "database"
            )
            else None
        )
        if tracker:
            tracker.file_hash = file_hash
            tracker.file_size = recorded_size
            tracker.file_path = recorded_path
            tracker.file_name = recorded_name
            tracker.is_downloaded = True
            if not tracker.downloaded_at:
                tracker.downloaded_at = datetime.now(UTC)
            if not tracker.first_resource_id and resource_id is not None:
                tracker.first_resource_id = resource_id
        else:
            # Tracker rows require a resource (first_resource_id NOT NULL),
            # so fetch-time persistence without a resource cannot create
            # them. Redirect final URLs are still passed via extra_urls and
            # recorded here whenever a resource is known; cross-URL redirect
            # reuse beyond that still re-downloads once (storage deduped by
            # hash) — documented limitation, same-URL never double-fetches.
            if resource_id is None:
                continue
            try:
                with session.begin_nested():
                    try:
                        from ...utilities.download_url import get_url_hash
                    except Exception:
                        get_url_hash = None  # type: ignore[assignment]
                    try:
                        u_hash = (
                            get_url_hash(canonical_url_key(u))
                            if get_url_hash
                            else hashlib.sha256(u.strip().encode()).hexdigest()
                        )
                    except Exception:
                        u_hash = hashlib.sha256(u.strip().encode()).hexdigest()
                    tracker = DownloadTracker(
                        url=u,
                        url_hash=u_hash,
                        first_resource_id=resource_id,
                        file_hash=file_hash,
                        file_size=recorded_size,
                        file_path=recorded_path,
                        file_name=recorded_name,
                        is_downloaded=True,
                        downloaded_at=datetime.now(UTC),
                    )
                    session.add(tracker)
            except Exception as exc:
                from sqlalchemy.exc import IntegrityError

                if not isinstance(exc, IntegrityError):
                    raise
                # begin_nested already rolled back to the savepoint; do not
                # full-rollback the outer transaction (would wipe the doc).

    try:
        target_collection = collection_id
        if not target_collection:
            default_coll = (
                session.query(Collection).filter_by(is_default=True).first()
            )
            if default_coll:
                target_collection = default_coll.id
            else:
                target_collection = get_default_library_id(username, password)
        if target_collection:
            ensure_in_collection(session, target_doc.id, target_collection)
    except Exception:
        logger.debug("pdf_persist collection link failed", exc_info=True)

    return target_doc, file_path_result


class DownloadService:
    """Service for downloading and managing research PDFs."""

    def __init__(
        self,
        username: str,
        password: Optional[str] = None,
        settings_snapshot: Optional[Dict[str, Any]] = None,
    ):
        """Initialize download service for a user.

        Args:
            username: The username to download for
            password: Optional password for encrypted database access
            settings_snapshot: Optional snapshot used to build the
                EgressContext for per-URL scope gating. When None,
                downloads run without scope enforcement (back-compat
                for the existing caller; new callers must pass it).
        """
        self.username = username
        self.password = password
        self.settings = get_settings_manager(username=username)
        self._closed = False
        # When no snapshot is passed (e.g. the library download routes, which
        # construct DownloadService(username, password) directly), build one
        # from this user's settings so egress policy is still enforced.
        # Without this, _egress_context stays None and _check_url_against_policy
        # returns (True, "no_context") — every download ungated. Best-effort:
        # on failure, fall through to the pre-policy behavior.
        # A real backend error (get_settings_snapshot raised) is different
        # from a test double returning a non-dict: under a private scope we
        # must NOT silently fall through to ungated downloads when we simply
        # failed to READ the user's settings. Track that case and fail closed.
        self._snapshot_build_failed = False
        if settings_snapshot is None:
            try:
                built = self.settings.get_settings_snapshot()
                # Only adopt a real dict — a test double or unavailable
                # backend hands back something else; treat that as "no
                # snapshot" (ungated back-compat) rather than letting the
                # policy evaluation fail closed on a non-dict.
                if isinstance(built, dict):
                    settings_snapshot = built
            except Exception:
                # The user HAS a settings backend (self.settings exists) but
                # reading it raised — a private scope could be configured and
                # we just couldn't see it. Fail closed rather than ungate.
                logger.bind(policy_audit=True).warning(
                    "DownloadService: settings snapshot build raised; "
                    "locking downloads closed (fail-safe)"
                )
                self._snapshot_build_failed = True
        self._settings_snapshot = settings_snapshot
        self._egress_context = self._build_egress_context(settings_snapshot)

        # Debug settings manager and user context
        logger.info(
            f"[DOWNLOAD_SERVICE] Settings manager initialized: {type(self.settings)}, username: {self.username}"
        )

        # Get library path from settings (uses centralized path, respects LDR_DATA_DIR)
        storage_path_setting = self.settings.get_setting(
            "research_library.storage_path",
            str(get_library_directory()),
        )
        logger.debug(
            f"[DOWNLOAD_SERVICE_INIT] Storage path setting retrieved: {storage_path_setting} (type: {type(storage_path_setting)})"
        )

        if storage_path_setting is None:
            logger.error(
                "[DOWNLOAD_SERVICE_INIT] CRITICAL: storage_path_setting is None!"
            )
            raise ValueError("Storage path setting cannot be None")

        # Per-user library root (issue #5521). PDFs are written to a per-user
        # subdirectory (<base>/<username>) instead of the shared base, so two
        # users' per-user autoincrement resource ids can't collide on the same
        # `pdfs/<resource_id>.pdf` path in one shared directory. This mirrors
        # get_library_storage_path()'s layout but derives the base from this
        # user's own settings manager (self.settings) so it stays correct in
        # background/scheduler threads that have no request context.
        base_library_root = (
            Path(os.path.expandvars(storage_path_setting))
            .expanduser()
            .resolve()
        )
        shared_library = self.settings.get_setting(
            "research_library.shared_library", False
        )
        self.library_root = str(
            apply_user_subdir(base_library_root, self.username, shared_library)
        )
        logger.debug(
            f"[DOWNLOAD_SERVICE_INIT] Library root resolved to: {self.library_root}"
        )

        # Create directory structure
        self._setup_directories()

        # Initialize modular downloaders

        # Get Semantic Scholar API key from settings
        semantic_scholar_api_key = self.settings.get_setting(
            "search.engine.web.semantic_scholar.api_key", ""
        )

        # Same for OpenAlex: the downloader hits api.openalex.org for every
        # work lookup, so it needs the key too. It is passed through raw —
        # ``OpenAlexDownloader.__init__`` runs ``normalize_openalex_api_key``
        # itself, so placeholders/sentinels/non-strings become None
        # (keyless free tier) there, in one place.
        openalex_api_key = self.settings.get_setting(
            "search.engine.web.openalex.api_key", ""
        )

        # ArxivDownloader precedes DirectPDFDownloader: for a paper URL it
        # builds the canonical PDF URL rather than fetching whatever the
        # input pointed at. It can only do that for URLs that name a paper,
        # and can_handle now says so, so arXiv's legacy /ftp/ PDF layout --
        # which yields no identifier -- still reaches DirectPDFDownloader
        # instead of being claimed and skipped.
        self.downloaders = [
            ArxivDownloader(
                timeout=30,
                username=username,
                password=password,
                settings_snapshot=self._settings_snapshot,
                enable_fetch_persistence=False,
            ),
            DirectPDFDownloader(
                timeout=30,
                username=username,
                password=password,
                settings_snapshot=self._settings_snapshot,
                enable_fetch_persistence=False,
            ),
            SemanticScholarDownloader(
                timeout=30,
                api_key=semantic_scholar_api_key
                if semantic_scholar_api_key
                else None,
                username=username,
                password=password,
                settings_snapshot=self._settings_snapshot,
                enable_fetch_persistence=False,
            ),
            # OpenAlex work lookup; the key is optional (it raises the
            # daily budget 10x) and None means the keyless free tier.
            OpenAlexDownloader(
                timeout=30,
                api_key=openalex_api_key,
                username=username,
                password=password,
                settings_snapshot=self._settings_snapshot,
                enable_fetch_persistence=False,
            ),
            PubMedDownloader(
                timeout=30,
                rate_limit_delay=1.0,
                username=username,
                password=password,
                settings_snapshot=self._settings_snapshot,
                enable_fetch_persistence=False,
            ),
            BioRxivDownloader(
                timeout=30,
                username=username,
                password=password,
                settings_snapshot=self._settings_snapshot,
                enable_fetch_persistence=False,
            ),
            OpenAccessDOIDownloader(
                timeout=30,
                username=username,
                password=password,
                settings_snapshot=self._settings_snapshot,
                enable_fetch_persistence=False,
            ),
            GenericDownloader(
                timeout=30,
                username=username,
                password=password,
                settings_snapshot=self._settings_snapshot,
                enable_fetch_persistence=False,
            ),  # Generic should be last (fallback)
        ]

        # Initialize retry manager for smart failure tracking
        self.retry_manager = RetryManager(username, password)
        logger.info(
            f"[DOWNLOAD_SERVICE] Initialized retry manager for user: {username}"
        )

        # PubMed rate limiting state
        self._pubmed_delay = 1.0  # 1 second delay for PubMed
        self._last_pubmed_request = 0.0  # Track last request time

    def close(self):
        """Close all downloader resources."""
        if self._closed:
            return
        self._closed = True

        from ...utilities.resource_utils import safe_close

        for downloader in self.downloaders:
            safe_close(downloader, "downloader")

        # Close the settings manager's DB session to return the connection
        # to the pool.  SettingsManager.close() is idempotent.
        safe_close(self.settings, "settings manager", allow_none=True)

        # Clear references to allow garbage collection
        self.downloaders = []
        self.retry_manager = None
        self.settings = None

    def __enter__(self):
        """Enter context manager."""
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Exit context manager, ensuring cleanup."""
        self.close()
        return False

    def _setup_directories(self):
        """Create library directory structure."""
        from ...security.directory_creation import create_directory

        # Only create the root and pdfs folder - flat structure.
        paths = [
            self.library_root,
            str(Path(self.library_root) / _TRUSTED_STORAGE_LEAF),
        ]
        for path in paths:
            create_directory(
                path,
                context="download service library directory",
            )

    def _normalize_url(self, url: str) -> str:
        """Normalize URL for consistent hashing.

        Legacy spelling (pre-B2 #6758): retained for back-compat callers
        and unit tests, but NOT used by :meth:`_get_url_hash` anymore —
        that path goes through the shared ``normalize_url_for_hash``
        (lowercase-before-sort) so writer and readers agree (issue #6760
        follow-up).
        """
        # Remove protocol variations
        url = re.sub(r"^https?://", "", url)
        # Remove www
        url = re.sub(r"^www\.", "", url)
        # Remove trailing slashes
        url = url.rstrip("/")
        # Sort query parameters
        if "?" in url:
            base, query = url.split("?", 1)
            params = sorted(query.split("&"))
            url = f"{base}?{'&'.join(params)}"
        return url.lower()

    def _get_url_hash(self, url: str) -> str:
        """Generate SHA256 hash of normalized URL.

        B2 (issue #6758): canonicalize the raw URL first so tracking
        params, fragments, default ports, and userinfo hash alike — the
        legacy ``_normalize_url`` strip alone strips the scheme before the
        canonical form can be parsed, which would silently keep the gap.

        The writer must hash exactly what the readers hash: ``get_url_hash``
        already applies :func:`normalize_url_for_hash` (canonicalize +
        strip + lowercase-before-sort), so composing ``_normalize_url`` here
        double-normalizes — ``sort(raw-case).lower()`` followed by a second
        ``sort(lower)`` — and diverges from the single-normalized reader on
        mixed-case query URLs (issue #6760 follow-up: writer hash in none
        of the reader candidates → sync deletes the document, re-download
        then hits ``uq_download_tracker_url_hash``). Pass the canonical
        form straight into ``get_url_hash``.
        """
        return get_url_hash(canonical_url_key(url))

    def _build_egress_context(self, settings_snapshot):
        """Build an EgressContext from the supplied snapshot. Returns
        None when no snapshot is available — callers fall through to
        the pre-policy behavior (back-compat with non-scheduler callers).

        Sets ``self._policy_locked = True`` when a snapshot WAS supplied
        but the policy itself cannot be evaluated (corrupt scope or
        missing primary). The check_url method honors
        this flag and fails closed — the previous code returned None on
        PolicyDeniedError and check_url then returned ``(True,
        "no_context")``, which silently allowed every download under a
        misconfigured policy.
        """
        self._policy_locked = False
        # Use an explicit `is None` check, not a truthiness test: an empty
        # dict {} is a real (if minimal) snapshot, and `if not {}` would
        # silently skip policy gating for it — fail-open. Match the
        # `is None` guard used by the LLM/embeddings gates.
        if settings_snapshot is None:
            return None
        try:
            from ...security.egress.policy import (
                PolicyDeniedError,
                context_from_snapshot,
                resolve_run_primary_engine,
            )
        except ImportError:
            logger.debug(
                "egress_policy unavailable in DownloadService; "
                "downloads will not be scope-gated"
            )
            return None

        try:
            return context_from_snapshot(
                settings_snapshot,
                resolve_run_primary_engine(
                    settings_snapshot, default=DEFAULT_SEARCH_TOOL
                ),
                username=self.username,
            )
        except (PolicyDeniedError, ValueError) as exc:
            logger.bind(policy_audit=True).warning(
                "DownloadService policy unavailable; locking out "
                "every URL check",
                reason=str(exc),
            )
            self._policy_locked = True
            return None

    def _check_url_against_policy(
        self, url: str, *, record_denial: bool = True
    ) -> Tuple[bool, str]:
        """Run evaluate_url against self._egress_context. Returns
        (allowed, reason). When no context is configured (legacy
        callers OR tests that mock __init__), returns (True, "no_context")
        so back-compat callers aren't broken — but new callers passing
        a snapshot DO get gated. Logged to policy_audit on denial.

        ``record_denial=False`` returns the same decision without recording
        a denial: it is not logged, here or by ``evaluate_url``, and does
        not count toward the run's denied-fetch quota. Only ``refill_rank``
        passes it, since it fetches nothing on the decision. Every other
        caller checks a URL before it is queued or fetched, and records its
        denials.
        """
        # ``_policy_locked`` is set by _build_egress_context when a
        # snapshot was supplied but the policy itself raised. Without
        # this short-circuit, _egress_context is None and the legacy
        # fall-through below would silently allow every URL.
        if getattr(self, "_policy_locked", False):
            return False, "policy_unavailable"

        # Reading the user's settings raised during __init__ — we could not
        # determine the scope, so fail closed instead of ungating downloads.
        if getattr(self, "_snapshot_build_failed", False):
            return False, "settings_unavailable"

        # Use getattr so tests that mock DownloadService.__init__ (and
        # therefore never set _egress_context) still work — they
        # operated under the pre-policy contract.
        egress_context = getattr(self, "_egress_context", None)
        if egress_context is None:
            return True, "no_context"
        from ...security.egress.policy import evaluate_url
        from ...security.ssrf_validator import redact_url_for_log

        decision = evaluate_url(
            url, egress_context, record_denial=record_denial
        )
        if not decision.allowed and record_denial:
            logger.bind(policy_audit=True).warning(
                "DownloadService URL denied by egress policy",
                # Redact userinfo creds / query-string tokens from the log.
                url=redact_url_for_log(url),
                scope=egress_context.scope.value,
                reason=decision.reason,
            )
        return decision.allowed, decision.reason

    def is_already_downloaded(self, url: str) -> Tuple[bool, Optional[str]]:
        """
        Check if URL is already downloaded.

        Returns:
            Tuple of (is_downloaded, file_path)
        """
        # B2 compat (#6758): pre-upgrade rows still carry a legacy url_hash;
        # match canonical first, then legacy spellings (sequential
        # filter_by so MagicMock-forced misses keep working).
        with get_user_db_session(self.username, self.password) as session:
            candidates = get_url_hash_candidates(url)
            tracker = None
            for candidate in candidates:
                tracker = (
                    session.query(DownloadTracker)
                    .filter_by(url_hash=candidate, is_downloaded=True)
                    .first()
                )
                if tracker is not None:
                    break

            if tracker and tracker.file_path:
                # Compute absolute path and verify file still exists. Pass the
                # username so per-user files resolve, with legacy-shared
                # fallback for pre-isolation downloads (issue #5521). Pass
                # self.settings so storage_path/shared_library are read from
                # this user's captured manager — the ambient one is db-less in
                # background/scheduler threads and would resolve the default
                # root, missing a user's previously saved storage_path. Use
                # getattr: __init__ always sets self.settings in production
                # (incl. scheduler threads), but test doubles that construct
                # a DownloadService via __new__/partial mocks may not — fall
                # back to None (the ambient get_settings_manager()) rather
                # than raising AttributeError.
                absolute_path = get_absolute_path_from_settings(
                    tracker.file_path,
                    self.username,
                    settings_manager=getattr(self, "settings", None),
                )
                if absolute_path and absolute_path.is_file():
                    return True, str(absolute_path)
                if absolute_path:
                    # File was deleted, mark as not downloaded
                    tracker.is_downloaded = False
                    session.commit()
                # If absolute_path is None, path was blocked - treat as not downloaded

            return False, None

    def get_text_content(self, resource_id: int) -> Optional[str]:
        """
        Get text content for a research resource.

        This will try to:
        1. Produce text through a source-specific downloader if available
        2. Extract text from downloaded PDF if exists
        3. Download PDF and extract text if not yet downloaded

        Args:
            resource_id: ID of the research resource

        Returns:
            Text content as string, or None if extraction failed
        """
        with get_user_db_session(self.username, self.password) as session:
            resource = session.get(ResearchResource, resource_id)
            if not resource:
                logger.error(f"Resource {resource_id} not found")
                return None

            url = resource.url

            # Egress policy gate — denied URLs short-circuit before any
            # downloader fires a request.
            allowed, reason = self._check_url_against_policy(url)
            if not allowed:
                logger.warning(
                    f"Skipping text extraction for {redact_url_for_log(url)}: refused by "
                    f"egress policy ({reason})"
                )
                return None

            # Find appropriate downloader
            for downloader in self.downloaders:
                if downloader.can_handle(url):
                    logger.info(
                        f"Using {downloader.__class__.__name__} for text extraction from {redact_url_for_log(url)}"
                    )
                    try:
                        # Try to get text content
                        text = downloader.download_text(url)
                        if text:
                            logger.info(
                                f"Successfully extracted text for: {resource.title[:50]}"
                            )
                            return text
                    except Exception as e:
                        # Type name only: downloader exception texts can
                        # embed the raw resource URL.
                        logger.warning(
                            f"Failed to extract text ({type(e).__name__}) "
                            f"for {redact_url_for_log(url)}"
                        )
                    break

            logger.warning(
                f"Could not extract text for {redact_url_for_log(url)}"
            )
            return None

    def queue_research_downloads(
        self,
        research_id: str,
        collection_id: Optional[str] = None,
        *,
        text_only: bool = False,
        run_research_ids: Optional[AbstractSet[str]] = None,
    ) -> int:
        """
        Queue all downloadable PDFs from a research session.

        Args:
            research_id: The research session ID
            collection_id: Optional target collection ID (defaults to Library if not provided)
            text_only: True when the queued resources only get their text
                extracted (``download_bulk``'s "text_only" mode). A completed
                document that awaits its PDF (see ``document_awaits_pdf``)
                already holds its text, so it then counts as done, like any
                completed document. A text extraction would not fetch its PDF
                either, so it would be queued and reported again on every run.
            run_research_ids: The research sessions whose queued downloads
                the same run downloads, as a set (``download_bulk`` builds
                one from its ``research_ids`` for the whole run and passes it
                for each research), or None when nothing downloads the queue
                right away. It is only looked up, never copied, unless it
                lacks ``research_id``. Decides which resource refills a
                document that awaits its PDF, see the notes.

        Returns:
            Number of items queued

        Notes:
            Resets existing FAILED/COMPLETED queue entries for the research back
            to PENDING, effectively retrying them. Resources that already have a
            PENDING queue entry or a COMPLETED Document are skipped, except a
            COMPLETED Document that awaits its PDF when PDFs are queued. As of #4685,
            ``download_bulk`` calls this unconditionally on every bulk run, so
            previously-failed downloads are automatically retried each time.
            A PROCESSING entry is in flight in another ``download_bulk`` stream,
            so it is left alone rather than reset: resetting it would let this
            run re-claim and re-download it (#4691).

            A document that awaits its PDF can be refilled through its own
            resource or any resource linked to it by content
            (``resource.document_id``), and one download refills it. So a
            run leaves the document to one of them (see
            ``_refilled_by_another_resource``): to one that a download holds,
            else to one already queued for the run, unless that one is not
            viable and another ranks before it, else to the one that ranks
            first in ``refill_rank``. That is the document's own resource
            unless it cannot succeed (its URL cannot be queued, the retry
            manager holds it back, or its latest attempt failed) while a
            linked one can. Once the document holds its PDF again,
            ``download_resource`` reports the document's own resource as
            already downloaded, and for a linked one whose URL the egress
            policy allows, ``_download_pdf`` reuses that stored PDF instead
            of fetching it. Of this research's resources the document's own
            one is considered first.
        """
        queued = 0
        # The research sessions whose queue the same run downloads (see
        # _refilled_by_another_resource). download_bulk passes one set for
        # the whole run, so it is only looked up here: copying it for each
        # research made the bulk pre-pass quadratic in the number of
        # research sessions.
        run = run_research_ids
        if run is not None and research_id not in run:
            run = run | {research_id}
        # The refill_rank of each resource compared below, by resource and
        # document owner, so that the egress policy and the retry manager
        # are asked once per resource in this call.
        ranks: Dict[Tuple[int, Optional[int]], RefillRank] = {}

        # Get default library collection if no collection_id provided
        if not collection_id:
            from ...database.library_init import get_default_library_id

            collection_id = get_default_library_id(self.username, self.password)

        with get_user_db_session(self.username, self.password) as session:
            # Get all resources for this research
            resources = (
                session.query(ResearchResource)
                .filter_by(research_id=research_id)
                .all()
            )

            # A document's own resource before the resources linked to it
            # by content, so that the own resource of a document awaiting
            # its PDF is considered first when this research has both. A
            # resource can also point to its own document through
            # document_id. The sort is stable and leaves the other
            # resources in order. It reads the owner of each document these
            # resources point to, in one query of bare columns: loading the
            # documents would load their text (#4560).
            owner_of_linked_document = (
                dict(
                    session.query(ResearchResource.id, Document.resource_id)
                    .join(Document, ResearchResource.document_id == Document.id)
                    .filter(ResearchResource.research_id == research_id)
                    .all()
                )
                if any(res.document_id for res in resources)
                else {}
            )

            def linked_to_another_resources_document(res) -> bool:
                return bool(res.document_id) and (
                    owner_of_linked_document.get(res.id) != res.id
                )

            for resource in sorted(
                resources, key=linked_to_another_resources_document
            ):
                if self._is_downloadable(resource):
                    # The document awaiting its PDF that this resource would
                    # refill, if any (see document_awaits_pdf).
                    awaiting = None
                    # Library resources linked via document_id are already
                    # done, unless PDFs are queued and the linked document
                    # still needs its PDF (see document_awaits_pdf).
                    if resource.document_id:
                        # Not its text, which nothing here reads (#4560).
                        linked = (
                            None
                            if text_only
                            else session.query(Document)
                            .options(defer(Document.text_content))
                            .filter(Document.id == resource.document_id)
                            .first()
                        )
                        if linked is None or not document_awaits_pdf(linked):
                            continue
                        awaiting = linked

                    # Egress policy gate before queueing.
                    allowed, reason = self._check_url_against_policy(
                        resource.url
                    )
                    if not allowed:
                        logger.warning(
                            f"Skipping queue for {redact_url_for_log(resource.url)}: refused by "
                            f"egress policy ({reason})"
                        )
                        continue

                    # Check if already queued
                    existing_queue = (
                        session.query(LibraryDownloadQueue)
                        .filter_by(
                            resource_id=resource.id,
                            status=DocumentStatus.PENDING,
                        )
                        .first()
                    )

                    # Check if already downloaded (trust the database status).
                    # When PDFs are queued, a completed document that still
                    # needs its PDF does not count (see document_awaits_pdf).
                    # An explicit link is authoritative; an older retained
                    # document can still name this resource as its owner.
                    existing_doc = (
                        awaiting
                        if resource.document_id
                        else session.query(Document)
                        .filter_by(
                            resource_id=resource.id,
                            status=DocumentStatus.COMPLETED,
                        )
                        .first()
                    )
                    if (
                        not text_only
                        and existing_doc is not None
                        and document_awaits_pdf(existing_doc)
                    ):
                        awaiting = existing_doc
                        existing_doc = None

                    # Queue if not already queued and not marked as completed
                    if not existing_queue and not existing_doc:
                        # One download refills a document awaiting its PDF:
                        # leave it to the resource that does.
                        if (
                            awaiting is not None
                            and self._refilled_by_another_resource(
                                session,
                                awaiting,
                                resource,
                                run,
                                research_id,
                                ranks,
                            )
                        ):
                            continue
                        # Check one more time if ANY queue entry exists (regardless of status)
                        any_queue = (
                            session.query(LibraryDownloadQueue)
                            .filter_by(resource_id=resource.id)
                            .first()
                        )

                        if any_queue:
                            # Reset the existing queue entry, but never one a
                            # concurrent download_bulk stream has claimed: a
                            # PROCESSING row is in flight, and resetting it to
                            # PENDING lets this stream re-claim and re-download
                            # a resource the other stream is still downloading
                            # (issue #4691). The status guard lives inside the
                            # UPDATE, not in an `if` above it, so the check and
                            # the write cannot straddle another stream's claim.
                            reset = (
                                session.query(LibraryDownloadQueue)
                                .filter(
                                    LibraryDownloadQueue.id == any_queue.id,
                                    LibraryDownloadQueue.status
                                    != DocumentStatus.PROCESSING,
                                )
                                .update(
                                    {
                                        LibraryDownloadQueue.status: DocumentStatus.PENDING,
                                        LibraryDownloadQueue.research_id: research_id,
                                        LibraryDownloadQueue.collection_id: collection_id,
                                    },
                                    synchronize_session=False,
                                )
                            )
                            if reset:
                                queued += 1
                        else:
                            # Add new queue entry
                            queue_entry = LibraryDownloadQueue(
                                resource_id=resource.id,
                                research_id=research_id,
                                collection_id=collection_id,
                                priority=0,
                                status=DocumentStatus.PENDING,
                            )
                            session.add(queue_entry)
                            queued += 1

            session.commit()
            logger.info(
                f"Queued {queued} downloads for research {research_id} to collection {collection_id}"
            )

        return queued

    def _links_without_fetch(self, document) -> bool:
        """Whether a resource that finds ``document`` by URL or content links to it.

        The dedup link sites of ``persist_research_sources`` and the
        auto-persist tracker dedup in ``download_resource`` link a resource
        to a completed document instead of downloading it again. A document
        that awaits its PDF (see ``document_awaits_pdf``) is not linked
        there, so that downloading the resource refills it, unless the
        current PDF storage size limit refuses its PDF
        (``_pdf_refused_at_current_limit``): downloading it would then
        fetch the whole PDF only for the limit to refuse it again, so the
        resource is linked as it was before the refusal kept the document
        awaiting its PDF. Once the limit is raised, the linked resource's
        download refills the document through its ``document_id``.
        """
        return not document_awaits_pdf(
            document
        ) or self._pdf_refused_at_current_limit(document)

    def _pdf_refused_at_current_limit(self, document) -> bool:
        """Whether the current PDF storage size limit refuses ``document``'s PDF.

        True for a document that awaits its PDF (see ``document_awaits_pdf``)
        whose recorded ``file_size``, the size of the PDF it last downloaded
        (the one the limit refused, see ``_keep_refused_pdf_text_only``),
        exceeds ``research_library.max_pdf_size_mb`` while the storage mode
        stores PDFs. ``PDFStorageManager.save_pdf`` would refuse those bytes
        again, so downloading them is pointless until the operator raises
        the limit; then this is False and the PDF is downloaded again. A
        storage mode that stores no PDF ("none", or a spelling
        ``PDFStorageManager`` does not know) refuses nothing here.
        """
        if document is None or not document_awaits_pdf(document):
            return False
        size = document.file_size
        if not isinstance(size, int) or size <= 0:
            return False
        storage_mode = resolve_pdf_storage_mode(
            self.settings.get_setting(
                "research_library.pdf_storage_mode", "none"
            )
        )
        if storage_mode not in ("database", "filesystem"):
            return False
        try:
            max_pdf_size_mb = int(
                self.settings.get_setting(
                    "research_library.max_pdf_size_mb", DEFAULT_MAX_PDF_SIZE_MB
                )
            )
        except (TypeError, ValueError):
            # Not a limit this check can compare against: leave the
            # decision to the download, which reads the same setting.
            return False
        return size > max_pdf_size_mb * 1024 * 1024

    def _is_downloadable(self, resource: ResearchResource) -> bool:
        """Check if a resource is likely downloadable as PDF.

        Delegates to the consolidated is_downloadable_url() from utils.
        """
        return is_downloadable_url(resource.url)

    def refill_rank(
        self, resource_id: int, url: Optional[str], owner_id: Optional[int]
    ) -> RefillRank:
        """The rank of a resource that would refill a document awaiting its PDF.

        ``owner_id`` is the document's own resource (``Document.resource_id``).
        See ``RefillRank``: a viable resource ranks before one that is not,
        and among viable resources the document's own one ranks first. Of
        the others, one that can be queued ranks before one that cannot, and
        one the retry manager would retry now before one it holds back. The
        rank asks the egress policy and the retry manager, whose record of
        attempts the "Download All New" retry-policy filter reads too.
        ``queue_research_downloads`` and the "Download All New" route both
        choose the resource that refills a document by this rank.

        Ranking fetches nothing, so it records no denial of the egress
        policy: a refused URL is neither written to the audit log nor
        counted toward the run's denied-fetch quota (see
        ``_check_url_against_policy``). Fetching the URL still checks it and
        records the denial, and so does queueing it in
        ``queue_research_downloads``.
        """
        cannot_queue = not (
            url
            and is_downloadable_url(url)
            and self._check_url_against_policy(url, record_denial=False)[0]
        )
        held_back = failed_last = False
        if self.retry_manager:
            held_back = not self.retry_manager.should_retry_resource(
                resource_id
            ).can_retry
            status = self.retry_manager.status_tracker.get_resource_status(
                resource_id
            )
            failed_last = status is not None and status["status"] in (
                RetryStatus.TEMPORARILY_FAILED.value,
                RetryStatus.PERMANENTLY_FAILED.value,
            )
        return RefillRank(
            cannot_queue=cannot_queue,
            held_back=held_back,
            failed_last=failed_last,
            linked=resource_id != owner_id,
            resource_id=resource_id,
        )

    def _refilled_by_another_resource(
        self,
        session: Session,
        document: Document,
        resource: ResearchResource,
        run: Optional[AbstractSet[str]],
        research_id: str,
        ranks: Dict[Tuple[int, Optional[int]], RefillRank],
    ) -> bool:
        """Whether a resource other than ``resource`` refills ``document``.

        A document that awaits its PDF (see ``document_awaits_pdf``) is
        refilled by a download of its own resource (``Document.resource_id``,
        unless that resource points to another document through
        ``document_id``) or of any resource linked to it by content
        (``ResearchResource.document_id``): each downloads into it, see
        ``get_document_for_resource``. One such download refills it, so
        ``queue_research_downloads`` queues ``resource`` only when this is
        False. ``research_id`` is the research being queued, ``run`` the
        research sessions of the run (see ``queue_research_downloads``),
        and ``ranks`` holds the ``refill_rank`` of the resources compared
        so far in that call. True when another of these resources

        * has a PROCESSING queue entry that a download still holds (see
          ``download_claim_is_live``): another stream is downloading it.
          An entry that a restart left PROCESSING does not count, and that
          resource is not queued either, since no reset touches a
          PROCESSING entry;
        * has a PENDING queue entry that the same run downloads, i.e. of a
          research session in ``run``, and is viable or ranks before
          ``resource`` (see ``RefillRank``). Without a run (``run`` None,
          when nothing downloads the queue right away) any PENDING entry
          counts: the run that downloads the queue later checks again with
          its own research sessions. Within a run, a PENDING entry of a
          research session outside it does not count, since the run would
          not download it;
        * ranks before ``resource`` and would be queued by the same call or
          run: its research session is ``research_id`` or one in ``run``,
          and it has no queue entry PENDING or PROCESSING. Ranking before
          ``resource``, which can be queued, it can be queued too.

        So, unless one of them is being downloaded, a resource that cannot
        succeed does not keep a viable one from refilling the document, and
        of the viable ones in the run the document's own resource refills
        it, whichever research the run queues first, unless another viable
        one is already queued for the run.
        """
        # Bare columns: the full rows would load each resource's
        # content_preview (#4560).
        others = (
            session.query(
                ResearchResource.id,
                ResearchResource.url,
                ResearchResource.research_id,
            )
            .filter(
                ResearchResource.id != resource.id,
                or_(
                    ResearchResource.document_id == document.id,
                    and_(
                        ResearchResource.id == document.resource_id,
                        ResearchResource.document_id.is_(None),
                    ),
                ),
            )
            .all()
        )
        if not others:
            return False
        entries = {
            entry.resource_id: entry
            for entry in session.query(
                LibraryDownloadQueue.resource_id,
                LibraryDownloadQueue.status,
                LibraryDownloadQueue.research_id,
                LibraryDownloadQueue.last_attempt_at,
            ).filter(
                LibraryDownloadQueue.resource_id.in_(
                    [other.id for other in others]
                ),
                LibraryDownloadQueue.status.in_(
                    (DocumentStatus.PENDING, DocumentStatus.PROCESSING)
                ),
            )
        }

        def rank_of(res) -> RefillRank:
            key = (res.id, document.resource_id)
            if key not in ranks:
                ranks[key] = self.refill_rank(
                    res.id, res.url, document.resource_id
                )
            return ranks[key]

        own_rank = rank_of(resource)
        queued_now = run if run is not None else {research_id}
        for other in others:
            entry = entries.get(other.id)
            if entry is not None and entry.status == DocumentStatus.PROCESSING:
                if download_claim_is_live(entry.last_attempt_at):
                    return True
                continue
            rank = rank_of(other)
            if entry is not None:
                if (run is None or entry.research_id in run) and (
                    rank.viable or rank < own_rank
                ):
                    return True
                continue
            if rank < own_rank and other.research_id in queued_now:
                return True
        return False

    def persist_research_sources(
        self,
        research_id: str,
        collection_id: Optional[str] = None,
    ) -> Dict[str, Any]:
        """
        Persist all sources from a research session into the Library.

        Eliminates the PDF double-fetch: PDFs already persisted on-the-spot
        during the run are linked without network I/O. Plain web-page text
        has no fetch-time persistence and is still fetched here by design.
        For each research resource:
        - If PDF storage is enabled (pdf_storage_mode is 'database' or 'filesystem'),
          the scheduler allows PDFs (document_scheduler.download_pdfs), and the
          URL is downloadable, download the PDF (and extract its text).
          If PDF download fails, fall back to extracting and saving text.
        - Otherwise extract and persist text content directly.
        - Resources that are already completed and do not await a PDF are skipped.
        - Deduplication: URL-based via DownloadTracker (including redirect
          final URLs), content-hash-based via Document.document_hash.

        Args:
            research_id: The UUID of the research session.
            collection_id: Optional collection ID to add the documents to (defaults to Library).

        Returns:
            Dict summarizing the persistence run:
            {'total': int, 'persisted': int, 'failed': int, 'skipped': int}
        """
        start_time = time.perf_counter()
        persisted_count = 0
        failed_count = 0

        with get_user_db_session(self.username, self.password) as session:
            resources = (
                session.query(ResearchResource)
                .filter_by(research_id=research_id)
                .order_by(ResearchResource.id.asc())
                .all()
            )
            if not resources:
                logger.info(
                    f"No research resources to persist for research {research_id}"
                )
                return {"total": 0, "persisted": 0, "failed": 0, "skipped": 0}

            pdf_storage_mode = resolve_pdf_storage_mode(
                self.settings.get_setting(
                    "research_library.pdf_storage_mode", "database"
                )
            )
            try:
                scheduler_allows_pdfs = bool(
                    self.settings.get_setting(
                        "document_scheduler.download_pdfs", False
                    )
                )
            except Exception:
                scheduler_allows_pdfs = False
            enable_pdf_storage = (
                pdf_storage_mode != "none" and scheduler_allows_pdfs
            )

            items_to_process = []
            skipped_count = 0
            for r in resources:
                doc = get_document_for_resource(session, r)
                if doc is None and r.url:
                    tracker = find_tracker_by_url(session, r.url)
                    if (
                        tracker
                        and getattr(tracker, "is_downloaded", False) is True
                        and getattr(tracker, "file_hash", None)
                    ):
                        existing_by_hash = (
                            session.query(Document)
                            .filter_by(
                                document_hash=tracker.file_hash,
                                status=DocumentStatus.COMPLETED,
                            )
                            .first()
                        )
                        if existing_by_hash and self._links_without_fetch(
                            existing_by_hash
                        ):
                            r.document_id = existing_by_hash.id
                            doc = existing_by_hash
                            session.commit()
                            persisted_count += 1
                            continue

                    existing_by_url = (
                        session.query(Document)
                        .filter_by(
                            original_url=r.url,
                            status=DocumentStatus.COMPLETED,
                        )
                        .order_by(Document.created_at.desc())
                        .first()
                    )
                    if existing_by_url and self._links_without_fetch(
                        existing_by_url
                    ):
                        r.document_id = existing_by_url.id
                        doc = existing_by_url
                        session.commit()
                        persisted_count += 1
                        continue
                is_dl = self._is_downloadable(r) or (
                    doc is not None and getattr(doc, "file_type", None) == "pdf"
                )
                if doc is not None and doc.status == DocumentStatus.COMPLETED:
                    if (
                        enable_pdf_storage
                        and is_dl
                        and document_awaits_pdf(doc)
                        and not self._pdf_refused_at_current_limit(doc)
                    ):
                        items_to_process.append((r.id, r.url, is_dl))
                    else:
                        skipped_count += 1
                else:
                    items_to_process.append((r.id, r.url, is_dl))

        for resource_id, url, is_dl in items_to_process:
            try:
                if enable_pdf_storage and is_dl:
                    success, error = self.download_resource(
                        resource_id, collection_id=collection_id
                    )
                    if success:
                        persisted_count += 1
                        logger.info(
                            f"Auto-persisted PDF for resource {resource_id} ({redact_url_for_log(url)})"
                        )
                    else:
                        if error == PDF_STORAGE_SIZE_LIMIT_REASON:
                            logger.info(
                                f"PDF of resource {resource_id} not stored: it "
                                "exceeds the configured PDF storage size "
                                "limit; attempting text fallback"
                            )
                        else:
                            logger.info(
                                f"PDF download failed for resource {resource_id} ({failure_reason_for_log(error)}), attempting text fallback"
                            )
                        text_success, text_error = self.download_as_text(
                            resource_id
                        )
                        if text_success:
                            persisted_count += 1
                            logger.info(
                                f"Auto-persisted fallback text for resource {resource_id}"
                            )
                        else:
                            failed_count += 1
                            logger.warning(
                                f"Failed to persist resource {resource_id}: {failure_reason_for_log(text_error)}"
                            )
                else:
                    success, error = self.download_as_text(resource_id)
                    if success:
                        persisted_count += 1
                        logger.info(
                            f"Auto-persisted text for resource {resource_id} ({redact_url_for_log(url)})"
                        )
                    else:
                        failed_count += 1
                        logger.warning(
                            f"Failed to persist text for resource {resource_id}: {failure_reason_for_log(error)}"
                        )
            except Exception as exc:
                failed_count += 1
                log_exception_type(
                    f"Error persisting resource {resource_id}", exc
                )

        elapsed = time.perf_counter() - start_time
        summary = {
            "total": len(resources),
            "persisted": persisted_count,
            "failed": failed_count,
            "skipped": skipped_count,
        }
        logger.info(
            f"Auto-persisted research sources for {research_id} in {elapsed:.2f}s: "
            f"{persisted_count} persisted, {failed_count} failed, {skipped_count} skipped (total {len(resources)})"
        )
        return summary

    def download_resource(
        self, resource_id: int, collection_id: Optional[str] = None
    ) -> Tuple[bool, Optional[str]]:
        """
        Download a specific resource.

        Returns:
            Tuple of (success: bool, skip_reason: str or None)
        """
        linked_doc_id: Optional[str] = None
        with get_user_db_session(self.username, self.password) as session:
            resource = session.get(ResearchResource, resource_id)
            if not resource:
                logger.error(f"Resource {resource_id} not found")
                return False, "Resource not found"

            # Check if already downloaded (trust the database after sync).
            # A completed document that the sync made text-only because its
            # PDF file was missing still needs its PDF, so it is downloaded
            # again into that document (see document_awaits_pdf).
            existing_query = session.query(Document).filter_by(
                resource_id=resource_id, status=DocumentStatus.COMPLETED
            )
            if resource.document_id:
                # Never short-circuit on a retained older document. Keep
                # linked-document retries on the normal path so they repair
                # queue, tracker, and collection bookkeeping.
                existing_query = existing_query.filter(
                    Document.id == resource.document_id
                )
            existing_doc = existing_query.first()
            if existing_doc and not document_awaits_pdf(existing_doc):
                logger.info(
                    "Resource already downloaded (according to database)"
                )
                return True, None

            # A document that keeps its text because the PDF storage size
            # limit refused its PDF is not downloaded again while the limit
            # still refuses a PDF of that size. Nothing is fetched or
            # recorded: the retry manager keeps no cooldown for it, so the
            # text routes that ask it are not held back, and the PDF is
            # downloaded as soon as the operator raises the limit.
            # A resource linked to another resource's document by content
            # (``document_id``) refills that document, which the query
            # above leaves out.
            refill_doc = existing_doc
            if refill_doc is None and resource.document_id:
                refill_doc = session.get(Document, resource.document_id)
            if self._pdf_refused_at_current_limit(refill_doc):
                logger.info(
                    f"Not downloading the PDF of resource {resource_id}: it "
                    "exceeds the configured PDF storage size limit"
                )
                return False, PDF_STORAGE_SIZE_LIMIT_REASON

            # Get collection_id from queue entry if it exists
            queue_entry = (
                session.query(LibraryDownloadQueue)
                .filter_by(resource_id=resource_id)
                .first()
            )
            if not collection_id:
                collection_id = (
                    queue_entry.collection_id
                    if queue_entry and queue_entry.collection_id
                    else None
                )

            # Create download tracker entry. B2 compat (#6758): reuse a
            # pre-upgrade legacy-hash row when present instead of inserting
            # a second tracker for the same URL.
            url_hash = self._get_url_hash(resource.url)
            tracker = find_tracker_by_url(session, resource.url)

            # Check if another resource with the same URL is already
            # downloaded (tracker dedup). Auto-persist only: with the
            # setting off, library downloads keep main's flow (including
            # its collection, tracker and retry bookkeeping) exactly.
            if (
                _is_auto_persist_enabled(
                    getattr(self, "_settings_snapshot", None),
                    getattr(self, "username", None),
                )
                and tracker
                and getattr(tracker, "is_downloaded", False) is True
                and isinstance(getattr(tracker, "first_resource_id", None), int)
                and tracker.first_resource_id != resource.id
            ):
                first_resource = session.get(
                    ResearchResource, tracker.first_resource_id
                )
                first_doc = (
                    get_document_for_resource(session, first_resource)
                    if first_resource
                    else None
                )
                if not first_doc and tracker.file_hash:
                    first_doc = (
                        session.query(Document)
                        .filter_by(document_hash=tracker.file_hash)
                        .first()
                    )
                if not first_doc:
                    first_doc = (
                        session.query(Document)
                        .filter_by(original_url=resource.url)
                        .first()
                    )
                if (
                    first_doc
                    and first_doc.status == DocumentStatus.COMPLETED
                    and self._links_without_fetch(first_doc)
                ):
                    # Link without skipping bookkeeping: the caller may
                    # have queued this resource into a specific collection,
                    # and the post-download block below still owns queue,
                    # retry and auto-index handling for that collection.
                    resource.document_id = first_doc.id
                    try:
                        target_collection = collection_id
                        if not target_collection:
                            from ...database.library_init import (
                                get_default_library_id as _get_default_lib,
                            )

                            target_collection = _get_default_lib(
                                self.username, self.password
                            )
                        if target_collection:
                            from ...database.models.library import Collection

                            coll_exists = (
                                session.query(Collection.id)
                                .filter_by(id=target_collection)
                                .first()
                                is not None
                            )
                            if coll_exists:
                                with session.begin_nested():
                                    ensure_in_collection(
                                        session, first_doc.id, target_collection
                                    )
                                    session.flush()
                            else:
                                logger.warning(
                                    f"Collection {target_collection} not found, skipping dedup link"
                                )
                    except Exception as exc:
                        log_exception_type(
                            "Failed to link deduped document to collection",
                            exc,
                        )
                    session.commit()
                    linked_doc_id = first_doc.id
                    logger.info("Resource already downloaded via tracker link")

            if not tracker:
                # Wrap the new-tracker INSERT in a SAVEPOINT so it is
                # structurally separated from the shared thread-local
                # session's outer transaction. Three reasons:
                #   1. The savepoint commit makes the new tracker row
                #      durable even if a later step on this thread rolls
                #      back the outer transaction.
                #   2. If the INSERT itself raises (e.g. UNIQUE conflict
                #      on a concurrent same-URL insert), only the savepoint
                #      rolls back — the caller's outer transaction state
                #      is untouched.
                #   3. The savepoint scopes ``session.expunge(tracker)``
                #      below: the pending INSERT is part of the savepoint's
                #      write-set, so the expunge does not cancel a write
                #      that was about to promote.
                # Note: the savepoint does NOT prevent the unconditional
                # ``session.commit()`` at the bottom of this block from
                # promoting caller-staged rows already in the outer
                # transaction — SQLAlchemy's savepoint commit only absorbs
                # the inner writes; the outer commit still flushes
                # everything. None of the three current call sites stage
                # rows across the call (see callers in web/routers/library.py);
                # the discipline is documented at the call sites.
                # Honesty note on durability: with nothing else staged, the
                # SAVEPOINT is the first statement in autocommit so its
                # RELEASE commits immediately (a second connection already
                # sees the row; a later rollback() is a no-op) — durability
                # here rests on that driver behavior, not on savepoint
                # nesting alone.
                with session.begin_nested():
                    tracker = DownloadTracker(
                        url=resource.url,
                        url_hash=url_hash,
                        first_resource_id=resource.id,
                        is_downloaded=False,
                    )
                    session.add(tracker)
                    # Explicit flush inside the savepoint — the savepoint's
                    # __exit__ does auto-flush its writes into the outer
                    # transaction, but a future SQLAlchemy change or an
                    # autoflush=False caller would silently drop the INSERT
                    # before ``session.expunge(tracker)`` cancels it. The
                    # flush makes the FK-on-first-download invariant explicit
                    # at this site rather than relying on savepoint-exit
                    # side effects. See PR #6564 P1 review.
                    session.flush()

            # Snapshot plain values for the post-download block (#6560, #6564)
            resource_id_val = resource.id
            resource_url = resource.url

            # Expunge instances so their loaded attributes do not expire on commit
            # and accessing them outside the session will not trigger lazy reload.
            if hasattr(session, "expunge"):
                session.expunge(resource)
                session.expunge(tracker)

            # Unconditionally commit to release the connection back to QueuePool (#6564).
            # The new-tracker INSERT is enclosed in the savepoint above and is
            # therefore already durable; this commit only closes the outer
            # transaction and does not promote any caller-staged row.
            session.commit()

        # Attempt download outside database session (#6560), unless the
        # tracker dedup above already linked a completed document.
        if linked_doc_id is not None:
            success, skip_reason, status_code = True, None, None
        else:
            success, skip_reason, status_code = self._download_pdf(
                resource, tracker, None, collection_id
            )

        # Open short transaction for post-download queue and retry updates
        with get_user_db_session(self.username, self.password) as session:
            # Record attempt with retry manager for smart failure tracking
            self.retry_manager.record_attempt(
                resource_id=resource_id_val,
                result=(success, skip_reason),
                status_code=status_code,
                url=resource_url,
                details=skip_reason
                or (
                    "Successfully downloaded" if success else "Download failed"
                ),
                session=session,
            )

            # Update queue status if exists
            queue_entry = (
                session.query(LibraryDownloadQueue)
                .filter_by(resource_id=resource_id_val)
                .first()
            )

            if queue_entry:
                queue_entry.status = (
                    DocumentStatus.COMPLETED
                    if success
                    else DocumentStatus.FAILED
                )
                queue_entry.completed_at = datetime.now(UTC)

            session.commit()

            # Trigger auto-indexing for successfully downloaded documents
            if success and self.password:
                try:
                    from ...web.routers.rag import trigger_auto_index
                    from ...database.library_init import get_default_library_id

                    # Resolve via get_document_for_resource so a dedup-linked
                    # resource (resource.document_id -> canonical Document
                    # owned by another resource) still triggers indexing —
                    # the old resource_id-only lookup missed it and skipped
                    # auto-indexing in the requested collection (#6758).
                    resource_for_index = session.get(
                        ResearchResource, resource_id
                    )
                    doc = (
                        get_document_for_resource(session, resource_for_index)
                        if resource_for_index is not None
                        else None
                    )
                    if doc is None:
                        doc = (
                            session.query(Document)
                            .filter_by(resource_id=resource_id)
                            .order_by(Document.created_at.desc())
                            .first()
                        )
                    if doc:
                        # Use collection_id from queue entry or default Library
                        # NB: pass username string, not the SQLAlchemy session
                        target_collection = (
                            collection_id
                            or get_default_library_id(
                                self.username, self.password
                            )
                        )
                        if target_collection:
                            trigger_auto_index(
                                [doc.id],
                                target_collection,
                                self.username,
                                self.password,
                            )
                except Exception as exc:
                    # The Document SELECT above runs on the shared session; a
                    # connection-level failure there leaves it needing a
                    # rollback. Auto-indexing is best-effort so we swallow and
                    # return — the with-block exits normally and
                    # get_user_db_session's rollback never fires. Recover the
                    # session here so a later op on it (same request/thread)
                    # doesn't cascade. (No-op for the other failures reachable
                    # here: get_default_library_id self-heals via its own inner
                    # get_user_db_session block, and trigger_auto_index does its
                    # DB work off-thread, so neither dirties this session.)
                    safe_rollback(session, "download_resource auto-index")
                    log_exception_type("Failed to trigger auto-indexing", exc)
                finally:
                    # Release any checked-out connection from the auto-index query
                    session.commit()

        return success, skip_reason

    def _download_pdf(
        self,
        resource: ResearchResource,
        tracker: DownloadTracker,
        session: Optional[Session] = None,
        collection_id: Optional[str] = None,
    ) -> Tuple[bool, Optional[str], Optional[int]]:
        """
        Perform the actual PDF download.

        Args:
            resource: The research resource to download
            tracker: Download tracker for this URL
            session: Database session (optional; if None, short transaction opened after download)
            collection_id: Optional target collection ID (defaults to Library if not provided)

        Returns:
            Tuple of (success: bool, skip_reason: Optional[str], status_code: Optional[int])
        """
        url = resource.url
        # Snapshot the title before any session work. When the caller passes
        # session=None this instance is detached, and its columns are expired
        # by the caller's own commit, so the value has to be read while it is
        # still loaded. Canonical text production below uses it for its log
        # lines and runs with no session open.
        resource_title = resource.title

        # Egress policy gate at the network-fire point. The entry caller
        # (download_resource) does not gate, so this is the true PEP — a
        # denied URL must never reach a downloader regardless of route.
        allowed, reason = self._check_url_against_policy(url)
        if not allowed:
            logger.warning(
                f"Skipping PDF download for {redact_url_for_log(url)}: refused by egress "
                f"policy ({reason})"
            )
            return False, f"egress_policy_denied:{reason}", None

        attempted_at = datetime.now(UTC)
        # The held-aside rate-limit result is declared before the try on
        # purpose: once a 429 is held, anything later in the try can raise (a
        # fallback downloader, or the failure-recording transaction), and while
        # no PDF content has been obtained the outer except must still
        # reinstate the 429 classification rather than let the exception text
        # replace it (#7588).
        pdf_content = None
        held_rate_limit: List[DownloadResult] = []
        try:
            # Use modular downloaders with detailed skip reasons.
            # Download occurs outside the database session (#6560).
            selected_downloader = None
            skip_reason = None
            status_code = None

            if resource.document_id:
                # A duplicate resource points to another resource's saved
                # snapshot. Reuse its bytes while still repairing this
                # resource's tracker, collection membership and queue state.
                # Missing storage falls through to the normal retry path.
                cache_session_cm = (
                    nullcontext(session)
                    if session is not None
                    else get_user_db_session(self.username, self.password)
                )
                with cache_session_cm as cache_session:
                    cached_pdf = self._load_cached_pdf(cache_session, resource)
                    if session is None:
                        # This read belongs to the download. Release it even
                        # when an outer route still owns a session scope.
                        cache_session.rollback()
                    if cached_pdf is not None:
                        pdf_content = cached_pdf[0]

            if pdf_content is None:
                result, selected_downloader = self._download_pdf_with_fallback(
                    url, held_rate_limit=held_rate_limit
                )
                if result.is_success and result.content:
                    pdf_content = result.content
                skip_reason = result.skip_reason
                status_code = result.status_code
            # When pdf_content came from the local cache, selected_downloader
            # stays None (as on main) so text production stays local and
            # never sends arXiv duplicates through a network path.

            # Produce the canonical text BEFORE the persistence block.
            # download_text_with_source may issue a network (HTML) request,
            # and holding an uncommitted write transaction (Document row +
            # save_pdf blob) open across that round-trip extends the write
            # lock for the full network latency. No session is open here at
            # all, so that request also cannot sit on a checked-out
            # connection (#6560). Production and persistence both stay
            # best-effort: a failure in either must not fail the PDF
            # download itself.
            text_details: tuple[str, str, str] | None = None
            if pdf_content:
                try:
                    if isinstance(selected_downloader, ArxivDownloader):
                        logger.info(
                            f"Producing canonical arXiv text for: {resource_title[:50]}"
                        )
                        arxiv_text = (
                            selected_downloader.download_text_with_source(
                                url,
                                pdf_content=pdf_content,
                            )
                        )
                        if arxiv_text is not None:
                            extraction_method, extraction_source = (
                                _arxiv_text_provenance(arxiv_text.source)
                            )
                            text_details = (
                                arxiv_text.text,
                                extraction_method,
                                extraction_source,
                            )
                    else:
                        logger.info(
                            f"Extracting text from downloaded PDF for: {resource_title[:50]}"
                        )
                        text = self._extract_text_from_pdf(pdf_content)
                        if text:
                            text_details = (text, "pdf_extraction", "local_pdf")

                    if text_details is None:
                        logger.warning(
                            f"Text production returned empty text for: {resource_title[:50]}"
                        )
                except Exception as exc:
                    logger.opt(exception=False).warning(
                        "Failed to produce text after PDF download "
                        "({error_type}), but PDF download succeeded",
                        error_type=type(exc).__name__,
                    )
                    text_details = None

            # Open short transaction only after download completion to stage
            # the DownloadAttempt and update document records (#6560).
            session_cm = (
                nullcontext(session)
                if session is not None
                else get_user_db_session(self.username, self.password)
            )

            with session_cm as sess:
                if session is None:
                    db_resource = sess.get(ResearchResource, resource.id)
                    db_tracker = (
                        sess.query(DownloadTracker)
                        .filter_by(url_hash=tracker.url_hash)
                        .first()
                    )
                    target_resource = (
                        db_resource if db_resource is not None else resource
                    )
                    target_tracker = (
                        db_tracker if db_tracker is not None else tracker
                    )
                else:
                    target_resource = resource
                    target_tracker = tracker

                # Snapshot scalar resource values used AFTER the staging commit
                # so attribute access does not lazy-refresh a checked-out
                # connection through the staging session immediately before
                # text production's network/CPU work. PR #6564 P2 review.
                # Only the title is read in log lines after the staging
                # commit; the freshly fetched `db_resource` below already has
                # its scalar columns eagerly loaded, so passing it to
                # `_save_text_with_db` / `get_document_for_resource` is
                # safe (those read `id` and `document_id`, never lazy-loaded).
                staging_resource_title = target_resource.title

                # Helper for attempt number calculation
                attempts = getattr(target_tracker, "download_attempts", None)
                if (
                    attempts is not None
                    and not isinstance(attempts, (list, tuple))
                    and hasattr(attempts, "count")
                ):
                    attempt_num = attempts.count() + 1
                elif isinstance(attempts, (list, tuple)):
                    attempt_num = len(attempts) + 1
                elif (
                    hasattr(tracker, "download_attempts")
                    and not isinstance(tracker.download_attempts, (list, tuple))
                    and hasattr(tracker.download_attempts, "count")
                ):
                    attempt_num = tracker.download_attempts.count() + 1
                elif isinstance(
                    getattr(tracker, "download_attempts", None), (list, tuple)
                ):
                    attempt_num = len(tracker.download_attempts) + 1
                else:
                    attempt_num = 1
                if not pdf_content:
                    error_msg = skip_reason or "Failed to download PDF content"
                    attempt = DownloadAttempt(
                        url_hash=tracker.url_hash,
                        attempt_number=attempt_num,
                        attempted_at=attempted_at,
                        succeeded=False,
                        error_message=error_msg,
                    )
                    sess.add(attempt)
                    # Record the failure in its own transaction so the row
                    # survives whatever the caller does with the session
                    # after this early return.
                    sess.commit()
                    logger.info(
                        "Download failed with reason: "
                        f"{failure_reason_for_log(error_msg)}"
                    )
                    return False, error_msg, status_code

                # Get PDF storage mode setting. Resolve through the operator gate
                # BEFORE any PDF is written: a stored value or an
                # LDR_RESEARCH_LIBRARY_PDF_STORAGE_MODE env override could still
                # read "filesystem" even though the settings UI withholds that
                # option, so the unencrypted mode is coerced to the encrypted
                # "database" default here unless the operator opted in. This is
                # the true enforcement point — hiding the dropdown alone is not
                # enough.
                pdf_storage_mode = resolve_pdf_storage_mode(
                    self.settings.get_setting(
                        "research_library.pdf_storage_mode", "none"
                    )
                )
                max_pdf_size_mb = int(
                    self.settings.get_setting(
                        "research_library.max_pdf_size_mb",
                        DEFAULT_MAX_PDF_SIZE_MB,
                    )
                )
                logger.info(
                    f"[DOWNLOAD_SERVICE] PDF storage mode: {pdf_storage_mode}"
                )

                # Update tracker
                import hashlib

                file_hash_val = hashlib.sha256(pdf_content).hexdigest()
                file_size_val = len(pdf_content)
                downloaded_at_val = datetime.now(UTC)

                existing_doc = get_document_for_resource(sess, target_resource)
                if (
                    existing_doc
                    and document_awaits_pdf(existing_doc)
                    and existing_doc.document_hash != file_hash_val
                    and sess.query(Document.id)
                    .filter(Document.document_hash == file_hash_val)
                    .first()
                    is not None
                ):
                    # Keeping the old hash while storing different bytes
                    # poisons later content deduplication: a download of
                    # the original PDF would then serve the replacement.
                    # Preserve both documents until the caller can relink
                    # to the canonical owner, before changing any storage,
                    # text, or tracker state.
                    error_msg = (
                        "PDF refill conflicts with another library document"
                    )
                    sess.add(
                        DownloadAttempt(
                            url_hash=target_tracker.url_hash,
                            attempt_number=attempt_num,
                            attempted_at=attempted_at,
                            succeeded=False,
                            error_message=error_msg,
                        )
                    )
                    sess.commit()
                    return False, error_msg, status_code

                target_tracker.file_hash = file_hash_val
                target_tracker.file_size = file_size_val
                target_tracker.is_downloaded = True
                target_tracker.downloaded_at = downloaded_at_val
                if hasattr(tracker, "file_hash"):
                    tracker.file_hash = file_hash_val
                    tracker.file_size = file_size_val
                    tracker.is_downloaded = True
                    tracker.downloaded_at = downloaded_at_val

                # Read and write PDFs only within the per-user root.
                pdf_storage_manager = PDFStorageManager(
                    library_root=self.library_root,
                    storage_mode=pdf_storage_mode,
                    max_pdf_size_mb=max_pdf_size_mb,
                )

                # Update attempt with success info
                attempt = DownloadAttempt(
                    url_hash=tracker.url_hash,
                    attempt_number=attempt_num,
                    attempted_at=attempted_at,
                    succeeded=True,
                    bytes_downloaded=len(pdf_content),
                )
                sess.add(attempt)

                canonical_retry = False
                # Set when the size limit refuses this PDF: the document then
                # keeps its text and awaits its PDF until the limit allows it
                # (see _keep_refused_pdf_text_only).
                pdf_refused = False
                # Older placeholder recovery could leave a nonempty PDF
                # labelled "unknown"; that content still needs preservation.
                if (
                    existing_doc
                    and existing_doc.document_hash != file_hash_val
                    and not document_awaits_pdf(existing_doc)
                    and (
                        existing_doc.status == DocumentStatus.COMPLETED
                        or (
                            existing_doc.file_type in ("pdf", "unknown")
                            and existing_doc.file_size > 0
                        )
                        or pdf_storage_manager.has_pdf(existing_doc, sess)
                    )
                ):
                    # Preserve a saved snapshot even if later extraction
                    # failed, or the link appeared while the download was in
                    # flight. Resolve changed bytes by hash without rewriting
                    # the document used by other resources.
                    # A sync-marked missing PDF may refill the same record;
                    # the collision guard above already protects other owners.
                    existing_doc = None
                    canonical_retry = True
                if (
                    existing_doc
                    and existing_doc.status == DocumentStatus.FAILED
                ):
                    # A retry can discover bytes already owned by another
                    # document. Resolve that owner BEFORE replacing the failed
                    # hash, otherwise the next autoflush violates uniqueness.
                    owner = (
                        sess.query(Document)
                        .filter_by(document_hash=file_hash_val)
                        .first()
                    )
                    if owner and owner.id != existing_doc.id:
                        _link_retry_to_canonical(
                            sess, target_resource, existing_doc, owner
                        )
                        existing_doc = None
                        canonical_retry = True
                if (
                    existing_doc
                    and existing_doc.resource_id != target_resource.id
                    and existing_doc.document_hash == file_hash_val
                ):
                    # A repeated retry now resolves through document_id.
                    # It may itself be FAILED after another extraction attempt.
                    # Use the dedup storage path again so a none-mode retry
                    # cannot downgrade the canonical PDF's stored bytes.
                    existing_doc = None
                    canonical_retry = True

                if existing_doc:
                    pdf_document_id = existing_doc.id
                    refills_missing_pdf = document_awaits_pdf(existing_doc)
                    # Update existing document. Only replace document_hash when
                    # transitioning from FAILED — that's the placeholder hash from
                    # _record_failed_text_extraction. For any other prior state
                    # the hash is already a real content hash and clobbering it
                    # risks UNIQUE-constraint collisions (issue #3827).
                    was_failed = existing_doc.status == DocumentStatus.FAILED
                    if was_failed:
                        existing_doc.document_hash = target_tracker.file_hash
                        existing_doc.error_message = None
                    existing_doc.file_size = len(pdf_content)
                    existing_doc.status = DocumentStatus.COMPLETED
                    existing_doc.processed_at = datetime.now(UTC)

                    # A document that the library sync made text-only after
                    # its PDF file went missing gets its PDF back here, in
                    # the same document, whatever the bytes hash to (see
                    # document_awaits_pdf). A publisher or arXiv can serve
                    # a regenerated PDF, so the bytes can differ from the
                    # lost ones. The document then takes their hash, as a
                    # new download would. A hash already owned by another
                    # document was refused before any writes above.
                    if (
                        refills_missing_pdf
                        and existing_doc.document_hash != file_hash_val
                    ):
                        existing_doc.document_hash = file_hash_val

                    if refills_missing_pdf and collection_id:
                        # A linked resource may request a different collection.
                        # Keep its membership when changed bytes refill this
                        # record instead of taking the canonical dedup path.
                        ensure_in_collection(
                            sess, existing_doc.id, collection_id
                        )

                    # Save PDF using storage manager (updates storage_mode and file_path)
                    file_path_result, _ = pdf_storage_manager.save_pdf(
                        pdf_content=pdf_content,
                        document=existing_doc,
                        session=sess,
                        filename=f"{target_resource.id}.pdf",
                        url=url,
                        resource_id=target_resource.id,
                    )
                    if file_path_result:
                        # The PDF is now stored in this document, so label
                        # it a PDF, as a new download is. A failed-download
                        # placeholder is labelled "unknown" and a text
                        # extraction "text". Either label hides the stored
                        # PDF from PDFStorageManager.has_pdf. Once the sync
                        # has made the document text-only, it also keeps
                        # document_awaits_pdf from asking for the PDF again.
                        existing_doc.file_type = "pdf"
                        existing_doc.mime_type = "application/pdf"
                    elif _pdf_storage_refused(
                        file_path_result, pdf_storage_manager.storage_mode
                    ):
                        # The size limit refused the PDF. Keep the document
                        # and the text saved below, labelled as storing no
                        # PDF, and record the refused size, so the PDF is
                        # downloaded again only once the limit allows it. A
                        # refill of a document that awaits its PDF stays
                        # awaiting it.
                        pdf_refused = True
                        _keep_refused_pdf_text_only(
                            existing_doc,
                            sess,
                            pdf_storage_manager,
                            len(pdf_content),
                        )
                    elif refills_missing_pdf:
                        # The PDF of a document that still needed one was
                        # downloaded but not stored, because storage mode
                        # "none" stores no PDF. Leave the document as a
                        # "none"-mode download leaves one, so that the
                        # download paths stop fetching it again.
                        existing_doc.file_path = None

                    # Update tracker
                    target_tracker.file_path = (
                        file_path_result if file_path_result else None
                    )
                    target_tracker.file_name = (
                        Path(file_path_result).name
                        if file_path_result and file_path_result != "database"
                        else None
                    )
                    if hasattr(tracker, "file_path"):
                        tracker.file_path = target_tracker.file_path
                        tracker.file_name = target_tracker.file_name
                else:
                    # B1 (issue #6758): content-hash dedup across resources.
                    # Two different URLs can yield identical PDF bytes. Without
                    # a pre-check the INSERT below violates UNIQUE(document_hash)
                    # and the second resource ends up document-less after the
                    # IntegrityError rollback. Mirror the text-path pre-check in
                    # _save_text_with_db: link to the canonical Document instead.
                    existing_by_hash = (
                        sess.query(Document)
                        .filter_by(document_hash=target_tracker.file_hash)
                        .first()
                    )
                    if existing_by_hash:
                        pdf_document_id = existing_by_hash.id
                        target_resource.document_id = existing_by_hash.id
                        if canonical_retry:
                            if (
                                existing_by_hash.status
                                in (
                                    DocumentStatus.COMPLETED,
                                    DocumentStatus.FAILED,
                                )
                                and existing_by_hash.text_content
                            ):
                                # Preserve the canonical extraction and its
                                # indexed chunks even when a later extraction
                                # failed. FAILED does not mean text is absent.
                                text_details = None
                            if existing_by_hash.status == DocumentStatus.FAILED:
                                existing_by_hash.status = (
                                    DocumentStatus.COMPLETED
                                )
                                existing_by_hash.error_message = None
                                existing_by_hash.processed_at = datetime.now(
                                    UTC
                                )
                        try:
                            dedup_collection_id = (
                                collection_id
                                or get_default_library_id(
                                    self.username, self.password
                                )
                            )
                        except Exception as exc:
                            log_exception_type(
                                "Failed to get library collection for dedup link",
                                exc,
                            )
                            dedup_collection_id = None
                        if dedup_collection_id:
                            ensure_in_collection(
                                sess,
                                existing_by_hash.id,
                                dedup_collection_id,
                            )
                        else:
                            logger.warning(
                                f"Library collection not found - deduped document {existing_by_hash.id} will not be linked to default collection"
                            )
                        # Preserve/upgrade PDF storage on the dedup hit path.
                        # Without this, a first download with storage "none"
                        # followed by identical bytes with storage "database"
                        # leaves the canonical document with no saved PDF while
                        # the second tracker claims success. Only upgrade (never
                        # downgrade): when the canonical already stores the PDF,
                        # mirror its location onto this tracker for consistency.
                        upgrade_write_failed = False
                        try:
                            missing_pdf = canonical_retry and not (
                                pdf_storage_manager.has_pdf(
                                    existing_by_hash, sess
                                )
                            )
                            previous_paths = {existing_by_hash.file_path}
                            if existing_by_hash.storage_mode == "database":
                                previous_paths.add("database")
                            needs_upgrade = (
                                existing_by_hash.storage_mode in (None, "none")
                                or missing_pdf
                            ) and (
                                pdf_storage_mode in ("database", "filesystem")
                            )
                            if needs_upgrade:
                                try:
                                    file_path_result, _ = (
                                        pdf_storage_manager.save_pdf(
                                            pdf_content=pdf_content,
                                            document=existing_by_hash,
                                            session=sess,
                                            filename=f"{target_resource.id}.pdf",
                                            url=url,
                                            resource_id=target_resource.id,
                                        )
                                    )
                                except Exception:
                                    # A write that fails, for example on a
                                    # full disk, fails the download also
                                    # without a canonical retry (issue
                                    # #5868). Linking would record a
                                    # success with no PDF stored and leave
                                    # the resource counted as downloaded,
                                    # so nothing would fetch it again. The
                                    # rollback keeps the canonical document
                                    # as it was, and the next download
                                    # links it and stores the PDF.
                                    upgrade_write_failed = True
                                    raise
                                if _pdf_storage_refused(
                                    file_path_result,
                                    pdf_storage_manager.storage_mode,
                                ):
                                    # The size limit refused the upgrade.
                                    # Link to the canonical document as
                                    # before, and keep it text-only until
                                    # the limit allows its PDF (see
                                    # _keep_refused_pdf_text_only).
                                    pdf_refused = True
                                    _keep_refused_pdf_text_only(
                                        existing_by_hash,
                                        sess,
                                        pdf_storage_manager,
                                        len(pdf_content),
                                    )
                                target_tracker.file_path = (
                                    file_path_result
                                    if file_path_result
                                    else None
                                )
                                target_tracker.file_name = (
                                    Path(file_path_result).name
                                    if file_path_result
                                    and file_path_result != "database"
                                    else None
                                )
                                if file_path_result:
                                    # The PDF is now stored in this document:
                                    # label it a PDF, as the existing-document
                                    # branch above does.
                                    existing_by_hash.file_type = "pdf"
                                    existing_by_hash.mime_type = (
                                        "application/pdf"
                                    )
                                # Keep sibling trackers for the same bytes
                                # consistent: the canonical URL's tracker still
                                # has file_path=None from its none-mode download,
                                # which would otherwise make sync treat the now
                                # upgraded document as missing.
                                try:
                                    siblings = (
                                        sess.query(DownloadTracker)
                                        .filter_by(
                                            file_hash=target_tracker.file_hash
                                        )
                                        .all()
                                    )
                                    for sibling in siblings:
                                        if not sibling.file_path or (
                                            missing_pdf
                                            and sibling.file_path
                                            in previous_paths
                                        ):
                                            sibling.file_path = (
                                                target_tracker.file_path
                                            )
                                            sibling.file_name = (
                                                target_tracker.file_name
                                            )
                                except Exception as exc:
                                    log_exception_type(
                                        "Failed to sync sibling trackers after storage upgrade",
                                        exc,
                                    )
                                    if canonical_retry:
                                        raise
                            elif missing_pdf:
                                # none-mode retries may recover text without
                                # storing bytes. Do not advertise a lost file.
                                target_tracker.file_path = None
                                target_tracker.file_name = None
                            elif existing_by_hash.storage_mode == "database":
                                target_tracker.file_path = "database"
                                target_tracker.file_name = None
                            elif (
                                existing_by_hash.file_path
                                in FILE_PATH_SENTINELS
                            ):
                                # Never mirror file_path sentinels
                                # (``blob_deleted``, ``text_only_not_stored``,
                                # ``metadata_only``) into the tracker: they name
                                # no file, and the sync mid-branch would resolve
                                # them as a relative path, miss, and run
                                # ``delete_document_completely`` on the
                                # text/chunks/vectors the sentinel was protecting
                                # (issue #6760 follow-up). Leave the duplicate
                                # tracker path-less, the same shape as a fresh
                                # none-mode download.
                                target_tracker.file_path = None
                                target_tracker.file_name = None
                            elif existing_by_hash.file_path:
                                target_tracker.file_path = (
                                    existing_by_hash.file_path
                                )
                                target_tracker.file_name = (
                                    Path(existing_by_hash.file_path).name
                                    if existing_by_hash.file_path != "database"
                                    else None
                                )
                            else:
                                # Canonical has no stored file (none-mode): same
                                # shape as a fresh none-mode download
                                # (is_downloaded=True, no path). Bounded
                                # re-download, not data loss.
                                target_tracker.file_path = None
                                target_tracker.file_name = None
                            if (
                                document_awaits_pdf(existing_by_hash)
                                and not pdf_refused
                            ):
                                # Storage was disabled. Settle a linked
                                # refill as a text-only download, just like
                                # the own-document path, so every bulk run
                                # does not fetch it again. One the size
                                # limit refused keeps awaiting its PDF; the
                                # limit check in download_resource stops the
                                # fetches until the limit allows it.
                                existing_by_hash.file_path = None
                        except Exception as exc:
                            log_exception_type(
                                "Failed to reuse/upgrade deduped PDF storage",
                                exc,
                            )
                            if canonical_retry or upgrade_write_failed:
                                raise
                        if hasattr(tracker, "file_path"):
                            tracker.file_path = target_tracker.file_path
                            tracker.file_name = target_tracker.file_name
                        logger.info(
                            f"Linked resource {target_resource.id} to existing Document "
                            f"{existing_by_hash.id} (matched on content hash)"
                        )
                    else:
                        doc, file_path_result = _save_pdf_document_record(
                            session=sess,
                            pdf_storage_manager=pdf_storage_manager,
                            pdf_content=pdf_content,
                            file_hash=target_tracker.file_hash,
                            url=url,
                            title=target_resource.title,
                            username=self.username,
                            password=self.password,
                            collection_id=collection_id,
                            resource_id=target_resource.id,
                            research_id=target_resource.research_id,
                            pdf_storage_mode=pdf_storage_mode,
                        )
                        pdf_document_id = doc.id
                        target_resource.document_id = doc.id
                        if _pdf_storage_refused(
                            file_path_result, pdf_storage_manager.storage_mode
                        ):
                            # The size limit refused the PDF: keep the new
                            # document and its text, awaiting its PDF until
                            # the limit allows it.
                            pdf_refused = True
                            _keep_refused_pdf_text_only(
                                doc,
                                sess,
                                pdf_storage_manager,
                                len(pdf_content),
                            )
                        target_tracker.file_path = (
                            file_path_result if file_path_result else None
                        )
                        target_tracker.file_name = (
                            Path(file_path_result).name
                            if file_path_result
                            and file_path_result != "database"
                            else None
                        )
                        if hasattr(tracker, "file_path"):
                            tracker.file_path = target_tracker.file_path
                            tracker.file_name = target_tracker.file_name

                if pdf_refused:
                    logger.warning(
                        "PDF not stored: it exceeds the configured PDF "
                        f"storage size limit ({max_pdf_size_mb} MB); keeping "
                        f"its text: {redact_url_for_log(resource.url)}"
                    )
                elif pdf_storage_mode == "database":
                    logger.info(
                        f"Successfully stored PDF in database: {redact_url_for_log(resource.url)}"
                    )
                elif pdf_storage_mode == "filesystem":
                    logger.info(f"Successfully downloaded: {tracker.file_path}")
                else:
                    logger.info(
                        f"Successfully extracted text from: {redact_url_for_log(resource.url)}"
                    )

                # Commit the PDF persistence as its own transaction BEFORE
                # the best-effort text save below. Until this point the
                # attempt row, the Document row, the PDF blob and the
                # collection link are all uncommitted. _save_text_with_db
                # rolls its session back before re-raising (issue #3827),
                # so without this commit a DB error in the text save would
                # discard every one of those writes while the caller went on
                # to mark the queue COMPLETED and record a retry success —
                # the user is told success and the database keeps nothing.
                # The caller's later commit stays correct: it opens a fresh
                # transaction for the retry/queue bookkeeping that follows.
                sess.commit()

            # Save the produced text only after the PDF has been stored and
            # committed, in its own transaction.
            if text_details is not None:
                text, extraction_method, extraction_source = text_details
                text_session_cm = (
                    nullcontext(session)
                    if session is not None
                    else get_user_db_session(self.username, self.password)
                )
                try:
                    with text_session_cm as text_sess:
                        # The resource link can change after the PDF commit.
                        # Claim the exact stored snapshot before saving its
                        # extracted text; a no-op write also prevents deletion
                        # or hash replacement until this transaction ends.
                        claimed = (
                            text_sess.query(Document)
                            .filter_by(
                                id=pdf_document_id,
                                document_hash=file_hash_val,
                            )
                            .update(
                                {Document.id: pdf_document_id},
                                synchronize_session=False,
                            )
                        )
                        if claimed != 1:
                            if session is None:
                                text_sess.rollback()
                            logger.info(
                                "Skipping text because its PDF snapshot changed"
                            )
                            return True, None, status_code

                        # Save text to encrypted database
                        self._save_text_with_db(
                            resource=target_resource,
                            text=text,
                            session=text_sess,
                            extraction_method=extraction_method,
                            extraction_source=extraction_source,
                            pdf_document_id=pdf_document_id,
                        )
                        if session is None:
                            text_sess.commit()
                        logger.info(
                            f"Successfully produced and saved text for: {staging_resource_title[:50]}"
                        )
                except Exception as exc:
                    log_exception_type(
                        "Failed to save text after PDF download, but PDF download succeeded",
                        exc,
                    )

            return True, None, status_code

        except Exception as e:
            logger.opt(exception=False).warning(
                "Download failed ({error_type}) for {url}",
                error_type=type(e).__name__,
                url=redact_url_for_log(url),
            )
            # Two distinct outputs:
            #   * server-side diagnostic (persisted in DownloadAttempt.error_message)
            #     — full str(e), with credential scrubbing applied before storage.
            #   * caller-safe skip_reason (the second tuple element, and what
            #     flows into retry-manager / bulk-stream SSE messages) — must
            #     never carry SQL text, target URLs, or file paths. The Zotero
            #     sync_service uses the same shape: look up str(exc) in a fixed
            #     map of author-written constants and otherwise return just the
            #     exception class name. sanitize_error_for_client() is not a
            #     substitute — its docstring says it scrubs credentials only.
            server_error = sanitize_error_for_client(str(e))
            skip_reason = _client_safe_download_message(e)
            result_status = None
            rate_limit_reinstated = (
                bool(held_rate_limit) and pdf_content is None
            )
            if rate_limit_reinstated:
                # A rate-limit result was already held from earlier in the
                # chain, and no PDF content had been obtained, when this
                # exception was raised (by a later downloader or by the
                # failure-recording transaction). The exception does not
                # replace it: the 429 stays the final classification and the
                # tracker keeps its accessible state, exactly as on the
                # non-exception rate-limit path (#7588).
                skip_reason = held_rate_limit[0].skip_reason
                result_status = held_rate_limit[0].status_code
            else:
                tracker.is_accessible = False
            if session is not None:
                safe_rollback(session, "_download_pdf")
                session.add(
                    DownloadAttempt(
                        url_hash=tracker.url_hash,
                        attempt_number=tracker.download_attempts.count() + 1
                        if hasattr(tracker, "download_attempts")
                        else 1,
                        attempted_at=datetime.now(UTC),
                        succeeded=False,
                        error_type=type(e).__name__,
                        error_message=server_error,
                    )
                )
            else:
                try:
                    with get_user_db_session(
                        self.username, self.password
                    ) as err_sess:
                        db_tracker = (
                            err_sess.query(DownloadTracker)
                            .filter_by(url_hash=tracker.url_hash)
                            .first()
                        )
                        if db_tracker and not rate_limit_reinstated:
                            db_tracker.is_accessible = False
                        err_sess.add(
                            DownloadAttempt(
                                url_hash=tracker.url_hash,
                                attempt_number=tracker.download_attempts.count()
                                + 1
                                if hasattr(tracker, "download_attempts")
                                else 1,
                                attempted_at=datetime.now(UTC),
                                succeeded=False,
                                error_type=type(e).__name__,
                                error_message=server_error,
                            )
                        )
                        err_sess.commit()
                except Exception as exc:
                    log_exception_type(
                        "Failed to record failed attempt in database", exc
                    )
            return False, skip_reason, result_status

    def _extract_text_from_pdf(self, pdf_content: bytes) -> Optional[str]:
        """
        Extract text from PDF content using multiple methods for best results.

        pdfplumber is tried first; PyPDF runs only when pdfplumber produced
        no text. What is bounded (``utilities/pdf_extraction_limits``): at
        most ``MAX_PDF_EXTRACTION_PAGES`` pages are extracted per attempt,
        at most ``MAX_PDF_EXTRACTED_CHARS`` characters are returned
        (separators included; the page that crosses the ceiling is
        truncated to the budget left), and ONE CPU-time budget of
        ``MAX_PDF_EXTRACTION_CPU_SECONDS`` covers the whole extraction,
        both attempts included. It is measured with
        ``time.thread_time()``, the calling thread's own CPU time, so
        waiting for the GIL or for other extractions does not spend it.
        If the budget is spent when pdfplumber finishes without text,
        the PyPDF fallback is not started. Every pdfplumber page the walk
        reaches is closed once done with, the break paths included, which
        releases its per-page caches (parsed layout objects, text map).
        Closing does not release what pdfminer decoded and parsed for the
        page -- decoded streams, parsed objects, fonts -- which it keeps
        on the document, nor does pypdf release its own; both walks hand
        each page to a releaser (``utilities/pdf_stream_release``) that
        releases those caches once they reach
        ``MAX_PDF_RETAINED_DECODED_BYTES``, so what the walk holds
        between pages stays near that threshold.

        What is NOT bounded: the budget is checked between pages only,
        so a single page's ``extract_text()`` cannot be interrupted. The
        pdfplumber path walks pages lazily, but the PyPDF fallback may
        eagerly materialize the page tree during ``PdfReader`` construction
        or its first access to ``pages``. That operation can take O(all
        pages) time and memory before the page ceiling can stop the walk.
        What one page decodes and parses is held until the release after it.

        Args:
            pdf_content: Raw PDF bytes

        Returns:
            Extracted text or None if extraction fails
        """
        # One CPU budget for the whole extraction, shared by both attempts:
        # a per-attempt budget would let a document that exhausts the
        # pdfplumber budget buy a second full budget (and a second
        # page-tree parse) in the PyPDF fallback. thread_time() counts
        # only this thread's CPU, not wall time, so GIL waits and other
        # requests' extractions do not spend it (GIL-switch overhead
        # does; the budget is sized for that, see the constant).
        deadline = time.thread_time() + MAX_PDF_EXTRACTION_CPU_SECONDS
        try:
            # First try with pdfplumber (better for complex layouts)
            import io

            from ...utilities.pdf_stream_release import (
                PdfplumberWalkReleaser,
                PypdfWalkReleaser,
            )
            from ...utilities.resource_utils import safe_close
            from ...utilities.pdf_page_walk import iter_pdfplumber_pages

            # Extraction ceilings: the storage cap bounds file SIZE, not
            # extraction COMPLEXITY. These limits bound work and retained
            # text during the lazy page walk. The page ceiling is checked
            # before extracting text from the first page beyond the limit;
            # character and CPU budgets are enforced during the walk.
            pdf_stream = io.BytesIO(pdf_content)
            pdf = pdfplumber.open(pdf_stream)
            try:
                text_parts = []
                extracted_chars = 0
                releaser = PdfplumberWalkReleaser(pdf)
                for page_number, page in enumerate(
                    iter_pdfplumber_pages(pdf, releaser)
                ):
                    # pdfplumber keeps each page's parsed layout objects
                    # and text map cached on the Page until close(): about
                    # 25 MB per dense page, so walking to the page ceiling
                    # without closing would hold gigabytes. Close every
                    # page this loop reaches, on the break paths too.
                    try:
                        if page_number >= MAX_PDF_EXTRACTION_PAGES:
                            logger.warning(
                                "PDF extraction stopped at page ceiling "
                                f"({MAX_PDF_EXTRACTION_PAGES}); document exceeds "
                                "the page ceiling"
                            )
                            break
                        if time.thread_time() >= deadline:
                            logger.warning(
                                "PDF extraction stopped at the CPU-time ceiling "
                                f"({MAX_PDF_EXTRACTION_CPU_SECONDS}s) after "
                                f"{page_number} pages"
                            )
                            break
                        # The count includes the next join separator. Stop
                        # before extraction when no text from this page fits.
                        remaining = MAX_PDF_EXTRACTED_CHARS - extracted_chars
                        if remaining <= 0:
                            logger.warning(
                                "PDF extraction stopped at character ceiling "
                                f"({MAX_PDF_EXTRACTED_CHARS})"
                            )
                            break
                        page_text = page.extract_text()
                        if page_text:
                            if len(page_text) > remaining:
                                # Keep the slice that fits instead of dropping
                                # the tripping page: a first page over the
                                # ceiling must still yield the budget's worth
                                # of text, not nothing.
                                if remaining > 0:
                                    text_parts.append(page_text[:remaining])
                                    extracted_chars += remaining
                                logger.warning(
                                    "PDF extraction truncated at character "
                                    f"ceiling ({MAX_PDF_EXTRACTED_CHARS})"
                                )
                                break
                            # + 2 for the "\n\n" separator the join adds.
                            extracted_chars += len(page_text) + 2
                            text_parts.append(page_text)
                    finally:
                        # safe_close: a failing close() must not mask the
                        # page's own exception or discard text kept so far.
                        safe_close(page, "pdfplumber page")
                        # close() leaves what pdfminer decoded and parsed
                        # for the page cached on the document; release it
                        # once it reaches MAX_PDF_RETAINED_DECODED_BYTES.
                        releaser.after_page()

                if text_parts:
                    return "\n\n".join(text_parts)
            finally:
                safe_close(pdf_stream, "PDF stream")

            # Fallback to PyPDF if pdfplumber produced no text -- but only
            # within the same budget: do not start a second parse once the
            # CPU budget is spent.
            if time.thread_time() >= deadline:
                logger.warning(
                    "PDF extraction CPU-time ceiling "
                    f"({MAX_PDF_EXTRACTION_CPU_SECONDS}s) reached before any "
                    "text was extracted; not starting the PyPDF fallback"
                )
                return None

            reader = PdfReader(io.BytesIO(pdf_content))
            text_parts = []
            extracted_chars = 0
            releaser = PypdfWalkReleaser(reader)
            for page_number, page in enumerate(reader.pages):
                if page_number >= MAX_PDF_EXTRACTION_PAGES:
                    logger.warning(
                        "PDF extraction (PyPDF fallback) stopped at page "
                        f"ceiling ({MAX_PDF_EXTRACTION_PAGES})"
                    )
                    break
                if time.thread_time() >= deadline:
                    logger.warning(
                        "PDF extraction (PyPDF fallback) stopped at the "
                        f"CPU-time ceiling ({MAX_PDF_EXTRACTION_CPU_SECONDS}s) "
                        f"after {page_number} pages"
                    )
                    break
                remaining = MAX_PDF_EXTRACTED_CHARS - extracted_chars
                if remaining <= 0:
                    logger.warning(
                        "PDF extraction (PyPDF fallback) stopped at "
                        f"character ceiling ({MAX_PDF_EXTRACTED_CHARS})"
                    )
                    break
                releaser.before_page()
                text = page.extract_text()
                # pypdf caches what it decodes and parses on the reader too.
                releaser.after_page()
                if text:
                    if len(text) > remaining:
                        if remaining > 0:
                            text_parts.append(text[:remaining])
                            extracted_chars += remaining
                        logger.warning(
                            "PDF extraction (PyPDF fallback) truncated at "
                            f"character ceiling ({MAX_PDF_EXTRACTED_CHARS})"
                        )
                        break
                    # + 2 for the "\n\n" separator the join adds.
                    extracted_chars += len(text) + 2
                    text_parts.append(text)

            if text_parts:
                return "\n\n".join(text_parts)

            logger.warning("No text could be extracted from PDF")
            return None

        except Exception as exc:
            log_exception_type("Failed to extract text from PDF", exc)
            return None

    def download_as_text(self, resource_id: int) -> Tuple[bool, Optional[str]]:
        """
        Download resource and extract text to encrypted database.

        Args:
            resource_id: ID of the resource to download

        Returns:
            Tuple of (success, error_message)
        """
        with get_user_db_session(self.username, self.password) as session:
            # Get the resource
            resource = (
                session.query(ResearchResource)
                .filter_by(id=resource_id)
                .first()
            )
            if not resource:
                return False, "Resource not found"

            # Handle library resources — content already in the database
            # (local-only, no network — skip retry checks)
            if resource.source_type == "library" or (
                resource.url and resource.url.startswith("/library/document/")
            ):
                return self._try_library_text_extraction(session, resource)

            # Try existing text in database
            result = self._try_existing_text(session, resource_id)
            if result is not None:
                return result

            # Try legacy text files on disk
            result = self._try_legacy_text_file(session, resource, resource_id)
            if result is not None:
                return result

            # Canonical arXiv sources, HTML first. This is network-bearing,
            # so it applies the retry gate itself; when that denies it, it
            # only reads the stored PDF and otherwise returns None rather
            # than a verdict, so the local-only strategies below still run.
            result = self._try_arxiv_text_extraction(session, resource)
            if result is not None:
                self._record_retry_attempt(resource, result, session)
                return result

            # Try extracting from existing PDF (pure filesystem, no network)
            result = self._try_existing_pdf_extraction(
                session, resource, resource_id
            )
            if result is not None:
                return result

            # Check retry eligibility before network-dependent extraction
            if self.retry_manager:
                retry_decision = self.retry_manager.should_retry_resource(
                    resource_id
                )
                if not retry_decision.can_retry:
                    # The reason can embed a stored failure_message; rows
                    # written before the domain fix may hold URL userinfo.
                    # The caller still gets the reason unchanged (the
                    # user's own data).
                    logger.info(
                        f"Skipping resource {resource_id}: "
                        f"{failure_reason_for_log(retry_decision.reason)}"
                    )
                    return False, retry_decision.reason

            # Try source-specific direct text extraction
            result = self._try_api_text_extraction(session, resource)
            if result is not None:
                self._record_retry_attempt(resource, result, session)
                return result

            # Fallback: Download PDF and extract
            result = self._fallback_pdf_extraction(session, resource)
            self._record_retry_attempt(resource, result, session)
            return result

    def _record_retry_attempt(
        self, resource, result: Tuple[bool, Optional[str]], session=None
    ) -> None:
        """Record a download attempt with the retry manager."""
        if not self.retry_manager:
            return
        self.retry_manager.record_attempt(
            resource_id=resource.id,
            result=result,
            url=resource.url or "",
            details=result[1]
            or (
                "Successfully extracted text"
                if result[0]
                else "Text extraction failed"
            ),
            session=session,
        )

    def _try_library_text_extraction(
        self, session, resource
    ) -> Tuple[bool, Optional[str]]:
        """Handle library resources — content already exists in local database.

        Returns:
            Tuple of (success, error_message). On success: (True, None).
            On failure: (False, description of what went wrong).
        """
        # 1. Extract document UUID from metadata (authoritative) or URL (fallback)
        doc_id = None
        metadata = (resource.resource_metadata or {}).get("original_data", {})
        if isinstance(metadata, dict):
            meta_inner = metadata.get("metadata", {})
            if isinstance(meta_inner, dict):
                doc_id = meta_inner.get("source_id") or meta_inner.get(
                    "document_id"
                )

        if not doc_id and resource.url:
            # Parse from /library/document/{uuid} or /library/document/{uuid}/pdf
            match = re.match(r"^/library/document/([^/]+)", resource.url)
            if match:
                doc_id = match.group(1)

        if not doc_id:
            return False, "Could not extract library document ID"

        # 2. Query the existing Document by its primary key (UUID string)
        doc = session.query(Document).filter_by(id=doc_id).first()
        if not doc:
            return False, f"Library document {doc_id} not found in database"

        # 3. If text already exists and extraction succeeded, return success
        if _has_reusable_text(doc):
            logger.info(f"Library document {doc_id} already has text content")
            resource.document_id = doc.id
            session.commit()
            return True, None

        # 4. Try extracting text from stored PDF
        pdf_storage_mode = self.settings.get_setting(
            "research_library.pdf_storage_mode", "none"
        )
        pdf_manager = PDFStorageManager(
            library_root=self.library_root,
            storage_mode=pdf_storage_mode,
        )
        pdf_content = pdf_manager.load_pdf(doc, session)

        if not pdf_content:
            return (
                False,
                f"Library document {doc_id} has no text or PDF content",
            )

        text = self._extract_text_from_pdf(pdf_content)
        if not text:
            return (
                False,
                f"Failed to extract text from library document {doc_id} PDF",
            )

        # 5. Update Document directly (don't use _save_text_with_db — it
        # queries by resource_id which mismatches for library docs)
        doc.text_content = text
        doc.character_count = len(text)
        doc.word_count = len(text.split())
        doc.extraction_method = "pdf_extraction"
        doc.extraction_source = "pdfplumber"
        doc.extraction_quality = "medium"
        # A truncated extraction (page, character or CPU-time ceiling) is
        # only signalled by the warnings logged in
        # ``_extract_text_from_pdf``: this row records the text but not
        # that it is partial, so a 700-page book yields the text of its
        # first 500 pages and is indexed as if that were the whole book.
        # The ``Document`` model (``database/models/library.py``) has no
        # free-form metadata column to hang ``extraction_truncated`` on —
        # its JSON columns are ``authors`` and ``tags``, and
        # ``document_metadata`` belongs to ``DocumentChunk``. Persisting
        # the flag needs a schema change (a nullable ``Boolean`` beside
        # ``has_formatting_issues``/``has_encoding_issues``, or a JSON
        # ``extraction_metadata``), which is left to the owner. It would
        # be set here and wherever else ``_extract_text_from_pdf`` output
        # is saved: the three ``_save_text_with_db`` calls fed by it (in
        # ``_download_pdf``, ``_try_existing_pdf_extraction`` and
        # ``_fallback_pdf_extraction``), plus the ``_save_text_with_db``
        # calls in ``_try_arxiv_text_extraction``,
        # ``_text_from_cached_pdf`` (the stored-PDF read on a retry or
        # egress denial) and ``_try_api_text_extraction`` for downloader
        # text (arXiv PDF text among it) that
        # ``BaseDownloader.extract_text_from_pdf`` bounds the same way.

        resource.document_id = doc.id
        session.commit()

        logger.info(
            f"Extracted text from library document {doc_id} "
            f"({doc.word_count} words)"
        )
        return True, None

    def _try_existing_text(
        self, session, resource_id: int
    ) -> Optional[Tuple[bool, Optional[str]]]:
        """Check if text already exists in database (in Document.text_content)."""
        resource = session.get(ResearchResource, resource_id)
        existing_doc = (
            get_document_for_resource(session, resource)
            if resource is not None
            else session.query(Document)
            .filter_by(resource_id=resource_id)
            .first()
        )

        # URL/tracker reuse is an auto-persist behaviour: with the setting
        # off, main's flow (linked document only, then canonical steps)
        # is preserved exactly.
        auto_persist_on = _is_auto_persist_enabled(
            getattr(self, "_settings_snapshot", None),
            getattr(self, "username", None),
        )
        if auto_persist_on and not existing_doc and resource and resource.url:
            tracker = find_tracker_by_url(session, resource.url)
            if tracker and getattr(tracker, "is_downloaded", False) is True:
                tracked_doc = None
                if tracker.file_hash:
                    tracked_doc = (
                        session.query(Document)
                        .filter_by(
                            document_hash=tracker.file_hash,
                            status=DocumentStatus.COMPLETED,
                        )
                        .first()
                    )
                if not tracked_doc and (
                    isinstance(getattr(tracker, "first_resource_id", None), int)
                    and tracker.first_resource_id != resource.id
                ):
                    first_resource = session.get(
                        ResearchResource, tracker.first_resource_id
                    )
                    tracked_doc = (
                        get_document_for_resource(session, first_resource)
                        if first_resource
                        else None
                    )
                if not tracked_doc:
                    tracked_doc = (
                        session.query(Document)
                        .filter_by(
                            original_url=resource.url,
                            status=DocumentStatus.COMPLETED,
                        )
                        .order_by(Document.created_at.desc())
                        .first()
                    )
                if tracked_doc and _has_reusable_text(tracked_doc):
                    resource.document_id = tracked_doc.id
                    if not tracker.first_resource_id:
                        tracker.first_resource_id = resource.id
                    session.commit()
                    logger.info(
                        f"Reused existing text via tracker for resource_id={resource_id}"
                    )
                    return True, None

            existing_by_url = (
                session.query(Document)
                .filter_by(
                    original_url=resource.url,
                    status=DocumentStatus.COMPLETED,
                )
                .order_by(Document.created_at.desc())
                .first()
            )
            if existing_by_url and _has_reusable_text(existing_by_url):
                resource.document_id = existing_by_url.id
                session.commit()
                logger.info(
                    f"Reused existing text via on-the-spot document for resource_id={resource_id}"
                )
                return True, None
        if not existing_doc:
            return None

        # Check if text content exists and extraction was successful
        if _has_reusable_text(existing_doc):
            logger.info(
                f"Text content already exists in Document for resource_id={resource_id}, extraction_method={existing_doc.extraction_method}"
            )
            return True, None

        # No text content or failed extraction
        logger.debug(
            f"Document exists but no valid text content: resource_id={resource_id}, extraction_method={existing_doc.extraction_method}"
        )
        return None  # Fall through to re-extraction

    def _try_legacy_text_file(
        self, session, resource, resource_id: int
    ) -> Optional[Tuple[bool, Optional[str]]]:
        """Check for legacy text files on disk."""
        txt_path = Path(self.library_root) / "txt"
        existing_files = (
            # nosemgrep: semgrep.rules.path-traversal-risk, reason: Per-user library text directory and integer resource ID from the user DB; this only enumerates legacy names
            list(txt_path.glob(f"*_{resource_id}.txt"))
            if txt_path.exists()
            else []
        )

        if not existing_files:
            return None

        logger.info(f"Text file already exists on disk: {existing_files[0]}")
        self._create_text_document_record(
            session,
            resource,
            existing_files[0],
            extraction_method="unknown",
            extraction_source="legacy_file",
        )
        session.commit()
        return True, None

    def _try_arxiv_text_extraction(
        self, session: Session, resource
    ) -> Optional[Tuple[bool, Optional[str]]]:
        """Produce text from canonical arXiv sources, or None to fall through.

        None means "this path does not apply", never "this resource has no
        text": the caller must go on to the local-only strategies. A URL that
        names no paper returns None. So does an exhausted retry budget once
        the stored PDF has been tried, because this path is the only
        network-bearing one that runs ahead of _try_existing_pdf_extraction.
        Deciding the whole call here would have denied a resource with a
        stored PDF the local read it used to get (download_as_text applies
        the same gate itself, after that read, so the budget still holds).

        Both network denials read the stored PDF first, entirely locally,
        through _text_from_cached_pdf. That reader loads the PDF through
        PDFStorageManager, so it covers the default database storage, where
        the document has no file path and _try_existing_pdf_extraction
        finds nothing. Text produced that way satisfies the call. Otherwise
        (no stored PDF, no text, or a failed read or save) a retry denial
        returns None and an egress denial stands with its own reason.
        """
        url = resource.url or ""
        if not is_arxiv_paper_url(url):
            return None

        # Retry gate before any network access. A denial still reads the
        # stored PDF, which makes no network request and so is outside what
        # the retry gate limits. Only a success is returned from here;
        # anything else returns None rather than a verdict, so the
        # local-only strategies below still run and download_as_text's own
        # gate, after them, returns the denial (so the budget still holds).
        if self.retry_manager:
            retry_decision = self.retry_manager.should_retry_resource(
                resource.id
            )
            if not retry_decision.can_retry:
                # The reason can embed a stored failure_message (see
                # download_as_text).
                logger.info(
                    "Skipping canonical arXiv text for resource {}: {}",
                    resource.id,
                    failure_reason_for_log(retry_decision.reason),
                )
                try:
                    cached_outcome = self._text_from_cached_pdf(
                        session, resource
                    )
                except Exception as exc:
                    safe_rollback(session, "_try_arxiv_text_extraction")
                    log_exception_type(
                        "Stored arXiv PDF text extraction failed", exc
                    )
                    return None
                if cached_outcome is not None and cached_outcome[0]:
                    return cached_outcome
                return None

        try:
            downloader = self._get_downloader(url)
            if not isinstance(downloader, ArxivDownloader):
                return False, "No arXiv downloader available"

            allowed, reason = self._check_url_against_policy(url)
            if not allowed:
                # Egress policy refuses the network, but a stored PDF can
                # still produce text entirely locally before the denial
                # stands. Only a success replaces the denial: a failed
                # read or save must not bury the egress_policy_denied
                # reason the caller records.
                try:
                    cached_outcome = self._text_from_cached_pdf(
                        session, resource
                    )
                except Exception as exc:
                    safe_rollback(session, "_try_arxiv_text_extraction")
                    log_exception_type(
                        "Stored arXiv PDF text extraction failed", exc
                    )
                    cached_outcome = None
                if cached_outcome is not None and cached_outcome[0]:
                    return cached_outcome
                return False, f"egress_policy_denied:{reason}"

            pdf_content: bytes | None = None
            pdf_document_id: str | None = None
            cached_pdf = self._load_cached_arxiv_pdf(session, resource)
            if cached_pdf is not None:
                pdf_content, pdf_document_id = cached_pdf

            if pdf_content is None:
                arxiv_text = downloader.download_text_with_source(url)
            else:
                arxiv_text = downloader.download_text_with_source(
                    url,
                    pdf_content=pdf_content,
                )
        except Exception as exc:
            safe_rollback(session, "_try_arxiv_text_extraction")
            logger.opt(exception=False).warning(
                "Canonical arXiv text extraction failed ({error_type})",
                error_type=type(exc).__name__,
            )
            # Caller-visible: fixed token only. sanitize_error_for_client()
            # scrubs credentials but leaves SQL text / URLs / paths intact,
            # and this string flows into download_as_text's return and the
            # bulk-stream SSE `error` field — route through the same mapping
            # as _download_pdf (PR #6564 follow-up).
            safe_error = _client_safe_download_message(exc)
            return False, safe_error

        if arxiv_text is None:
            return False, "Could not retrieve arXiv text"

        extraction_method, extraction_source = _arxiv_text_provenance(
            arxiv_text.source
        )
        try:
            _ = self._save_text_with_db(
                resource,
                arxiv_text.text,
                session,
                extraction_method=extraction_method,
                extraction_source=extraction_source,
                pdf_document_id=pdf_document_id,
            )
            session.commit()
        except Exception as exc:
            safe_rollback(session, "_try_arxiv_text_extraction")
            log_exception_type("Failed to save canonical arXiv text", exc)
            # Same caller-safe mapping as above — never str(exc) (see comment
            # on the fetch-failure path).
            safe_error = _client_safe_download_message(exc)
            return False, safe_error

        return True, None

    def _load_cached_arxiv_pdf(
        self, session: Session, resource
    ) -> Tuple[bytes, str] | None:
        return self._load_cached_pdf(session, resource)

    def _load_cached_pdf(
        self, session: Session, resource
    ) -> Tuple[bytes, str] | None:
        """Return (pdf_bytes, document_id) for a stored completed PDF."""
        pdf_document = get_document_for_resource(session, resource)
        if (
            pdf_document is None
            or getattr(pdf_document, "status", None)
            not in {DocumentStatus.COMPLETED, DocumentStatus.COMPLETED.value}
            or getattr(pdf_document, "file_type", None) != "pdf"
        ):
            return None
        settings = self.settings
        if settings is None:
            return None
        pdf_storage_mode = resolve_pdf_storage_mode(
            settings.get_setting("research_library.pdf_storage_mode", "none")
        )
        pdf_manager = PDFStorageManager(
            library_root=Path(self.library_root),
            storage_mode=pdf_storage_mode,
        )
        pdf_content = pdf_manager.load_pdf(pdf_document, session)
        if pdf_content is None:
            return None
        return pdf_content, getattr(pdf_document, "id")

    def _text_from_cached_pdf(
        self, session: Session, resource
    ) -> Optional[Tuple[bool, Optional[str]]]:
        """Produce text from the stored PDF without any network access.

        Used when the egress policy or the retry gate denies the network
        but the PDF downloaded by an earlier attempt is still on hand. Returns None when there is
        no usable stored PDF or it yields no text, so the caller's verdict
        stands; a completed extraction returns (True, None).
        """
        cached_pdf = self._load_cached_arxiv_pdf(session, resource)
        if cached_pdf is None:
            return None
        pdf_content, pdf_document_id = cached_pdf
        text = ArxivDownloader.extract_text_from_pdf(pdf_content)
        if not text:
            return None
        # Same attribution download_text_with_source gives PDF text saved
        # with these labels. Callers only reach here for an arXiv paper
        # URL, so the id is always present.
        arxiv_id = extract_arxiv_id(resource.url or "")
        if arxiv_id:
            text = attribute_arxiv_text(text, arxiv_id)
        try:
            _ = self._save_text_with_db(
                resource,
                text,
                session,
                extraction_method="pdf_extraction",
                extraction_source="local_pdf",
                pdf_document_id=pdf_document_id,
            )
            session.commit()
        except Exception as exc:
            safe_rollback(session, "_try_arxiv_text_extraction")
            log_exception_type("Failed to save local arXiv PDF text", exc)
            safe_error = _client_safe_download_message(exc)
            return False, safe_error
        return True, None

    def _try_existing_pdf_extraction(
        self, session, resource, resource_id: int
    ) -> Optional[Tuple[bool, Optional[str]]]:
        """Extract text from the PDF file of the resource's document.

        Reads the file that the completed document's own ``file_path``
        names (filesystem storage), without a request, and saves its text
        into that document. A PDF stored in the database is not read here
        (``_text_from_cached_pdf`` reads one for an arXiv paper). Returns
        None, so that the caller tries its other sources, when the resource
        has no document, the document is not completed or names no file,
        its file is missing or yields no text, or the read or the save
        fails.
        """
        # An older retained snapshot may still name this resource as its
        # owner after the resource links to a newer document.
        pdf_document = get_document_for_resource(session, resource)

        # A loaded status is a DocumentStatus member, which never equals
        # the string "completed".
        if not pdf_document or pdf_document.status != DocumentStatus.COMPLETED:
            return None

        # Validate path to prevent path traversal attacks. Resolve against the
        # per-user root only; shared legacy filenames cannot prove ownership. Pass
        # self.settings so storage_path/shared_library come from this user's
        # captured manager — the ambient one is db-less in background/scheduler
        # threads (this runs under the document scheduler) and would resolve
        # the default root, missing a user's previously saved storage_path. Use
        # getattr: __init__ always sets self.settings in production (incl.
        # scheduler threads), but test doubles that construct a
        # DownloadService via __new__/partial mocks may not — fall back to
        # None (the ambient get_settings_manager()) rather than raising
        # AttributeError.
        if (
            not pdf_document.file_path
            or pdf_document.file_path in FILE_PATH_SENTINELS
        ):
            return None
        pdf_path = get_absolute_path_from_settings(
            pdf_document.file_path,
            self.username,
            settings_manager=getattr(self, "settings", None),
        )
        if not pdf_path or not pdf_path.is_file():
            return None

        logger.info(f"Found existing PDF, extracting text from: {pdf_path}")
        try:
            # nosemgrep: semgrep.rules.path-traversal-risk, reason: get_absolute_path_from_settings requires user context and confines this read to the selected library root; missing PDFs never fall back to another root
            with open(pdf_path, "rb") as f:
                pdf_content = f.read()
            text = self._extract_text_from_pdf(pdf_content)

            if not text:
                return None

            self._save_text_with_db(
                resource,
                text,
                session,
                extraction_method="pdf_extraction",
                extraction_source="pdfplumber",
                pdf_document_id=pdf_document.id,
            )
            session.commit()
            return True, None

        except Exception as exc:
            log_exception_type("Failed to extract text from existing PDF", exc)
            # The commit above can fail and poison the shared session.
            # download_as_text swallows this None and may then return cleanly
            # (e.g. a non-retry decision), so the with-block exits without an
            # exception — get_user_db_session's rollback never fires. Recover
            # the session here or the next operation on this thread cascades.
            safe_rollback(session, "_try_existing_pdf_extraction")
            return None  # Fall through to other methods

    def _download_pdf_with_fallback(
        self,
        url: str,
        held_rate_limit: Optional[List[DownloadResult]] = None,
    ) -> Tuple[DownloadResult, Optional[BaseDownloader]]:
        """Try matching PDF downloaders, preserving retryable failures.

        A 429 does not stop the chain: rate limits are transient and
        host-scoped, so a downloader for a different host can still succeed.
        The first rate-limit result is held aside instead — if the chain
        exhausts without a success it is the final classification, and a
        later fallback failure never replaces it (#7588). When the caller
        passes ``held_rate_limit``, the held result is also appended there as
        soon as it is seen, so the caller can still reinstate it if a later
        downloader raises.
        """
        selected_downloader = None
        best_permanent = None
        best_temporary = None
        rate_limited = None
        classifier = FailureClassifier()

        for downloader in self.downloaders:
            if not downloader.can_handle(url):
                continue
            logger.info(
                "Using {} for {}",
                downloader.__class__.__name__,
                redact_url_for_log(url),
            )
            result = downloader.download_with_result(url, ContentType.PDF)
            selected_downloader = downloader
            if result.is_success and result.content:
                return result, downloader
            if result.status_code == 429:
                if rate_limited is None:
                    rate_limited = DownloadResult(
                        skip_reason=result.skip_reason or "Rate limited (429)",
                        status_code=result.status_code,
                    )
                    if held_rate_limit is not None:
                        held_rate_limit.append(rate_limited)
                if result.skip_reason:
                    logger.info(
                        "Download skipped: {}",
                        failure_reason_for_log(result.skip_reason),
                    )
            elif result.skip_reason:
                failure = classifier.classify_failure(
                    "download_failed",
                    status_code=result.status_code,
                    url=url,
                    details=result.skip_reason,
                )
                if failure.is_permanent():
                    best_permanent = result
                else:
                    best_temporary = result
                logger.info(
                    "Download skipped: {}",
                    failure_reason_for_log(result.skip_reason),
                )
            # arXiv handles its own fallbacks; GenericDownloader is last.
            if isinstance(downloader, (ArxivDownloader, GenericDownloader)):
                break

        if selected_downloader is None:
            logger.error("No downloader found for {}", redact_url_for_log(url))
            return DownloadResult(
                skip_reason="No compatible downloader available"
            ), None
        return (
            rate_limited
            or best_temporary
            or best_permanent
            or DownloadResult(),
            selected_downloader,
        )

    def _try_api_text_extraction(
        self, session, resource
    ) -> Optional[Tuple[bool, Optional[str]]]:
        """Try source-specific direct text extraction."""
        logger.info(
            f"Attempting direct API text extraction from: {redact_url_for_log(resource.url)}"
        )

        downloader = self._get_downloader(resource.url)
        if not downloader:
            return None

        # Egress policy gate. Return a non-None tuple (not None) on denial
        # so download_as_text hard-stops here instead of falling through
        # to _fallback_pdf_extraction and firing a second request.
        allowed, reason = self._check_url_against_policy(resource.url)
        if not allowed:
            logger.warning(
                f"Skipping API text extraction for {redact_url_for_log(resource.url)}: refused "
                f"by egress policy ({reason})"
            )
            return False, f"egress_policy_denied:{reason}"

        if isinstance(downloader, OpenAccessDOIDownloader):
            # DOI text comes from a PDF. Resolve it once in the PDF chain so
            # an inconclusive lookup can still reach the generic fallback.
            return None

        if isinstance(downloader, ArxivDownloader):
            arxiv_text = downloader.download_text_with_source(resource.url)
            if arxiv_text is None:
                return None
            text = arxiv_text.text
            extraction_method, extraction_source = _arxiv_text_provenance(
                arxiv_text.source
            )
        else:
            result = downloader.download_with_result(
                resource.url, ContentType.TEXT
            )
            if not result.is_success or not result.content:
                return None

            text = (
                result.content.decode("utf-8", errors="ignore")
                if isinstance(result.content, bytes)
                else result.content
            )
            extraction_method = "native_api"
            extraction_source = (
                "pubmed_api"
                if isinstance(downloader, PubMedDownloader)
                else "unknown"
            )

        try:
            self._save_text_with_db(
                resource,
                text,
                session,
                extraction_method=extraction_method,
                extraction_source=extraction_source,
            )
            session.commit()
            logger.info(
                f"✓ SUCCESS: Got text from {extraction_source.upper()} for '{resource.title[:50]}...'"
            )
            return True, None
        except Exception as e:
            # Roll back FIRST: the failed commit/flush poisoned the shared
            # session, and even dereferencing resource.id in the log line below
            # can trigger a refresh that re-raises PendingRollbackError.
            # download_as_text reuses this session (_record_retry_attempt)
            # after we return, so it must be clean.
            safe_rollback(session, "_try_api_text_extraction")
            log_exception_type("Failed to save text for resource", e)
            # Caller-visible: fixed token only (CWE-209). sanitize_error_for_client
            # leaves SQL text intact and this flows into download_as_text's
            # return and the bulk-stream SSE `error` field.
            safe_error = _client_safe_download_message(e)
            return False, safe_error

    def _fallback_pdf_extraction(
        self, session, resource
    ) -> Tuple[bool, Optional[str]]:
        """Fallback: Download PDF to memory and extract text."""
        logger.info(
            f"Direct text extraction unavailable; trying in-memory PDF for: {redact_url_for_log(resource.url)}"
        )

        # Egress policy gate at the network-fire point.
        allowed, reason = self._check_url_against_policy(resource.url)
        if not allowed:
            error_msg = f"egress_policy_denied:{reason}"
            logger.warning(
                f"Skipping fallback PDF download for {redact_url_for_log(resource.url)}: "
                f"refused by egress policy ({reason})"
            )
            self._record_failed_text_extraction(
                session, resource, error=error_msg
            )
            session.commit()
            return False, error_msg

        result, downloader = self._download_pdf_with_fallback(resource.url)
        if downloader is None:
            error_msg = "No compatible downloader found"
            self._record_failed_text_extraction(
                session, resource, error=error_msg
            )
            session.commit()
            return False, error_msg

        if not result.is_success or not result.content:
            error_msg = result.skip_reason or "Failed to download PDF"
            logger.warning(
                f"✗ FAILED: Could not download PDF for '{resource.title[:50]}...' | Error: {failure_reason_for_log(error_msg)}"
            )
            self._record_failed_text_extraction(
                session, resource, error=f"PDF download failed: {error_msg}"
            )
            session.commit()
            return False, f"PDF extraction failed: {error_msg}"

        # Extract text from PDF
        text = self._extract_text_from_pdf(result.content)
        if not text:
            error_msg = "PDF text extraction returned empty text"
            logger.warning(
                f"Failed to extract text from PDF for: {redact_url_for_log(resource.url)}"
            )
            self._record_failed_text_extraction(
                session, resource, error=error_msg
            )
            session.commit()
            return False, error_msg

        try:
            self._save_text_with_db(
                resource,
                text,
                session,
                extraction_method="pdf_extraction",
                extraction_source="pdfplumber_fallback",
            )
            session.commit()
            logger.info(
                f"✓ SUCCESS: Extracted text from '{resource.title[:50]}...'"
            )
            return True, None
        except Exception as e:
            # Roll back FIRST (see _try_api_text_extraction): dereferencing
            # resource.id while the session is poisoned would itself re-raise
            # PendingRollbackError. download_as_text reuses this session after
            # we return.
            safe_rollback(session, "_fallback_pdf_extraction")
            log_exception_type("Failed to save text for resource", e)
            # Caller-visible: fixed token only (CWE-209) — same mapping as
            # _download_pdf; never str(e).
            safe_error = _client_safe_download_message(e)
            return False, safe_error

    def persist_pdf_on_the_spot(
        self,
        url: str,
        pdf_content: bytes,
        text: Optional[str] = None,
        title: Optional[str] = None,
        extra_urls: Optional[List[str]] = None,
        collection_id: Optional[str] = None,
        resource_id: Optional[int] = None,
        check_setting: bool = True,
    ) -> Optional[str]:
        """Persist a downloaded PDF directly into the Library database and storage."""
        return persist_downloaded_pdf_on_the_spot(
            url=url,
            pdf_content=pdf_content,
            text=text,
            title=title,
            username=self.username,
            password=self.password,
            settings_snapshot=self._settings_snapshot,
            library_root=self.library_root,
            extra_urls=extra_urls,
            collection_id=collection_id,
            resource_id=resource_id,
            check_setting=check_setting,
        )

    def _get_downloader(self, url: str):
        """
        Get the appropriate downloader for a URL.

        Args:
            url: The URL to download from

        Returns:
            The appropriate downloader instance or None
        """
        for downloader in self.downloaders:
            if downloader.can_handle(url):
                return downloader
        return None

    def _download_generic(self, url: str) -> Optional[bytes]:
        """Generic PDF download method."""
        try:
            headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
            }
            response = safe_get(
                url, headers=headers, timeout=30, allow_redirects=True
            )
            response.raise_for_status()

            # Verify it's a PDF
            content_type = response.headers.get("Content-Type", "")
            body = response.content
            if not BaseDownloader._has_pdf_signature(body):
                logger.warning(f"Response is not a PDF: {content_type}")
                return None

            return body

        except Exception as e:
            # raise_for_status()/SafeSession texts embed the raw URL —
            # log the exception type and the redacted URL only.
            logger.warning(
                f"Generic download failed ({type(e).__name__}) for "
                f"{redact_url_for_log(url)}"
            )
            return None

    def _download_arxiv(self, url: str) -> Optional[bytes]:
        """Download from arXiv."""
        try:
            # Convert abstract URL to PDF URL
            pdf_url = url.replace("abs", "pdf")
            if not pdf_url.endswith(".pdf"):
                pdf_url += ".pdf"

            return self._download_generic(pdf_url)
        except Exception as e:
            logger.warning(
                f"arXiv download failed ({type(e).__name__}) for "
                f"{redact_url_for_log(url)}"
            )
            return None

    def _try_europe_pmc(self, pmid: str) -> Optional[bytes]:
        """Try downloading from Europe PMC which often has better PDF availability."""
        try:
            # Europe PMC API is more reliable for PDFs
            # Check if PDF is available
            api_url = f"https://www.ebi.ac.uk/europepmc/webservices/rest/search?query=EXT_ID:{pmid}&format=json"
            response = safe_get(api_url, timeout=10)

            if response.status_code == 200:
                data = response.json()
                results = data.get("resultList", {}).get("result", [])

                if results:
                    article = results[0]
                    # Check if article has open access PDF
                    if (
                        article.get("isOpenAccess") == "Y"
                        and article.get("hasPDF") == "Y"
                    ):
                        pmcid = article.get("pmcid")
                        if pmcid:
                            # Europe PMC PDF URL
                            pdf_url = f"https://europepmc.org/backend/ptpmcrender.fcgi?accid={pmcid}&blobtype=pdf"
                            logger.info(
                                f"Found Europe PMC PDF for PMID {pmid}: {pmcid}"
                            )

                            headers = {
                                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
                            }

                            pdf_response = safe_get(
                                pdf_url,
                                headers=headers,
                                timeout=30,
                                allow_redirects=True,
                            )
                            if pdf_response.status_code == 200:
                                body = pdf_response.content
                                if BaseDownloader._has_pdf_signature(body):
                                    return body
        except Exception as e:
            logger.debug(f"Europe PMC download failed ({type(e).__name__})")

        return None

    def _download_pubmed(self, url: str) -> Optional[bytes]:
        """Download from PubMed/PubMed Central with rate limiting."""
        try:
            # Apply rate limiting for PubMed requests
            current_time = time.time()
            time_since_last = current_time - self._last_pubmed_request
            if time_since_last < self._pubmed_delay:
                sleep_time = self._pubmed_delay - time_since_last
                logger.debug(
                    f"Rate limiting: sleeping {sleep_time:.2f}s before PubMed request"
                )
                time.sleep(sleep_time)
            self._last_pubmed_request = time.time()

            # If it's already a PMC article, download directly
            if "/articles/PMC" in url:
                pmc_match = re.search(r"(PMC\d+)", url)
                if pmc_match:
                    pmc_id = pmc_match.group(1)

                    # Try Europe PMC (more reliable)
                    europe_url = f"https://europepmc.org/backend/ptpmcrender.fcgi?accid={pmc_id}&blobtype=pdf"
                    headers = {
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
                    }

                    try:
                        response = safe_get(
                            europe_url,
                            headers=headers,
                            timeout=30,
                            allow_redirects=True,
                        )
                        if response.status_code == 200:
                            body = response.content
                            if BaseDownloader._has_pdf_signature(body):
                                logger.info(
                                    f"Downloaded PDF via Europe PMC for {pmc_id}"
                                )
                                return body
                    except Exception as e:
                        logger.debug(
                            f"Direct Europe PMC download failed ({type(e).__name__})"
                        )
                        return None

            # If it's a regular PubMed URL, try to find PMC version
            elif urlparse(url).hostname == "pubmed.ncbi.nlm.nih.gov":
                # Extract PMID from URL
                pmid_match = re.search(r"/(\d+)/?", url)
                if pmid_match:
                    pmid = pmid_match.group(1)
                    logger.info(f"Attempting to download PDF for PMID: {pmid}")

                    # Try Europe PMC first (more reliable)
                    pdf_content = self._try_europe_pmc(pmid)
                    if pdf_content:
                        return pdf_content

                    # First try using NCBI E-utilities API to find PMC ID
                    try:
                        # Use elink to convert PMID to PMCID
                        elink_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi"
                        params = {
                            "dbfrom": "pubmed",
                            "db": "pmc",
                            "id": pmid,
                            "retmode": "json",
                        }

                        api_response = safe_get(
                            elink_url, params=params, timeout=10
                        )
                        if api_response.status_code == 200:
                            data = api_response.json()
                            # Parse the response to find PMC ID
                            link_sets = data.get("linksets", [])
                            if link_sets and "linksetdbs" in link_sets[0]:
                                for linksetdb in link_sets[0]["linksetdbs"]:
                                    if linksetdb.get(
                                        "dbto"
                                    ) == "pmc" and linksetdb.get("links"):
                                        pmc_id_num = linksetdb["links"][0]
                                        # Now fetch PMC details to get the correct PMC ID format
                                        esummary_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
                                        summary_params = {
                                            "db": "pmc",
                                            "id": pmc_id_num,
                                            "retmode": "json",
                                        }
                                        summary_response = safe_get(
                                            esummary_url,
                                            params=summary_params,
                                            timeout=10,
                                        )
                                        if summary_response.status_code == 200:
                                            summary_data = (
                                                summary_response.json()
                                            )
                                            result = summary_data.get(
                                                "result", {}
                                            ).get(str(pmc_id_num), {})
                                            if result:
                                                # PMC IDs in the API don't have the "PMC" prefix
                                                pmc_id = f"PMC{pmc_id_num}"
                                                logger.info(
                                                    f"Found PMC ID via API: {pmc_id} for PMID: {pmid}"
                                                )

                                                # Try Europe PMC with the PMC ID
                                                europe_url = f"https://europepmc.org/backend/ptpmcrender.fcgi?accid={pmc_id}&blobtype=pdf"

                                                time.sleep(self._pubmed_delay)

                                                headers = {
                                                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
                                                }

                                                try:
                                                    response = safe_get(
                                                        europe_url,
                                                        headers=headers,
                                                        timeout=30,
                                                        allow_redirects=True,
                                                    )
                                                    if (
                                                        response.status_code
                                                        == 200
                                                    ):
                                                        body = response.content
                                                        if BaseDownloader._has_pdf_signature(
                                                            body
                                                        ):
                                                            logger.info(
                                                                f"Downloaded PDF via Europe PMC for {pmc_id}"
                                                            )
                                                            return body
                                                except Exception as e:
                                                    logger.debug(
                                                        f"Europe PMC download with PMC ID failed ({type(e).__name__})"
                                                    )
                    except Exception as e:
                        logger.debug(
                            f"API lookup failed, trying webpage scraping ({type(e).__name__})"
                        )

                    # Fallback to webpage scraping if API fails
                    try:
                        headers = {
                            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
                        }
                        response = safe_get(url, headers=headers, timeout=10)
                        if response.status_code == 200:
                            # Look for PMC ID in the page
                            pmc_match = re.search(r"PMC\d+", response.text)
                            if pmc_match:
                                pmc_id = pmc_match.group(0)
                                logger.info(
                                    f"Found PMC ID via webpage: {pmc_id} for PMID: {pmid}"
                                )

                                # Add delay before downloading PDF
                                time.sleep(self._pubmed_delay)

                                # Try Europe PMC with the PMC ID (more reliable)
                                europe_url = f"https://europepmc.org/backend/ptpmcrender.fcgi?accid={pmc_id}&blobtype=pdf"
                                headers = {
                                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
                                }

                                try:
                                    response = safe_get(
                                        europe_url,
                                        headers=headers,
                                        timeout=30,
                                        allow_redirects=True,
                                    )
                                    if response.status_code == 200:
                                        body = response.content
                                        if BaseDownloader._has_pdf_signature(
                                            body
                                        ):
                                            logger.info(
                                                f"Downloaded PDF via Europe PMC for {pmc_id}"
                                            )
                                            return body
                                except Exception as e:
                                    logger.debug(
                                        f"Europe PMC download failed ({type(e).__name__})"
                                    )
                            else:
                                logger.info(
                                    f"No PMC version found for PMID: {pmid}"
                                )
                    except requests.exceptions.HTTPError as e:
                        if e.response.status_code == 429:
                            logger.warning(
                                "Rate limited by PubMed, increasing delay"
                            )
                            self._pubmed_delay = min(
                                self._pubmed_delay * 2, 5.0
                            )  # Max 5 seconds
                        raise
                    except Exception as e:
                        logger.debug(
                            f"Could not check for PMC version "
                            f"({type(e).__name__})"
                        )

            return self._download_generic(url)
        except Exception as e:
            # A re-raised HTTPError's text embeds the raw resource URL.
            logger.warning(
                f"PubMed download failed ({type(e).__name__}) for "
                f"{redact_url_for_log(url)}"
            )
            return None

    def _download_semantic_scholar(self, url: str) -> Optional[bytes]:
        """Download from Semantic Scholar."""
        # Semantic Scholar doesn't host PDFs directly
        # Would need to extract actual PDF URL from page
        return None

    def _download_biorxiv(self, url: str) -> Optional[bytes]:
        """Download from bioRxiv."""
        try:
            # Convert to PDF URL
            pdf_url = url.replace(".org/", ".org/content/")
            pdf_url = re.sub(r"v\d+$", "", pdf_url)  # Remove version
            pdf_url += ".full.pdf"

            return self._download_generic(pdf_url)
        except Exception as e:
            logger.warning(
                f"bioRxiv download failed ({type(e).__name__}) for "
                f"{redact_url_for_log(url)}"
            )
            return None

    def _save_text_with_db(
        self,
        resource: ResearchResource,
        text: str,
        session: Session,
        extraction_method: str,
        extraction_source: str,
        pdf_document_id: str | None = None,
    ) -> Optional[str]:
        """
        Save extracted text to encrypted database.

        Args:
            resource: The research resource
            text: Extracted text content
            session: Database session
            extraction_method: How the text was extracted
            extraction_source: Specific tool/API used
            pdf_document_id: ID of PDF document if extracted from PDF

        Returns:
            None (previously returned text file path, now removed)
        """
        try:
            if not text.strip():
                return None

            # Calculate text metadata for database
            word_count = len(text.split())
            character_count = len(text)
            extraction_quality = _text_extraction_quality(
                extraction_method, extraction_source
            )

            # Find the document by pdf_document_id or resource_id. Both reads
            # touch scalar columns (`document_id`, `id`) that are eagerly
            # loaded when `resource` was queried, so passing a detached
            # `resource` from a snapshot block (post-staging-commit) is safe
            # and does not lazy-refresh through this session. PR #6564 P2.
            doc = None
            if pdf_document_id:
                doc = (
                    session.query(Document)
                    .filter_by(id=pdf_document_id)
                    .first()
                )
            else:
                doc = get_document_for_resource(session, resource)

            if doc and (
                doc.status == DocumentStatus.FAILED
                or doc.resource_id != resource.id
            ):
                text_hash = hashlib.sha256(text.encode()).hexdigest()
                if (
                    doc.status == DocumentStatus.FAILED
                    and pdf_document_id is None
                ):
                    # Text-only retries can resolve a different hash owner;
                    # an explicit PDF target must keep its snapshot identity.
                    owner = (
                        session.query(Document)
                        .filter_by(document_hash=text_hash)
                        .first()
                    )
                    if owner and owner.id != doc.id:
                        _link_retry_to_canonical(session, resource, doc, owner)
                        doc = owner
                if (
                    doc.document_hash == text_hash
                    and doc.status
                    in (DocumentStatus.COMPLETED, DocumentStatus.FAILED)
                    and doc.text_content
                ):
                    # The hash can identify original bytes, not extracted
                    # text. Reusing those bytes must preserve the owner's
                    # extraction, provenance and existing collection indexes.
                    if doc.status == DocumentStatus.FAILED:
                        doc.status = DocumentStatus.COMPLETED
                        doc.error_message = None
                        doc.processed_at = datetime.now(UTC)
                    return None

            if doc:
                old_text = doc.text_content or ""
                if old_text.strip():
                    # Duplicate PDFs share one extraction. Automatically replace
                    # it only with nondecreasing quality AND text coverage. A
                    # native API abstract must not displace a longer full paper;
                    # whitespace padding is not additional coverage. Ties keep
                    # the original text and provenance stable.
                    ranks = {"low": 0, "medium": 1, "high": 2}
                    old_quality = doc.extraction_quality
                    if old_quality not in ranks:
                        old_quality = _text_extraction_quality(
                            doc.extraction_method, doc.extraction_source
                        )
                    old_rank = _text_extraction_rank(
                        old_quality,
                        doc.extraction_method,
                        doc.extraction_source,
                    )
                    new_rank = _text_extraction_rank(
                        extraction_quality, extraction_method, extraction_source
                    )
                    old_size = len("".join(old_text.split()))
                    new_size = len("".join(text.split()))
                    if (
                        new_rank < old_rank
                        or new_size < old_size
                        or (new_rank == old_rank and new_size == old_size)
                    ):
                        if doc.status == DocumentStatus.FAILED:
                            doc.status = DocumentStatus.COMPLETED
                            doc.error_message = None
                        return None

                # Claim the observed extraction before changing it. SQLite
                # ignores SELECT FOR UPDATE: a conditional no-op write instead
                # holds its write lock until the caller commits. If another
                # worker already changed the extraction, preserve that result.
                claimed = (
                    session.query(Document)
                    .filter(
                        Document.id == doc.id,
                        Document.text_content == doc.text_content,
                        Document.extraction_quality == doc.extraction_quality,
                        Document.extraction_method == doc.extraction_method,
                        Document.extraction_source == doc.extraction_source,
                        Document.document_hash == doc.document_hash,
                        Document.status == doc.status,
                    )
                    .update({Document.id: doc.id}, synchronize_session=False)
                )
                if claimed == 0:
                    session.expire(doc)
                    return None

                if old_text != text:
                    # Keep the vector IDs for replacement, but persist why
                    # these chunks are pending. Live vectors alone must not
                    # let reconciliation mark an older extraction current.
                    for chunk in (
                        session.query(DocumentChunk)
                        .options(load_only(DocumentChunk.document_metadata))
                        .filter_by(source_type="document", source_id=doc.id)
                        .all()
                    ):
                        chunk.document_metadata = {
                            **(chunk.document_metadata or {}),
                            "_ldr_text_invalidated": True,
                        }
                    # The legacy flag and canonical per-index marker must both
                    # move with the text, in this transaction. Leave chunk rows
                    # available for replace-on-reindex to remove their vectors.
                    affected_indexes = (
                        session.query(RagDocumentStatus.rag_index_id)
                        .filter_by(document_id=doc.id)
                        .distinct()
                        .all()
                    )
                    session.query(DocumentCollection).filter_by(
                        document_id=doc.id
                    ).update(
                        {DocumentCollection.indexed: False},
                        synchronize_session="fetch",
                    )
                    session.query(RagDocumentStatus).filter_by(
                        document_id=doc.id
                    ).delete(synchronize_session="fetch")
                    # Reindex treats a removed status as a new contribution.
                    # Remove its old contribution now, in the same transaction,
                    # or each text improvement inflates the index totals.
                    for (index_id,) in affected_indexes:
                        chunk_count, document_count = (
                            session.query(
                                func.sum(RagDocumentStatus.chunk_count),
                                func.count(),
                            )
                            .filter_by(rag_index_id=index_id)
                            .one()
                        )
                        session.query(RAGIndex).filter_by(id=index_id).update(
                            {
                                RAGIndex.chunk_count: chunk_count or 0,
                                RAGIndex.total_documents: document_count,
                            },
                            synchronize_session="fetch",
                        )

                # Update existing document with extracted text. Only replace
                # a failed placeholder's hash on a text-only retry (#3827).
                # An explicit PDF document already has the downloaded byte
                # hash, including a failed canonical document reused by dedup.
                was_failed = doc.status == DocumentStatus.FAILED
                doc.text_content = text
                doc.character_count = character_count
                doc.word_count = word_count
                doc.extraction_method = extraction_method
                doc.extraction_source = extraction_source
                doc.status = DocumentStatus.COMPLETED
                if was_failed and pdf_document_id is None:
                    doc.document_hash = hashlib.sha256(
                        text.encode()
                    ).hexdigest()
                if was_failed:
                    doc.error_message = None
                doc.processed_at = datetime.now(UTC)

                doc.extraction_quality = extraction_quality

                logger.debug(
                    f"Updated document {doc.id} with extracted text ({word_count} words)"
                )
            else:
                # Create a new Document for text-only extraction
                # Generate hash from text content
                text_hash = hashlib.sha256(text.encode()).hexdigest()

                # Dedup against existing content hash. If another resource
                # already produced identical extracted text, link this
                # resource to the canonical Document instead of inserting
                # a duplicate that would violate the UNIQUE constraint
                # (issue #3827). Mirrors research_history_indexer.py:322.
                existing_by_hash = (
                    session.query(Document)
                    .filter_by(document_hash=text_hash)
                    .first()
                )
                if existing_by_hash:
                    resource.document_id = existing_by_hash.id
                    library_collection = (
                        session.query(Collection)
                        .filter_by(name="Library")
                        .first()
                    )
                    if library_collection:
                        ensure_in_collection(
                            session,
                            existing_by_hash.id,
                            library_collection.id,
                        )
                    else:
                        logger.warning(
                            f"Library collection not found - deduped document {existing_by_hash.id} will not be linked to default collection"
                        )
                    logger.info(
                        f"Linked resource {resource.id} to existing Document "
                        f"{existing_by_hash.id} (matched on content hash)"
                    )
                    return None

                # Get source type for research downloads
                try:
                    source_type_id = get_source_type_id(
                        self.username, "research_download", self.password
                    )
                except Exception as exc:
                    log_exception_type(
                        "Failed to get source type for text document", exc
                    )
                    raise

                # Create new document
                doc_id = str(uuid.uuid4())
                doc = Document(
                    id=doc_id,
                    source_type_id=source_type_id,
                    resource_id=resource.id,
                    research_id=resource.research_id,
                    document_hash=text_hash,
                    original_url=resource.url,
                    file_path=FILE_PATH_TEXT_ONLY,
                    file_size=character_count,  # Use character count as file size for text-only
                    file_type="text",
                    mime_type="text/plain",
                    title=resource.title,
                    text_content=text,
                    character_count=character_count,
                    word_count=word_count,
                    extraction_method=extraction_method,
                    extraction_source=extraction_source,
                    extraction_quality=extraction_quality,
                    status=DocumentStatus.COMPLETED,
                    processed_at=datetime.now(UTC),
                )
                session.add(doc)

                # Link to default Library collection
                library_collection = (
                    session.query(Collection).filter_by(name="Library").first()
                )
                if library_collection:
                    ensure_in_collection(session, doc_id, library_collection.id)
                else:
                    logger.warning(
                        f"Library collection not found - document {doc_id} will not be linked to default collection"
                    )

                logger.info(
                    f"Created new document {doc_id} for text-only extraction ({word_count} words)"
                )

            logger.info(
                f"Saved text to encrypted database ({word_count} words)"
            )
            return None

        except Exception as exc:
            # Rollback BEFORE re-raising so the shared thread-local session
            # is clean by the time the caller's loop reaches its next
            # iteration. Without this, an IntegrityError leaves the session
            # in PendingRollbackError state and every subsequent ORM access
            # cascades (issue #3827).
            safe_rollback(session, "_save_text_with_db")
            log_exception_type("Error saving text to encrypted database", exc)
            raise  # Re-raise so caller can handle the error

    def _create_text_document_record(
        self,
        session: Session,
        resource: ResearchResource,
        file_path: Path,
        extraction_method: str,
        extraction_source: str,
    ):
        """Update existing Document with text from file (for legacy text files)."""
        try:
            # Read file to get metadata
            text = file_path.read_text(encoding="utf-8", errors="ignore")
            word_count = len(text.split())
            character_count = len(text)

            # Find the Document for this resource
            doc = get_document_for_resource(session, resource)

            if doc:
                # Update existing document with text content
                doc.text_content = text
                doc.character_count = character_count
                doc.word_count = word_count
                doc.extraction_method = extraction_method
                doc.extraction_source = extraction_source
                doc.extraction_quality = (
                    "low"  # Unknown quality for legacy files
                )
                logger.info(
                    f"Updated document {doc.id} with text from file: {file_path.name}"
                )
            else:
                logger.warning(
                    f"No document found to update for resource {resource.id}"
                )

        except Exception as exc:
            log_exception_type(
                "Error updating document with text from file", exc
            )

    def _record_failed_text_extraction(
        self, session: Session, resource: ResearchResource, error: str
    ):
        """Record a failed text extraction attempt in the Document."""
        try:
            # Find the Document for this resource
            doc = get_document_for_resource(session, resource)

            if doc:
                # Update document with extraction error
                doc.error_message = error
                doc.extraction_method = "failed"
                doc.extraction_quality = "low"
                doc.status = DocumentStatus.FAILED
                logger.info(
                    f"Recorded failed text extraction for document {doc.id}: "
                    f"{failure_reason_for_log(error)}"
                )
            else:
                # Create a new Document for failed extraction
                # This enables tracking failures and retry capability
                source_type_id = get_source_type_id(
                    self.username, "research_download", self.password
                )

                # Deterministic hash so retries update the same record
                failed_hash = hashlib.sha256(
                    f"failed:{resource.url}:{resource.id}".encode()
                ).hexdigest()

                doc_id = str(uuid.uuid4())
                doc = Document(
                    id=doc_id,
                    source_type_id=source_type_id,
                    resource_id=resource.id,
                    research_id=resource.research_id,
                    document_hash=failed_hash,
                    original_url=resource.url,
                    file_path=None,
                    file_size=0,
                    file_type="unknown",
                    title=resource.title,
                    status=DocumentStatus.FAILED,
                    error_message=error,
                    extraction_method="failed",
                    extraction_quality="low",
                    processed_at=datetime.now(UTC),
                )
                session.add(doc)

                logger.info(
                    f"Created failed document {doc_id} for resource "
                    f"{resource.id}: {failure_reason_for_log(error)}"
                )

        except Exception as exc:
            log_exception_type("Error recording failed text extraction", exc)


def _do_persist_downloaded_pdf_on_the_spot(
    url: str,
    pdf_content: bytes,
    text: Optional[str] = None,
    title: Optional[str] = None,
    username: Optional[str] = None,
    password: Optional[str] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
    library_root: Optional[Union[str, Path]] = None,
    extra_urls: Optional[List[str]] = None,
    collection_id: Optional[str] = None,
    resource_id: Optional[int] = None,
    check_setting: bool = False,
) -> Optional[str]:
    """Inner synchronous implementation of on-the-spot PDF persistence."""
    if not _is_pdf_bytes(pdf_content):
        return None

    if isinstance(settings_snapshot, dict) and (
        settings_snapshot.get("research_library.auto_persist_research_sources")
        is False
    ):
        return None

    if check_setting and not _is_auto_persist_enabled(
        settings_snapshot, username
    ):
        return None

    username, password, settings_snapshot = _resolve_persist_identity(
        username, password, settings_snapshot
    )

    if not username:
        logger.debug(
            f"pdf_persist skipped no user for {redact_url_for_log(url)}"
        )
        return None

    try:
        import hashlib

        file_hash_val = hashlib.sha256(pdf_content).hexdigest()

        if text is None or not text.strip():
            try:
                from ..downloaders.base import BaseDownloader

                extracted = BaseDownloader.extract_text_from_pdf(pdf_content)
                if extracted and extracted.strip():
                    text = extracted
            except Exception:  # allow: silent-exception
                pass

        if not text or not text.strip():
            # Garbage magic without extractable text: do not mint an empty
            # COMPLETED row the sweep would trust forever.
            logger.debug(
                "pdf_persist_skipped_no_text url={url}",
                url=redact_url_for_log(url),
            )
            return None

        pdf_storage_mode = _resolve_pdf_storage_mode(
            settings_snapshot, username
        )
        max_pdf_size_mb = DEFAULT_MAX_PDF_SIZE_MB
        custom_library_path = None
        if isinstance(settings_snapshot, dict):
            try:
                max_pdf_size_mb = int(
                    unwrap_setting(
                        settings_snapshot.get(
                            "research_library.max_pdf_size_mb",
                            DEFAULT_MAX_PDF_SIZE_MB,
                        )
                    )
                )
            except Exception:  # allow: silent-exception
                pass
            custom_library_path = unwrap_setting(
                settings_snapshot.get("research_library.storage_path")
            )
        else:
            try:
                settings = get_settings_manager(username=username)
                max_pdf_size_mb = int(
                    unwrap_setting(
                        settings.get_setting(
                            "research_library.max_pdf_size_mb",
                            DEFAULT_MAX_PDF_SIZE_MB,
                        )
                    )
                )
                custom_library_path = unwrap_setting(
                    settings.get_setting("research_library.storage_path")
                )
            except Exception:  # allow: silent-exception
                pass

        if library_root is None:
            if custom_library_path:
                from ..utils import apply_user_subdir

                library_root = apply_user_subdir(
                    Path(custom_library_path), username, False
                )
            else:
                from ..utils import get_library_storage_path

                try:
                    library_root = get_library_storage_path(username)
                except Exception:
                    library_root = Path("library")
        else:
            library_root = Path(library_root)

        with get_user_db_session(username, password) as session:
            pdf_storage_manager = PDFStorageManager(
                library_root=library_root,
                storage_mode=pdf_storage_mode,
                max_pdf_size_mb=max_pdf_size_mb,
                username=username,
            )

            target_doc, _ = _save_pdf_document_record(
                session=session,
                pdf_storage_manager=pdf_storage_manager,
                pdf_content=pdf_content,
                file_hash=file_hash_val,
                url=url,
                text=text,
                title=title,
                username=username,
                password=password,
                collection_id=collection_id,
                resource_id=resource_id,
                extra_urls=extra_urls,
                pdf_storage_mode=pdf_storage_mode,
                require_text=True,
            )
            if target_doc is None:
                try:
                    session.rollback()
                except Exception:  # allow: silent-exception
                    pass
                return None
            session.commit()
            return target_doc.id

    except Exception as exc:
        safe_msg = redact_secrets(str(exc), password)
        logger.warning(
            f"Failed on-the-spot persistence for {redact_url_for_log(url)}: {safe_msg}"
        )
        return None


def persist_downloaded_pdf_on_the_spot(
    url: str,
    pdf_content: bytes,
    text: Optional[str] = None,
    title: Optional[str] = None,
    username: Optional[str] = None,
    password: Optional[str] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
    library_root: Optional[Union[str, Path]] = None,
    extra_urls: Optional[List[str]] = None,
    collection_id: Optional[str] = None,
    resource_id: Optional[int] = None,
    run_in_background: bool = False,
    check_setting: bool = False,
) -> Optional[str]:
    """Persist a downloaded PDF directly into Library DB and storage on the spot.

    Single fetch-time writer for research PDF fetches. Library downloads use
    the synchronous ``DownloadService._download_pdf`` path instead, so each
    PDF is persisted once. When ``run_in_background=True`` the write runs in
    a bounded (4 pending / 20 MB) worker pool; when full it is skipped and
    the completion sweep handles it — payloads are never retained unbounded.
    Only eliminates the PDF double-fetch; plain web-page text is still
    fetched at completion by design.
    """
    if isinstance(settings_snapshot, dict) and (
        unwrap_setting(
            settings_snapshot.get(
                "research_library.auto_persist_research_sources"
            )
        )
        is False
    ):
        return None

    if check_setting and not _is_auto_persist_enabled(
        settings_snapshot, username
    ):
        return None
    if run_in_background:
        payload_len = len(pdf_content) if pdf_content else 0
        acquired = _PDF_PERSIST_SEMAPHORE.acquire(blocking=False)
        if not acquired:
            logger.debug(
                f"pdf_persist skipped backpressure for {redact_url_for_log(url)}"
            )
            return None
        bytes_reserved = False
        submitted = False
        try:
            with _PDF_PERSIST_BYTES_LOCK:
                global _PDF_PERSIST_BYTES
                if (
                    _PDF_PERSIST_BYTES + payload_len
                    > _PDF_PERSIST_MAX_PENDING_BYTES
                ):
                    logger.debug(
                        f"pdf_persist skipped byte cap for {redact_url_for_log(url)}"
                    )
                    return None
                _PDF_PERSIST_BYTES += payload_len
                bytes_reserved = True

            def _bounded_persist() -> None:
                global _PDF_PERSIST_BYTES
                try:
                    _do_persist_downloaded_pdf_on_the_spot(
                        url=url,
                        pdf_content=pdf_content,
                        text=text,
                        title=title,
                        username=username,
                        password=password,
                        settings_snapshot=settings_snapshot,
                        library_root=library_root,
                        extra_urls=extra_urls,
                        collection_id=collection_id,
                        resource_id=resource_id,
                        check_setting=check_setting,
                    )
                finally:
                    with _PDF_PERSIST_BYTES_LOCK:
                        _PDF_PERSIST_BYTES = max(
                            0, _PDF_PERSIST_BYTES - payload_len
                        )
                    _PDF_PERSIST_SEMAPHORE.release()
                    try:
                        from ...database.thread_local_session import (
                            cleanup_current_thread,
                        )

                        cleanup_current_thread()
                    except Exception:  # allow: silent-exception
                        pass

            executor = _get_pdf_persistence_executor()
            executor.submit(_bounded_persist)
            submitted = True
            # Slot and bytes released in _bounded_persist finally.
            return None
        except Exception as exc:
            safe_msg = redact_secrets(str(exc), password)
            logger.warning(
                f"Failed to submit background PDF persistence for {redact_url_for_log(url)}: {safe_msg}"
            )
            return None
        finally:
            if not submitted:
                if bytes_reserved:
                    with _PDF_PERSIST_BYTES_LOCK:
                        _PDF_PERSIST_BYTES = max(
                            0, _PDF_PERSIST_BYTES - payload_len
                        )
                _PDF_PERSIST_SEMAPHORE.release()

    return _do_persist_downloaded_pdf_on_the_spot(
        url=url,
        pdf_content=pdf_content,
        text=text,
        title=title,
        username=username,
        password=password,
        settings_snapshot=settings_snapshot,
        library_root=library_root,
        extra_urls=extra_urls,
        collection_id=collection_id,
        resource_id=resource_id,
        check_setting=check_setting,
    )


def auto_persist_research_sources(
    username: str,
    research_id: str,
    db_password: Optional[str] = None,
    collection_id: Optional[str] = None,
) -> Dict[str, Any]:
    """Auto-persist research sources (PDFs and text) to the Library.

    Default-off behind ``research_library.auto_persist_research_sources``.
    PDF downloads additionally honor ``document_scheduler.download_pdfs``
    so users who disabled PDF downloads are not overridden.
    Safe to call from any context — exceptions are caught and logged.
    """
    try:
        settings = get_settings_manager(username=username)
        try:
            auto_enabled = unwrap_setting(
                settings.get_setting(
                    "research_library.auto_persist_research_sources", False
                )
            )
        except Exception:
            auto_enabled = False
        if auto_enabled is not True:
            logger.debug(
                f"auto_persist skipped disabled for research {research_id}"
            )
            return {
                "total": 0,
                "persisted": 0,
                "failed": 0,
                "skipped": 0,
                "skipped_reason": "disabled",
            }
        settings_snapshot = None
        try:
            settings_snapshot = settings.get_settings_snapshot()
        except Exception:  # allow: silent-exception
            pass

        with DownloadService(
            username,
            password=db_password,
            settings_snapshot=settings_snapshot,
        ) as download_service:
            return download_service.persist_research_sources(
                research_id, collection_id=collection_id
            )
    except Exception as exc:
        safe_msg = redact_secrets(str(exc), db_password)
        logger.warning(
            f"Failed to auto-persist sources for research {research_id} (user {username}): {safe_msg}"
        )
        return {
            "total": 0,
            "persisted": 0,
            "failed": 0,
            "skipped": 0,
            "error": safe_msg,
        }