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.

21.2 KB
Raw Blame
"""Multi-embedding-model SWITCH-AND-REVERT flow, at the LibraryRAGService level.

Drives the real production sequence a user hits when they change the RAG
embedding model in settings, index, change it again, and then change it BACK:

  1. Index a collection's document under embedding model A -> search finds it.
  2. Switch to model B, force-reindex the SAME document -> a SECOND RAGIndex
     row (for B) is created and promoted ``is_current``; A's row is demoted.
     Model A's ``DocumentChunk`` rows must survive (only B's are new/replaced)
     -- the facade's replace-lookup is scoped by ``embedding_model``, so a
     same-collection/-source reindex under a new model must not delete the
     prior model's only copy of the chunk text.
  3. A mere SEARCH back under model A (a read path) must NOT silently
     re-promote A's row to current -- only a write path may do that. This is
     the "control": it proves the assertions below actually exercise the
     ``promote_current`` gate rather than something that is always true.
  4. Reverting for real (the write path -- ``_get_or_create_rag_index`` with
     ``promote_current=True``, exactly as ``index_document``/``_get_vector_index``
     invoke it on a write) must resolve A's ORIGINAL row BY ITS index_hash (not
     create a third row), re-promote it to current (demoting B), and leave A's
     stored ``index_type``/``distance_metric``/``normalize_vectors`` exactly as
     they were created -- NOT re-stamped with B's differing defaults. A search
     afterwards must return A's original content from A's original, untouched
     vectors -- proven by an embed_documents call counter that never increments
     again after step 1's initial index.

Mirrors the real-components harness pattern from
``tests/vector_stores/test_no_plaintext_production_path.py`` (deterministic
network-free fake embeddings, an in-memory sqlite engine/session shared across
every ``get_user_db_session`` call site, ``LDR_DATA_DIR`` pointed at
``tmp_path`` so the real cache/rag_indices tree is used, and a real
``LibraryRAGService`` with only the embedding backend faked).
"""

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,
    DocumentChunk,
    DocumentStatus,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)

SECRET_A = "ZZZ_MODEL_A_ORIGINAL_SECRET_71bc4de0"

_RAG_MOD = "local_deep_research.research_library.services.library_rag_service"


class _FakeEmbeddings(Embeddings):
    """Deterministic, network-free embeddings distinguished by model name and
    dimension. Counts ``embed_documents`` calls (a real "was this re-embedded?"
    signal, distinct from ``embed_query``, which WRITE-path indexing's
    dimension-check pre-flight calls, and which search also calls to embed
    its own query -- neither indicates a re-embed of any document. Search's
    OWN pre-flight probe is SKIPPED on this read path (see
    ``LibraryRAGService.search``'s docstring): a wrong-dimension or
    otherwise degenerate query is rejected by the vector store's
    ``_prepare`` instead, without ever calling ``embed_query`` for a
    probe)."""

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

    def _vec(self, text: str):
        rng = np.random.default_rng(abs(hash((self.name, text))) % (2**32))
        return rng.random(self.dim).tolist()

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

    def embed_query(self, text):
        self.query_embed_calls += 1
        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`` (mirrors
    ``test_no_plaintext_production_path.py``'s service-level fixture)."""
    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,
    )

    # Integrity records live in the (mocked-away) DB side of this concern;
    # stub the manager so the test stays focused on the switch/revert
    # bookkeeping rather than integrity-record plumbing.
    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_doc(shared_world):
    """A real Collection + Document (containing SECRET_A) in the shared DB."""
    session = shared_world
    collection_id = uuid.uuid4().hex
    document_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="Switch/revert collection"))

    import hashlib

    body = (
        f"This document's original body mentions {SECRET_A} several times. " * 5
    )
    session.add(
        Document(
            id=document_id,
            source_type_id=source_type.id,
            document_hash=hashlib.sha256(body.encode()).hexdigest(),
            title="Switch/revert document",
            text_content=body,
            original_url="http://example.com/switch-revert",
            file_size=len(body),
            file_type="txt",
            status=DocumentStatus.COMPLETED,
        )
    )
    session.commit()
    return collection_id, document_id


