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.

test: preserve canonical text on deduplicated PDF downloads (#7414)

Matt Van Hornc3c3dd04ce30
diff --git a/tests/research_library/services/test_download_service_3827_fix.py b/tests/research_library/services/test_download_service_3827_fix.py
index edc5646091..f6d102b3ef 100644
--- a/tests/research_library/services/test_download_service_3827_fix.py
+++ b/tests/research_library/services/test_download_service_3827_fix.py
@@ -27,10 +27,11 @@ 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
+from sqlalchemy import create_engine, text
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import sessionmaker
@@ -39,10 +40,20 @@ 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,
@@ -2588,3 +2599,416 @@ def test_process_user_documents_egress_backstop_survives_read_session_close(
"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
+
+
[email protected]("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()