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.

fix(rag): reconcile only chunk rows whose document still exists (#6785)

Lukasa2a824a5192c
diff --git a/changelog.d/6748.bugfix.md b/changelog.d/6748.bugfix.md
new file mode 100644
index 0000000000..0255c871bb
--- /dev/null
+++ b/changelog.d/6748.bugfix.md
@@ -0,0 +1 @@
+Background RAG indexing no longer fails with `FOREIGN KEY constraint failed` after a document delete whose chunk purge was skipped: collection reconciliation now counts only chunk rows whose document still exists, so it no longer writes an index status row for a deleted document, and the collection's indexed-document and chunk counts leave those rows out too.
diff --git a/src/local_deep_research/research_library/services/library_rag_service.py b/src/local_deep_research/research_library/services/library_rag_service.py
index 396d5b6f6d..8c2b5aecda 100644
--- a/src/local_deep_research/research_library/services/library_rag_service.py
+++ b/src/local_deep_research/research_library/services/library_rag_service.py
@@ -3194,6 +3194,14 @@ class LibraryRAGService:
self.embedding_provider
),
)
+ # Chunk rows can outlive their document: source_id has
+ # no foreign key, and the purge after a delete is best
+ # effort. A status row for such a document would break
+ # the commit below on RagDocumentStatus's foreign key to
+ # documents (#6748), so only live documents count.
+ .filter(
+ exists().where(Document.id == DocumentChunk.source_id)
+ )
.all()
)
ids_by_document: Dict[str, Set[int]] = {}
diff --git a/tests/research_library/services/test_reconcile_collection_index.py b/tests/research_library/services/test_reconcile_collection_index.py
index 7b8d990739..01450365bb 100644
--- a/tests/research_library/services/test_reconcile_collection_index.py
+++ b/tests/research_library/services/test_reconcile_collection_index.py
@@ -399,6 +399,8 @@ class TestReconcileDimensionDrift:
# a document that was indexed under the OLD, pre-drift dimension.
q = session.query.return_value.filter_by.return_value
q.first.return_value = db_row
+ # The chunk query also keeps only rows whose document still exists.
+ q.filter.return_value = q
q.all.return_value = [(101, "doc-1")]
svc._reset_index_state_for_rebuild = MagicMock()
diff --git a/tests/research_library/services/test_reconcile_orphaned_chunks.py b/tests/research_library/services/test_reconcile_orphaned_chunks.py
new file mode 100644
index 0000000000..789adaa62b
--- /dev/null
+++ b/tests/research_library/services/test_reconcile_orphaned_chunks.py
@@ -0,0 +1,189 @@
+"""``reconcile_collection_index`` against chunk rows whose document is gone.
+
+``DocumentChunk.source_id`` has no foreign key, and the purge after a
+document delete is best effort, so chunk rows can outlive their document.
+When their vectors were still live, reconcile wrote a ``RagDocumentStatus``
+row for the deleted document and its commit failed on that table's foreign
+key to ``documents`` -- every reconcile of the collection, until the vectors
+left the index (#6748).
+
+This runs against a real on-disk SQLite database with ``PRAGMA
+foreign_keys = ON``, as production does; a mocked session cannot see a
+foreign-key violation.
+"""
+
+import hashlib
+import uuid
+from contextlib import contextmanager
+from unittest.mock import MagicMock, patch
+
+import pytest
+from sqlalchemy import create_engine, event, select
+from sqlalchemy.orm import sessionmaker
+
+from local_deep_research.database.models import Base
+from local_deep_research.database.models.library import (
+ Collection,
+ Document,
+ DocumentChunk,
+ DocumentCollection,
+ DocumentStatus,
+ EmbeddingProvider,
+ RAGIndex,
+ RagDocumentStatus,
+ SourceType,
+)
+
+_MOD = "local_deep_research.research_library.services.library_rag_service"
+_COLLECTION = "col-1"
+_LIVE = "doc-live"
+_GONE = "doc-gone"
+
+
+def _chunk(doc_id: str, index: int) -> DocumentChunk:
+ text = f"chunk-{index}-of-{doc_id}"
+ return DocumentChunk(
+ chunk_hash=hashlib.sha256(text.encode()).hexdigest(),
+ source_type="document",
+ source_id=doc_id,
+ collection_name=f"collection_{_COLLECTION}",
+ chunk_text=text,
+ chunk_index=index,
+ start_char=0,
+ end_char=len(text),
+ word_count=1,
+ embedding_id=str(uuid.uuid5(uuid.NAMESPACE_URL, text)),
+ embedding_model="fake-model",
+ embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
+ embedding_dimension=8,
+ )
+
+
+def database(tmp_path):
+ engine = create_engine(f"sqlite:///{tmp_path / 'reconcile.db'}")
+
+ @event.listens_for(engine, "connect")
+ def _fk_on(dbapi_connection, _record):
+ cursor = dbapi_connection.cursor()
+ cursor.execute("PRAGMA foreign_keys = ON")
+ cursor.close()
+
+ Base.metadata.create_all(engine)
+ session = sessionmaker(bind=engine)()
+
+ source_type = SourceType(
+ id=uuid.uuid4().hex, name="document", display_name="Document"
+ )
+ session.add(source_type)
+ session.add(
+ Collection(
+ id=_COLLECTION,
+ name="Library",
+ collection_type="user_collection",
+ )
+ )
+ session.add(
+ Document(
+ id=_LIVE,
+ source_type_id=source_type.id,
+ document_hash=hashlib.sha256(_LIVE.encode()).hexdigest(),
+ file_path=f"pdfs/{_LIVE}.pdf",
+ file_size=4096,
+ file_type="pdf",
+ mime_type="application/pdf",
+ storage_mode="database",
+ title="Live",
+ text_content="text",
+ status=DocumentStatus.COMPLETED,
+ )
+ )
+ rag_index = RAGIndex(
+ collection_name=f"collection_{_COLLECTION}",
+ embedding_model="fake-model",
+ embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
+ embedding_dimension=8,
+ index_path=f"/nonexistent/rag_indices/{_COLLECTION}",
+ index_hash=hashlib.sha256(_COLLECTION.encode()).hexdigest(),
+ chunk_size=100,
+ chunk_overlap=10,
+ )
+ session.add(rag_index)
+ session.flush()
+ session.add(
+ DocumentCollection(
+ document_id=_LIVE, collection_id=_COLLECTION, indexed=True
+ )
+ )
+ # Two chunks of the live document, and one left behind by a document
+ # whose purge was skipped: its row in ``documents`` is gone.
+ session.add_all([_chunk(_LIVE, 0), _chunk(_LIVE, 1), _chunk(_GONE, 0)])
+ session.commit()
+
+ @contextmanager
+ def shared_session(*_args, **_kwargs):
+ try:
+ yield session
+ except Exception:
+ session.rollback()
+ raise
+
+ yield engine, session, shared_session, rag_index
+ session.close()
+ engine.dispose()
+
+
+def _service(rag_index):
+ with (
+ patch(f"{_MOD}.LocalEmbeddingManager") as manager,
+ patch(f"{_MOD}.get_user_db_session"),
+ patch(f"{_MOD}.FileIntegrityManager"),
+ patch(f"{_MOD}.get_text_splitter"),
+ ):
+ manager.return_value.embeddings = MagicMock()
+ from local_deep_research.research_library.services.library_rag_service import (
+ LibraryRAGService,
+ )
+
+ service = LibraryRAGService(
+ username="testuser",
+ db_password="pw",
+ embedding_model="fake-model",
+ embedding_provider=EmbeddingProvider.SENTENCE_TRANSFORMERS.value,
+ )
+ service.rag_index_record = rag_index
+ return service
+
+
+def test_reconcile_skips_chunks_whose_document_is_gone(database):
+ engine, session, shared_session, rag_index = database
+ service = _service(rag_index)
+ # Every chunk's vector is still live, the deleted document's included:
+ # the case that used to fail the commit.
+ live_ids = [chunk_id for (chunk_id,) in session.query(DocumentChunk.id)]
+ vector_index = MagicMock()
+ vector_index.live_ids.return_value = live_ids
+
+ with (
+ patch.object(service, "_get_vector_index", return_value=vector_index),
+ patch(f"{_MOD}.get_user_db_session", shared_session),
+ ):
+ result = service.reconcile_collection_index(_COLLECTION)
+
+ assert result["indexed_documents"] == 1
+ assert result["indexed_chunks"] == 2
+ # The left-behind chunk's vector is live but belongs to no document.
+ assert result["orphan_vectors"] == 1
+ with engine.connect() as fresh:
+ statuses = fresh.execute(
+ select(
+ RagDocumentStatus.document_id,
+ RagDocumentStatus.collection_id,
+ RagDocumentStatus.chunk_count,
+ )
+ ).all()
+ stored = fresh.execute(
+ select(RAGIndex.total_documents, RAGIndex.chunk_count)
+ ).one()
+ assert statuses == [(_LIVE, _COLLECTION, 2)]
+ assert tuple(stored) == (1, 2)