~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(library): stop UNIQUE-collision cascade in Download Manager (#3827) (#3941)
LearningCircuit31a324735d78
diff --git a/changelog.d/3827.bugfix.md b/changelog.d/3827.bugfix.mdnew file mode 100644index 0000000000..c9fa275a7c--- /dev/null+++ b/changelog.d/3827.bugfix.md@@ -0,0 +1 @@+Fix Download Manager failure after 20–30 PDFs with `UNIQUE constraint failed: documents.document_hash` and the cascading PendingRollbackError that prevented further downloads.diff --git a/src/local_deep_research/database/session_context.py b/src/local_deep_research/database/session_context.pyindex b42de90db8..c3712b8b93 100644--- a/src/local_deep_research/database/session_context.py+++ b/src/local_deep_research/database/session_context.py@@ -31,6 +31,27 @@ class DatabaseSessionError(Exception):pass+def safe_rollback(session: Session, context: str = "") -> None:+ """Roll back the session, swallowing and logging any rollback failure.++ SQLAlchemy requires explicit rollback after a failed flush/commit before+ the session is usable again. Skipping it leaves the session in+ PendingRollbackError state and every subsequent ORM operation cascades.++ This helper exists so call sites can recover the session in one line+ without repeating the try/except/log boilerplate at every except handler.+ ``context`` is included in the error log so failed rollbacks can be+ traced back to the call site.+ """+ try:+ session.rollback()+ except Exception:+ if context:+ logger.exception(f"Failed to rollback session: {context}")+ else:+ logger.exception("Failed to rollback session")++def get_g_db_session() -> Optional[Session]:"""Lazily create and cache a DB session on Flask g for the current request.diff --git a/src/local_deep_research/research_library/routes/library_routes.py b/src/local_deep_research/research_library/routes/library_routes.pyindex 3ec2adc7fa..4b6086eba3 100644--- a/src/local_deep_research/research_library/routes/library_routes.py+++ b/src/local_deep_research/research_library/routes/library_routes.py@@ -26,7 +26,7 @@ from loguru import loggerfrom ...security.decorators import require_json_bodyfrom ...web.auth.decorators import login_requiredfrom ...web.utils.templates import render_template_with_defaults-from ...database.session_context import get_user_db_session+from ...database.session_context import get_user_db_session, safe_rollbackfrom ...database.models.research import ResearchResourcefrom ...database.models.library import (Document as Document,@@ -888,6 +888,14 @@ def download_bulk():f"{'Download' if mode == 'pdf' else 'Text extraction'} result: success={success}, status={status}, skip_reason={skip_reason}")except Exception as e:+ # Roll back FIRST: the next loop iteration's+ # session.query(ResearchResource).get(...) at the+ # top of the for-body runs BEFORE the next+ # try/except, so a poisoned session here would+ # cascade into PendingRollbackError on the next+ # item before this handler ever runs again+ # (issue #3827).+ safe_rollback(session, "SSE download")# Log error but continue processingerror_msg = str(e)error_type = type(e).__name__diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.pyindex 95da2aa318..ce03728561 100644--- a/src/local_deep_research/research_library/services/download_service.py+++ b/src/local_deep_research/research_library/services/download_service.py@@ -40,7 +40,7 @@ from ...database.models.library import (from .pdf_storage_manager import PDFStorageManagerfrom ...database.models.research import ResearchResourcefrom ...database.library_init import get_source_type_id, get_default_library_id-from ...database.session_context import get_user_db_session+from ...database.session_context import get_user_db_session, safe_rollbackfrom ...utilities.db_utils import get_settings_managerfrom ...library.download_management import RetryManagerfrom ...config.paths import get_library_directory@@ -612,8 +612,14 @@ class DownloadService:existing_doc = get_document_for_resource(session, resource)if existing_doc:- # Update existing document- existing_doc.document_hash = tracker.file_hash+ # Update existing document. Only replace document_hash when+ # transitioning from FAILED — that's the placeholder hash from+ # _record_failed_text_extraction. For any other prior state+ # the hash is already a real content hash and clobbering it+ # risks UNIQUE-constraint collisions (issue #3827).+ was_failed = existing_doc.status == DocumentStatus.FAILED+ if was_failed:+ existing_doc.document_hash = tracker.file_hashexisting_doc.file_size = len(pdf_content)existing_doc.status = DocumentStatus.COMPLETEDexisting_doc.processed_at = datetime.now(UTC)@@ -1582,14 +1588,22 @@ class DownloadService:doc = get_document_for_resource(session, resource)if doc:- # Update existing document with extracted text+ # Update existing document with extracted text. Only replace+ # document_hash when transitioning from FAILED — see issue+ # #3827. PDF-bytes hashes set at creation must remain stable;+ # text-content hashes collide far more often (identical+ # extracted text from different PDFs).+ was_failed = doc.status == DocumentStatus.FAILEDdoc.text_content = textdoc.character_count = character_countdoc.word_count = word_countdoc.extraction_method = extraction_methoddoc.extraction_source = extraction_sourcedoc.status = DocumentStatus.COMPLETED- doc.document_hash = hashlib.sha256(text.encode()).hexdigest()+ if was_failed:+ doc.document_hash = hashlib.sha256(+ text.encode()+ ).hexdigest()doc.processed_at = datetime.now(UTC)# Set quality based on method@@ -1611,6 +1625,35 @@ class DownloadService:# Generate hash from text contenttext_hash = hashlib.sha256(text.encode()).hexdigest()+ # Dedup against existing content hash. If another resource+ # already produced identical extracted text, link this+ # resource to the canonical Document instead of inserting+ # a duplicate that would violate the UNIQUE constraint+ # (issue #3827). Mirrors research_history_indexer.py:322.+ existing_by_hash = (+ session.query(Document)+ .filter_by(document_hash=text_hash)+ .first()+ )+ if existing_by_hash:+ resource.document_id = existing_by_hash.id+ library_collection = (+ session.query(Collection)+ .filter_by(name="Library")+ .first()+ )+ if library_collection:+ ensure_in_collection(+ session,+ existing_by_hash.id,+ library_collection.id,+ )+ logger.info(+ f"Linked resource {resource.id} to existing Document "+ f"{existing_by_hash.id} (matched on content hash)"+ )+ return None+# Get source type for research downloadstry:source_type_id = get_source_type_id(@@ -1670,6 +1713,12 @@ class DownloadService:return Noneexcept Exception:+ # Rollback BEFORE re-raising so the shared thread-local session+ # is clean by the time the caller's loop reaches its next+ # iteration. Without this, an IntegrityError leaves the session+ # in PendingRollbackError state and every subsequent ORM access+ # cascades (issue #3827).+ safe_rollback(session, "_save_text_with_db")logger.exception("Error saving text to encrypted database")raise # Re-raise so caller can handle the errordiff --git a/src/local_deep_research/scheduler/background.py b/src/local_deep_research/scheduler/background.pyindex c3fb08b8f9..5e99680f1e 100644--- a/src/local_deep_research/scheduler/background.py+++ b/src/local_deep_research/scheduler/background.py@@ -19,6 +19,7 @@ from apscheduler.schedulers.background import BackgroundSchedulerfrom apscheduler.jobstores.base import JobLookupErrorfrom ..constants import ResearchStatusfrom ..database.credential_store_base import CredentialStoreBase+from ..database.session_context import safe_rollbackfrom ..database.thread_local_session import thread_cleanup# RAG indexing imports@@ -863,6 +864,12 @@ class BackgroundJobScheduler:f"[DOC_SCHEDULER] Queued {queued_count} PDF downloads for research {research.id}")except Exception:+ # Recover the shared thread-local session+ # before continuing — without rollback the+ # next phase (text extract / RAG) and the+ # post-loop last_run commit run on a+ # poisoned session (issue #3827).+ safe_rollback(db, "DOC_SCHEDULER PDF download")logger.exception(f"[DOC_SCHEDULER] Failed to download PDFs for research {research.id}")@@ -919,6 +926,14 @@ class BackgroundJobScheduler:f"[DOC_SCHEDULER] Failed to extract text for resource {resource.id}: {error}")except Exception as resource_error:+ # Roll back FIRST so the next+ # iteration's queries don't+ # cascade on a poisoned session+ # (issue #3827).+ safe_rollback(+ db,+ "DOC_SCHEDULER resource",+ )logger.exception(f"[DOC_SCHEDULER] Error processing resource {resource.id}: {resource_error}")@@ -926,6 +941,9 @@ class BackgroundJobScheduler:f"[DOC_SCHEDULER] Text extraction completed for research {research.id}: {processed_count}/{len(resources)} resources processed")except Exception:+ safe_rollback(+ db, "DOC_SCHEDULER text extraction"+ )logger.exception(f"[DOC_SCHEDULER] Failed to extract text for research {research.id}")@@ -1038,6 +1056,7 @@ class BackgroundJobScheduler:f"{indexed_count}/{len(documents_to_index)} documents indexed")except Exception:+ safe_rollback(db, "DOC_SCHEDULER RAG")logger.exception(f"[DOC_SCHEDULER] Failed to generate RAG embeddings for research {research.id}")@@ -1048,6 +1067,7 @@ class BackgroundJobScheduler:)except Exception:+ safe_rollback(db, "DOC_SCHEDULER research")logger.exception(f"[DOC_SCHEDULER] Error processing research {research.id} for user {username}")diff --git a/tests/database/test_session_context.py b/tests/database/test_session_context.pyindex d424f87f6b..f3a70604d8 100644--- a/tests/database/test_session_context.py+++ b/tests/database/test_session_context.py@@ -263,3 +263,39 @@ class TestUnencryptedDbPlaceholder:assert UNENCRYPTED_DB_PLACEHOLDER == "unencrypted-mode"assert isinstance(UNENCRYPTED_DB_PLACEHOLDER, str)+++class TestSafeRollback:+ """Tests for the safe_rollback helper introduced for issue #3827."""++ def test_calls_session_rollback(self):+ from local_deep_research.database.session_context import (+ safe_rollback,+ )++ session = Mock()+ safe_rollback(session, "ctx")+ session.rollback.assert_called_once()++ def test_swallows_rollback_exception(self):+ """Rollback errors must not propagate — call sites are exception+ handlers themselves and a raise here would mask the original error.+ """+ from local_deep_research.database.session_context import (+ safe_rollback,+ )++ session = Mock()+ session.rollback.side_effect = RuntimeError("simulated rollback fail")+ # Must NOT raise.+ safe_rollback(session, "ctx")+ session.rollback.assert_called_once()++ def test_works_without_context_argument(self):+ from local_deep_research.database.session_context import (+ safe_rollback,+ )++ session = Mock()+ safe_rollback(session)+ session.rollback.assert_called_once()diff --git a/tests/research_library/services/test_download_service_3827_fix.py b/tests/research_library/services/test_download_service_3827_fix.pynew file mode 100644index 0000000000..79250b2027--- /dev/null+++ b/tests/research_library/services/test_download_service_3827_fix.py@@ -0,0 +1,1024 @@+"""+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 unittest.mock import MagicMock, patch++import pytest+from sqlalchemy import create_engine+from sqlalchemy.exc import IntegrityError+from sqlalchemy.orm import sessionmaker++from local_deep_research.database.models import Base+from local_deep_research.database.models.library import (+ Collection,+ Document,+ DocumentStatus,+ SourceType,+)+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"+++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()+++def source_type(session):+ src = SourceType(+ id=str(uuid.uuid4()),+ name="research_download",+ display_name="Research Download",+ )+ session.add(src)+ session.commit()+ return src+++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+++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+++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+++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.+# ---------------------------------------------------------------------------+++ "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_rolls_back_session_when_download_raises():+ """Drive `download_bulk` with two queue items; the first triggers+ IntegrityError. Verify `session.rollback()` is called on the OUTER+ request session before the next iteration would re-query.+ """+ from contextlib import contextmanager+ from flask import Flask++ from local_deep_research.research_library.routes import (+ library_routes 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()+ # Track session.query side effects in order:+ # - count() for total pending (returns 2)+ # - .all() for queue_items list (returns [a, b])+ # - .get(101) for resource A (line 843 first iteration)+ # - .get(102) for resource B (line 843 second iteration — must succeed+ # after rollback)+ count_q = MagicMock()+ count_q.filter_by.return_value = count_q+ count_q.count.return_value = 2+ items_q = MagicMock()+ items_q.filter_by.return_value = items_q+ items_q.all.return_value = [q_item_a, q_item_b]+ resource_q = MagicMock()+ resource_q.get.side_effect = [resource_a, resource_b]+ outer_session.query.side_effect = [+ count_q,+ items_q,+ resource_q,+ resource_q,+ ]++ @contextmanager+ def fake_db_session(*a, **kw):+ yield outer_session++ download_service_mock = MagicMock()+ download_service_mock.__enter__ = MagicMock(+ return_value=download_service_mock+ )+ download_service_mock.__exit__ = MagicMock(return_value=False)+ # First call raises an IntegrityError-like; second succeeds.+ download_service_mock.download_resource.side_effect = [+ IntegrityError("INSERT", {}, Exception("UNIQUE constraint failed")),+ (True, None),+ ]++ app = Flask(__name__)+ app.config["SECRET_KEY"] = "test"+ app.config["TESTING"] = True+ app.register_blueprint(routes_mod.library_bp)++ 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+ ),+ patch(+ "local_deep_research.web.auth.decorators.db_manager",+ MagicMock(+ is_user_connected=MagicMock(return_value=True),+ connections={"testuser": True},+ has_encryption=False,+ ),+ ),+ ):+ with app.test_client() as client:+ with client.session_transaction() as sess:+ sess["username"] = "testuser"+ sess["session_id"] = "sid"+ resp = client.post(+ "/library/api/download-bulk",+ json={"research_ids": ["r1"], "mode": "pdf"},+ )+ # 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.get_data(as_text=True)++ assert resp.status_code == 200, body+ # The first item raised IntegrityError; the rollback must have fired+ # at the top of the per-item except handler so the loop could query+ # resource B for the second iteration.+ assert outer_session.rollback.called, (+ "session.rollback() must be called when download_resource raises, "+ "before the next iteration's session.query at line 843."+ )+ # Both queue items were attempted (not just the first — confirms the+ # loop continued on a clean session).+ assert download_service_mock.download_resource.call_count == 2+ # And the second item completed successfully — proves the session was+ # actually clean (rollback worked, not just was called).+ assert '"file": "B", "status": "success"' in body+++# ---------------------------------------------------------------------------+# Change 3: the resource-level except in `_process_user_documents` calls+# db.rollback() at the TOP of the block. Drives the relevant path of the+# scheduler with focused mocks and verifies the rollback fires.+# ---------------------------------------------------------------------------+++def test_process_user_documents_rolls_back_on_resource_error():+ """When `download_as_text` raises while processing a resource inside+ `_process_user_documents`, the per-resource except handler must call+ `db.rollback()` so the next resource's queries don't cascade on a+ poisoned session.+ """+ 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="",+ )++ # Build a minimal research session with two downloadable resources.+ research = MagicMock()+ research.id = "research-1"+ research.title = "T"+ research.completed_at = None+ resource_a = MagicMock()+ resource_a.id = 1+ resource_a.url = "https://example.com/a.pdf"+ resource_b = MagicMock()+ resource_b.id = 2+ resource_b.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]+ resource_query = MagicMock()+ resource_query.filter_by.return_value = resource_query+ resource_query.all.return_value = [resource_a, resource_b]+ # First db.query() = ResearchHistory query, second = ResearchResource.+ db.query.side_effect = [research_query, resource_query]++ @MagicMock+ def _ctx():+ pass++ 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)+ # First resource raises, second succeeds. With the rollback in place,+ # the second call must complete normally.+ download_service.download_as_text.side_effect = [+ IntegrityError("INSERT", {}, Exception("UNIQUE constraint failed")),+ (True, None),+ ]++ settings_manager = MagicMock()++ 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")++ # The per-resource except at line ~921 must have called db.rollback()+ # so the second resource's call could proceed on a clean session.+ assert db.rollback.called, (+ "db.rollback() must fire in the resource-level except so the loop "+ "can continue without PendingRollbackError on the next iteration."+ )+ # Both resources were attempted.+ assert download_service.download_as_text.call_count == 2+++# ---------------------------------------------------------------------------+# Change 3 (continued): the text-extraction WRAPPER except at ~line 944 also+# calls safe_rollback. Triggered when an exception escapes the inner+# per-resource handling — e.g. the `db.query(ResearchResource).filter_by(...)`+# call itself raises before the per-resource loop starts.+# ---------------------------------------------------------------------------+++def test_process_user_documents_rolls_back_on_text_extraction_wrapper_error():+ """If the text-extraction block raises before the per-resource loop —+ e.g. the resources query itself fails — the WRAPPER except at line ~944+ must roll back, otherwise the subsequent RAG block and the post-loop+ last_run commit run on a poisoned session.+ """+ 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=False,+ extract_text=True,+ generate_rag=False,+ last_run="",+ )++ research = MagicMock()+ research.id = "research-x"+ research.title = "T"+ research.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]+ # Second db.query() call (for ResearchResource) — RAISES, which escapes+ # the inner per-resource try/except entirely and lands in the wrapper+ # at line ~944.+ resource_query_failure = RuntimeError("simulated DB query failure")+ db.query.side_effect = [research_query, resource_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)++ 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=MagicMock(),+ ),+ patch(+ "local_deep_research.research_library.services.download_service.DownloadService",+ return_value=download_service,+ ),+ ):+ scheduler._process_user_documents("testuser")++ # The wrapper at line ~944 must have rolled back so any later code+ # (RAG, last_run commit) sees a clean session.+ assert db.rollback.called, (+ "db.rollback() must fire in the text-extraction wrapper except at "+ "line ~944 so subsequent phases run on a clean session."+ )+++# ---------------------------------------------------------------------------+# Change 3 (continued): the RAG WRAPPER except at ~line 1058 also calls+# safe_rollback. Triggered when an exception escapes the RAG block — e.g.+# LibraryRAGService construction itself fails.+# ---------------------------------------------------------------------------+++def test_process_user_documents_rolls_back_on_rag_wrapper_error():+ """If the RAG generation block raises (e.g. LibraryRAGService init+ fails), the WRAPPER except at line ~1058 must roll back so the+ post-loop last_run commit doesn't trip on a poisoned session.+ """+ 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=False,+ extract_text=False,+ generate_rag=True,+ last_run="",+ )++ research = MagicMock()+ research.id = "research-rag"+ research.title = "T"+ research.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]+ db.query.return_value = research_query++ from contextlib import contextmanager as _cm++ @_cm+ def fake_get_user_db_session(*a, **kw):+ yield db++ # Make LibraryRAGService construction raise — escapes to line ~1058.+ rag_failure = RuntimeError("simulated RAG init failure")++ 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=MagicMock(),+ ),+ patch(+ "local_deep_research.scheduler.background.LibraryRAGService",+ side_effect=rag_failure,+ ),+ ):+ scheduler._process_user_documents("testuser")++ assert db.rollback.called, (+ "db.rollback() must fire in the RAG wrapper except at line ~1058 "+ "so the post-loop last_run commit runs on a clean session."+ )diff --git a/tests/research_library/services/test_download_service_coverage.py b/tests/research_library/services/test_download_service_coverage.pyindex 223b9de28c..732f7bace3 100644--- a/tests/research_library/services/test_download_service_coverage.py+++ b/tests/research_library/services/test_download_service_coverage.py@@ -1372,8 +1372,12 @@ class TestSaveTextWithDb:patch(f"{MODULE}.uuid.uuid4", return_value="new-uuid"),patch(f"{MODULE}.ensure_in_collection") as mock_ensure,):- # session.query(Collection).filter_by(name="Library").first() -> library_col- session.query.return_value.filter_by.return_value.first.return_value = library_col+ # First filter_by().first() = dedup lookup (no existing doc),+ # second = Library collection lookup.+ session.query.return_value.filter_by.return_value.first.side_effect = [+ None,+ library_col,+ ]svc._save_text_with_db(resource,"New text",@@ -1413,6 +1417,11 @@ class TestSaveTextWithDb:def test_get_source_type_raises(self, svc):session = MagicMock()resource = MagicMock()+ # No existing Document on dedup lookup, so we fall through to+ # source-type resolution which raises.+ session.query.return_value.filter_by.return_value.first.return_value = (+ None+ )with (patch(f"{MODULE}.get_document_for_resource", return_value=None),diff --git a/tests/research_library/services/test_download_service_deep_coverage.py b/tests/research_library/services/test_download_service_deep_coverage.pyindex c396d914a2..4f4fe127cf 100644--- a/tests/research_library/services/test_download_service_deep_coverage.py+++ b/tests/research_library/services/test_download_service_deep_coverage.py@@ -830,9 +830,15 @@ class TestSaveTextWithDbDeep:patch(f"{MODULE}.get_document_for_resource", return_value=None),patch(f"{MODULE}.get_source_type_id", return_value="src-2"),patch(f"{MODULE}.uuid") as mock_uuid,+ patch(f"{MODULE}.ensure_in_collection"),):mock_uuid.uuid4.return_value = "new-uuid-2"- session.query.return_value.filter_by.return_value.first.return_value = library_col+ # First filter_by().first() = dedup lookup (no match), second+ # = Library collection lookup.+ session.query.return_value.filter_by.return_value.first.side_effect = [+ None,+ library_col,+ ]svc._save_text_with_db(resource,"text",