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.

116 KB
Raw Blame
"""
Regression tests for issue #3827 — Download Manager fails with
``UNIQUE constraint failed: documents.document_hash`` after 20–30 PDFs.

The bug had two parts:

1. ``_save_text_with_db`` and ``_download_pdf`` unconditionally overwrote
   ``Document.document_hash`` on every existing-document update. The PDF-bytes
   hash (essentially collision-free) was clobbered with a text-content hash
   (collision-prone). After enough downloads, two PDFs that extracted to
   identical text produced the same hash and the second commit raised
   ``IntegrityError``.

2. No rollback ran after the IntegrityError, so the shared thread-local
   session stayed in ``PendingRollbackError`` state and every subsequent
   operation cascaded.

The fix preserves PR #2590's retry-from-FAILED intent (still replace the
placeholder ``failed:url:resource_id`` hash with a real content hash) while
leaving stable hashes alone on normal updates, adds a lookup-before-insert in
the new-Document branch, and rolls back on the inner exception path.

These tests use a real in-memory SQLite session so the UNIQUE constraint
behaviour is exercised end-to-end.
"""

import hashlib
import uuid
from contextlib import contextmanager
from datetime import UTC, datetime
from unittest.mock import MagicMock, patch

import pytest
from sqlalchemy import create_engine, text
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.download_tracker import DownloadTracker
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentBlob,
    DocumentChunk,
    DocumentCollection,
    DocumentStatus,
    EmbeddingProvider,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)
from local_deep_research.research_library.downloaders.arxiv import (
    ArxivDownloader,
    ArxivTextResult,
    ArxivTextSource,
)
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)
from local_deep_research.research_library.services.download_service import (
    DownloadService,
)


MODULE = "local_deep_research.research_library.services.download_service"


@pytest.fixture
def session():
    """Real in-memory SQLite session — exercises actual UNIQUE constraints."""
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    yield session
    session.close()
    engine.dispose()


@pytest.fixture
def source_type(session):
    src = SourceType(
        id=str(uuid.uuid4()),
        name="research_download",
        display_name="Research Download",
    )
    session.add(src)
    session.commit()
    return src


@pytest.fixture
def library_collection(session):
    coll = Collection(
        id=str(uuid.uuid4()),
        name="Library",
        is_default=True,
        collection_type="default_library",
    )
    session.add(coll)
    session.commit()
    return coll


@pytest.fixture
def research(session):
    """ResearchHistory row required by ResearchResource FK."""
    rh = ResearchHistory(
        id=str(uuid.uuid4()),
        query="test query",
        mode="quick",
        status="completed",
        created_at="2026-05-09T00:00:00",
    )
    session.add(rh)
    session.commit()
    return rh


@pytest.fixture
def make_resource(session, research):
    def _make(url):
        r = ResearchResource(
            research_id=research.id,
            title="Test Paper",
            url=url,
            source_type="academic",
            created_at="2026-05-09T00:00:00",
        )
        session.add(r)
        session.commit()
        return r

    return _make


@pytest.fixture
def svc():
    """DownloadService stub — only the methods we test directly are exercised."""
    with patch.object(DownloadService, "__init__", lambda self, *a, **kw: None):
        s = DownloadService.__new__(DownloadService)
        s.username = "test_user"
        s.password = "test_pass"
        s._closed = False
        return s


# ---------------------------------------------------------------------------
# Change 1 / site 2: _save_text_with_db must NOT overwrite hash on
# normal (status=COMPLETED) updates — this is the primary #3827 bug.
# ---------------------------------------------------------------------------


def test_completed_doc_hash_is_not_overwritten_by_text_extraction(
    session, source_type, make_resource, svc
):
    """Two PDFs with different bytes whose extractors return identical text
    used to collide on the text-hash overwrite. With the fix, each Document
    keeps its PDF-bytes hash and no UNIQUE collision occurs.
    """
    res_a = make_resource("https://arxiv.org/abs/2401.A")
    res_b = make_resource("https://arxiv.org/abs/2401.B")

    pdf_a_hash = hashlib.sha256(b"PDF-A bytes").hexdigest()
    pdf_b_hash = hashlib.sha256(b"PDF-B bytes").hexdigest()
    identical_text = "Identical extracted body text from both PDFs."

    # Mirror what _download_pdf would have created at line 664.
    doc_a = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res_a.id,
        research_id=res_a.research_id,
        document_hash=pdf_a_hash,
        original_url=res_a.url,
        file_size=11,
        file_type="pdf",
        title=res_a.title,
        status=DocumentStatus.COMPLETED,
    )
    doc_b = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res_b.id,
        research_id=res_b.research_id,
        document_hash=pdf_b_hash,
        original_url=res_b.url,
        file_size=11,
        file_type="pdf",
        title=res_b.title,
        status=DocumentStatus.COMPLETED,
    )
    session.add_all([doc_a, doc_b])
    session.commit()

    with patch(f"{MODULE}.get_document_for_resource") as gdr:
        gdr.side_effect = [doc_a, doc_b]
        svc._save_text_with_db(
            res_a,
            identical_text,
            session,
            extraction_method="pdf_extraction",
            extraction_source="pdfplumber",
            pdf_document_id=doc_a.id,
        )
        svc._save_text_with_db(
            res_b,
            identical_text,
            session,
            extraction_method="pdf_extraction",
            extraction_source="pdfplumber",
            pdf_document_id=doc_b.id,
        )
        # The second commit used to raise IntegrityError. With the fix, both
        # commits succeed and the hashes are unchanged.
        session.commit()

    assert doc_a.document_hash == pdf_a_hash
    assert doc_b.document_hash == pdf_b_hash
    assert doc_a.text_content == identical_text
    assert doc_b.text_content == identical_text


# ---------------------------------------------------------------------------
# Change 1 retry preservation: PR #2590's intent is preserved — a Document
# whose status was FAILED still gets its placeholder hash replaced when
# real text becomes available.
# ---------------------------------------------------------------------------


def test_failed_doc_hash_is_replaced_with_text_hash_on_retry(
    session, source_type, make_resource, svc
):
    res = make_resource("https://arxiv.org/abs/2401.C")
    placeholder = hashlib.sha256(
        f"failed:{res.url}:{res.id}".encode()
    ).hexdigest()

    failed_doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res.id,
        research_id=res.research_id,
        document_hash=placeholder,
        original_url=res.url,
        file_size=0,
        file_type="unknown",
        title=res.title,
        status=DocumentStatus.FAILED,
    )
    session.add(failed_doc)
    session.commit()

    extracted_text = "Real text extracted on retry."
    expected_hash = hashlib.sha256(extracted_text.encode()).hexdigest()

    with patch(f"{MODULE}.get_document_for_resource", return_value=failed_doc):
        svc._save_text_with_db(
            res,
            extracted_text,
            session,
            extraction_method="native_api",
            extraction_source="arxiv",
        )
        session.commit()

    assert failed_doc.document_hash == expected_hash
    assert failed_doc.status == DocumentStatus.COMPLETED


# ---------------------------------------------------------------------------
# Change 2: lookup-before-insert in the new-text-only-Document branch
# prevents UNIQUE collisions when two resources have no PDF and produce
# identical extracted text via the API path.
# ---------------------------------------------------------------------------


def test_text_only_extraction_dedups_on_existing_content_hash(
    session, source_type, library_collection, make_resource, svc
):
    res_a = make_resource("https://example.com/api/A")
    res_b = make_resource("https://example.com/api/B")

    text = "Same text from two different API endpoints."
    text_hash = hashlib.sha256(text.encode()).hexdigest()

    with (
        patch(f"{MODULE}.get_document_for_resource", return_value=None),
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
    ):
        svc._save_text_with_db(
            res_a,
            text,
            session,
            extraction_method="native_api",
            extraction_source="arxiv",
        )
        session.commit()
        svc._save_text_with_db(
            res_b,
            text,
            session,
            extraction_method="native_api",
            extraction_source="arxiv",
        )
        session.commit()

    docs = session.query(Document).filter_by(document_hash=text_hash).all()
    assert len(docs) == 1, (
        f"Dedup should have produced exactly one Document, got {len(docs)}"
    )
    canonical = docs[0]

    session.refresh(res_b)
    assert res_b.document_id == canonical.id, (
        "Second resource should be linked to the canonical Document."
    )


# ---------------------------------------------------------------------------
# Change 5: _save_text_with_db's outer except calls session.rollback() before
# re-raising, so the caller's loop sees a clean session on the next iteration.
# ---------------------------------------------------------------------------


def test_save_text_with_db_rolls_back_session_on_exception(svc):
    """When _save_text_with_db's inner work raises, the session must be
    rolled back before the exception propagates so the next caller's
    iteration doesn't trip over PendingRollbackError.
    """
    session = MagicMock()
    resource = MagicMock()

    with patch(
        f"{MODULE}.get_document_for_resource",
        side_effect=RuntimeError("simulated flush failure"),
    ):
        with pytest.raises(RuntimeError, match="simulated flush failure"):
            svc._save_text_with_db(
                resource,
                "some text",
                session,
                extraction_method="native_api",
                extraction_source="arxiv",
            )

    session.rollback.assert_called_once()


# ---------------------------------------------------------------------------
# Change 1 / site 1: _download_pdf existing-doc branch should leave the hash
# alone for COMPLETED docs and replace the placeholder for FAILED docs.
# We exercise the branch directly via the same code path used by _download_pdf
# (ASCII-test of the conditional is the cleanest assertion here).
# ---------------------------------------------------------------------------


def test_download_pdf_existing_completed_doc_hash_is_stable(
    session, source_type, make_resource
):
    """Mirror line 614-619's behaviour: a COMPLETED doc must not have its
    hash overwritten on re-download. We construct the in-memory state and
    simulate the conditional update directly.
    """
    res = make_resource("https://arxiv.org/abs/2401.D")
    original_hash = hashlib.sha256(b"original PDF").hexdigest()
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res.id,
        research_id=res.research_id,
        document_hash=original_hash,
        original_url=res.url,
        file_size=12,
        file_type="pdf",
        title=res.title,
        status=DocumentStatus.COMPLETED,
    )
    session.add(doc)
    session.commit()

    new_pdf_hash = hashlib.sha256(b"different PDF bytes").hexdigest()

    # Mirrored conditional from download_service.py:614-622
    was_failed = doc.status == DocumentStatus.FAILED
    if was_failed:
        doc.document_hash = new_pdf_hash
    doc.status = DocumentStatus.COMPLETED
    session.commit()

    assert doc.document_hash == original_hash, (
        "Completed doc's hash must remain stable; only FAILED retries replace it."
    )


def test_download_pdf_existing_failed_doc_hash_is_replaced(
    session, source_type, make_resource
):
    res = make_resource("https://arxiv.org/abs/2401.E")
    placeholder = hashlib.sha256(
        f"failed:{res.url}:{res.id}".encode()
    ).hexdigest()
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res.id,
        research_id=res.research_id,
        document_hash=placeholder,
        original_url=res.url,
        file_size=0,
        file_type="unknown",
        title=res.title,
        status=DocumentStatus.FAILED,
    )
    session.add(doc)
    session.commit()

    new_pdf_hash = hashlib.sha256(
        b"PDF bytes from successful retry"
    ).hexdigest()

    was_failed = doc.status == DocumentStatus.FAILED
    if was_failed:
        doc.document_hash = new_pdf_hash
    doc.status = DocumentStatus.COMPLETED
    session.commit()

    assert doc.document_hash == new_pdf_hash
    assert doc.status == DocumentStatus.COMPLETED


# ---------------------------------------------------------------------------
# Sanity check: confirm that WITHOUT the fix, two identical-text updates DO
# violate the unique constraint. This guards against a regression that
# silently makes test 1 trivially pass (e.g. a schema change that drops the
# constraint).
# ---------------------------------------------------------------------------


def test_unique_constraint_still_enforced_at_db_level(
    session, source_type, make_resource
):
    res_a = make_resource("https://example.com/a")
    res_b = make_resource("https://example.com/b")

    shared_hash = hashlib.sha256(b"shared content").hexdigest()
    doc_a = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res_a.id,
        research_id=res_a.research_id,
        document_hash=shared_hash,
        original_url=res_a.url,
        file_size=14,
        file_type="text",
        title="A",
        status=DocumentStatus.COMPLETED,
    )
    doc_b = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res_b.id,
        research_id=res_b.research_id,
        document_hash=shared_hash,
        original_url=res_b.url,
        file_size=14,
        file_type="text",
        title="B",
        status=DocumentStatus.COMPLETED,
    )
    session.add_all([doc_a, doc_b])
    with pytest.raises(IntegrityError):
        session.commit()
    session.rollback()


