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.

20.5 KB
Raw Blame
"""Full legacy-migration lifecycle test at the SHARED (pre-per-user-scoping)
cache path -- the real production upgrade order, end to end in one chain.

Before this file, each phase of the cutover had isolated coverage (phase-1 in
``test_legacy_cleanup_no_plaintext.py``, the per-user relocation in
``test_migrate_legacy_index_files.py``, phase-2 in
``test_legacy_rekey_orchestrator.py``) but nothing drove the FULL upgrade
sequence a real deployment hits: a pre-cutover install has its FAISS index
sitting directly under the shared ``cache/rag_indices/`` root (no per-user
subdirectory existed yet), and the very first thing that happens after
upgrading is, in order:

  1. server startup runs phase-1 (``migrate_legacy_docstores``) against the
     SHARED root -- extracting sidecars, deleting plaintext ``.pkl``s;
  2. the first per-user touch (search/index) relocates that shared-root index
     into the new per-user subdirectory
     (``LibraryRAGService._migrate_legacy_index_files``, invoked via
     ``_preflight_index_path``);
  3. login-time phase-2 (``rekey_user_indexes``) converts the relocated
     (still old-format) ``.faiss`` into a real ``IndexIDMap2`` keyed by
     ``DocumentChunk.id``, without re-embedding;
  4. ordinary indexing/search then has to keep working against that
     migrated-in-place index -- a new document must MERGE into it (not
     create a second, duplicate ``RAGIndex`` row), and old content must
     still be searchable, rehydrated from the DB by id;
  5. (if the harness allows) deleting the collection's index must leave
     nothing at either the legacy OR the relocated path.

A ``_CountingEmbeddings`` fake proves the "no re-embed" guarantee the same
way ``test_legacy_rekey_orchestrator.py`` does: ``embed_documents_calls``
stays pinned at 1 (the original legacy build) through the ENTIRE migrate ->
relocate -> rekey -> search chain, and only increments by exactly 1 when a
genuinely NEW document is indexed afterward. A whole-tmp_path plaintext grep
(mirroring ``test_no_plaintext_production_path.py``) is re-run after every
stage, so a regression that lets the SECRET marker leak into any on-disk
artifact (not just the ``.faiss``/sidecar) is caught immediately.
"""

import hashlib
import uuid
from contextlib import contextmanager
from unittest.mock import MagicMock

import faiss
import pytest
from langchain_community.vectorstores import FAISS
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,
    DocumentCollection,
    DocumentStatus,
    EmbeddingProvider,
    RAGIndex,
    SourceType,
)
from local_deep_research.utilities.db_utils import get_settings_manager
from local_deep_research.vector_stores import get_vector_store_class
from local_deep_research.vector_stores.legacy_cleanup import (
    _rag_cache_root,
    migrate_legacy_docstores,
)
from local_deep_research.vector_stores.legacy_rekey import (
    REKEY_MARKER_SETTING,
    rekey_user_indexes,
)

SECRET = "ZZZ_FULL_LIFECYCLE_MARKER_9c31f0"
FRESH_MARKER = "FRESH_DOC_MARKER_5b7e"

_RAG_MOD = "local_deep_research.research_library.services.library_rag_service"
_FACADE_MOD = "local_deep_research.vector_stores.facade"
_REKEY_MOD = "local_deep_research.vector_stores.legacy_rekey"
_DELETE_MOD = (
    "local_deep_research.research_library.deletion.services.collection_deletion"
)


class _CountingEmbeddings(Embeddings):
    """Deterministic, network-free embeddings that COUNT calls, so the test
    can assert the migrated chunks are never re-embedded (mirrors
    ``test_legacy_rekey_orchestrator.py``'s harness)."""

    def __init__(self, dim: int = 2):
        self.dim = dim
        self.embed_documents_calls = 0
        self.embed_query_calls = 0

    def _vec(self, text: str):
        h = abs(hash(text))
        return [float((h >> (8 * i)) % 97) for i in range(self.dim)]

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

    def embed_query(self, text):
        self.embed_query_calls += 1
        return self._vec(text)


def _make_legacy_index(dir_path, ids, texts, embeddings, *, index_name):
    """Write a REAL old-format ``<index_name>.faiss`` + ``.pkl`` pair,
    exactly as the pre-cutover LangChain-FAISS wrapper did, directly under
    the given (SHARED, pre-per-user-scoping) directory."""
    dir_path.mkdir(parents=True, exist_ok=True)
    metadatas = [{"chunk": i} for i in range(len(texts))]
    vs = FAISS.from_texts(texts, embeddings, metadatas=metadatas, ids=ids)
    vs.save_local(str(dir_path), index_name=index_name)


