~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): skip user uploads during sync and re-download (#3948)
LearningCircuit2365285ea0d4
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.pyindex 5095172fc6..a8fc189cd6 100644--- a/src/local_deep_research/research_library/services/library_service.py+++ b/src/local_deep_research/research_library/services/library_service.py@@ -1122,10 +1122,14 @@ class LibraryService:Statistics about the sync operation"""with get_user_db_session(self.username) as session:- # Get all documents marked as completed+ # Sync only research downloads — uploads have no original_url+ # and no DownloadTracker, so they don't belong in this routine+ # (and the destructive `else` branch below would otherwise+ # silently delete every uploaded document).documents = (session.query(Document).filter_by(status=DocumentStatus.COMPLETED)+ .filter(Document.original_url.isnot(None)).all())@@ -1209,6 +1213,14 @@ class LibraryService:for doc_id in document_ids:doc = session.query(Document).get(doc_id)if doc:+ if not doc.original_url:+ # Uploads have no source URL and no DownloadTracker,+ # so re-download is not meaningful for them.+ logger.warning(+ f"Skipping mark-for-redownload on {doc_id[:8]}…: "+ "document has no original_url (likely user upload)"+ )+ continue# Get tracker and reset ittracker = (session.query(DownloadTracker)diff --git a/tests/research_library/services/test_library_service.py b/tests/research_library/services/test_library_service.pyindex 367100531f..f214ef8619 100644--- a/tests/research_library/services/test_library_service.py+++ b/tests/research_library/services/test_library_service.py@@ -904,3 +904,139 @@ class TestLibraryServiceGetStoragePath:result = service._get_storage_path()assert isinstance(result, str)+++# ============== Integration tests against a real session ==============+# These use library_session (in-memory SQLite) to exercise behavior that+# mocks can't reproduce — specifically, a real Document with+# original_url=None, which mocks would silently coerce into a string.+++class TestSyncLibraryUploadsIntegration:+ """Regression for #3869: uploads must not crash sync nor be deleted by it."""++ def test_sync_skips_uploads_and_does_not_delete_them(+ self,+ library_session,+ mock_source_type,+ mock_upload_source_type,+ mocker,+ ):+ """Upload (original_url=None) survives sync; download with no tracker is processed."""+ import hashlib+ import uuid+ from contextlib import contextmanager++ from local_deep_research.database.models.library import (+ Document,+ DocumentStatus,+ )+ from local_deep_research.research_library.services.library_service import (+ LibraryService,+ )++ upload_id = str(uuid.uuid4())+ upload = Document(+ id=upload_id,+ source_type_id=mock_upload_source_type.id,+ document_hash=hashlib.sha256(b"upload").hexdigest(),+ original_url=None,+ file_size=1024,+ file_type="pdf",+ title="User Upload",+ status=DocumentStatus.COMPLETED,+ )+ download = Document(+ id=str(uuid.uuid4()),+ source_type_id=mock_source_type.id,+ document_hash=hashlib.sha256(b"download").hexdigest(),+ original_url="https://example.com/paper.pdf",+ file_size=2048,+ file_type="pdf",+ title="Research Download",+ status=DocumentStatus.COMPLETED,+ )+ library_session.add_all([upload, download])+ library_session.commit()++ @contextmanager+ def _session_cm(*_args, **_kwargs):+ yield library_session++ mocker.patch(+ "local_deep_research.research_library.services.library_service.get_user_db_session",+ side_effect=_session_cm,+ )++ with patch.object(+ LibraryService, "__init__", lambda self, username: None+ ):+ service = LibraryService.__new__(LibraryService)+ service.username = "test_user"++ stats = service.sync_library_with_filesystem()++ # Upload was excluded from the sync (regression guard for both the+ # TypeError crash and the silent cascade-delete in the else branch).+ assert library_session.query(Document).get(upload_id) is not None+ assert stats["total_documents"] == 1 # only the download is counted+++class TestMarkForRedownloadUploadsIntegration:+ """Regression for #3869: mark_for_redownload must not crash on uploads."""++ def test_mark_for_redownload_skips_upload_documents(+ self,+ library_session,+ mock_upload_source_type,+ mocker,+ caplog,+ ):+ """Calling mark_for_redownload with an upload's id is a no-op, not a crash."""+ import hashlib+ import uuid+ from contextlib import contextmanager++ from local_deep_research.database.models.library import (+ Document,+ DocumentStatus,+ )+ from local_deep_research.research_library.services.library_service import (+ LibraryService,+ )++ upload_id = str(uuid.uuid4())+ upload = Document(+ id=upload_id,+ source_type_id=mock_upload_source_type.id,+ document_hash=hashlib.sha256(b"upload-mfr").hexdigest(),+ original_url=None,+ file_size=512,+ file_type="pdf",+ title="User Upload",+ status=DocumentStatus.COMPLETED,+ )+ library_session.add(upload)+ library_session.commit()++ @contextmanager+ def _session_cm(*_args, **_kwargs):+ yield library_session++ mocker.patch(+ "local_deep_research.research_library.services.library_service.get_user_db_session",+ side_effect=_session_cm,+ )++ with patch.object(+ LibraryService, "__init__", lambda self, username: None+ ):+ service = LibraryService.__new__(LibraryService)+ service.username = "test_user"++ count = service.mark_for_redownload([upload_id])++ assert count == 0+ # Upload's status must be unchanged.+ refreshed = library_session.query(Document).get(upload_id)+ assert refreshed.status == DocumentStatus.COMPLETEDdiff --git a/tests/research_library/services/test_library_service_crud_deep.py b/tests/research_library/services/test_library_service_crud_deep.pyindex df71f99990..0a0e34601c 100644--- a/tests/research_library/services/test_library_service_crud_deep.py+++ b/tests/research_library/services/test_library_service_crud_deep.py@@ -525,7 +525,7 @@ class TestSyncLibraryWithFilesystem:"""No completed documents → all stats zero."""service = _make_service()mock_session = MagicMock()- mock_session.query.return_value.filter_by.return_value.all.return_value = []+ mock_session.query.return_value.filter_by.return_value.filter.return_value.all.return_value = []_mock_session_cm(mocker, mock_session)result = service.sync_library_with_filesystem()@@ -547,7 +547,7 @@ class TestSyncLibraryWithFilesystem:mock_tracker = Mock()mock_tracker.file_path = "pdfs/doc.pdf"- mock_session.query.return_value.filter_by.return_value.all.return_value = [+ mock_session.query.return_value.filter_by.return_value.filter.return_value.all.return_value = [mock_doc]@@ -562,7 +562,9 @@ class TestSyncLibraryWithFilesystem:if call_count["n"] == 1:# Document queryq = MagicMock()- q.filter_by.return_value.all.return_value = [mock_doc]+ q.filter_by.return_value.filter.return_value.all.return_value = [+ mock_doc+ ]return qreturn tracker_query@@ -599,7 +601,9 @@ class TestSyncLibraryWithFilesystem:call_count["n"] += 1if call_count["n"] == 1:q = MagicMock()- q.filter_by.return_value.all.return_value = [mock_doc]+ q.filter_by.return_value.filter.return_value.all.return_value = [+ mock_doc+ ]return qtq = MagicMock()tq.filter_by.return_value.first.return_value = mock_tracker@@ -639,7 +643,9 @@ class TestSyncLibraryWithFilesystem:call_count["n"] += 1if call_count["n"] == 1:q = MagicMock()- q.filter_by.return_value.all.return_value = [mock_doc]+ q.filter_by.return_value.filter.return_value.all.return_value = [+ mock_doc+ ]return qtq = MagicMock()tq.filter_by.return_value.first.return_value = None@@ -677,7 +683,7 @@ class TestSyncLibraryWithFilesystem:call_count["n"] += 1if call_count["n"] == 1:q = MagicMock()- q.filter_by.return_value.all.return_value = docs+ q.filter_by.return_value.filter.return_value.all.return_value = docsreturn qidx = call_count["n"] - 2tq = MagicMock()