# ---------------------------------------------------------------------------
# Stress test: replays the exact bug scenario from the issue report — many
# PDFs whose extractors collapse to identical text. Without the fix this
# raised IntegrityError after a handful; with the fix the entire batch
# completes.
# ---------------------------------------------------------------------------


def test_25_pdfs_with_identical_extracted_text_all_succeed(
    session, source_type, make_resource, svc
):
    """The original report describes failure 'after 20 to 30 PDFs'. This test
    drives 25 distinct PDF Documents whose extractors all return the same
    text — the worst-case happy-path scenario for the old code, where every
    `_save_text_with_db` call would clobber the hash to the same text-hash
    and the second commit onwards would raise UNIQUE constraint failures.
    """
    n = 25
    docs = []
    for i in range(n):
        res = make_resource(f"https://arxiv.org/abs/2401.{i:04d}")
        # Each PDF has unique bytes (and therefore a unique PDF-bytes hash).
        pdf_hash = hashlib.sha256(f"PDF-bytes-{i}".encode()).hexdigest()
        doc = Document(
            id=str(uuid.uuid4()),
            source_type_id=source_type.id,
            resource_id=res.id,
            research_id=res.research_id,
            document_hash=pdf_hash,
            original_url=res.url,
            file_size=64,
            file_type="pdf",
            title=res.title,
            status=DocumentStatus.COMPLETED,
        )
        session.add(doc)
        docs.append((res, doc, pdf_hash))
    session.commit()

    identical_text = "Same extracted text from all 25 PDFs."

    # Iterate exactly as the production loop does — each call goes through
    # the existing-Document branch (line 1584) of _save_text_with_db.
    with patch(f"{MODULE}.get_document_for_resource") as gdr:
        gdr.side_effect = [d for (_, d, _) in docs]
        for res, doc, _ in docs:
            svc._save_text_with_db(
                res,
                identical_text,
                session,
                extraction_method="pdf_extraction",
                extraction_source="pdfplumber",
                pdf_document_id=doc.id,
            )
        # Single commit at the end so any pending IntegrityError surfaces here.
        session.commit()

    # Every Document kept its unique PDF-bytes hash; none collapsed to the
    # text-hash that would have collided.
    for _, doc, pdf_hash in docs:
        assert doc.document_hash == pdf_hash
        assert doc.text_content == identical_text

    # And of course no duplicates exist in the table.
    hashes = {d.document_hash for (_, d, _) in docs}
    assert len(hashes) == n


# ---------------------------------------------------------------------------
# Status-matrix coverage: confirms the conditional fires ONLY for
# DocumentStatus.FAILED. Future-proofs against someone broadening the
# trigger and silently re-introducing the bug.
# ---------------------------------------------------------------------------


@pytest.mark.parametrize(
    "prior_status,expected_overwrite",
    [
        (DocumentStatus.FAILED, True),
        (DocumentStatus.COMPLETED, False),
        (DocumentStatus.PENDING, False),
        (DocumentStatus.PROCESSING, False),
    ],
)
def test_save_text_with_db_overwrite_is_gated_on_failed_only(
    session, source_type, make_resource, svc, prior_status, expected_overwrite
):
    res = make_resource(f"https://example.com/status/{prior_status.value}")
    original_hash = hashlib.sha256(
        f"original-{prior_status.value}".encode()
    ).hexdigest()
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res.id,
        research_id=res.research_id,
        document_hash=original_hash,
        original_url=res.url,
        file_size=10,
        file_type="pdf",
        title=res.title,
        status=prior_status,
    )
    session.add(doc)
    session.commit()

    text = f"Extracted text for {prior_status.value}"
    text_hash = hashlib.sha256(text.encode()).hexdigest()

    with patch(f"{MODULE}.get_document_for_resource", return_value=doc):
        svc._save_text_with_db(
            res,
            text,
            session,
            extraction_method="pdf_extraction",
            extraction_source="pdfplumber",
        )
        session.commit()

    if expected_overwrite:
        assert doc.document_hash == text_hash, (
            f"Hash should have been replaced for prior status {prior_status}"
        )
    else:
        assert doc.document_hash == original_hash, (
            f"Hash must remain stable for prior status {prior_status}; "
            f"only FAILED triggers replacement."
        )
    # In every case the doc ends up COMPLETED.
    assert doc.status == DocumentStatus.COMPLETED


# ---------------------------------------------------------------------------
# Change 4: the SSE bulk-download generator's per-item except calls
# session.rollback() at the TOP of the block, so the next iteration's
# pre-loop session.query (line 843) doesn't trip on a poisoned session.
# This is structural — we drive the `download_bulk` route end-to-end with
# Flask's test client.
# ---------------------------------------------------------------------------


def test_download_bulk_second_item_processes_after_first_download_raises():
    """Drive the FastAPI ``/library/api/download-bulk`` SSE endpoint with two
    queue items; the first triggers IntegrityError. Verify the second item is
    still processed on a clean session — the #3827 invariant.

    The SSE generator now opens a fresh, SHORT-LIVED ``get_user_db_session``
    block for each DB op (claim, resource lookup, finalize/release) so that no
    session scope is ever held across a ``yield`` (which under Starlette's
    per-``next()`` threadpool dispatch could straddle two OS threads and
    corrupt the thread-local scope-depth counter). Staleness between ops is
    cleared by ``get_session``'s rollback-on-fresh-entry guard, so no explicit
    ``safe_rollback`` is needed anymore. This test verifies the behavioral
    outcome: item B completes even though item A raised.
    """
    from contextlib import contextmanager

    from fastapi.testclient import TestClient

    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.dependencies.auth import require_auth
    import local_deep_research.web.routers.library as routes_mod

    # Two queue items so we can prove the second iteration sees a clean
    # session.
    q_item_a = MagicMock()
    q_item_a.resource_id = 101
    q_item_a.collection_id = None
    q_item_b = MagicMock()
    q_item_b.resource_id = 102
    q_item_b.collection_id = None

    resource_a = MagicMock()
    resource_a.id = 101
    resource_a.url = "https://example.com/a.pdf"
    resource_a.title = "A"
    resource_a.research_id = "r1"
    resource_b = MagicMock()
    resource_b.id = 102
    resource_b.url = "https://example.com/b.pdf"
    resource_b.title = "B"
    resource_b.research_id = "r1"

    outer_session = MagicMock()
    # Route session.query(...) by model so the call order is robust to the
    # atomic PENDING -> PROCESSING claim added for issue #4691 (which issues
    # extra DownloadQueue UPDATEs per item):
    #  - DownloadQueue queries serve count() (2 pending), .all() ([a, b]) and
    #    the claim/release .update() (returns 1 = claim wins)
    #  - ResearchResource queries serve .get(101)/.get(102)
    queue_q = MagicMock()
    queue_q.filter_by.return_value = queue_q
    queue_q.count.return_value = 2
    queue_q.all.return_value = [q_item_a, q_item_b]
    queue_q.update.return_value = 1
    resource_q = MagicMock()
    outer_session.get.side_effect = [resource_a, resource_b]

    def _query_router(model):
        if getattr(model, "__name__", "") == "DownloadQueue":
            return queue_q
        return resource_q

    outer_session.query.side_effect = _query_router

    @contextmanager
    def fake_db_session(*a, **kw):
        yield outer_session

    download_service_mock = MagicMock()
    # First call raises an IntegrityError-like; second succeeds.
    download_service_mock.download_resource.side_effect = [
        IntegrityError("INSERT", {}, Exception("UNIQUE constraint failed")),
        (True, None),
    ]

    app.dependency_overrides[require_auth] = lambda: "testuser"
    try:
        with (
            patch.object(
                routes_mod, "get_user_db_session", side_effect=fake_db_session
            ),
            patch.object(
                routes_mod,
                "get_authenticated_user_password",
                return_value="pw",
            ),
            patch.object(
                routes_mod,
                "DownloadService",
                return_value=download_service_mock,
            ),
        ):
            client = TestClient(app)
            # Mint a session-bound CSRF token for the state-changing POST.
            csrf = client.get("/auth/csrf-token").json()["csrf_token"]
            resp = client.post(
                "/library/api/download-bulk",
                json={"research_ids": ["r1"], "mode": "pdf"},
                headers={"X-CSRFToken": csrf},
            )
            # The SSE generator is lazy — it only runs as the body is
            # consumed. Force consumption so the per-item loop executes
            # and the rollback paths fire.
            body = resp.text

    finally:
        app.dependency_overrides.pop(require_auth, None)

    assert resp.status_code == 200, body
    # Both queue items were attempted (not just the first — confirms the loop
    # continued after item A raised, on a fresh per-op session).
    assert download_service_mock.download_resource.call_count == 2
    # And the second item completed successfully — proves item A's
    # IntegrityError did not cascade into item B's processing (#3827).
    assert '"file": "B", "status": "success"' in body


# ---------------------------------------------------------------------------
# Change 3 (rewritten for scoped sessions): ``_process_user_documents`` no
# longer holds ONE session across the download loop. It reads everything it
# needs in a short read session, runs the loop with NO held session (each
# ``DownloadService`` call opens its own), then writes ``last_run`` in a
# short write session.
#
# That is the root-cause fix for the production ``database is locked``
# contention: a long-lived SQLite read snapshot pins the WAL and blocks
# every other writer for as long as the PDF downloads take.
#
# The #3827 requirement (one bad resource must not poison the next) is now
# structural rather than defensive -- there is no shared session to poison.
# These tests pin both properties.
# ---------------------------------------------------------------------------


def test_process_user_documents_closes_read_session_before_downloading():
    """The read session must be CLOSED before the first download starts.

    This is the structural guarantee that fixes the ``database is locked``
    contention: no SQLite read snapshot may be held while network IO runs.
    Before the fix, a single session stayed open across the whole loop.

    Also pins the #3827 requirement: a resource that raises must not stop
    the next resource from being processed. That is now guaranteed because
    ``download_as_text`` opens and closes its own session -- there is no
    shared session for a failure to poison.
    """
    from datetime import datetime, UTC

    from local_deep_research.scheduler.background import (
        BackgroundJobScheduler,
        DocumentSchedulerSettings,
    )

    # Reset the singleton so we get a fresh scheduler.
    BackgroundJobScheduler._instance = None
    with patch("local_deep_research.scheduler.background.BackgroundScheduler"):
        scheduler = BackgroundJobScheduler()

    scheduler.user_sessions["testuser"] = {
        "scheduled_jobs": set(),
        "last_activity": datetime.now(UTC),
    }
    scheduler._credential_store.store("testuser", "testpass")

    settings = DocumentSchedulerSettings(
        download_pdfs=False,
        extract_text=True,
        generate_rag=False,
        last_run="",
    )

    # A single research row exposing only the three columns the refactor
    # selects, plus two downloadable resources.
    research_row = MagicMock(id="research-1", title="T", completed_at=None)
    resource_rows = [
        MagicMock(id=1, url="https://example.com/a.pdf"),
        MagicMock(id=2, url="https://example.com/b.pdf"),
    ]

    db = MagicMock()
    research_query = MagicMock()
    research_query.filter.return_value = research_query
    research_query.order_by.return_value = research_query
    research_query.limit.return_value = research_query
    research_query.all.return_value = [research_row]
    resource_query = MagicMock()
    resource_query.filter_by.return_value = resource_query
    resource_query.all.return_value = resource_rows
    # Read session: ResearchHistory columns, then ResearchResource columns.
    db.query.side_effect = [research_query, resource_query]

    # Record the interleaving of session lifetime and download calls so we
    # can assert ordering (the read session must close before the first
    # download).
    events: list[str] = []

    from contextlib import contextmanager as _cm

    @_cm
    def fake_get_user_db_session(*a, **kw):
        events.append("session_open")
        try:
            yield db
        finally:
            events.append("session_close")

    download_service = MagicMock()
    download_service.__enter__ = MagicMock(return_value=download_service)
    download_service.__exit__ = MagicMock(return_value=False)

    # A single ``side_effect`` callable (not a list): the list form RETURNS
    # its items rather than calling them, so a closure is the only way to
    # both record ordering and selectively fail the first resource.
    def _download_as_text(resource_id):
        events.append(f"download:{resource_id}")
        if resource_id == 1:
            raise IntegrityError(
                "INSERT", {}, Exception("UNIQUE constraint failed")
            )
        return (True, None)

    download_service.download_as_text.side_effect = _download_as_text

    settings_manager = MagicMock()
    # The egress backstop aborts unless the strict snapshot is a real dict
    # with a resolvable primary engine; supply one so processing reaches the
    # download loop this test exercises.
    settings_manager.get_settings_snapshot.return_value = {
        "search.tool": "searxng"
    }

    with (
        patch.object(
            scheduler,
            "_get_document_scheduler_settings",
            return_value=settings,
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=fake_get_user_db_session,
        ),
        patch(
            "local_deep_research.settings.manager.SettingsManager",
            return_value=settings_manager,
        ),
        patch(
            "local_deep_research.research_library.services.download_service.DownloadService",
            return_value=download_service,
        ),
        patch(
            "local_deep_research.research_library.utils.is_downloadable_url",
            return_value=True,
        ),
    ):
        scheduler._process_user_documents("testuser")

    # Both resources were attempted: the first one's IntegrityError did not
    # cascade into the second (#3827).
    assert download_service.download_as_text.call_count == 2, (
        "A failing resource must not prevent the next from being processed."
    )

    # The read session closed BEFORE the first download. This is the whole
    # point of the refactor: no SQLite transaction is held during network IO.
    first_close = events.index("session_close")
    first_download = next(
        i for i, e in enumerate(events) if e.startswith("download:")
    )
    assert first_close < first_download, (
        "The read session must be closed before the first download so no "
        f"SQLite read snapshot is held during network IO. Events: {events}"
    )

    # And the write session (the final last_run update) opens only AFTER the
    # downloads, so the write lock is held for one settings row, not the
    # whole pass.
    last_download = max(
        i for i, e in enumerate(events) if e.startswith("download:")
    )
    last_open = len(events) - 1 - events[::-1].index("session_open")
    assert last_download < last_open, (
        "The last_run write session must open after all downloads have "
        f"finished. Events: {events}"
    )


