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.

6.0 KB
Raw Blame
"""``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,
    )


@pytest.fixture
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)