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.

11.9 KB
Raw Blame
"""``get_rag_stats`` must describe ONE index, not every index in the collection.

A collection accumulates ``RagDocumentStatus`` rows from more than one
``RAGIndex`` whenever the embedding configuration changes between indexing
runs: documents indexed under model A keep their rows, and documents indexed
afterwards under model B get rows pointing at B's index. Counting those rows by
``collection_id`` alone reports documents that the current index cannot search,
and disagrees with the ``RAGIndex`` row's own aggregate for the same index.

Both ``search_engine_library`` and ``search_engine_collection`` now gate on
the cheaper, index-scoped ``has_indexed_documents`` instead (see
``test_collection_search_query_count.py``) and no longer call
``get_rag_stats``, so the count below no longer decides whether a search
runs at all.

Uses the real-components harness from ``test_multi_model_switch_revert.py``:
network-free fake embeddings, one in-memory sqlite session shared by every
``get_user_db_session`` call site, ``LDR_DATA_DIR`` under ``tmp_path``, and a
real ``LibraryRAGService`` with only the embedding backend faked.
"""

import hashlib
import uuid
from contextlib import contextmanager

import numpy as np
import pytest
from langchain_core.embeddings import Embeddings
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,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)

_RAG_MOD = "local_deep_research.research_library.services.library_rag_service"

USERNAME = "statsuser"
DB_PASSWORD = "pw"  # gitleaks:allow


class _FakeEmbeddings(Embeddings):
    """Deterministic, network-free embeddings keyed by model name and
    dimension, so two configurations produce two distinct indexes."""

    def __init__(self, name: str, dim: int):
        self.name = name
        self.dim = dim

    def _vec(self, text: str):
        # sha256, not hash(): str hashing is salted per process.
        seed = hashlib.sha256(f"{self.name}\x00{text}".encode()).digest()[:4]
        rng = np.random.default_rng(int.from_bytes(seed, "big"))
        return rng.random(self.dim).tolist()

    def embed_documents(self, texts):
        return [self._vec(t) for t in texts]

    def embed_query(self, text):
        return self._vec(text)


@pytest.fixture
def shared_world(tmp_path, monkeypatch, mocker):
    """One in-memory DB shared by every ``get_user_db_session`` call site, and
    a real on-disk cache tree under ``tmp_path``."""
    from unittest.mock import MagicMock

    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))

    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()

    @contextmanager
    def _shared_session(*_a, **_k):
        yield session

    mocker.patch(f"{_RAG_MOD}.get_user_db_session", _shared_session)
    mocker.patch(
        "local_deep_research.vector_stores.facade.get_user_db_session",
        _shared_session,
    )

    fim = MagicMock()
    fim.verify_file.return_value = (True, None)
    fim.record_file.return_value = None
    mocker.patch(f"{_RAG_MOD}.FileIntegrityManager", return_value=fim)

    yield session

    session.close()
    engine.dispose()


@pytest.fixture
def seeded_collection(shared_world):
    """A collection holding two real documents, both linked but unindexed."""
    session = shared_world
    collection_id = uuid.uuid4().hex

    source_type = SourceType(
        id=uuid.uuid4().hex, name="document", display_name="Document"
    )
    session.add(source_type)
    session.add(Collection(id=collection_id, name="Stats scope collection"))

    document_ids = []
    for label in ("first", "second"):
        document_id = uuid.uuid4().hex
        body = f"The {label} document says something about vector indexes. " * 5
        session.add(
            Document(
                id=document_id,
                source_type_id=source_type.id,
                document_hash=hashlib.sha256(body.encode()).hexdigest(),
                title=f"Stats scope {label} document",
                text_content=body,
                original_url=f"http://example.com/stats-scope-{label}",
                file_size=len(body),
                file_type="txt",
                status=DocumentStatus.COMPLETED,
            )
        )
        session.add(
            DocumentCollection(
                document_id=document_id,
                collection_id=collection_id,
                indexed=False,
                chunk_count=0,
            )
        )
        document_ids.append(document_id)

    session.commit()
    return collection_id, document_ids[0], document_ids[1]


def _make_service(model, dim, **overrides):
    """A real LibraryRAGService with only the embedding backend faked."""
    from unittest.mock import MagicMock

    from local_deep_research.research_library.services.library_rag_service import (
        LibraryRAGService,
    )

    emb_mgr = MagicMock()
    emb_mgr.embeddings = _FakeEmbeddings(name=model, dim=dim)

    kwargs = dict(
        username=USERNAME,
        db_password=DB_PASSWORD,
        embedding_model=model,
        embedding_provider="sentence_transformers",
        embedding_manager=emb_mgr,
        index_type="flat",
        distance_metric="cosine",
        normalize_vectors=True,
    )
    kwargs.update(overrides)
    return LibraryRAGService(**kwargs)