# ---------------------------------------------------------------------------
# Issue #7525: the resource prefetch introduced by the scoped-session refactor
# (#6720) ran even with extract_text off, and a prefetch query failure escaped
# the read session — skipping download_pdfs queueing for every research and
# the last_run update. The prefetch is now gated on extract_text and contained
# per research (log the exception type, store an empty list).
# ---------------------------------------------------------------------------


def test_process_user_documents_resource_prefetch_failure_is_contained():
    """A database failure while materialising one research's resource list
    must stay contained to that research's extract_text work (#7525).

    Before the fix the prefetch had no try/except, so one failed query
    (e.g. a locked DB) escaped the read session, skipped ``download_pdfs``
    queueing for EVERY research and left ``last_run`` unset. This test
    replaces the previous fail-closed pin (prefetch failure must not
    advance ``last_run``) per #7525: the failure is
    now logged (type name only), the research gets an empty resource list,
    PDF queueing still runs for every research, and ``last_run`` advances.
    The fail-closed policy for UPSTREAM setup failures (DB open, settings
    manager init, the initial research query — see the ``Intentionally NOT
    wrapped in try/finally`` comment in the implementation) is unchanged.
    """
    from datetime import datetime, UTC

    from local_deep_research.scheduler.background import (
        BackgroundJobScheduler,
        DocumentSchedulerSettings,
    )

    BackgroundJobScheduler._instance = None
    with patch("local_deep_research.scheduler.background.BackgroundScheduler"):
        scheduler = BackgroundJobScheduler()

    scheduler.user_sessions["testuser"] = {
        "scheduled_jobs": set(),
        "last_activity": datetime.now(UTC),
    }
    scheduler._credential_store.store("testuser", "testpass")

    settings = DocumentSchedulerSettings(
        download_pdfs=True,
        extract_text=True,
        generate_rag=False,
        last_run="",
    )

    research_rows = [
        MagicMock(id="research-a", title="A", completed_at=None),
        MagicMock(id="research-b", title="B", completed_at=None),
    ]

    db = MagicMock()
    research_query = MagicMock()
    research_query.filter.return_value = research_query
    research_query.order_by.return_value = research_query
    research_query.limit.return_value = research_query
    research_query.all.return_value = research_rows
    # Both per-research ResearchResource queries raise: the failure must be
    # caught per research instead of escaping the read session.
    db.query.side_effect = [
        research_query,
        RuntimeError("simulated DB query failure"),
        RuntimeError("simulated DB query failure"),
    ]

    from contextlib import contextmanager as _cm

    @_cm
    def fake_get_user_db_session(*a, **kw):
        yield db

    download_service = MagicMock()
    download_service.__enter__ = MagicMock(return_value=download_service)
    download_service.__exit__ = MagicMock(return_value=False)
    download_service.queue_research_downloads.return_value = 0

    settings_manager = MagicMock()
    settings_manager.get_settings_snapshot.return_value = {
        "search.tool": "searxng"
    }

    with (
        patch.object(
            scheduler,
            "_get_document_scheduler_settings",
            return_value=settings,
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=fake_get_user_db_session,
        ),
        patch(
            "local_deep_research.settings.manager.SettingsManager",
            return_value=settings_manager,
        ),
        patch(
            "local_deep_research.research_library.services.download_service.DownloadService",
            return_value=download_service,
        ),
    ):
        scheduler._process_user_documents("testuser")

    # The first research's failure did not stop the second's prefetch from
    # being attempted: ResearchHistory + one ResearchResource query each.
    assert db.query.call_count == 3, (
        "A prefetch failure must not abort the prefetch loop; "
        f"saw {db.query.call_count} queries."
    )

    # PDF queueing still ran for EVERY research.
    queued_ids = [
        call.args[0]
        for call in download_service.queue_research_downloads.call_args_list
    ]
    assert queued_ids == ["research-a", "research-b"], (
        "A prefetch failure must not block download_pdfs queueing; "
        f"queued {queued_ids!r}."
    )

    # The failed researches have no materialised resources, so the
    # extract_text pass had nothing to do.
    download_service.download_as_text.assert_not_called()

    # last_run advances: the pass completed, so the next tick must not
    # re-process these researches forever.
    last_run_calls = [
        call
        for call in settings_manager.set_setting.call_args_list
        if call.args and call.args[0] == "document_scheduler.last_run"
    ]
    assert last_run_calls, (
        "last_run must advance when the pass completes despite a contained "
        "prefetch failure."
    )


def test_process_user_documents_extract_text_off_skips_resource_prefetch():
    """With ``extract_text`` off, no ResearchResource query runs at all —
    only the extract_text pass reads the prefetched lists — and PDF
    queueing proceeds as normal (#7525).
    """
    from datetime import datetime, UTC

    from local_deep_research.scheduler.background import (
        BackgroundJobScheduler,
        DocumentSchedulerSettings,
    )

    BackgroundJobScheduler._instance = None
    with patch("local_deep_research.scheduler.background.BackgroundScheduler"):
        scheduler = BackgroundJobScheduler()

    scheduler.user_sessions["testuser"] = {
        "scheduled_jobs": set(),
        "last_activity": datetime.now(UTC),
    }
    scheduler._credential_store.store("testuser", "testpass")

    settings = DocumentSchedulerSettings(
        download_pdfs=True,
        extract_text=False,
        generate_rag=False,
        last_run="",
    )

    research_row = MagicMock(id="research-x", title="T", completed_at=None)

    db = MagicMock()
    research_query = MagicMock()
    research_query.filter.return_value = research_query
    research_query.order_by.return_value = research_query
    research_query.limit.return_value = research_query
    research_query.all.return_value = [research_row]
    # Only the ResearchHistory query may run. A second db.query call (the
    # prefetch) would exhaust this side_effect and fail the pass, exactly
    # as the ungated prefetch did before the fix.
    db.query.side_effect = [research_query]

    from contextlib import contextmanager as _cm

    @_cm
    def fake_get_user_db_session(*a, **kw):
        yield db

    download_service = MagicMock()
    download_service.__enter__ = MagicMock(return_value=download_service)
    download_service.__exit__ = MagicMock(return_value=False)
    download_service.queue_research_downloads.return_value = 0

    settings_manager = MagicMock()
    settings_manager.get_settings_snapshot.return_value = {
        "search.tool": "searxng"
    }

    with (
        patch.object(
            scheduler,
            "_get_document_scheduler_settings",
            return_value=settings,
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=fake_get_user_db_session,
        ),
        patch(
            "local_deep_research.settings.manager.SettingsManager",
            return_value=settings_manager,
        ),
        patch(
            "local_deep_research.research_library.services.download_service.DownloadService",
            return_value=download_service,
        ),
    ):
        scheduler._process_user_documents("testuser")

    # No ResearchResource prefetch ran with extract_text off.
    assert db.query.call_count == 1, (
        "extract_text=False must skip the resource prefetch; "
        f"saw {db.query.call_count} queries."
    )

    # Downloads were still queued, and no text extraction was attempted.
    download_service.queue_research_downloads.assert_called_once_with(
        "research-x"
    )
    download_service.download_as_text.assert_not_called()

    # The pass completed, so last_run advances.
    last_run_calls = [
        call
        for call in settings_manager.set_setting.call_args_list
        if call.args and call.args[0] == "document_scheduler.last_run"
    ]
    assert last_run_calls, (
        "last_run must advance on a completed extract_text=False pass."
    )


# ---------------------------------------------------------------------------
# Change 3 (continued): RAG indexing of research downloads has been RETIRED from
# _process_user_documents. It now lives in the unified
# _reconcile_unindexed_documents reconciler (its own scheduled job). These tests
# assert the inline RAG block is gone: generate_rag no longer drives any RAG
# work in this download/extract pass, and the module no longer imports
# LibraryRAGService at all.
# ---------------------------------------------------------------------------


def test_process_user_documents_no_longer_indexes_rag_inline():
    """With generate_rag=True but download/extract OFF, _process_user_documents
    short-circuits (no download/extract work) and never builds a RAG service —
    the inline RAG-indexing block has been retired into the reconciler. There is
    no longer a RAG wrapper rollback path here to exercise.
    """
    from datetime import datetime, UTC

    import local_deep_research.scheduler.background as bg
    from local_deep_research.scheduler.background import (
        BackgroundJobScheduler,
        DocumentSchedulerSettings,
    )

    # The retired block was the only consumer of LibraryRAGService at module
    # scope; its removal must hold so a regression can't quietly reintroduce
    # inline indexing.
    assert not hasattr(bg, "LibraryRAGService"), (
        "LibraryRAGService should no longer be imported in background.py — "
        "RAG indexing moved to _reconcile_unindexed_documents."
    )

    BackgroundJobScheduler._instance = None
    with patch("local_deep_research.scheduler.background.BackgroundScheduler"):
        scheduler = BackgroundJobScheduler()

    scheduler.user_sessions["testuser"] = {
        "scheduled_jobs": set(),
        "last_activity": datetime.now(UTC),
    }
    scheduler._credential_store.store("testuser", "testpass")

    settings = DocumentSchedulerSettings(
        download_pdfs=False,
        extract_text=False,
        generate_rag=True,
        last_run="",
    )

    from contextlib import contextmanager as _cm

    db = MagicMock()

    @_cm
    def fake_get_user_db_session(*a, **kw):
        yield db

    with (
        patch.object(
            scheduler,
            "_get_document_scheduler_settings",
            return_value=settings,
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=fake_get_user_db_session,
        ) as mock_session,
        patch(
            "local_deep_research.settings.manager.SettingsManager",
            return_value=MagicMock(),
        ),
    ):
        scheduler._process_user_documents("testuser")

    # generate_rag alone no longer enables this pass: it short-circuits before
    # opening a DB session (download/extract are both off).
    mock_session.assert_not_called()


# ---------------------------------------------------------------------------
# Issue #6758, B1: the PDF research-download path had no content-hash
# pre-check. Two different URLs yielding identical PDF bytes went straight to
# INSERT and relied on UNIQUE(document_hash) -> IntegrityError ->
# safe_rollback, leaving the second resource document-less (re-downloadable,
# reads as "dedup did nothing"). The fix mirrors the text-path pre-check
# above: query Document(document_hash=file_hash) before INSERT and, on a hit,
# link resource.document_id (+ ensure_in_collection) instead of inserting.
# ---------------------------------------------------------------------------