def _grep_all(root, needle: bytes):
    """Every on-disk file under ``root`` that contains ``needle``."""
    return [
        str(f)
        for f in root.rglob("*")
        if f.is_file() and needle in f.read_bytes()
    ]


@contextmanager
def _session_ctx(session):
    yield session


@pytest.fixture
def db_session():
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    yield session
    session.close()
    engine.dispose()


@pytest.fixture
def patched_sites(db_session, mocker):
    """Route every ``get_user_db_session`` call-site this lifecycle touches
    to ONE shared in-memory session (facade, the RAG service, phase-2 rekey,
    and collection deletion), and stub the orthogonal file-integrity
    bookkeeping -- mirrors ``test_no_plaintext_production_path.py`` and
    ``test_legacy_rekey_orchestrator.py``'s harness style."""

    def _ctx(*_a, **_k):
        return _session_ctx(db_session)

    mocker.patch(f"{_FACADE_MOD}.get_user_db_session", _ctx)
    mocker.patch(f"{_RAG_MOD}.get_user_db_session", _ctx)
    mocker.patch(f"{_REKEY_MOD}.get_user_db_session", _ctx)
    mocker.patch(f"{_DELETE_MOD}.get_user_db_session", _ctx)

    fim = MagicMock()
    fim.verify_file.return_value = (True, None)
    fim.record_file.return_value = None
    mocker.patch(f"{_RAG_MOD}.FileIntegrityManager", return_value=fim)
    # legacy_rekey constructs its own FileIntegrityManager; patch the class
    # itself (no return_value) so every attribute access on an instance is a
    # harmless MagicMock -- integrity bookkeeping is orthogonal to the
    # migration invariants under test here.
    mocker.patch(f"{_REKEY_MOD}.FileIntegrityManager")

    return db_session


def _add_chunk(
    session,
    *,
    embedding_id: str,
    chunk_text: str,
    collection_name: str,
    source_id: str,
    chunk_index: int,
    embedding_model: str,
    embedding_dim: int = 2,
) -> int:
    chunk = DocumentChunk(
        chunk_hash=f"hash-{embedding_id}",
        source_type="document",
        source_id=source_id,
        collection_name=collection_name,
        chunk_text=chunk_text,
        chunk_index=chunk_index,
        start_char=0,
        end_char=len(chunk_text),
        word_count=len(chunk_text.split()),
        embedding_id=embedding_id,
        embedding_model=embedding_model,
        embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
        embedding_dimension=embedding_dim,
    )
    session.add(chunk)
    session.flush()
    return chunk.id


def _marker_is_set(session, username: str) -> bool:
    settings = get_settings_manager(db_session=session, username=username)
    return settings.get_bool_setting(REKEY_MARKER_SETTING, False)