class TestRagStatsIndexScope:
    def test_stats_count_only_the_configured_index(
        self, shared_world, seeded_collection
    ):
        """Indexing one document under model A and the other under model B
        leaves status rows from both indexes in the collection. Each service
        must report its own index's documents and chunks."""
        session = shared_world
        collection_id, doc_a, doc_b = seeded_collection

        svc_a = _make_service("model-A", dim=8)
        result_a = svc_a.index_document(doc_a, collection_id)
        assert result_a["status"] == "success", result_a

        svc_b = _make_service("model-B", dim=4)
        result_b = svc_b.index_document(doc_b, collection_id)
        assert result_b["status"] == "success", result_b

        rows = (
            session.query(RagDocumentStatus)
            .filter_by(collection_id=collection_id)
            .all()
        )
        assert len(rows) == 2
        assert len({row.rag_index_id for row in rows}) == 2, (
            "the two documents must sit under two different indexes for this "
            "test to exercise anything"
        )

        index_b = (
            session.query(RAGIndex)
            .filter_by(
                collection_name=f"collection_{collection_id}",
                embedding_model="model-B",
            )
            .one()
        )
        index_a = (
            session.query(RAGIndex)
            .filter_by(
                collection_name=f"collection_{collection_id}",
                embedding_model="model-A",
            )
            .one()
        )

        stats_b = svc_b.get_rag_stats(collection_id)
        assert stats_b["total_documents"] == 2
        assert stats_b["indexed_documents"] == 1
        assert stats_b["unindexed_documents"] == 1
        assert stats_b["total_chunks"] == result_b["chunk_count"]

        # The counts must agree with the index's own aggregate, which
        # index_document maintains per index.
        assert stats_b["indexed_documents"] == index_b.total_documents
        assert stats_b["total_chunks"] == index_b.chunk_count

        # The other index is unchanged and reports its own document, so the
        # scoping is per configuration rather than "only the current index".
        stats_a = svc_a.get_rag_stats(collection_id)
        assert stats_a["indexed_documents"] == 1
        assert stats_a["total_chunks"] == result_a["chunk_count"]
        assert stats_a["indexed_documents"] == index_a.total_documents
        assert stats_a["total_chunks"] == index_a.chunk_count

        # embedding_info describes the index the counts came from. Sampling
        # a chunk of the collection would answer "model-A" for both services,
        # since model-A indexed first.
        assert stats_b["embedding_info"]["model"] == "model-B"
        assert stats_b["embedding_info"]["dimension"] == 4
        assert stats_a["embedding_info"]["model"] == "model-A"
        assert stats_a["embedding_info"]["dimension"] == 8

    def test_stats_are_zero_for_a_configuration_with_no_index(
        self, shared_world, seeded_collection, loguru_caplog
    ):
        """A configuration that has never indexed this collection has no
        index row, so nothing is indexed for it, however many rows other
        indexes left behind."""
        session = shared_world
        collection_id, doc_a, _doc_b = seeded_collection

        svc_a = _make_service("model-A", dim=8)
        assert svc_a.index_document(doc_a, collection_id)["status"] == "success"
        assert (
            session.query(RagDocumentStatus)
            .filter_by(collection_id=collection_id)
            .count()
            == 1
        )

        svc_c = _make_service("model-C", dim=6)

        with loguru_caplog.at_level("WARNING"):
            stats_c = svc_c.get_rag_stats(collection_id)

        assert stats_c["total_documents"] == 2
        assert stats_c["indexed_documents"] == 0
        assert stats_c["unindexed_documents"] == 2
        assert stats_c["total_chunks"] == 0

        # Zero here means "not indexed under THIS configuration", which reads
        # identically to "not indexed at all" unless the log says otherwise.
        assert stats_c["embedding_info"] == {}
        assert collection_id in loguru_caplog.text
        assert "model-C" in loguru_caplog.text
        assert (
            "1 document(s) are indexed under other configurations"
            in loguru_caplog.text
        )

        assert (
            session.query(RAGIndex)
            .filter_by(
                collection_name=f"collection_{collection_id}",
                embedding_model="model-C",
            )
            .count()
            == 0
        ), "reading stats must not create an index row"

    def test_has_indexed_documents_warns_on_foreign_rows_but_not_when_eligible(
        self, shared_world, seeded_collection, loguru_caplog_full
    ):
        """``has_indexed_documents`` keeps the same "indexed under other
        configurations" diagnostic ``get_rag_stats`` logs (see
        ``test_stats_are_zero_for_a_configuration_with_no_index`` above),
        emitted from its own ``rag_index is None`` branch so the eligible
        (hot) path pays nothing for it.

        Fails under a revert that drops that branch's foreign-rows count
        and ``logger.warning(...)`` call: the miss-path assertions below
        would find an empty ``loguru_caplog_full.text``.
        """
        session = shared_world
        collection_id, doc_a, _doc_b = seeded_collection

        svc_a = _make_service("model-A", dim=8)
        assert svc_a.index_document(doc_a, collection_id)["status"] == "success"
        assert (
            session.query(RagDocumentStatus)
            .filter_by(collection_id=collection_id)
            .count()
            == 1
        )

        svc_c = _make_service("model-C", dim=6)

        with loguru_caplog_full.at_level("WARNING"):
            assert svc_c.has_indexed_documents(collection_id) is False

        assert collection_id in loguru_caplog_full.text
        assert "model-C" in loguru_caplog_full.text
        assert (
            "1 document(s) are indexed under other configurations"
            in loguru_caplog_full.text
        )

        # The eligible path must stay silent -- resolving svc_a's own
        # index and finding its own status row takes the early ``return
        # bool(...)`` above the foreign-rows count and warning.
        loguru_caplog_full.clear()
        with loguru_caplog_full.at_level("WARNING"):
            assert svc_a.has_indexed_documents(collection_id) is True
        assert loguru_caplog_full.text == ""