def test_pdf_path_dedups_identical_bytes_across_different_urls(
    session, source_type, library_collection, make_resource, svc
):
    """Two resources with different URLs but byte-identical PDFs share one
    Document; the second resource links to it instead of INSERTing.
    """
    res_a = make_resource("https://example.com/paper-A.pdf")
    res_b = make_resource("https://mirror.example.org/paper-B.pdf")
    pdf_content = b"%PDF-1.4 identical bytes served from two different URLs"
    file_hash = hashlib.sha256(pdf_content).hexdigest()

    # Wire the svc stub for the success path: server-side settings with
    # storage "none" (no filesystem writes), an open egress gate, and one
    # downloader serving identical bytes for both URLs.
    svc.library_root = "/tmp/ldr-test-library"
    svc.legacy_library_root = "/tmp/ldr-test-library"
    svc.settings = MagicMock()
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "none",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True,
        content=pdf_content,
        status_code=200,
        skip_reason=None,
    )
    svc.downloaders = [downloader]

    tracker_a = DownloadTracker(
        url=res_a.url,
        url_hash="hash-6758-B1-A",
        first_resource_id=res_a.id,
        is_downloaded=False,
    )
    tracker_b = DownloadTracker(
        url=res_b.url,
        url_hash="hash-6758-B1-B",
        first_resource_id=res_b.id,
        is_downloaded=False,
    )
    session.add_all([tracker_a, tracker_b])
    session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
    ):
        success_a, _, _ = svc._download_pdf(res_a, tracker_a, session)
        session.commit()
        success_b, _, _ = svc._download_pdf(res_b, tracker_b, session)
        session.commit()

    assert success_a is True
    assert success_b is True

    # Pre-fix the second INSERT raised IntegrityError (UNIQUE document_hash)
    # and rolled back; with the fix exactly one Document exists.
    docs = session.query(Document).filter_by(document_hash=file_hash).all()
    assert len(docs) == 1, (
        f"B1 dedup should have produced exactly one Document, got {len(docs)}"
    )
    canonical = docs[0]
    assert canonical.file_type == "pdf"

    session.refresh(res_b)
    assert res_b.document_id == canonical.id, (
        "Second resource should be linked to the canonical Document."
    )

    links = (
        session.query(DocumentCollection)
        .filter_by(
            document_id=canonical.id, collection_id=library_collection.id
        )
        .all()
    )
    assert len(links) == 1, (
        "Canonical document must be linked to the Library collection "
        "exactly once."
    )


def test_pdf_path_still_inserts_for_distinct_bytes(
    session, source_type, library_collection, make_resource, svc
):
    """Guard against an over-eager dedup: different PDF bytes still produce
    distinct Documents (the pre-check must only fire on hash equality).
    """
    res_a = make_resource("https://example.com/unique-A.pdf")
    res_b = make_resource("https://example.com/unique-B.pdf")
    pdf_a = b"%PDF-1.4 unique bytes A"
    pdf_b = b"%PDF-1.4 unique bytes B"

    svc.library_root = "/tmp/ldr-test-library"
    svc.legacy_library_root = "/tmp/ldr-test-library"
    svc.settings = MagicMock()
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "none",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.side_effect = [
        MagicMock(
            is_success=True,
            content=pdf_a,
            status_code=200,
            skip_reason=None,
        ),
        MagicMock(
            is_success=True,
            content=pdf_b,
            status_code=200,
            skip_reason=None,
        ),
    ]
    svc.downloaders = [downloader]

    tracker_a = DownloadTracker(
        url=res_a.url,
        url_hash="hash-6758-B1-distinct-A",
        first_resource_id=res_a.id,
        is_downloaded=False,
    )
    tracker_b = DownloadTracker(
        url=res_b.url,
        url_hash="hash-6758-B1-distinct-B",
        first_resource_id=res_b.id,
        is_downloaded=False,
    )
    session.add_all([tracker_a, tracker_b])
    session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
    ):
        success_a, _, _ = svc._download_pdf(res_a, tracker_a, session)
        session.commit()
        success_b, _, _ = svc._download_pdf(res_b, tracker_b, session)
        session.commit()

    assert success_a is True
    assert success_b is True
    assert session.query(Document).count() == 2
    session.refresh(res_a)
    session.refresh(res_b)
    doc_a = session.get(Document, res_a.document_id)
    doc_b = session.get(Document, res_b.document_id)
    assert doc_a.id != doc_b.id
    assert doc_a.document_hash == hashlib.sha256(pdf_a).hexdigest()
    assert doc_b.document_hash == hashlib.sha256(pdf_b).hexdigest()


# ---------------------------------------------------------------------------
# Issue #6758 follow-up: storage upgrade on the content-hash hit path.
# First download with storage "none", then identical bytes with storage
# "database" — the canonical Document must gain a blob instead of staying
# storage_mode="none" with no saved PDF, and both trackers must agree.
# ---------------------------------------------------------------------------


def test_pdf_dedup_upgrades_storage_from_none_to_database(
    session, source_type, library_collection, make_resource, svc
):
    """Reproduces the AI-review failure: none → database must upgrade."""
    from local_deep_research.database.models.library import DocumentBlob

    res_a = make_resource("https://example.com/storage-A.pdf")
    res_b = make_resource("https://mirror.example.org/storage-B.pdf")
    pdf_content = b"%PDF-1.4 identical bytes for storage-upgrade test"
    file_hash = hashlib.sha256(pdf_content).hexdigest()

    svc.library_root = "/tmp/ldr-test-library"
    svc.legacy_library_root = "/tmp/ldr-test-library"
    svc.settings = MagicMock()
    # Mutable mode so the first call stores nothing and the second stores.
    current_mode = {"mode": "none"}
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": current_mode["mode"],
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True,
        content=pdf_content,
        status_code=200,
        skip_reason=None,
    )
    svc.downloaders = [downloader]

    tracker_a = DownloadTracker(
        url=res_a.url,
        url_hash="hash-6758-storage-A",
        first_resource_id=res_a.id,
        is_downloaded=False,
    )
    tracker_b = DownloadTracker(
        url=res_b.url,
        url_hash="hash-6758-storage-B",
        first_resource_id=res_b.id,
        is_downloaded=False,
    )
    session.add_all([tracker_a, tracker_b])
    session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
    ):
        success_a, _, _ = svc._download_pdf(res_a, tracker_a, session)
        session.commit()
        # Canonical is storage-mode none with no blob and no path.
        canonical = (
            session.query(Document).filter_by(document_hash=file_hash).one()
        )
        assert canonical.storage_mode == "none"
        assert (
            session.query(DocumentBlob)
            .filter_by(document_id=canonical.id)
            .first()
            is None
        )
        assert tracker_a.file_path is None

        current_mode["mode"] = "database"
        success_b, _, _ = svc._download_pdf(res_b, tracker_b, session)
        session.commit()

    assert success_a is True
    assert success_b is True
    session.refresh(canonical)
    # Upgraded: blob present, mode flipped, second tracker consistent.
    assert canonical.storage_mode == "database"
    assert (
        session.query(DocumentBlob).filter_by(document_id=canonical.id).first()
        is not None
    )
    session.refresh(tracker_b)
    assert tracker_b.file_path == "database"
    assert tracker_b.file_name is None
    # Sibling (first) tracker was backfilled so sync still finds the doc.
    session.refresh(tracker_a)
    assert tracker_a.file_path == "database"
    session.refresh(res_b)
    assert res_b.document_id == canonical.id
    # Still exactly one Document — upgrade, not duplicate.
    assert (
        session.query(Document).filter_by(document_hash=file_hash).count() == 1
    )


def test_pdf_dedup_preserves_existing_database_storage(
    session, source_type, library_collection, make_resource, svc
):
    """First database, second none — must NOT downgrade the canonical."""
    from local_deep_research.database.models.library import DocumentBlob

    res_a = make_resource("https://example.com/preserve-A.pdf")
    res_b = make_resource("https://mirror.example.org/preserve-B.pdf")
    pdf_content = b"%PDF-1.4 identical bytes for preserve test"

    svc.library_root = "/tmp/ldr-test-library"
    svc.legacy_library_root = "/tmp/ldr-test-library"
    svc.settings = MagicMock()
    current_mode = {"mode": "database"}
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": current_mode["mode"],
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True,
        content=pdf_content,
        status_code=200,
        skip_reason=None,
    )
    svc.downloaders = [downloader]

    tracker_a = DownloadTracker(
        url=res_a.url,
        url_hash="hash-6758-preserve-A",
        first_resource_id=res_a.id,
        is_downloaded=False,
    )
    tracker_b = DownloadTracker(
        url=res_b.url,
        url_hash="hash-6758-preserve-B",
        first_resource_id=res_b.id,
        is_downloaded=False,
    )
    session.add_all([tracker_a, tracker_b])
    session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
    ):
        success_a, _, _ = svc._download_pdf(res_a, tracker_a, session)
        session.commit()
        file_hash = hashlib.sha256(pdf_content).hexdigest()
        canonical = (
            session.query(Document).filter_by(document_hash=file_hash).one()
        )
        assert canonical.storage_mode == "database"

        current_mode["mode"] = "none"
        success_b, _, _ = svc._download_pdf(res_b, tracker_b, session)
        session.commit()

    assert success_a is True
    assert success_b is True
    session.refresh(canonical)
    assert canonical.storage_mode == "database"
    assert (
        session.query(DocumentBlob).filter_by(document_id=canonical.id).first()
        is not None
    )
    session.refresh(tracker_b)
    # Second tracker mirrors the canonical's database storage.
    assert tracker_b.file_path == "database"


# ---------------------------------------------------------------------------
# Issue #6758 follow-up: dedup link must still auto-index in the requested
# collection. The old download_resource lookup (Document.resource_id ==
# resource.id) misses the reused canonical (owned by another resource), so
# trigger_auto_index was skipped for duplicates queued elsewhere.
# ---------------------------------------------------------------------------


def test_pdf_dedup_resolves_for_auto_index_in_different_collection(
    session, source_type, library_collection, make_resource, svc
):
    """Identical bytes under a second resource into a different collection
    must resolve to the canonical Document (old resource_id-only lookup
    returned None and skipped indexing)."""
    from local_deep_research.research_library.utils import (
        get_document_for_resource,
    )

    second_collection = Collection(
        id=str(uuid.uuid4()),
        name="Second",
        is_default=False,
        collection_type="user",
    )
    session.add(second_collection)
    session.commit()

    res_a = make_resource("https://example.com/index-A.pdf")
    res_b = make_resource("https://mirror.example.org/index-B.pdf")
    pdf_content = b"%PDF-1.4 identical bytes for auto-index test"

    svc.library_root = "/tmp/ldr-test-library"
    svc.legacy_library_root = "/tmp/ldr-test-library"
    svc.settings = MagicMock()
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "none",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True,
        content=pdf_content,
        status_code=200,
        skip_reason=None,
    )
    svc.downloaders = [downloader]

    tracker_a = DownloadTracker(
        url=res_a.url,
        url_hash="hash-6758-index-A",
        first_resource_id=res_a.id,
        is_downloaded=False,
    )
    tracker_b = DownloadTracker(
        url=res_b.url,
        url_hash="hash-6758-index-B",
        first_resource_id=res_b.id,
        is_downloaded=False,
    )
    session.add_all([tracker_a, tracker_b])
    session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
    ):
        svc._download_pdf(res_a, tracker_a, session)
        session.commit()
        # Second resource explicitly queued into the other collection.
        svc._download_pdf(
            res_b, tracker_b, session, collection_id=second_collection.id
        )
        session.commit()

    session.refresh(res_b)
    assert res_b.document_id is not None
    # Old lookup misses (canonical owned by res_a).
    old_lookup = session.query(Document).filter_by(resource_id=res_b.id).first()
    assert old_lookup is None
    # Helper used by the fixed download_resource finds the canonical.
    resolved = get_document_for_resource(session, res_b)
    assert resolved is not None
    assert resolved.id == res_b.document_id
    # Canonical linked into the requested (different) collection.
    link = (
        session.query(DocumentCollection)
        .filter_by(document_id=resolved.id, collection_id=second_collection.id)
        .first()
    )
    assert link is not None


# ---------------------------------------------------------------------------
# Issue #6758 B2: legacy url_hash rows must still match post-upgrade lookups.
# Seeds pre-B2 spellings and asserts the compat path finds them.
# ---------------------------------------------------------------------------


