~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Unit tests for ``LibraryRAGService._migrate_legacy_index_files``.
Before per-user path scoping, FAISS indexes lived directly under the shared
``cache/rag_indices/`` directory. ``_migrate_legacy_index_files`` is the
one-time migration that relocates such a legacy index into the new per-user
subdirectory the rest of the service now reads/writes.
Two invariants are pinned here:
(a) A legacy ``.faiss`` + its phase-1 ``.idmap.json`` sidecar are BOTH
relocated to the per-user path, while any legacy plaintext ``.pkl``
companion is DELETED (never relocated) — the new store keeps chunk text
only in the encrypted per-user DB, so carrying the old pickle docstore
forward would leak plaintext into the new location.
(b) Ordering/idempotency: the method moves the sidecar first and the
``.faiss`` last specifically so that the re-entry guard (which keys only
on ``index_path`` — the ``.faiss`` — already existing) cannot leave the
sidecar orphaned at the legacy path after a crash between the two
renames. We simulate that exact partial state (sidecar already moved,
``.faiss`` still legacy) and assert re-invocation converges to a single,
correctly-placed sidecar with no duplication and no data loss — without
needing to touch the embeddings/re-embed path at all.
Both tests build real on-disk files (a tiny valid ``faiss.IndexIDMap2``, a
JSON sidecar, a legacy ``.pkl``) and a real in-memory SQLite session for the
``RAGIndex`` row the method updates; only the embedding-manager-heavy
``LibraryRAGService.__init__`` is bypassed (via ``__new__``) since the method
under test never touches embeddings.
"""
import json
from contextlib import contextmanager
from pathlib import Path
import faiss
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 (
EmbeddingProvider,
RAGIndex,
RAGIndexStatus,
)
from local_deep_research.research_library.services.library_rag_service import (
LibraryRAGService,
)
_MOD = "local_deep_research.research_library.services.library_rag_service"
# ---------------------------------------------------------------------------
# Helpers / fixtures
# ---------------------------------------------------------------------------
def _write_tiny_faiss(path, dim: int = 4) -> None:
"""A minimal but genuinely valid IndexIDMap2 file — real faiss bytes,
not a stub — so a rename/load round-trip is meaningful."""
index = faiss.IndexIDMap2(faiss.IndexFlatL2(dim))
index.add_with_ids(
np.zeros((1, dim), dtype="float32"), np.array([1], dtype="int64")
)
faiss.write_index(index, str(path))
def _make_service(
username: str = "alice", db_password: str = "pw"
) -> LibraryRAGService:
"""Construct a LibraryRAGService without running __init__.
__init__ builds a real embedding manager, a settings snapshot from the
DB, and runs egress-policy checks — none of which
``_migrate_legacy_index_files`` touches. It only reads
``self.username``, ``self.db_password`` (via ``self._db_password``),
and ``self.integrity_manager.record_file``. Bypassing __init__ with
``__new__`` keeps the test scoped to the method under test while every
file/DB interaction inside it is still the real thing.
"""
svc = LibraryRAGService.__new__(LibraryRAGService)
svc.username = username
svc._db_password = db_password
svc.integrity_manager = _RecordingIntegrityManager()
return svc
class _RecordingIntegrityManager:
"""Minimal stand-in for FileIntegrityManager.record_file.
Not a MagicMock: we want a real assertable call log without pretending
the mock itself proves anything, and without pulling in the full
integrity-manager machinery (DB-backed hashing) that isn't part of the
invariant under test here.
"""
def __init__(self):
self.recorded = []
def record_file(self, path, **kwargs):
self.recorded.append(path)
@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_db(db_session, mocker):
@contextmanager
def _mock_session(*_a, **_k):
yield db_session
mocker.patch(f"{_MOD}.get_user_db_session", _mock_session)
return db_session
def _make_rag_index(session, index_path) -> RAGIndex:
idx = RAGIndex(
collection_name="collection_c1",
embedding_model="fake-model",
embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
embedding_dimension=4,
index_path=str(index_path),
index_hash="a" * 64,
chunk_size=500,
chunk_overlap=50,
status=RAGIndexStatus.ACTIVE,
is_current=True,
)
session.add(idx)
session.commit()
session.refresh(idx)
return idx
# ---------------------------------------------------------------------------
# (a) Relocation: .faiss + sidecar move, .pkl is deleted not relocated
# ---------------------------------------------------------------------------
class TestRelocatesFaissAndSidecarDeletesPkl:
def test_migration_moves_faiss_and_sidecar_deletes_pkl(
self, tmp_path, monkeypatch, patched_db
):
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
from local_deep_research.config.paths import get_cache_directory
legacy_root = get_cache_directory() / "rag_indices"
legacy_root.mkdir(parents=True, exist_ok=True)
legacy_path = legacy_root / "deadbeef.faiss"
legacy_sidecar = legacy_root / "deadbeef.idmap.json"
legacy_pkl = legacy_root / "deadbeef.pkl"
_write_tiny_faiss(legacy_path)
legacy_sidecar.write_text(json.dumps({"1": 101}))
legacy_pkl.write_bytes(b"legacy plaintext docstore bytes")
new_dir = get_cache_directory() / "rag_indices" / "userscope"
new_dir.mkdir(parents=True, exist_ok=True)
index_path = new_dir / "deadbeef.faiss"
rag_index = _make_rag_index(patched_db, index_path)
svc = _make_service()
svc._migrate_legacy_index_files(legacy_path, index_path, rag_index)
# The .faiss landed at the new per-user path...
assert index_path.exists()
assert not legacy_path.exists()
# It really is a valid, loadable IndexIDMap2 (not just bytes moved
# around) — proves we moved the real file, not a placeholder.
reloaded = faiss.read_index(str(index_path))
assert faiss.downcast_index(reloaded).ntotal == 1
# ...the sidecar moved alongside it...
new_sidecar = new_dir / "deadbeef.idmap.json"
assert new_sidecar.exists()
assert not legacy_sidecar.exists()
assert json.loads(new_sidecar.read_text()) == {"1": 101}
# ...and the legacy plaintext .pkl was DELETED, not relocated: the
# core no-plaintext-at-rest invariant for this migration path.
assert not legacy_pkl.exists()
new_pkl = new_dir / "deadbeef.pkl"
assert not new_pkl.exists()
# The DB row was repointed at the new location.
patched_db.refresh(rag_index)
assert rag_index.index_path == str(index_path)
# And the integrity record was refreshed under the NEW path (the old
# record, keyed by the legacy path, no longer applies).
assert svc.integrity_manager.recorded == [index_path]
def test_no_sidecar_no_pkl_just_moves_faiss(
self, tmp_path, monkeypatch, patched_db
):
"""Bare legacy .faiss with neither companion still migrates cleanly
(sidecar/pkl handling must not assume they exist)."""
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
from local_deep_research.config.paths import get_cache_directory
legacy_root = get_cache_directory() / "rag_indices"
legacy_root.mkdir(parents=True, exist_ok=True)
legacy_path = legacy_root / "cafe1234.faiss"
_write_tiny_faiss(legacy_path)
new_dir = legacy_root / "userscope"
new_dir.mkdir(parents=True, exist_ok=True)
index_path = new_dir / "cafe1234.faiss"
rag_index = _make_rag_index(patched_db, index_path)
svc = _make_service()
svc._migrate_legacy_index_files(legacy_path, index_path, rag_index)
assert index_path.exists()
assert not legacy_path.exists()
assert list(new_dir.glob("*.idmap.json")) == []
assert list(new_dir.glob("*.pkl")) == []
# ---------------------------------------------------------------------------
# (b) Crash-recovery ordering / idempotency
# ---------------------------------------------------------------------------
class TestCrashRecoveryOrderingIdempotency:
def test_reentry_after_sidecar_moved_but_faiss_not_yet_converges(
self, tmp_path, monkeypatch, patched_db
):
"""Simulates a crash landing exactly between the two renames the
method performs: the sidecar already sits at the NEW path, but the
.faiss is still at the legacy path (the guard — ``index_path.exists()``
— is keyed only on the .faiss, so it is still False and re-entry is
NOT short-circuited). Re-invocation must:
* still relocate the (only-remaining-at-legacy) .faiss,
* leave exactly ONE sidecar at the new path (no duplicate, no
clobber-with-empty, no loss),
* NOT require re-embedding anything (this whole path is a pure
file move; asserting the sidecar's bytes are byte-identical to
the pre-crash content is the proxy for "no re-embed happened").
"""
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
from local_deep_research.config.paths import get_cache_directory
legacy_root = get_cache_directory() / "rag_indices"
legacy_root.mkdir(parents=True, exist_ok=True)
legacy_path = legacy_root / "feedface.faiss"
_write_tiny_faiss(legacy_path)
# NOTE: no legacy sidecar left on disk — it already "moved" below,
# simulating the crash point being AFTER the first rename.
new_dir = legacy_root / "userscope"
new_dir.mkdir(parents=True, exist_ok=True)
index_path = new_dir / "feedface.faiss"
new_sidecar = new_dir / "feedface.idmap.json"
sidecar_payload = {"1": 101, "2": 202}
new_sidecar.write_text(json.dumps(sidecar_payload))
rag_index = _make_rag_index(patched_db, index_path)
svc = _make_service()
# Re-entry: the guard only checks index_path.exists(), which is
# still False (the .faiss never made it across before the "crash"),
# so the method must run again rather than no-op.
svc._migrate_legacy_index_files(legacy_path, index_path, rag_index)
assert index_path.exists()
assert not legacy_path.exists()
# Exactly one sidecar at the new location, contents untouched —
# not duplicated, not overwritten with something empty/new, and no
# re-embed occurred (the payload is byte-for-byte the pre-crash one).
sidecars = list(new_dir.glob("*.idmap.json"))
assert len(sidecars) == 1
assert json.loads(new_sidecar.read_text()) == sidecar_payload
patched_db.refresh(rag_index)
assert rag_index.index_path == str(index_path)
def test_sidecar_is_renamed_strictly_before_the_faiss(
self, tmp_path, monkeypatch, patched_db
):
"""Directly pins the ORDER of the two renames (not just an end
state): the sidecar rename must complete before the .faiss rename is
even attempted. This is the concrete mechanism the source comment
relies on for crash-safety — the re-entry guard checks only
``index_path.exists()`` (the .faiss), so if a future refactor moved
the .faiss FIRST, a crash between the two renames would leave the
guard satisfied (.faiss present) while the sidecar sits orphaned at
the legacy path forever, with no way for re-entry to ever repair it.
Recording actual ``Path.rename`` call order catches that reversal
directly, independent of any specific crash-state fixture.
"""
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
from local_deep_research.config.paths import get_cache_directory
legacy_root = get_cache_directory() / "rag_indices"
legacy_root.mkdir(parents=True, exist_ok=True)
legacy_path = legacy_root / "0rder1ng.faiss"
legacy_sidecar = legacy_root / "0rder1ng.idmap.json"
_write_tiny_faiss(legacy_path)
legacy_sidecar.write_text(json.dumps({"1": 101}))
new_dir = legacy_root / "userscope"
new_dir.mkdir(parents=True, exist_ok=True)
index_path = new_dir / "0rder1ng.faiss"
rag_index = _make_rag_index(patched_db, index_path)
svc = _make_service()
rename_order = []
original_rename = Path.rename
def _recording_rename(self_path, target):
rename_order.append(self_path.name)
return original_rename(self_path, target)
monkeypatch.setattr(Path, "rename", _recording_rename)
svc._migrate_legacy_index_files(legacy_path, index_path, rag_index)
# Both renames happened...
assert "0rder1ng.idmap.json" in rename_order
assert "0rder1ng.faiss" in rename_order
# ...and the sidecar's rename was recorded strictly before the
# .faiss's -- reversing this would silently reintroduce an
# unrecoverable-orphan crash window (see docstring above).
assert rename_order.index("0rder1ng.idmap.json") < rename_order.index(
"0rder1ng.faiss"
)
def test_reentry_once_faiss_already_migrated_is_a_pure_noop(
self, tmp_path, monkeypatch, patched_db
):
"""Once the .faiss has already landed at index_path (migration fully
completed), the guard must short-circuit: calling again must not
touch the DB row a second time, must not error even though the
legacy path is long gone, and must leave the already-migrated files
alone."""
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
from local_deep_research.config.paths import get_cache_directory
legacy_root = get_cache_directory() / "rag_indices"
legacy_root.mkdir(parents=True, exist_ok=True)
legacy_path = legacy_root / "0ff1ce00.faiss"
# Legacy path does not exist any more — full prior migration.
new_dir = legacy_root / "userscope"
new_dir.mkdir(parents=True, exist_ok=True)
index_path = new_dir / "0ff1ce00.faiss"
_write_tiny_faiss(index_path)
new_sidecar = new_dir / "0ff1ce00.idmap.json"
new_sidecar.write_text(json.dumps({"1": 101}))
rag_index = _make_rag_index(patched_db, index_path)
# Simulate the row having already been repointed by a prior run and
# sanity-checked since (bump last_updated implicitly via commit).
original_path_value = rag_index.index_path
svc = _make_service()
svc._migrate_legacy_index_files(legacy_path, index_path, rag_index)
# Nothing changed: no integrity re-record, DB row untouched, files
# exactly as they were.
assert svc.integrity_manager.recorded == []
patched_db.refresh(rag_index)
assert rag_index.index_path == original_path_value
assert index_path.exists()
assert json.loads(new_sidecar.read_text()) == {"1": 101}
# ---------------------------------------------------------------------------
# Sanity check that the assertions above actually bite: a deliberately-wrong
# "migration" (delete-then-recreate instead of rename) would corrupt the
# faiss payload identity check, and a naive "always overwrite sidecar" would
# fail the byte-identity check in the crash-recovery test. We don't need to
# monkeypatch the method to prove this: the .idmap.json equality assertions
# and the faiss ntotal check are only meaningful if they can fail, which the
# following demonstrates on deliberately corrupted fixtures.
# ---------------------------------------------------------------------------
class TestAssertionsAreNotTautological:
def test_ntotal_check_would_fail_on_empty_index(self, tmp_path):
empty_index = faiss.IndexIDMap2(faiss.IndexFlatL2(4))
path = tmp_path / "empty.faiss"
faiss.write_index(empty_index, str(path))
reloaded = faiss.read_index(str(path))
assert faiss.downcast_index(reloaded).ntotal == 0
# i.e. this would NOT satisfy `== 1` as asserted against the real
# migrated file above -- the check is discriminating, not vacuous.
def test_sidecar_equality_check_would_fail_on_altered_payload(
self, tmp_path
):
sidecar = tmp_path / "s.idmap.json"
sidecar.write_text(json.dumps({"1": 101, "2": 202}))
assert json.loads(sidecar.read_text()) != {"1": 999}