def _make_service(username, db_password, model, dim, **overrides):
    """Build 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()
    fake = _FakeEmbeddings(name=model, dim=dim)
    emb_mgr.embeddings = fake

    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)
    svc = LibraryRAGService(**kwargs)
    return svc, fake


def _current_rows(session, collection_name):
    return (
        session.query(RAGIndex).filter_by(collection_name=collection_name).all()
    )


class TestMultiModelSwitchAndRevert:
    def test_switching_a_to_b_to_a_keeps_each_index_aggregate_scoped(
        self, shared_world, seeded_doc
    ):
        session = shared_world
        collection_id, document_id = seeded_doc
        svc_a, _ = _make_service("switchuser", "pw", "model-A", dim=8)
        svc_b, _ = _make_service("switchuser", "pw", "model-B", dim=4)

        try:
            first_a = svc_a.index_document(document_id, collection_id)
            assert first_a["status"] == "success", first_a
            index_a = (
                session.query(RAGIndex)
                .filter_by(
                    collection_name=f"collection_{collection_id}",
                    embedding_model="model-A",
                )
                .one()
            )
            assert (index_a.chunk_count, index_a.total_documents) == (
                first_a["chunk_count"],
                1,
            )

            first_b = svc_b.index_document(
                document_id, collection_id, force_reindex=True
            )
            assert first_b["status"] == "success", first_b
            index_b = (
                session.query(RAGIndex)
                .filter_by(
                    collection_name=f"collection_{collection_id}",
                    embedding_model="model-B",
                )
                .one()
            )
            assert (index_a.chunk_count, index_a.total_documents) == (0, 0)
            assert (index_b.chunk_count, index_b.total_documents) == (
                first_b["chunk_count"],
                1,
            )

            # Existing installations can already carry inflated counters.
            # Re-promoting A must repair them from the canonical status rows.
            index_a.chunk_count = 99
            index_a.total_documents = 9
            session.commit()

            second_a = svc_a.index_document(
                document_id, collection_id, force_reindex=True
            )
            assert second_a["status"] == "success", second_a
            assert (index_a.chunk_count, index_a.total_documents) == (
                second_a["chunk_count"],
                1,
            )
            assert (index_b.chunk_count, index_b.total_documents) == (0, 0)
            status = (
                session.query(RagDocumentStatus)
                .filter_by(document_id=document_id, collection_id=collection_id)
                .one()
            )
            assert status.rag_index_id == index_a.id
        finally:
            svc_a.close()
            svc_b.close()

    def test_prepared_write_transfers_aggregate_between_models(
        self, shared_world, seeded_doc
    ):
        session = shared_world
        collection_id, document_id = seeded_doc
        svc_a, _ = _make_service("switchuser", "pw", "model-A", dim=8)
        svc_b, _ = _make_service("switchuser", "pw", "model-B", dim=4)

        try:
            first_a = svc_a.index_document(document_id, collection_id)
            assert first_a["status"] == "success", first_a
            index_a = (
                session.query(RAGIndex)
                .filter_by(
                    collection_name=f"collection_{collection_id}",
                    embedding_model="model-A",
                )
                .one()
            )

            prepared_b = svc_b._prepare_document(
                document_id, collection_id, force_reindex=True
            )
            assert prepared_b["status"] == "prepared", prepared_b
            written_b = svc_b._write_prepared_document(prepared_b["prepared"])
            assert written_b["status"] == "success", written_b
            index_b = (
                session.query(RAGIndex)
                .filter_by(
                    collection_name=f"collection_{collection_id}",
                    embedding_model="model-B",
                )
                .one()
            )
            assert (index_a.chunk_count, index_a.total_documents) == (0, 0)
            assert (index_b.chunk_count, index_b.total_documents) == (
                written_b["chunk_count"],
                1,
            )

            prepared_a = svc_a._prepare_document(
                document_id, collection_id, force_reindex=True
            )
            assert prepared_a["status"] == "prepared", prepared_a
            written_a = svc_a._write_prepared_document(prepared_a["prepared"])
            assert written_a["status"] == "success", written_a
            assert (index_a.chunk_count, index_a.total_documents) == (
                written_a["chunk_count"],
                1,
            )
            assert (index_b.chunk_count, index_b.total_documents) == (0, 0)
        finally:
            svc_a.close()
            svc_b.close()

    def test_switch_to_new_model_then_revert_resolves_and_repromotes_original(
        self, shared_world, seeded_doc
    ):
        session = shared_world
        collection_id, document_id = seeded_doc
        collection_name = f"collection_{collection_id}"
        username = "switchuser"
        db_password = "pw"  # gitleaks:allow

        # ------------------------------------------------------------------
        # Step 1: index under model A.
        # ------------------------------------------------------------------
        svc_a, fake_a = _make_service(
            username,
            db_password,
            "model-A",
            dim=8,
            index_type="flat",
            distance_metric="cosine",
            normalize_vectors=True,
        )

        result_a1 = svc_a.index_document(document_id, collection_id)
        assert result_a1["status"] == "success", result_a1
        assert result_a1["chunk_count"] > 0

        rows_after_a = _current_rows(session, collection_name)
        assert len(rows_after_a) == 1, (
            "expected exactly one RAGIndex row after the first index"
        )
        row_a = rows_after_a[0]
        assert row_a.is_current is True
        assert row_a.embedding_model == "model-A"
        index_hash_a = row_a.index_hash
        row_a_id = row_a.id
        assert row_a.index_type == "flat"
        assert row_a.distance_metric == "cosine"
        assert row_a.normalize_vectors is True

        a_chunks_after_a = (
            session.query(DocumentChunk)
            .filter_by(source_id=document_id, embedding_model="model-A")
            .all()
        )
        assert len(a_chunks_after_a) == result_a1["chunk_count"]

        hits_a1 = svc_a.search(
            f"original body {SECRET_A}", collection_id, top_k=5
        )
        assert hits_a1, (
            "search under model A found nothing right after indexing"
        )
        assert any(SECRET_A in h.text for h in hits_a1)

        doc_embeds_after_a_index = fake_a.doc_embed_calls
        assert doc_embeds_after_a_index == 1

        # ------------------------------------------------------------------
        # Step 2: switch to model B, force-reindex the SAME document/collection.
        # ------------------------------------------------------------------
        svc_b, fake_b = _make_service(
            username,
            db_password,
            "model-B",
            dim=4,
            index_type="hnsw",
            distance_metric="l2",
            normalize_vectors=False,
        )

        result_b1 = svc_b.index_document(
            document_id, collection_id, force_reindex=True
        )
        assert result_b1["status"] == "success", result_b1
        assert result_b1["chunk_count"] > 0

        rows_after_b = _current_rows(session, collection_name)
        assert len(rows_after_b) == 2, (
            "expected a SECOND RAGIndex row for model B, not a mutation of A's"
        )
        current_rows_after_b = [r for r in rows_after_b if r.is_current]
        assert len(current_rows_after_b) == 1, (
            "exactly one RAGIndex row must be is_current after switching to B"
        )
        row_b = current_rows_after_b[0]
        assert row_b.embedding_model == "model-B"
        assert row_b.index_hash != index_hash_a
        status_b = (
            session.query(RagDocumentStatus)
            .filter_by(document_id=document_id, collection_id=collection_id)
            .one()
        )
        assert status_b.rag_index_id == row_b.id
        assert status_b.chunk_count == result_b1["chunk_count"]
        assert row_b.chunk_count == result_b1["chunk_count"]
        assert row_b.total_documents == 1

        # A's row must now be demoted, but NOT deleted/mutated in its own config.
        row_a_reloaded = session.query(RAGIndex).filter_by(id=row_a_id).first()
        assert row_a_reloaded.is_current is False
        assert row_a_reloaded.index_hash == index_hash_a
        assert row_a_reloaded.index_type == "flat"
        assert row_a_reloaded.distance_metric == "cosine"
        assert row_a_reloaded.normalize_vectors is True

        # CORE INVARIANT: model A's chunk rows (its only copy of the chunk
        # text) must survive model B's reindex of the SAME source/collection
        # -- the facade's replace-lookup is scoped by embedding_model.
        a_chunks_after_b = (
            session.query(DocumentChunk)
            .filter_by(source_id=document_id, embedding_model="model-A")
            .all()
        )
        assert len(a_chunks_after_b) == len(a_chunks_after_a), (
            "model A's chunk rows were destroyed by the model-B reindex of "
            "the same document/collection"
        )
        b_chunks_after_b = (
            session.query(DocumentChunk)
            .filter_by(source_id=document_id, embedding_model="model-B")
            .all()
        )
        assert len(b_chunks_after_b) == result_b1["chunk_count"]

        hits_b1 = svc_b.search(
            f"original body {SECRET_A}", collection_id, top_k=5
        )
        assert hits_b1, "search under model B found nothing after reindexing"
        assert any(SECRET_A in h.text for h in hits_b1)

        # A's own on-disk store was never touched by B's reindex (physically
        # separate .faiss file/index_hash) -- no re-embed happened for A.
        assert fake_a.doc_embed_calls == doc_embeds_after_a_index

        # ------------------------------------------------------------------
        # Step 3 (CONTROL): a mere SEARCH back under model A is a READ path
        # and must NOT silently re-promote A to current. If this assertion
        # were vacuously true (e.g. because nothing ever demotes/promotes),
        # the "exactly one current row flips back to A" assertion in step 4
        # would prove nothing -- this control shows the read/write gating
        # actually matters.
        # ------------------------------------------------------------------
        hits_a_readonly = svc_a.search(
            f"original body {SECRET_A}", collection_id, top_k=5
        )
        assert hits_a_readonly, (
            "model A's original vectors must still be searchable via a "
            "plain read-path search, even while B is current"
        )
        assert any(SECRET_A in h.text for h in hits_a_readonly)

        rows_after_readonly_search = _current_rows(session, collection_name)
        current_after_readonly = [
            r for r in rows_after_readonly_search if r.is_current
        ]
        assert len(current_after_readonly) == 1
        assert current_after_readonly[0].embedding_model == "model-B", (
            "a mere search under the reverted model must not steal the "
            "is_current pointer back from B -- only a write path may repromote"
        )
        # And the read-path search did not force any re-embed of A's chunks.
        assert fake_a.doc_embed_calls == doc_embeds_after_a_index

        # ------------------------------------------------------------------
        # Step 4: revert for real via the WRITE-path resolution
        # (_get_or_create_rag_index(..., promote_current=True) -- exactly how
        # index_document/_get_vector_index invoke it on a write).
        # ------------------------------------------------------------------
        resolved = svc_a._get_or_create_rag_index(
            collection_id, promote_current=True
        )
        assert resolved.index_hash == index_hash_a, (
            "revert must resolve A's ORIGINAL row by index_hash, not create "
            "a new one"
        )
        assert resolved.id == row_a_id
        assert resolved.is_current is True

        rows_after_revert = _current_rows(session, collection_name)
        assert len(rows_after_revert) == 2, (
            "revert must not create a THIRD row -- only two configs exist"
        )
        current_after_revert = [r for r in rows_after_revert if r.is_current]
        assert len(current_after_revert) == 1, (
            "exactly one RAGIndex row must be is_current after reverting"
        )
        assert current_after_revert[0].id == row_a_id
        assert current_after_revert[0].embedding_model == "model-A"

        # B's row must now be demoted (but intact, not deleted).
        row_b_reloaded = session.query(RAGIndex).filter_by(id=row_b.id).first()
        assert row_b_reloaded is not None
        assert row_b_reloaded.is_current is False

        # A's stored index_type/distance_metric/normalize_vectors must NOT
        # have been re-stamped with B's differing defaults (hnsw/l2/False)
        # by the revert/re-promotion.
        assert resolved.index_type == "flat"
        assert resolved.distance_metric == "cosine"
        assert resolved.normalize_vectors is True

        # The revert/re-promotion is pure bookkeeping -- it must not have
        # re-embedded anything (no new FAISS writes for A).
        assert fake_a.doc_embed_calls == doc_embeds_after_a_index

        # ------------------------------------------------------------------
        # Final observable outcome: search under A returns A's ORIGINAL
        # content, from vectors that were never touched/rebuilt throughout
        # the whole switch-and-revert sequence.
        # ------------------------------------------------------------------
        hits_a_final = svc_a.search(
            f"original body {SECRET_A}", collection_id, top_k=5
        )
        assert hits_a_final, "search under model A after revert found nothing"
        assert any(SECRET_A in h.text for h in hits_a_final)
        assert fake_a.doc_embed_calls == doc_embeds_after_a_index, (
            "search after revert must not have triggered a re-embed of A's "
            "document -- its original vectors were reused as-is"
        )

        svc_a.close()
        svc_b.close()