def test_legacy_url_hash_rows_still_matched(session, make_resource):
    """Mixed-scheme production state: old rows found by new lookups."""
    from local_deep_research.research_library.utils import (
        find_tracker_by_url,
        get_legacy_url_hashes,
        get_url_hash,
        get_url_hash_candidates,
    )

    # URL shapes that B2 re-keys (tracking params, fragment, default port,
    # userinfo) — plain URLs keep their hash and are covered elsewhere.
    urls = [
        "https://example.com/paper?utm_source=newsletter",
        "https://example.com/paper#section-2",
        "https://example.com:443/paper",
        "https://user:[email protected]/paper",
        "https://example.com/s?z=1&a=2",
    ]
    for i, url in enumerate(urls):
        res = make_resource(f"https://seed.example/{i}")
        new_hash = get_url_hash(url)
        legacy_hashes = get_legacy_url_hashes(url)
        # At least one legacy spelling must diverge, otherwise this URL
        # does not exercise the mixed-scheme path.
        assert any(h != new_hash for h in legacy_hashes), url
        assert new_hash in get_url_hash_candidates(url)
        for legacy in legacy_hashes:
            assert legacy in get_url_hash_candidates(url)

        # Seed the download-service legacy spelling (the most common
        # pre-B2 writer) and verify the compat lookup finds it.
        import re as _re

        legacy_dl = _re.sub(r"^https?://", "", url)
        legacy_dl = _re.sub(r"^www\.", "", legacy_dl).rstrip("/")
        if "?" in legacy_dl:
            base, query = legacy_dl.split("?", 1)
            legacy_dl = f"{base}?{'&'.join(sorted(query.split('&')))}"
        legacy_dl_hash = hashlib.sha256(legacy_dl.lower().encode()).hexdigest()

        tracker = DownloadTracker(
            url=url,
            url_hash=legacy_dl_hash,
            first_resource_id=res.id,
            is_downloaded=True,
            file_path="pdfs/legacy.pdf",
        )
        session.add(tracker)
        session.commit()

        found = find_tracker_by_url(session, url)
        assert found is not None, f"legacy row missed for {url}"
        assert found.id == tracker.id

        # Raw-legacy spelling (cascade_helper's old bare-hash writer) also
        # resolves when seeded instead.
        session.delete(found)
        session.commit()
        raw_hash = hashlib.sha256(url.lower().encode()).hexdigest()
        tracker2 = DownloadTracker(
            url=url,
            url_hash=raw_hash,
            first_resource_id=res.id,
            is_downloaded=True,
            file_path="pdfs/legacy.pdf",
        )
        session.add(tracker2)
        session.commit()
        found2 = find_tracker_by_url(session, url)
        assert found2 is not None, f"raw legacy row missed for {url}"
        assert found2.id == tracker2.id
        session.delete(found2)
        session.commit()


# ---------------------------------------------------------------------------
# Minor: new failure branches need coverage — dedup-path collection-lookup
# fallback and canonical_url_key exception fallback.
# ---------------------------------------------------------------------------


def test_pdf_dedup_links_without_collection_on_lookup_failure(
    session, source_type, library_collection, make_resource, svc
):
    """When get_default_library_id raises on the dedup path, the resource
    still links to the canonical Document (collection link skipped)."""
    res_a = make_resource("https://example.com/fallback-A.pdf")
    res_b = make_resource("https://mirror.example.org/fallback-B.pdf")
    pdf_content = b"%PDF-1.4 identical bytes for fallback test"
    file_hash = hashlib.sha256(pdf_content).hexdigest()

    svc.library_root = "/tmp/ldr-test-library"
    svc.legacy_library_root = "/tmp/ldr-test-library"
    svc.settings = MagicMock()
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "none",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True,
        content=pdf_content,
        status_code=200,
        skip_reason=None,
    )
    svc.downloaders = [downloader]

    tracker_a = DownloadTracker(
        url=res_a.url,
        url_hash="hash-6758-fallback-A",
        first_resource_id=res_a.id,
        is_downloaded=False,
    )
    tracker_b = DownloadTracker(
        url=res_b.url,
        url_hash="hash-6758-fallback-B",
        first_resource_id=res_b.id,
        is_downloaded=False,
    )
    session.add_all([tracker_a, tracker_b])
    session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
    ):
        svc._download_pdf(res_a, tracker_a, session)
        session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            side_effect=RuntimeError("library lookup down"),
        ),
    ):
        success_b, _, _ = svc._download_pdf(res_b, tracker_b, session)
        session.commit()

    assert success_b is True
    canonical = session.query(Document).filter_by(document_hash=file_hash).one()
    session.refresh(res_b)
    assert res_b.document_id == canonical.id


def test_normalize_url_for_hash_falls_back_on_canonical_failure():
    """canonical_url_key raising must not break hashing (raw fallback)."""
    from local_deep_research.research_library import utils as utils_mod

    # canonical_url_key is lazily imported inside normalize_url_for_hash
    # from utilities.url_utils, so patch it at the source.
    with patch(
        "local_deep_research.utilities.url_utils.canonical_url_key",
        side_effect=RuntimeError("boom"),
    ):
        result = utils_mod.normalize_url_for_hash(
            "https://example.com/fallback-unique-6758?b=2&a=1"
        )
        # Falls back to raw URL, then legacy strip/sort/lower.
        assert result == "example.com/fallback-unique-6758?a=1&b=2"
        hashed = utils_mod.get_url_hash(
            "https://example.com/fallback-unique-6758b"
        )
        assert len(hashed) == 64


# ---------------------------------------------------------------------------
# Minor: same URL serving updated bytes must not collapse to one Document.
# ---------------------------------------------------------------------------


def test_same_url_updated_bytes_creates_new_document(
    session, source_type, library_collection, make_resource, svc
):
    """Same URL, different bytes (server updated file) → new Document.

    Guards against an over-eager content-hash dedup that would link the
    second download to the stale canonical instead of inserting.
    """
    # Two resources sharing one URL (e.g. two researches citing it) whose
    # server bytes change between downloads.
    res_a = make_resource("https://example.com/evolving.pdf")
    res_b = make_resource("https://example.com/evolving.pdf")
    pdf_a = b"%PDF-1.4 version one bytes"
    pdf_b = b"%PDF-1.4 version two bytes - updated on server"
    assert (
        hashlib.sha256(pdf_a).hexdigest() != hashlib.sha256(pdf_b).hexdigest()
    )

    svc.library_root = "/tmp/ldr-test-library"
    svc.legacy_library_root = "/tmp/ldr-test-library"
    svc.settings = MagicMock()
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "none",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.side_effect = [
        MagicMock(
            is_success=True, content=pdf_a, status_code=200, skip_reason=None
        ),
        MagicMock(
            is_success=True, content=pdf_b, status_code=200, skip_reason=None
        ),
    ]
    svc.downloaders = [downloader]

    tracker_a = DownloadTracker(
        url=res_a.url,
        url_hash="hash-6758-evolving-A",
        first_resource_id=res_a.id,
        is_downloaded=False,
    )
    tracker_b = DownloadTracker(
        url=res_b.url,
        url_hash="hash-6758-evolving-B",
        first_resource_id=res_b.id,
        is_downloaded=False,
    )
    session.add_all([tracker_a, tracker_b])
    session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
    ):
        success_a, _, _ = svc._download_pdf(res_a, tracker_a, session)
        session.commit()
        success_b, _, _ = svc._download_pdf(res_b, tracker_b, session)
        session.commit()

    assert success_a is True
    assert success_b is True
    # Different bytes → two Documents, second not linked to first.
    assert session.query(Document).count() == 2
    session.refresh(res_b)
    first_doc = (
        session.query(Document)
        .filter_by(document_hash=hashlib.sha256(pdf_a).hexdigest())
        .one()
    )
    second_doc = (
        session.query(Document)
        .filter_by(document_hash=hashlib.sha256(pdf_b).hexdigest())
        .one()
    )
    assert first_doc.id != second_doc.id
    assert res_b.document_id == second_doc.id


# ---------------------------------------------------------------------------
# Issue #6760 follow-up blocker 1: tracker writer and readers must agree on
# mixed-case query URLs. The writer double-normalized
# (``_normalize_url(canonical(url))`` then ``get_url_hash``, i.e.
# sort(raw-case).lower() followed by a second sort(lower)), while the
# readers normalize once — so ``sort(raw).lower() != sort(lower(raw))``
# whenever mixed-case keys change relative order. The writer hash landed in
# none of the four reader candidates: sync then ran
# ``delete_document_completely`` on an intact document, and re-download
# INSERTed a duplicate tracker → ``IntegrityError`` on
# ``uq_download_tracker_url_hash`` (URL bricked).
# ---------------------------------------------------------------------------


def test_writer_hash_in_candidates_mixed_case_battery(svc):
    """The actual writer hash must be a reader candidate (mixed-case URLs).

    Uses ``DownloadService._get_url_hash`` (the production writer), not the
    reader spelling — the previous compat test built expectations with the
    reader and could not catch the divergence.
    """
    from local_deep_research.research_library.utils import (
        get_url_hash_candidates,
    )

    urls = [
        # Exact shapes from the follow-up review.
        "https://example.com/paper.pdf?q=x&Lang=fr&UserId=1",
        "https://example.com/p?a=1&Z=2&utm_source=twitter",
        "https://example.com/p?a=1&Z=2#frag",
        # Extra mixed-case / order permutations.
        "https://example.com/p?Z=2&a=1",
        "https://example.com/p?A=1&b=2&C=3",
        "HTTPS://EXAMPLE.COM/Paper.PDF?Q=X&LANG=FR",
        "https://example.com/p?UserId=1&q=x&Lang=fr",
    ]
    for url in urls:
        writer = svc._get_url_hash(url)
        assert writer in get_url_hash_candidates(url), url

    # Mixed-case order-insensitivity still holds through the writer.
    assert svc._get_url_hash(
        "https://example.com/p?a=1&Z=2"
    ) == svc._get_url_hash("https://example.com/p?Z=2&a=1")


# ---------------------------------------------------------------------------
# Issue #6760 follow-up blocker 2: the dedup mirror must not copy
# file_path sentinels, and sync must not delete documents behind one.
#
# Chain: database-mode PDF → blob purged (``file_path="blob_deleted"``,
# storage ``none``, document stays complete) → identical bytes arrive from
# a second URL with the default ``none`` mode → no upgrade → the
# ``"blob_deleted"`` sentinel was mirrored verbatim into the duplicate
# tracker → sync's mid-branch (which only accepted ``== "database"`` as
# blob-backed) fell to the file-exists check, always missed, and ran
# ``delete_document_completely`` on the text/chunks/vectors the purge
# deliberately preserved.
# ---------------------------------------------------------------------------


def test_pdf_dedup_does_not_mirror_blob_deleted_sentinel(
    session, source_type, library_collection, make_resource, svc
):
    """Post-purge canonical + identical bytes elsewhere → path-less tracker."""
    from local_deep_research.constants import FILE_PATH_BLOB_DELETED

    res_a = make_resource("https://example.com/purged-A.pdf")
    res_b = make_resource("https://mirror.example.org/purged-B.pdf")
    pdf_content = b"%PDF-1.4 identical bytes for sentinel-mirror test"
    file_hash = hashlib.sha256(pdf_content).hexdigest()

    # Canonical in its post-blob-purge state: complete document, storage
    # none, sentinel file_path (see document_deletion blob-purge path).
    canonical = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        resource_id=res_a.id,
        research_id=res_a.research_id,
        document_hash=file_hash,
        original_url=res_a.url,
        file_size=len(pdf_content),
        file_type="pdf",
        title=res_a.title,
        status=DocumentStatus.COMPLETED,
        storage_mode="none",
        file_path=FILE_PATH_BLOB_DELETED,
    )
    session.add(canonical)
    session.commit()

    svc.library_root = "/tmp/ldr-test-library"
    svc.legacy_library_root = "/tmp/ldr-test-library"
    svc.settings = MagicMock()
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": "none",
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    downloader = MagicMock()
    downloader.can_handle.return_value = True
    downloader.download_with_result.return_value = MagicMock(
        is_success=True,
        content=pdf_content,
        status_code=200,
        skip_reason=None,
    )
    svc.downloaders = [downloader]

    tracker_b = DownloadTracker(
        url=res_b.url,
        url_hash="hash-6760-sentinel-B",
        first_resource_id=res_b.id,
        is_downloaded=False,
    )
    session.add(tracker_b)
    session.commit()

    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
    ):
        success_b, _, _ = svc._download_pdf(res_b, tracker_b, session)
        session.commit()

    assert success_b is True
    session.refresh(tracker_b)
    session.refresh(canonical)
    # Linked to the canonical — not a duplicate Document.
    session.refresh(res_b)
    assert res_b.document_id == canonical.id
    assert (
        session.query(Document).filter_by(document_hash=file_hash).count() == 1
    )
    # The sentinel must NOT be mirrored: the duplicate tracker stays
    # path-less (same shape as a fresh none-mode download).
    assert tracker_b.file_path is None
    assert tracker_b.file_name is None
    # Canonical untouched (no downgrade, sentinel preserved on the doc).
    assert canonical.file_path == FILE_PATH_BLOB_DELETED
    assert canonical.storage_mode == "none"