class TestFullMigrationLifecycle:
    def test_shared_legacy_index_migrate_relocate_rekey_merge_delete(
        self, tmp_path, monkeypatch, patched_sites, mocker
    ):
        session = patched_sites
        username = "erin"
        db_password = "pw"
        embedding_model = "fake-model"
        embedding_provider = "sentence_transformers"

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

        collection_id = uuid.uuid4().hex
        collection_name = f"collection_{collection_id}"
        # The EXACT hash `_get_index_hash` computes -- so the RAGIndex row we
        # seed below is the one `_get_or_create_rag_index` finds (not a fresh
        # one it would otherwise create).
        index_hash = hashlib.sha256(
            f"{collection_name}:{embedding_model}:{embedding_provider}".encode()
        ).hexdigest()

        # =====================================================================
        # Step 1: a REALISTIC LEGACY index directly at the SHARED (NOT
        # per-user) cache/rag_indices root, with a RAGIndex row whose
        # index_path points there, and DocumentChunk rows carrying a SECRET
        # marker (chunk TEXT lives only in the DB -- never the .faiss/.pkl
        # bytes are trusted to hold it, but the pre-cutover .pkl format did).
        # =====================================================================
        legacy_root = _rag_cache_root()
        emb = _CountingEmbeddings(dim=2)
        legacy_uuids = [uuid.uuid4().hex for _ in range(2)]
        legacy_texts = [
            f"legacy chunk one mentioning {SECRET}",
            f"legacy chunk two also mentioning {SECRET}",
        ]
        _make_legacy_index(
            legacy_root, legacy_uuids, legacy_texts, emb, index_name=index_hash
        )
        legacy_faiss = legacy_root / f"{index_hash}.faiss"
        legacy_pkl = legacy_root / f"{index_hash}.pkl"
        legacy_sidecar = legacy_root / f"{index_hash}.idmap.json"
        assert legacy_faiss.is_file()
        assert legacy_pkl.is_file()
        # Control: the raw legacy pickle docstore really DOES hold the
        # plaintext marker -- proves the grep-based assertions below are not
        # vacuously passing on a fixture that never had it.
        assert SECRET.encode() in legacy_pkl.read_bytes()

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

        legacy_source_id = "legacy-doc-1"
        session.add(
            Document(
                id=legacy_source_id,
                source_type_id=source_type.id,
                document_hash=hashlib.sha256(
                    "".join(legacy_texts).encode()
                ).hexdigest(),
                title="Legacy document",
                text_content="\n".join(legacy_texts),
                file_size=sum(map(len, legacy_texts)),
                file_type="txt",
                status=DocumentStatus.COMPLETED,
            )
        )
        session.add(
            DocumentCollection(
                document_id=legacy_source_id, collection_id=collection_id
            )
        )
        session.commit()
        legacy_ids = [
            _add_chunk(
                session,
                embedding_id=u,
                chunk_text=t,
                collection_name=collection_name,
                source_id=legacy_source_id,
                chunk_index=i,
                embedding_model=embedding_model,
            )
            for i, (u, t) in enumerate(zip(legacy_uuids, legacy_texts))
        ]

        rag_index = RAGIndex(
            collection_name=collection_name,
            embedding_model=embedding_model,
            embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
            embedding_dimension=2,
            index_path=str(legacy_faiss),  # the SHARED, pre-scoping path
            index_hash=index_hash,
            chunk_size=500,
            chunk_overlap=50,
            # Pin the physical-build config so nothing here falls back to
            # (possibly DB-dependent) local_search_* settings lookups.
            index_type="flat",
            distance_metric="cosine",
            normalize_vectors=False,
            is_current=True,
        )
        session.add(rag_index)
        session.commit()
        session.refresh(rag_index)

        assert emb.embed_documents_calls == 1  # only the original legacy build

        # =====================================================================
        # Step 2 (phase-1): migrate_legacy_docstores() extracts the text-free
        # sidecar and deletes the plaintext .pkl.
        # =====================================================================
        migrate_result = migrate_legacy_docstores()
        assert migrate_result["extracted"] == 1
        assert not legacy_pkl.exists()
        assert legacy_sidecar.exists()
        assert SECRET.encode() not in legacy_sidecar.read_bytes()
        # Whole-tree grep: plaintext is gone from disk after phase-1 alone
        # (the DB is in-memory, so every on-disk artifact is fair game).
        assert _grep_all(tmp_path, SECRET.encode()) == []

        # =====================================================================
        # Step 3: trigger the per-user relocation. In production this is
        # invoked automatically by `_preflight_index_path` (called from every
        # `_get_vector_index` — i.e. every search/index) the first time a
        # user touches this collection after upgrading; we call the SAME
        # method directly here to isolate this phase from the load/embedding
        # machinery a full search/index call would also exercise.
        # =====================================================================
        emb_mgr = MagicMock()
        emb_mgr.embeddings = emb

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

        svc = LibraryRAGService(
            username=username,
            db_password=db_password,
            embedding_model=embedding_model,
            embedding_provider=embedding_provider,
            # Match the seeded rag_index row's build config. The expanded
            # index identity (migration 0028) now includes chunk_size/overlap,
            # so a mismatch here would resolve to a different, empty index.
            chunk_size=500,
            chunk_overlap=50,
            distance_metric="cosine",
            normalize_vectors=False,
            index_type="flat",
            embedding_manager=emb_mgr,
        )

        index_path = svc._get_index_path(index_hash)
        assert index_path != legacy_faiss
        assert not index_path.exists()

        svc._migrate_legacy_index_files(legacy_faiss, index_path, rag_index)

        assert index_path.exists()
        assert not legacy_faiss.exists()
        new_sidecar = index_path.with_name(index_path.stem + ".idmap.json")
        assert new_sidecar.exists()
        assert not legacy_sidecar.exists()
        session.refresh(rag_index)
        assert rag_index.index_path == str(index_path)

        # The relocated .faiss is STILL the raw legacy format -- relocation
        # is a pure file move; rekey (phase-2, next) hasn't run yet.
        raw = faiss.read_index(str(index_path))
        assert not isinstance(raw, faiss.IndexIDMap2)
        assert emb.embed_documents_calls == 1  # relocation never re-embeds

        # =====================================================================
        # Step 3 (phase-2): rekey_user_indexes() converts the relocated index
        # into a real IndexIDMap2 keyed by DocumentChunk.id, marker set.
        # =====================================================================
        stats = rekey_user_indexes(username, db_password)
        assert stats["checked"] == 1
        assert stats["rekeyed"] == 1
        assert stats["quarantined"] == 0
        assert not new_sidecar.exists()
        assert _marker_is_set(session, username) is True

        raw2 = faiss.read_index(str(index_path))
        assert isinstance(raw2, faiss.IndexIDMap2)

        store_cls = get_vector_store_class()
        store = store_cls.load(
            index_path,
            dimension=2,
            index_type="flat",
            metric="cosine",
            normalize=False,
        )
        assert set(store.live_ids()) == set(legacy_ids)

        # THE core "no re-embed" invariant: migrate + relocate + rekey never
        # called embed_documents again -- only the ORIGINAL legacy build did.
        assert emb.embed_documents_calls == 1
        assert emb.embed_query_calls == 0
        assert _grep_all(tmp_path, SECRET.encode()) == []

        # =====================================================================
        # Step 4: SEARCH through the real VectorIndex facade (via the
        # service) rehydrates the SECRET text from the DB by id -- the store
        # itself never held it -- and still doesn't re-embed the chunks.
        # =====================================================================
        hits = svc.search(f"legacy chunk {SECRET}", collection_id, top_k=5)
        assert hits
        assert any(SECRET in h.text for h in hits)
        assert emb.embed_documents_calls == 1  # search never re-embeds chunks
        assert emb.embed_query_calls > 0  # the query itself WAS embedded

        # =====================================================================
        # Step 5: a NEW document indexed normally MERGES into the relocated +
        # rekeyed index (searchable both ways), with NO duplicate RAGIndex
        # row created for this collection.
        # =====================================================================
        new_document_id = uuid.uuid4().hex
        new_body = (
            f"Brand new document body, unrelated content, mentions "
            f"{FRESH_MARKER} exactly once."
        )
        session.add(
            Document(
                id=new_document_id,
                source_type_id=source_type.id,
                document_hash=hashlib.sha256(new_body.encode()).hexdigest(),
                title="New doc",
                text_content=new_body,
                original_url="http://example.com/new",
                file_size=len(new_body),
                file_type="txt",
                status=DocumentStatus.COMPLETED,
            )
        )
        session.commit()

        docs_before = emb.embed_documents_calls
        result = svc.index_document(new_document_id, collection_id)
        assert result["status"] == "success", result
        assert result["chunk_count"] > 0

        # Exactly ONE more embed_documents call -- for the NEW doc's chunks
        # only. The migrated legacy content is still never re-embedded.
        assert emb.embed_documents_calls == docs_before + 1

        rag_rows = (
            session.query(RAGIndex)
            .filter_by(collection_name=collection_name)
            .all()
        )
        assert len(rag_rows) == 1, "a duplicate RAGIndex row was created"
        assert rag_rows[0].id == rag_index.id

        merged_store = store_cls.load(
            index_path,
            dimension=2,
            index_type="flat",
            metric="cosine",
            normalize=False,
        )
        new_chunk_ids = {
            row.id
            for row in session.query(DocumentChunk).filter_by(
                source_type="document", source_id=new_document_id
            )
        }
        assert new_chunk_ids
        assert set(merged_store.live_ids()) == set(legacy_ids) | new_chunk_ids

        old_hits = svc.search(f"legacy chunk {SECRET}", collection_id, top_k=5)
        assert any(SECRET in h.text for h in old_hits)
        new_hits = svc.search(FRESH_MARKER, collection_id, top_k=5)
        assert any(FRESH_MARKER in h.text for h in new_hits)
        # Cross-check: a query for the NEW marker must not accidentally
        # resolve to the OLD document (sanity that the merge kept both
        # sources distinct, not that search merely returns everything).
        assert all(
            SECRET not in h.text for h in new_hits if FRESH_MARKER in h.text
        )

        assert _grep_all(tmp_path, SECRET.encode()) == []
        assert _grep_all(tmp_path, FRESH_MARKER.encode()) == []

        # =====================================================================
        # Step 6 (if feasible): deleting the collection's index leaves
        # NOTHING at either the legacy or the relocated path.
        # =====================================================================
        from local_deep_research.research_library.deletion.services.collection_deletion import (
            CollectionDeletionService,
        )

        deleter = CollectionDeletionService(username)
        delete_result = deleter.delete_collection_index_only(collection_id)
        assert delete_result["deleted"] is True
        assert delete_result["indices_deleted"] == 1

        assert not index_path.exists()
        assert not legacy_faiss.exists()
        assert not new_sidecar.exists()
        assert not legacy_sidecar.exists()
        assert (
            session.query(RAGIndex)
            .filter_by(collection_name=collection_name)
            .count()
            == 0
        )

        svc.close()