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.

7.2 KB
Raw Blame
"""A second prepared writer must not erase a document indexed by its peer."""

import hashlib
from contextlib import contextmanager

import numpy as np
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentCollection,
    DocumentStatus,
    EmbeddingProvider,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)
from local_deep_research.research_library.services import (
    library_rag_service as rag_module,
)
from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
    _PreparedDocument,
)


@pytest.fixture
def indexed_peer(tmp_path, monkeypatch):
    engine = create_engine(f"sqlite:///{tmp_path / 'overlap.db'}")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()

    @contextmanager
    def db_session(*_args, **_kwargs):
        yield session

    monkeypatch.setattr(rag_module, "get_user_db_session", db_session)
    source_type = SourceType(
        id="source-type", name="document", display_name="Document"
    )
    collection = Collection(id="collection", name="Collection")
    body = "A document indexed by overlapping runs."
    document = Document(
        id="document",
        source_type_id=source_type.id,
        title="Document",
        text_content=body,
        document_hash=hashlib.sha256(body.encode()).hexdigest(),
        file_size=len(body),
        file_type="txt",
        status=DocumentStatus.COMPLETED,
    )
    link = DocumentCollection(
        document_id=document.id, collection_id=collection.id, indexed=False
    )
    index = RAGIndex(
        collection_name="collection_collection",
        embedding_model="test-model",
        embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
        embedding_dimension=2,
        index_path=str(tmp_path / "index.faiss"),
        index_hash="test-index",
        chunk_size=100,
        chunk_overlap=10,
        chunk_count=0,
        total_documents=0,
    )
    session.add_all([source_type, collection, document, link, index])
    session.commit()

    service = object.__new__(LibraryRAGService)
    service.username = "test-user"
    service._db_password = "test-password"  # gitleaks:allow
    service.embedding_model = "test-model"
    service.embedding_provider = "sentence_transformers"
    service.chunk_size = 100
    service.chunk_overlap = 10
    service.splitter_type = "recursive"
    service.text_separators = ["\n\n", "\n", ". ", " ", ""]
    service.distance_metric = "cosine"
    service.normalize_vectors = True
    service.index_type = "flat"
    service.rag_index_record = index
    yield session, service, body

    session.close()
    engine.dispose()


def _peer_finishes(session, service):
    link = session.query(DocumentCollection).one()
    link.indexed = True
    link.chunk_count = 2
    session.add(
        RagDocumentStatus(
            document_id="document",
            collection_id="collection",
            rag_index_id=service.rag_index_record.id,
            chunk_count=2,
        )
    )
    service.rag_index_record.chunk_count = 2
    service.rag_index_record.total_documents = 1
    session.commit()


def test_late_prepared_writer_keeps_peers_completed_index(indexed_peer):
    session, service, body = indexed_peer
    prepared = _PreparedDocument(
        document_id="document",
        collection_id="collection",
        chunk_inputs=[],
        vectors=np.empty((0, 2), dtype="float32"),
        content_hash=hashlib.sha256(body.encode()).hexdigest(),
        initial_indexed=False,
        status_exists=False,
        status_index_id=service.rag_index_record.id,
        had_collection_link=True,
    )
    _peer_finishes(session, service)

    result = service._write_prepared_document_once(prepared)

    session.expire_all()
    assert result["status"] == "skipped"
    assert session.query(DocumentCollection).one().indexed is True
    assert session.query(DocumentCollection).one().chunk_count == 2
    assert session.query(RagDocumentStatus).one().chunk_count == 2
    index = session.query(RAGIndex).one()
    assert (index.chunk_count, index.total_documents) == (2, 1)


def test_late_writer_keeps_peer_when_first_index_appears_after_prepare(
    indexed_peer,
):
    session, service, body = indexed_peer
    session.delete(service.rag_index_record)
    session.commit()
    service.rag_index_record = None
    prepared = _PreparedDocument(
        document_id="document",
        collection_id="collection",
        chunk_inputs=[],
        vectors=np.empty((0, 2), dtype="float32"),
        content_hash=hashlib.sha256(body.encode()).hexdigest(),
        initial_indexed=False,
        status_exists=False,
        status_index_id=None,
        had_collection_link=True,
    )

    # The peer creates the first index and finishes while this writer embeds.
    new_index = RAGIndex(
        collection_name="collection_collection",
        embedding_model=service.embedding_model,
        embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
        embedding_dimension=2,
        index_path="/new.faiss",
        index_hash=service._get_index_hash(
            "collection_collection",
            service.embedding_model,
            service.embedding_provider,
        ),
        chunk_size=service.chunk_size,
        chunk_overlap=service.chunk_overlap,
        chunk_count=0,
        total_documents=0,
    )
    session.add(new_index)
    session.commit()
    service.rag_index_record = new_index
    _peer_finishes(session, service)

    result = service._write_prepared_document_once(prepared)

    session.expire_all()
    assert result["status"] == "skipped"
    assert session.query(DocumentCollection).one().indexed is True
    assert session.query(RagDocumentStatus).one().rag_index_id == new_index.id
    assert session.query(RagDocumentStatus).one().chunk_count == 2
    assert (new_index.chunk_count, new_index.total_documents) == (2, 1)


def test_reindex_flag_removes_deleted_status_from_index_counters(indexed_peer):
    session, service, _body = indexed_peer
    _peer_finishes(session, service)
    session.add(
        Document(
            id="other-document",
            source_type_id="source-type",
            title="Other document",
            text_content="Other content",
            document_hash=hashlib.sha256(b"Other content").hexdigest(),
            file_size=13,
            file_type="txt",
            status=DocumentStatus.COMPLETED,
        )
    )
    session.add(
        RagDocumentStatus(
            document_id="other-document",
            collection_id="collection",
            rag_index_id=service.rag_index_record.id,
            chunk_count=3,
        )
    )
    service.rag_index_record.chunk_count = 5
    service.rag_index_record.total_documents = 2
    session.commit()

    service._flag_document_for_reindex(session, "document", "collection")
    service._flag_document_for_reindex(session, "document", "collection")

    session.expire_all()
    assert session.query(RagDocumentStatus).count() == 1
    assert session.query(DocumentCollection).one().indexed is False
    index = session.query(RAGIndex).one()
    assert (index.chunk_count, index.total_documents) == (3, 1)