def test_sync_leaves_sentinel_tracker_path_alone(mocker):
    """Sync mid-branch: a sentinel tracker path counts as found, no delete."""
    from contextlib import contextmanager
    from unittest.mock import Mock

    from local_deep_research.constants import FILE_PATH_BLOB_DELETED
    from local_deep_research.research_library.services.library_service import (
        LibraryService,
    )

    with patch.object(LibraryService, "__init__", lambda self, username: None):
        service = LibraryService.__new__(LibraryService)
        service.username = "test_user"

    mock_doc = Mock()
    mock_doc.id = "doc-sentinel-1"
    mock_doc.title = "Purged Doc"
    mock_doc.original_url = "https://example.com/purged-A.pdf"
    mock_doc.storage_mode = "none"
    mock_doc.file_path = FILE_PATH_BLOB_DELETED

    mock_tracker = Mock()
    mock_tracker.file_path = FILE_PATH_BLOB_DELETED
    mock_tracker.is_downloaded = True

    mock_session = MagicMock()
    docs_q = MagicMock()
    docs_q.filter_by.return_value.filter.return_value.options.return_value.all.return_value = [
        mock_doc
    ]

    def _query_router(model):
        name = getattr(model, "__name__", str(model))
        if "Document" in name and "Blob" not in name:
            return docs_q
        return MagicMock()

    mock_session.query.side_effect = _query_router

    @contextmanager
    def _cm(username, password=None):
        yield mock_session

    mocker.patch(
        "local_deep_research.research_library.services.library_service.get_user_db_session",
        side_effect=_cm,
    )
    mocker.patch(
        "local_deep_research.research_library.services.library_service.find_tracker_for_document",
        return_value=mock_tracker,
    )
    delete_mock = mocker.patch(
        "local_deep_research.research_library.deletion.utils.cascade_helper.CascadeHelper.delete_document_completely"
    )

    stats = service.sync_library_with_filesystem()

    assert stats["files_found"] == 1
    assert stats["files_missing"] == 0
    delete_mock.assert_not_called()


# ---------------------------------------------------------------------------
# Real-machinery contention test (reviewer ask #1 from PR #6720):
# ---------------------------------------------------------------------------
# Both earlier tests fake ``get_user_db_session`` with a do-nothing CM, which
# pins call ordering but NOT the actual SQLite lock-release semantics. The
# scoped-sessions fix relies on ``session_context.py``'s depth-0 rollback in
# the thread-local session manager: exiting the outermost
# ``get_user_db_session`` block on the scheduler thread must release the
# SQLite SHARED lock that an explicit BEGIN / autobegin read transaction
# holds, or competing writers (rate-limit tracker, research queue, UI) time
# out with ``database is locked``.
#
# This test wires the REAL ``get_user_db_session`` machinery to a real
# file-backed WAL SQLite database and, from a second connection inside the
# mocked ``download_as_text`` side-effect, runs:
#
#   * ``BEGIN IMMEDIATE; INSERT; COMMIT``         -- exercises the write lock
#   * ``PRAGMA wal_checkpoint(TRUNCATE)``        -- exercises the read-snapshot
#                                                   blocker (a TRUNCATE blocks
#                                                   on the longest-running read
#                                                   transaction)
#
# Both must succeed (no ``database is locked``) BETWEEN downloads. On main
# the read session is held for the whole loop, so the WAL checkpoint blocks
# or the second INSERT times out under a short ``busy_timeout``. On this
# branch the read session closes before the first download and the worker
# holds no SQLite transaction while network IO runs.
# ---------------------------------------------------------------------------


def _build_wal_engine(tmp_path):
    """Build a file-backed WAL SQLite engine with the app's metadata schema.

    Real WAL mode (not :memory:) because the contention the fix removes is
    only observable on a checkpointable database — :memory: has no WAL file
    to checkpoint and would silently pass on main.
    """
    from sqlalchemy import event
    from sqlalchemy.pool import QueuePool

    db_path = tmp_path / "contention_test.db"
    engine = create_engine(
        f"sqlite:///{db_path}",
        connect_args={"check_same_thread": False, "timeout": 30},
        poolclass=QueuePool,
    )

    # WAL + the same pragmas the encrypted_db engine applies. Registered as
    # a connect listener so every connection (including the one the
    # scheduler's thread-local session checks out) gets them, not just the
    # first.
    @event.listens_for(engine, "connect")
    def _enable_wal(dbapi, _connection_record):
        cursor = dbapi.cursor()
        cursor.execute("PRAGMA journal_mode = WAL")
        cursor.execute("PRAGMA synchronous = NORMAL")
        cursor.execute("PRAGMA foreign_keys = ON")
        cursor.execute("PRAGMA busy_timeout = 5000")
        cursor.close()

    Base.metadata.create_all(engine)
    return engine, db_path


def _seed_research_and_resources(engine, n_resources):
    """Insert one completed research row plus ``n_resources`` resource rows."""
    from sqlalchemy import text as _text

    Session = sessionmaker(bind=engine)
    research_id = str(uuid.uuid4())
    with Session() as session:
        research = ResearchHistory(
            id=research_id,
            query="contention test",
            mode="quick",
            status="completed",
            created_at="2026-09-21T00:00:00",
            completed_at="2026-09-21T00:00:01",
            title="Contention Test Research",
        )
        session.add(research)
        session.flush()
        for i in range(n_resources):
            session.add(
                ResearchResource(
                    research_id=research_id,
                    title=f"Resource {i}",
                    url=f"https://example.com/{i}.pdf",
                    source_type="academic",
                    created_at="2026-09-21T00:00:00",
                )
            )
        session.commit()
        # Probe table for the contention test. Created upfront (committed)
        # so the read session's staged INSERT and the contender's INSERT
        # are pure DML holding real locks, not DDL.
        session.execute(
            _text(
                "CREATE TABLE IF NOT EXISTS _contention_probe ("
                "id INTEGER PRIMARY KEY, label TEXT, ts TEXT)"
            )
        )
        session.commit()
        return research_id


def test_download_loop_releases_read_snapshot_real_sqlite(tmp_path):
    """Real-WAL contention probe: a competing BEGIN IMMEDIATE + INSERT +
    COMMIT plus a WAL TRUNCATE checkpoint must BOTH succeed between
    downloads on the scoped-sessions branch.

    The read session holds an explicit staged transaction (SELECT +
    uncommitted INSERT into ``_contention_probe``). If
    ``session_context``'s depth-0 rollback regresses to a no-op, that
    transaction survives into the download loop: the competing write
    fails with ``database is locked`` and the checkpoint returns
    ``busy=1``. Asserting the checkpoint ``busy`` column (not just
    exceptions) is required — TRUNCATE reports contention via busy=1
    without raising.
    """
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )
    from local_deep_research.scheduler.background import (
        BackgroundJobScheduler,
        DocumentSchedulerSettings,
    )

    engine, db_path = _build_wal_engine(tmp_path)
    SessionFactory = sessionmaker(bind=engine)
    _seed_research_and_resources(engine, n_resources=3)

    def _contending_write() -> None:
        """From a second connection: short-busy_timeout BEGIN IMMEDIATE +
        INSERT + COMMIT. A leaked RESERVED lock (staged/uncommitted write)
        from the scheduler thread surfaces as ``database is locked`` under
        the 200ms busy_timeout."""
        contender = SessionFactory()
        try:
            conn = contender.connection().connection.dbapi_connection
            conn.execute("PRAGMA busy_timeout = 200")
            conn.execute("BEGIN IMMEDIATE")
            conn.execute(
                "INSERT INTO _contention_probe (label, ts) "
                "VALUES (1, datetime('now'))"
            )
            conn.execute("COMMIT")
        finally:
            contender.close()

    def _contending_checkpoint() -> int:
        """From a second connection: WAL TRUNCATE checkpoint. Returns the
        ``busy`` column. A leaked transaction (SHARED read or RESERVED
        write) makes TRUNCATE return ``busy=1`` instead of raising, so
        callers must assert ``busy == 0`` — checking only for exceptions
        silently passes while blocked."""
        contender = SessionFactory()
        try:
            conn = contender.connection().connection.dbapi_connection
            conn.execute("PRAGMA busy_timeout = 200")
            # wal_checkpoint(TRUNCATE) is the assertion: it cannot
            # truncate the WAL while a reader/writer transaction is open.
            # The pragma returns (busy, log, checkpointed) rows; busy=1
            # means blocked. It does NOT raise on contention.
            rows = list(
                conn.execute("PRAGMA wal_checkpoint(TRUNCATE)").fetchall()
            )
            return int(rows[0][0]) if rows else 0
        finally:
            contender.close()

    # (kind, error, checkpoint_busy). write probes leave busy=None;
    # checkpoint probes record the busy column (0 == released, 1 == blocked).
    contention_attempts: list[tuple[str, str | None, int | None]] = []

    # Track the ordering of session open/close and downloads so the
    # failure surface is concrete on main (read session still open when
    # the first download runs).
    events: list[str] = []
    # Proves the read session actually held a transaction before close
    # (so the test is sensitive to a missing rollback) and that the
    # outermost exit released it (pool connection returned).
    held_proof: dict = {}
    _is_first_session = [True]

    @contextmanager
    def _tracking_session_cm(username, password):
        from sqlalchemy import text as _text

        events.append("session_open")
        with get_user_db_session(username, password) as session:
            if _is_first_session[0]:
                _is_first_session[0] = False
                # Explicit held transaction: a SELECT (SHARED read mark)
                # plus a staged, uncommitted INSERT (RESERVED write lock).
                # With the app's pysqlite ``isolation_level=""`` a pure
                # SELECT holds no WAL mark — implicit BEGIN fires only
                # before DML — so the staged write is what reproduces
                # production pain (competing writer gets
                # ``database is locked``; checkpoint returns busy=1).
                # If session_context's depth-0 rollback regresses to a
                # no-op, this transaction survives into the download loop
                # and both probes below fail.
                session.execute(
                    _text("SELECT id FROM research_history LIMIT 1")
                )
                session.execute(
                    _text(
                        "INSERT INTO _contention_probe (label, ts) "
                        "VALUES ('staged', datetime('now'))"
                    )
                )
                assert session.in_transaction(), (
                    "read session must hold a transaction to prove release"
                )
                held_proof["was_in_transaction"] = True
            yield session
        events.append("session_close")
        if "was_in_transaction" in held_proof and "released" not in held_proof:
            from local_deep_research.database.thread_local_session import (
                thread_session_manager,
            )

            cached = thread_session_manager.get_current_session()
            # After the outermost exit the depth-0 rollback must have
            # released the transaction and returned the pooled connection.
            # A no-op rollback leaves the cached session in-transaction
            # with its connection still checked out.
            still_in_txn = bool(
                cached.in_transaction() if cached is not None else False
            )
            held_proof["released"] = not still_in_txn
            held_proof["pool_checkedout"] = engine.pool.checkedout()

    def _download_as_text(resource_id):
        events.append(f"download:{resource_id}")
        # The probe: from a SECOND connection, write and checkpoint.
        # If the scheduler thread is still holding the staged transaction,
        # the write raises ``database is locked`` and the checkpoint
        # returns busy=1.
        try:
            _contending_write()
            contention_attempts.append(("write", None, None))
        except Exception as exc:  # noqa: BLE001 - capture SQLite error
            contention_attempts.append(("write", str(exc), None))
        try:
            busy = _contending_checkpoint()
            if busy != 0:
                contention_attempts.append(
                    ("checkpoint", f"wal_checkpoint busy={busy}", busy)
                )
            else:
                contention_attempts.append(("checkpoint", None, busy))
        except Exception as exc:  # noqa: BLE001 - capture SQLite error
            contention_attempts.append(("checkpoint", str(exc), None))
        return (True, None)

    BackgroundJobScheduler._instance = None
    with patch("local_deep_research.scheduler.background.BackgroundScheduler"):
        scheduler = BackgroundJobScheduler()
    scheduler.user_sessions["testuser"] = {
        "scheduled_jobs": set(),
        "last_activity": None,
    }
    scheduler._credential_store.store("testuser", "testpass")

    settings = DocumentSchedulerSettings(
        download_pdfs=False,
        extract_text=True,
        generate_rag=False,
        last_run="",
    )

    download_service_mock = MagicMock()
    download_service_mock.__enter__ = MagicMock(
        return_value=download_service_mock
    )
    download_service_mock.__exit__ = MagicMock(return_value=False)
    download_service_mock.download_as_text.side_effect = _download_as_text
    download_service_mock.queue_research_downloads.return_value = 0

    settings_manager = MagicMock()
    settings_manager.get_settings_snapshot.return_value = {
        "search.tool": "searxng"
    }

    with (
        patch.object(
            scheduler,
            "_get_document_scheduler_settings",
            return_value=settings,
        ),
        patch.object(db_manager, "has_encryption", False),
        patch.object(
            db_manager,
            "open_user_database",
            return_value=engine,
        ),
        patch.object(
            db_manager,
            "create_thread_safe_session_for_metrics",
            side_effect=lambda _username, _password: SessionFactory(),
        ),
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=_tracking_session_cm,
        ),
        patch(
            "local_deep_research.settings.manager.SettingsManager",
            return_value=settings_manager,
        ),
        patch(
            "local_deep_research.research_library.services.download_service.DownloadService",
            return_value=download_service_mock,
        ),
        patch(
            "local_deep_research.research_library.utils.is_downloadable_url",
            return_value=True,
        ),
    ):
        scheduler._process_user_documents("testuser")

    # All three resources were processed.
    download_events = [e for e in events if e.startswith("download:")]
    assert download_events == [
        "download:1",
        "download:2",
        "download:3",
    ], f"downloads must run for every resource; got {download_events}"

    # Between every download the WAL allowed a competing writer AND a
    # checkpoint. If the read session leaked into the loop (pre-fix
    # whole-pass session, or a depth-0 rollback regression), the write
    # carries ``database is locked`` and the checkpoint returns busy=1
    # under the 200ms busy_timeout.
    assert contention_attempts, "no contention probes were run"
    assert held_proof.get("was_in_transaction") is True, (
        "read session never held a transaction — the test would pass "
        "even with rollback disabled and proves nothing"
    )
    assert held_proof.get("released") is True, (
        "read session transaction survived its outermost close "
        f"(pool checkedout={held_proof.get('pool_checkedout')}); "
        "session_context depth-0 rollback did not release it"
    )
    for kind, err, busy in contention_attempts:
        assert err is None, (
            f"Contention probe failed for {kind}: {err!r}. The "
            "scoped-sessions fix relies on the depth-0 rollback in "
            "session_context.get_user_db_session releasing the SQLite "
            "transaction between downloads; a regression there "
            "would let the whole pass hold the WAL and block any "
            "competing writer (production: rate-limit tracker, "
            "research queue, UI)."
        )
        if kind == "checkpoint":
            assert busy == 0, (
                f"WAL checkpoint blocked (busy={busy}): a read/write "
                "transaction survived into the download loop"
            )

    # And the read session actually closed before the first download —
    # the structural contract the test pins. Two ``session_open`` /
    # ``session_close`` pairs are expected: the read session for
    # research + resources, and the final write session for the
    # ``last_run`` update.
    open_indices = [i for i, e in enumerate(events) if e == "session_open"]
    close_indices = [i for i, e in enumerate(events) if e == "session_close"]
    assert len(open_indices) >= 2 and len(close_indices) >= 2, (
        f"expected at least 2 outermost session open/close pairs; "
        f"events={events}"
    )
    first_close = close_indices[0]
    first_download = next(
        i for i, e in enumerate(events) if e.startswith("download:")
    )
    assert first_close < first_download, (
        "The read session must be closed before the first download so "
        f"no SQLite read snapshot is held during network IO. Events: {events}"
    )
    assert open_indices[1] > close_indices[0], (
        "The write session must open AFTER the read session closes. "
        f"Events: {events}"
    )

    # Cleanup: release the thread-local session (which is bound to this
    # test's engine) so later tests don't reuse a disposed engine, then
    # dispose the engine so the WAL/SHM handles are released.
    try:
        from local_deep_research.database.thread_local_session import (
            thread_session_manager,
        )

        thread_session_manager.cleanup_thread()
    except Exception:
        pass
    try:
        from local_deep_research.security.egress.audit_hook import (
            clear_active_context as _clear_ctx,
        )

        _clear_ctx()
    except Exception:
        pass
    engine.dispose()
    if db_path.exists():
        db_path.unlink()


