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.

Delete zero-length PDF blob on text-only move (#7925) (#8071)

Vladimir Babin82c7c33f0263
diff --git a/changelog.d/7925.bugfix.md b/changelog.d/7925.bugfix.md
new file mode 100644
index 0000000000..f31f1de61f
--- /dev/null
+++ b/changelog.d/7925.bugfix.md
@@ -0,0 +1 @@
+Library sync now deletes the zero-length PDF blob row when a document moves to text-only, so library search no longer shows a dead `/pdf` link for it.
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.py
index 01fe88c5a6..f408782e76 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -14,7 +14,7 @@ from urllib.parse import urlparse
from loguru import logger
from sqlalchemy import and_, or_, func, case, select
-from sqlalchemy.orm import aliased, defer
+from sqlalchemy.orm import aliased, defer, object_session
from ...constants import FILE_PATH_SENTINELS, FILE_PATH_TEXT_ONLY
from ...utilities.arxiv import is_arxiv_paper_url
@@ -330,6 +330,15 @@ class LibraryService:
"""
doc.storage_mode = "none"
doc.file_path = FILE_PATH_TEXT_ONLY
+ session = object_session(doc)
+ if session is not None:
+ # Only remove the blob row while it is still empty. A concurrent
+ # download may write real bytes into it between the earlier size
+ # read and this delete; the empty-blob predicate keeps those bytes.
+ session.query(DocumentBlob).filter(
+ DocumentBlob.document_id == doc.id,
+ func.coalesce(func.length(DocumentBlob.pdf_binary), 0) == 0,
+ ).delete(synchronize_session=False)
if tracker is not None and (
tracker.is_downloaded or tracker.file_path is not None
):
diff --git a/tests/research_library/services/test_library_sync_empty_content.py b/tests/research_library/services/test_library_sync_empty_content.py
index b3578a9668..5a34e101ca 100644
--- a/tests/research_library/services/test_library_sync_empty_content.py
+++ b/tests/research_library/services/test_library_sync_empty_content.py
@@ -109,6 +109,9 @@ def test_empty_pdf_is_refilled_in_same_document(
assert kept.text_content == SAVED_TEXT
assert kept.file_path == FILE_PATH_TEXT_ONLY
assert document_awaits_pdf(kept)
+ assert not PDFStorageManager.pdf_exists(store.root, kept, session)
+ if storage == "database":
+ assert session.get(DocumentBlob, document) is None
assert session.get(DocumentChunk, chunk) is not None
assert (
session.query(DocumentCollection)
@@ -138,6 +141,32 @@ def test_empty_pdf_is_refilled_in_same_document(
assert library.sync_library_with_filesystem()["files_missing"] == 0
+def test_clear_missing_pdf_keeps_a_concurrently_refilled_blob(
+ db, store, world, monkeypatch
+):
+ """A blob that gained real bytes before the delete must survive.
+
+ ``_clear_missing_pdf`` removes the zero-length blob row, but only while
+ it is still empty. If a concurrent download writes real bytes into the
+ row between the earlier size read and this delete, those bytes must be
+ kept. The delete is constrained to empty blobs, so the refilled row is
+ preserved.
+ """
+ document, _ = add_document(db, world, text=SAVED_TEXT)
+ row = db.session.get(Document, document)
+ row.storage_mode = "database"
+ db.session.add(DocumentBlob(document_id=document, pdf_binary=PDF_BYTES))
+ tracker = find_tracker_by_url(db.session, row.original_url)
+ db.session.commit()
+ service = make_library_service(db, store, monkeypatch)
+ service._clear_missing_pdf(row, tracker, {"trackers_updated": 0})
+ db.session.commit()
+ with db.fresh() as session:
+ blob = session.get(DocumentBlob, document)
+ assert blob is not None, "a refilled (nonempty) blob must be kept"
+ assert blob.pdf_binary == PDF_BYTES
+
+
@pytest.mark.parametrize(
"status",
[DocumentStatus.FAILED, DocumentStatus.PENDING, DocumentStatus.PROCESSING],