# ---------------------------------------------------------------------------
# Egress-backstop survival test (reviewer ask #2 from PR #6720):
# ---------------------------------------------------------------------------
# The scoped-sessions refactor asserts that the PEP-578 audit-hook egress
# backstop survives the read session closing. With no session in the loop
# the backstop becomes the SECONDARY net that still gates outbound HTTP from
# this worker thread — losing it would mean scheduled downloads could
# egress to a host the user's policy forbids, while the primary PEPs in
# DownloadService still block. The earlier tests either mocked
# ``_arm_egress_backstop`` entirely or wrapped it in the same ``with`` block
# as the queries, so a regression that cleared the context at the read
# session close (the most natural mistake) was invisible.
#
# This test calls the REAL ``_arm_egress_backstop`` and asserts
# ``get_active_context()`` is non-None inside the download loop's
# ``download_as_text`` side-effect — the security-relevant half of
# "session-free loop".
# ---------------------------------------------------------------------------


def test_process_user_documents_egress_backstop_survives_read_session_close(
    tmp_path,
):
    """The egress audit-hook context armed in the read session must still
    be active when ``download_as_text`` runs (after the read session has
    closed). A regression that cleared the context at session close would
    silently disarm the secondary net for every scheduled download.
    """
    from datetime import datetime, UTC

    from local_deep_research.scheduler.background import (
        BackgroundJobScheduler,
        DocumentSchedulerSettings,
    )
    from local_deep_research.security.egress.audit_hook import (
        clear_active_context,
        get_active_context,
    )

    # Build a scheduler with the standard test setup but allow the REAL
    # _arm_egress_backstop to run, so we exercise the thread-local
    # set_active_context() wiring.
    BackgroundJobScheduler._instance = None
    with patch("local_deep_research.scheduler.background.BackgroundScheduler"):
        scheduler = BackgroundJobScheduler()
    scheduler.user_sessions["testuser"] = {
        "scheduled_jobs": set(),
        "last_activity": datetime.now(UTC),
    }
    scheduler._credential_store.store("testuser", "testpass")

    settings = DocumentSchedulerSettings(
        download_pdfs=False,
        extract_text=True,
        generate_rag=False,
        last_run="",
    )

    # Track what get_active_context() returns from inside the download
    # loop. After the read session closes the context MUST still be set
    # for the secondary net to remain active.
    context_inside_loop: list[bool] = []

    def _download_as_text(resource_id):
        from local_deep_research.security.egress.audit_hook import (
            get_active_context as _get,
        )

        context_inside_loop.append(_get() is not None)
        return (True, None)

    # Standard MagicMock session — the contention probe above already
    # proved the real session machinery's release contract; this test is
    # about egress-context lifetime, not SQLite locks.
    from contextlib import contextmanager as _cm

    db = MagicMock()
    research_row = MagicMock(id="r-1", title="T", completed_at=None)
    resource_rows = [
        MagicMock(id=1, url="https://example.com/a.pdf"),
        MagicMock(id=2, url="https://example.com/b.pdf"),
    ]
    research_query = MagicMock()
    research_query.filter.return_value = research_query
    research_query.order_by.return_value = research_query
    research_query.limit.return_value = research_query
    research_query.all.return_value = [research_row]
    resource_query = MagicMock()
    resource_query.filter_by.return_value = resource_query
    resource_query.all.return_value = resource_rows
    db.query.side_effect = [research_query, resource_query]

    @_cm
    def fake_get_user_db_session(*a, **kw):
        yield db

    download_service_mock = MagicMock()
    download_service_mock.__enter__ = MagicMock(
        return_value=download_service_mock
    )
    download_service_mock.__exit__ = MagicMock(return_value=False)
    download_service_mock.download_as_text.side_effect = _download_as_text

    # SettingsManager must hand back a snapshot with a resolvable primary
    # engine — resolve_run_primary_engine is the single source of truth
    # and raises on missing search.tool, which the outer except in
    # _arm_egress_backstop catches and translates into a False return
    # (skipping the work).
    settings_manager = MagicMock()
    settings_manager.get_settings_snapshot.return_value = {
        "search.tool": "searxng"
    }

    # Defence in depth: even if a prior test leaked an active context
    # into this thread, start clean.
    clear_active_context()
    # Captured immediately after _process_user_documents returns but
    # BEFORE this test's own teardown clears the thread-local. If the
    # production @thread_cleanup regresses to a no-op, this stays
    # non-None and the test fails. Checking get_active_context() only
    # after the finally below would mask that regression because the
    # test itself would have cleared it.
    context_after_production_cleanup = None
    try:
        with (
            patch.object(
                scheduler,
                "_get_document_scheduler_settings",
                return_value=settings,
            ),
            patch(
                "local_deep_research.database.session_context.get_user_db_session",
                side_effect=fake_get_user_db_session,
            ),
            patch(
                "local_deep_research.settings.manager.SettingsManager",
                return_value=settings_manager,
            ),
            patch(
                "local_deep_research.research_library.services.download_service.DownloadService",
                return_value=download_service_mock,
            ),
            patch(
                "local_deep_research.research_library.utils.is_downloadable_url",
                return_value=True,
            ),
        ):
            scheduler._process_user_documents("testuser")
            # Production @thread_cleanup runs on exit of
            # _process_user_documents, so by the time we are here the
            # egress context must already be cleared by production —
            # not by this test's finally.
            context_after_production_cleanup = get_active_context()
    finally:
        clear_active_context()

    # Both downloads observed the egress backstop as active — proving the
    # thread-local context survived the read session close.
    assert context_inside_loop, (
        "download_as_text was never called; cannot verify egress survival"
    )
    assert all(context_inside_loop), (
        "The PEP-578 audit-hook egress context was NOT active inside "
        "the download loop. The scoped-sessions fix relies on the "
        "context surviving the read session close (thread-local, so "
        "the session close has no effect); a regression that cleared "
        "the context at session close would silently disarm the "
        "secondary egress net for every scheduled download."
        f" Observed: {context_inside_loop}"
    )

    # And the @thread_cleanup on _process_user_documents cleared the
    # backstop on exit, so the next worker on this thread doesn't
    # inherit the policy from a previous run. Assert the value captured
    # BEFORE this test's own finally — asserting get_active_context()
    # here would pass even if production never cleared, because the
    # finally above already did.
    assert context_after_production_cleanup is None, (
        "@thread_cleanup must clear the egress context on exit so the "
        "next worker on this thread doesn't inherit the previous user's "
        "policy."
    )


# ---------------------------------------------------------------------------
# Issue #7393: a second URL with the same PDF bytes is linked to the
# canonical Document, then its text save runs in a later transaction.
# Production reaches that path with session=None (separate staging and
# text sessions). An inferior extraction must not replace source-derived
# text; an empty canonical document may still be filled, and an
# equal-quality extraction with greater normalized coverage may replace
# it and invalidate indexes. The PDF byte hash stays the canonical id.
# ---------------------------------------------------------------------------

_FILESYSTEM_GATE_ENV = "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE"
_CANONICAL_TEXT = "The original paper has full results and detailed methods."
_LONGER_TEXT = (
    _CANONICAL_TEXT
    + " Additional experiments and conclusions complete the paper."
)
# Longer than the source text on purpose: pdfplumber often emits more
# characters (headers, footers) while ranking below HTML provenance.
_INFERIOR_TEXT = (
    _CANONICAL_TEXT
    + " pdfplumber also copied the running header, footer, and page numbers."
)
_METRICS_SESSION = (
    "local_deep_research.database.thread_local_session.get_metrics_session"
)


def _prepare_session_none_download(svc, session, storage, library_root, pdf):
    """Point _download_pdf(session=None) at the test session and storage mode."""
    svc.library_root = library_root
    svc.legacy_library_root = library_root
    svc.settings = MagicMock()
    svc.settings.get_setting = lambda key, default=None: {
        "research_library.pdf_storage_mode": storage,
        "research_library.max_pdf_size_mb": 50,
    }.get(key, default)
    svc._check_url_against_policy = lambda url: (True, "ok")  # noqa: E731
    svc._extract_text_from_pdf = MagicMock(return_value=_INFERIOR_TEXT)

    def _result(content):
        return MagicMock(
            is_success=True,
            content=content,
            status_code=200,
            skip_reason=None,
        )

    arxiv = MagicMock(spec=ArxivDownloader)
    arxiv.can_handle.side_effect = lambda url: "arxiv.org" in url
    arxiv.download_with_result.return_value = _result(pdf)
    arxiv.download_text_with_source.return_value = ArxivTextResult(
        text=_CANONICAL_TEXT,
        source=ArxivTextSource.ARXIV_HTML,
    )
    generic = MagicMock()
    generic.can_handle.side_effect = lambda url: "arxiv.org" not in url
    generic.download_with_result.return_value = _result(pdf)
    svc.downloaders = [arxiv, generic]
    return arxiv, generic


def _track(session, resource, url_hash):
    tracker = DownloadTracker(
        url=resource.url,
        url_hash=url_hash,
        first_resource_id=resource.id,
        is_downloaded=False,
    )
    session.add(tracker)
    session.flush()
    # Commit expires attached instances. Expunge first, matching
    # download_resource, so the caller keeps the loaded url and title.
    session.expunge(resource)
    session.expunge(tracker)
    session.commit()
    return tracker


def _download_detached(
    svc, session, source_type, library_collection, resource, tracker
):
    with (
        patch(f"{MODULE}.get_source_type_id", return_value=source_type.id),
        patch(
            f"{MODULE}.get_default_library_id",
            return_value=library_collection.id,
        ),
        patch(_METRICS_SESSION, lambda *args, **kwargs: session),
    ):
        result = svc._download_pdf(resource, tracker, None)
    # A poisoned session raises here. Committed staging and text rows then
    # survive the rollback; a text save left only in the open transaction
    # would not.
    session.execute(text("SELECT 1"))
    session.rollback()
    return result


def _seed_index(session, document, library_collection):
    link = (
        session.query(DocumentCollection)
        .filter_by(
            document_id=document.id,
            collection_id=library_collection.id,
        )
        .one()
    )
    link.indexed = True
    link.chunk_count = 1
    link.last_indexed_at = datetime(2026, 10, 1, tzinfo=UTC)
    index = RAGIndex(
        collection_name=f"collection_{library_collection.id}",
        embedding_model="test",
        embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
        embedding_dimension=2,
        index_path=f"/tmp/ldr-7393-{document.id}",
        index_hash=document.id,
        chunk_size=100,
        chunk_overlap=0,
        chunk_count=1,
        total_documents=1,
    )
    session.add(index)
    session.flush()
    session.add_all(
        [
            RagDocumentStatus(
                document_id=document.id,
                collection_id=library_collection.id,
                rag_index_id=index.id,
                chunk_count=1,
                indexed_at=datetime(2026, 10, 1, tzinfo=UTC),
            ),
            DocumentChunk(
                chunk_hash=document.id,
                source_type="document",
                source_id=document.id,
                collection_name=f"collection_{library_collection.id}",
                chunk_text=document.text_content or "old text",
                chunk_index=0,
                start_char=0,
                end_char=len(document.text_content or ""),
                word_count=len((document.text_content or "").split()),
                embedding_id=document.id,
                embedding_model="test",
                embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
            ),
        ]
    )
    session.commit()
    return index


@pytest.mark.parametrize("storage", ["none", "database", "filesystem"])
def test_session_none_dedup_preserves_better_canonical_text(
    session,
    source_type,
    library_collection,
    make_resource,
    svc,
    tmp_path,
    monkeypatch,
    storage,
):
    """Identical PDF bytes from a second host must keep the canonical
    source-derived text, provenance, hash, and index state.
    """
    monkeypatch.delenv(_FILESYSTEM_GATE_ENV, raising=False)
    if storage == "filesystem":
        monkeypatch.setenv(_FILESYSTEM_GATE_ENV, "true")

    pdf = b"%PDF-1.4 identical bytes for canonical text preservation"
    pdf_hash = hashlib.sha256(pdf).hexdigest()
    res_a = make_resource("https://arxiv.org/pdf/2601.12345")
    res_b = make_resource("https://mirror.example/paper.pdf")
    _prepare_session_none_download(svc, session, storage, tmp_path, pdf)
    tracker_a = _track(session, res_a, "hash-7393-preserve-a")
    tracker_b = _track(session, res_b, "hash-7393-preserve-b")

    success_a, reason_a, _ = _download_detached(
        svc, session, source_type, library_collection, res_a, tracker_a
    )
    assert success_a is True and reason_a is None
    canonical = session.query(Document).one()
    assert canonical.text_content == _CANONICAL_TEXT
    assert (
        canonical.extraction_method,
        canonical.extraction_source,
        canonical.extraction_quality,
    ) == ("html_extraction", "arxiv_html", "high")
    assert canonical.document_hash == pdf_hash
    _seed_index(session, canonical, library_collection)

    success_b, reason_b, _ = _download_detached(
        svc, session, source_type, library_collection, res_b, tracker_b
    )
    assert success_b is True and reason_b is None

    docs = session.query(Document).filter_by(document_hash=pdf_hash).all()
    assert len(docs) == 1
    canonical = docs[0]
    assert canonical.text_content == _CANONICAL_TEXT
    assert (
        canonical.extraction_method,
        canonical.extraction_source,
        canonical.extraction_quality,
    ) == ("html_extraction", "arxiv_html", "high")
    res_b = session.get(ResearchResource, res_b.id)
    assert res_b.document_id == canonical.id
    assert tracker_b.file_hash == pdf_hash
    assert tracker_b.is_downloaded is True
    db_tracker_b = (
        session.query(DownloadTracker)
        .filter_by(url_hash="hash-7393-preserve-b")
        .one()
    )
    assert db_tracker_b.file_hash == tracker_b.file_hash
    assert db_tracker_b.file_path == tracker_b.file_path
    assert db_tracker_b.is_downloaded is True
    link = (
        session.query(DocumentCollection)
        .filter_by(
            document_id=canonical.id,
            collection_id=library_collection.id,
        )
        .one()
    )
    assert link.indexed is True
    assert session.query(RagDocumentStatus).count() == 1
    assert session.query(DocumentChunk).count() == 1
    chunk = session.query(DocumentChunk).one()
    assert not (chunk.document_metadata or {}).get("_ldr_text_invalidated")
    index = session.query(RAGIndex).one()
    assert index.chunk_count == 1
    assert index.total_documents == 1
    if storage == "database":
        blob = session.get(DocumentBlob, canonical.id)
        assert blob.pdf_binary == pdf
        assert canonical.storage_mode == "database"
        assert tracker_b.file_path == "database"
    elif storage == "filesystem":
        assert canonical.storage_mode == "filesystem"
        assert tracker_b.file_path
        assert (tmp_path / tracker_b.file_path).is_file()
        db_tracker_a = (
            session.query(DownloadTracker)
            .filter_by(url_hash="hash-7393-preserve-a")
            .one()
        )
        assert db_tracker_a.file_path == canonical.file_path
    else:
        assert canonical.storage_mode == "none"
        assert tracker_b.file_path is None
        assert (
            session.query(DocumentBlob)
            .filter_by(document_id=canonical.id)
            .first()
            is None
        )


def test_session_none_dedup_fills_empty_text_then_replaces_greater_coverage(
    session, source_type, library_collection, make_resource, svc, tmp_path
):
    """No canonical text is filled in. Equal quality with greater
    normalized coverage then replaces it and marks the index stale.
    """
    pdf = b"%PDF-1.4 identical bytes for empty-then-better text"
    pdf_hash = hashlib.sha256(pdf).hexdigest()
    res_a = make_resource("https://example.com/empty.pdf")
    res_b = make_resource("https://mirror.example/empty.pdf")
    res_c = make_resource("https://other.example/empty.pdf")
    _prepare_session_none_download(svc, session, "none", tmp_path, pdf)
    svc.downloaders = [svc.downloaders[1]]
    svc._extract_text_from_pdf.side_effect = [
        None,
        _CANONICAL_TEXT,
        _LONGER_TEXT,
    ]
    tracker_a = _track(session, res_a, "hash-7393-empty-a")
    tracker_b = _track(session, res_b, "hash-7393-empty-b")
    tracker_c = _track(session, res_c, "hash-7393-empty-c")

    assert (
        _download_detached(
            svc, session, source_type, library_collection, res_a, tracker_a
        )[0]
        is True
    )
    canonical = session.query(Document).one()
    assert canonical.text_content is None
    assert canonical.document_hash == pdf_hash

    assert (
        _download_detached(
            svc, session, source_type, library_collection, res_b, tracker_b
        )[0]
        is True
    )
    session.refresh(canonical)
    res_b = session.get(ResearchResource, res_b.id)
    assert canonical.text_content == _CANONICAL_TEXT
    assert canonical.document_hash == pdf_hash
    assert canonical.id
    assert res_b.document_id == canonical.id
    assert (
        canonical.extraction_method,
        canonical.extraction_source,
        canonical.extraction_quality,
    ) == ("pdf_extraction", "local_pdf", "low")
    _seed_index(session, canonical, library_collection)

    assert (
        _download_detached(
            svc, session, source_type, library_collection, res_c, tracker_c
        )[0]
        is True
    )
    session.refresh(canonical)
    res_c = session.get(ResearchResource, res_c.id)
    assert session.query(Document).count() == 1
    assert canonical.text_content == _LONGER_TEXT
    assert canonical.word_count == len(_LONGER_TEXT.split())
    assert canonical.character_count == len(_LONGER_TEXT)
    assert canonical.document_hash == pdf_hash
    assert res_c.document_id == canonical.id
    assert (
        canonical.extraction_method,
        canonical.extraction_source,
        canonical.extraction_quality,
    ) == ("pdf_extraction", "local_pdf", "low")
    assert not any(link.indexed for link in session.query(DocumentCollection))
    assert session.query(RagDocumentStatus).count() == 0
    assert session.query(DocumentChunk).count() == 1
    assert (
        session.query(DocumentChunk)
        .one()
        .document_metadata["_ldr_text_invalidated"]
    )
    index = session.query(RAGIndex).one()
    assert index.chunk_count == 0
    assert index.total_documents == 0


def test_session_none_failed_canonical_keeps_pdf_hash_distinct_pdf_inserts(
    session, source_type, library_collection, make_resource, svc, tmp_path
):
    """Recovering a failed canonical record keeps its PDF byte hash. A
    different PDF still creates its own document.
    """
    pdf = b"%PDF-1.4 canonical bytes recovered after failure"
    other_pdf = b"%PDF-1.4 different bytes must not dedup"
    pdf_hash = hashlib.sha256(pdf).hexdigest()
    res_a = make_resource("https://arxiv.org/pdf/2601.99999")
    res_b = make_resource("https://mirror.example/failed.pdf")
    res_c = make_resource("https://example.com/other.pdf")
    _prepare_session_none_download(svc, session, "none", tmp_path, pdf)
    tracker_a = _track(session, res_a, "hash-7393-failed-a")
    tracker_b = _track(session, res_b, "hash-7393-failed-b")
    tracker_c = _track(session, res_c, "hash-7393-failed-c")

    assert (
        _download_detached(
            svc, session, source_type, library_collection, res_a, tracker_a
        )[0]
        is True
    )
    canonical = session.query(Document).one()
    canonical.status = DocumentStatus.FAILED
    canonical.error_message = "later extraction failed"
    session.commit()

    svc._extract_text_from_pdf.return_value = _INFERIOR_TEXT
    assert (
        _download_detached(
            svc, session, source_type, library_collection, res_b, tracker_b
        )[0]
        is True
    )
    session.refresh(canonical)
    res_b = session.get(ResearchResource, res_b.id)
    assert canonical.status == DocumentStatus.COMPLETED
    assert canonical.error_message is None
    assert canonical.text_content == _CANONICAL_TEXT
    assert (
        canonical.extraction_method,
        canonical.extraction_source,
        canonical.extraction_quality,
    ) == ("html_extraction", "arxiv_html", "high")
    assert canonical.document_hash == pdf_hash
    assert res_b.document_id == canonical.id

    svc.downloaders[1].download_with_result.return_value = MagicMock(
        is_success=True,
        content=other_pdf,
        status_code=200,
        skip_reason=None,
    )
    assert (
        _download_detached(
            svc, session, source_type, library_collection, res_c, tracker_c
        )[0]
        is True
    )
    session.refresh(canonical)
    res_c = session.get(ResearchResource, res_c.id)
    assert canonical.document_hash == pdf_hash
    assert session.query(Document).count() == 2
    other = session.get(Document, res_c.document_id)
    assert other.id != canonical.id
    assert other.document_hash == hashlib.sha256(other_pdf).hexdigest()