~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
"""Security regression test: the legacy-docstore migration leaves NO plaintext
chunk text or metadata on disk.
Builds a REAL legacy LangChain-FAISS index (``.faiss`` + pickled ``.pkl``
docstore) containing a known secret marker in both the chunk text and the
metadata, runs the startup migration, and then greps every file under the RAG
cache to prove the marker is gone — while the text-free ``.idmap.json`` sidecar
(needed for the no-re-embed re-key) survives.
Uses ``LDR_DATA_DIR`` -> ``tmp_path`` so it can never touch real data.
"""
import json
import uuid
import pytest
from langchain_community.vectorstores import FAISS
from langchain_core.embeddings import Embeddings
from local_deep_research.vector_stores.legacy_cleanup import (
migrate_legacy_docstores,
_rag_cache_root,
)
# Distinctive marker that must not survive anywhere on disk after migration.
SECRET = "ZZZ_PLAINTEXT_CHUNK_SECRET_9f3a1c"
class _FakeEmbeddings(Embeddings):
"""Deterministic 2-D embeddings — no model download, no network."""
def embed_documents(self, texts):
return [[float(len(t) % 7), 1.0] for t in texts]
def embed_query(self, query):
return [1.0, 0.0]
@pytest.fixture
def cache_root(tmp_path, monkeypatch):
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
return _rag_cache_root()
def _make_legacy_index(dir_path, ids, texts, *, index_name="idx"):
"""Write a real old-format ``<index_name>.faiss`` + ``.pkl`` pair."""
dir_path.mkdir(parents=True, exist_ok=True)
metadatas = [{"document_title": f"secret-title-{SECRET}"} for _ in texts]
vs = FAISS.from_texts(
texts, _FakeEmbeddings(), metadatas=metadatas, ids=ids
)
vs.save_local(str(dir_path), index_name=index_name)
def _grep_all(root, needle: bytes):
return [
str(f)
for f in root.rglob("*")
if f.is_file() and needle in f.read_bytes()
]
def test_migration_refuses_symlinked_cache_root(cache_root, tmp_path):
"""A SYMLINKED rag_indices/ root would make os.walk + root.chmod + the
.pkl-delete pass operate OUTSIDE the intended tree (os.walk and Path.chmod
both follow a symlinked TOP-LEVEL path; followlinks=False only guards
symlinked CHILDREN). Migration must refuse and flag it, not transparently
follow it — chmod'ing and pattern-deleting files it does not own."""
import os
outside = tmp_path / "outside"
outside.mkdir()
foreign_pkl = outside / "abc123.pkl"
foreign_pkl.write_bytes(b"not ours")
# Intentionally world-readable to prove the migration does NOT chmod it
# (the symlinked-root refusal must leave out-of-tree files untouched).
os.chmod(outside, 0o755) # noqa: S103
# cache_root = <data>/cache/rag_indices — make it a symlink to `outside`.
cache_root.parent.mkdir(parents=True, exist_ok=True)
try:
os.symlink(outside, cache_root, target_is_directory=True)
except (OSError, NotImplementedError):
pytest.skip("symlinks not supported on this platform")
result = migrate_legacy_docstores()
# Refused + flagged, nothing deleted.
assert result["scan_errors"] >= 1
assert result["deleted"] == 0
# The foreign .pkl outside the tree survives AND its dir mode is unchanged
# (root.chmod never followed the symlink).
assert foreign_pkl.exists()
assert (os.stat(outside).st_mode & 0o777) == 0o755
def test_migration_reports_scan_errors_for_unreadable_subdir(cache_root):
"""An unreadable subdirectory hides its .pkl from BOTH the delete pass and
the re-scan, so migrate must NOT report clean success — it must surface the
scan error (else a plaintext .pkl silently survives)."""
import os
if os.geteuid() == 0:
pytest.skip("running as root bypasses directory permissions")
blocked = cache_root / "blockeduser"
ids = [uuid.uuid4().hex]
_make_legacy_index(blocked, ids, [f"hidden {SECRET}"])
assert (blocked / "idx.pkl").exists()
os.chmod(blocked, 0o000) # make the subdir unscannable
try:
result = migrate_legacy_docstores()
# The blocked .pkl is invisible to the scan, so `remaining` reads 0 —
# but scan_errors must flag the incomplete migration.
assert result["scan_errors"] >= 1
finally:
os.chmod(blocked, 0o700) # restore so tmp cleanup can run
def test_migration_reports_scan_errors_for_symlinked_subdir(
cache_root, tmp_path
):
"""os.walk does not descend into a symlinked subdir (followlinks=False) and
does not call onerror for it, so a .pkl inside would be silently missed. The
migration must flag the symlinked dir as an unscanned (incomplete) case."""
import os
# Real directory (outside the walked tree) holding a legacy .pkl.
target = tmp_path / "real_target"
ids = [uuid.uuid4().hex]
_make_legacy_index(target, ids, [f"behind-symlink {SECRET}"])
assert (target / "idx.pkl").exists()
# Symlink it under the RAG cache root — os.walk will skip it silently.
cache_root.mkdir(parents=True, exist_ok=True)
link = cache_root / "linked"
try:
os.symlink(target, link, target_is_directory=True)
except (OSError, NotImplementedError):
pytest.skip("symlinks not supported on this platform")
result = migrate_legacy_docstores()
assert result["scan_errors"] >= 1
def test_migration_flags_symlinked_pkl_and_spares_real_target(
cache_root, tmp_path
):
"""A .pkl that is a symlink is a trap: unlink() would remove only the link,
leaving the real plaintext target intact while the run logs clean success.
The migration must flag it (scan_errors) and NOT touch the real file."""
import os
# Real plaintext docstore OUTSIDE the cache root.
real = tmp_path / "outside"
_make_legacy_index(real, [uuid.uuid4().hex], [f"outside {SECRET}"])
real_pkl = real / "idx.pkl"
assert SECRET.encode() in real_pkl.read_bytes()
# A symlink to it under the RAG cache root.
cache_root.mkdir(parents=True, exist_ok=True)
link = cache_root / "linked.pkl"
try:
os.symlink(real_pkl, link)
except (OSError, NotImplementedError):
pytest.skip("symlinks not supported on this platform")
result = migrate_legacy_docstores()
# Flagged as incomplete, and the real plaintext file is untouched.
assert result["scan_errors"] >= 1
assert real_pkl.exists()
assert SECRET.encode() in real_pkl.read_bytes()
def test_migration_leaves_no_plaintext_on_disk(cache_root):
userdir = cache_root / "userhash1"
# Real embedding_id format: uuid4().hex (what _store_chunks_to_db writes).
ids = [uuid.uuid4().hex for _ in range(3)]
texts = [f"one {SECRET} x", f"two {SECRET}", "three plain"]
_make_legacy_index(userdir, ids, texts)
pkl = userdir / "idx.pkl"
# Precondition: the plaintext really is in the legacy .pkl before migration.
assert SECRET.encode() in pkl.read_bytes()
result = migrate_legacy_docstores()
assert result["extracted"] == 1
assert result["deleted"] == 1
assert result["remaining"] == 0
assert not pkl.exists()
assert (userdir / "idx.faiss").exists() # vectors kept for the re-key
# The core assertion: the secret marker (text AND metadata) is gone from
# EVERY file under the RAG cache — nothing plaintext left on disk.
assert _grep_all(cache_root, SECRET.encode()) == []
# The sidecar preserves exactly the text-free position->uuid map.
sidecar = userdir / "idx.idmap.json"
assert sidecar.exists()
assert SECRET not in sidecar.read_text()
idmap = json.loads(sidecar.read_text())
assert set(idmap.values()) == set(ids)
def test_quarantined_pkl_is_deleted(cache_root):
userdir = cache_root / "userhash2"
_make_legacy_index(userdir, ["u1", "u2"], [f"q {SECRET}", "plain"])
# Simulate the quarantine rename the old code does on a corrupt index.
(userdir / "idx.pkl").rename(userdir / "idx.pkl.corrupt-123456789")
result = migrate_legacy_docstores()
assert result["deleted"] == 1
assert result["remaining"] == 0
# Quarantined docstores also hold plaintext -> must be gone, no sidecar.
assert _grep_all(cache_root, SECRET.encode()) == []
assert not list(userdir.glob("*.pkl*"))
def test_unreadable_pkl_deleted_and_flagged_for_reindex(cache_root):
userdir = cache_root / "userhash3"
userdir.mkdir(parents=True)
# A .pkl that is not a valid pickle -> extraction fails.
bad = userdir / "idx.pkl"
bad.write_bytes(b"not a pickle " + SECRET.encode())
result = migrate_legacy_docstores()
assert result["reindex_fallback"] == 1
assert result["deleted"] == 1
assert result["remaining"] == 0
assert not bad.exists() # deleted anyway -> plaintext removed
assert _grep_all(cache_root, SECRET.encode()) == []
assert not (userdir / "idx.idmap.json").exists() # no map to preserve
def test_idempotent_on_clean_tree(cache_root):
cache_root.mkdir(parents=True, exist_ok=True)
assert migrate_legacy_docstores()["found"] == 0
# Second run on an already-clean tree is a no-op, not an error.
assert migrate_legacy_docstores()["found"] == 0
def test_second_migration_run_after_real_migration_preserves_sidecar_and_is_noop(
cache_root,
):
"""Phase-1 run on a REAL legacy .pkl writes the .idmap.json sidecar that
phase-2 (login-time rekey) depends on. A second phase-1 run — startup
always re-runs it — must find nothing left to migrate (the .pkl is already
gone) and must NOT touch the sidecar bytes; phase-2 reads that exact
position->uuid map, so clobbering or rewriting it here would corrupt the
no-re-embed re-key."""
userdir = cache_root / "userF"
ids = [uuid.uuid4().hex for _ in range(2)]
_make_legacy_index(userdir, ids, [f"one {SECRET}", "two"])
first = migrate_legacy_docstores()
assert first["extracted"] == 1
assert first["deleted"] == 1
sidecar = userdir / "idx.idmap.json"
assert sidecar.exists()
sidecar_bytes = sidecar.read_bytes()
second = migrate_legacy_docstores()
# Nothing left to migrate: the .pkl is already gone.
assert second["found"] == 0
assert second["extracted"] == 0
assert second["deleted"] == 0
# Byte-identical: the sidecar phase-2 needs was not touched, let alone
# rewritten, by the no-op second run.
assert sidecar.read_bytes() == sidecar_bytes
def test_extract_map_sweeps_stale_tmp_but_spares_fresh_concurrent_tmp(
cache_root,
):
"""``_write_sidecar_atomic``'s stale-``.tmp`` sweep must remove an orphaned
temp left by a hard-killed prior migration run, but never touch a FRESH
concurrent worker's still-in-flight temp for the same sidecar (gunicorn
-w N can start phase-1 in two workers at once). Creates both temps with
the REAL ``tempfile.mkstemp`` prefix/suffix the production write uses, so
this fails outright if the sweep's glob and the write's own mkstemp call
ever drift apart."""
import os
import tempfile
import time
from pathlib import Path
from local_deep_research.vector_stores.legacy_cleanup import (
_STALE_TMP_AGE_SECONDS,
_write_sidecar_atomic,
)
userdir = cache_root / "userG"
userdir.mkdir(parents=True)
sidecar = userdir / "idx.idmap.json"
# Stale sibling temp: same prefix/suffix the real write uses, backdated
# past the age threshold -> must be swept.
stale_fd, stale_name = tempfile.mkstemp(
dir=str(userdir), prefix=f"{sidecar.name}.", suffix=".tmp"
)
os.close(stale_fd)
stale = Path(stale_name)
old_time = time.time() - _STALE_TMP_AGE_SECONDS - 60
os.utime(stale, (old_time, old_time))
# Fresh concurrent temp: identical naming, just created -> must be spared.
fresh_fd, fresh_name = tempfile.mkstemp(
dir=str(userdir), prefix=f"{sidecar.name}.", suffix=".tmp"
)
os.close(fresh_fd)
fresh = Path(fresh_name)
_write_sidecar_atomic(sidecar, {"0": uuid.uuid4().hex})
assert not stale.exists()
assert fresh.exists()
def _write_raw_pkl(dir_path, index_to_docstore_id, *, index_name="idx"):
"""Write a ``.pkl`` with an arbitrary (restricted-unpickler-compatible) map
+ a matching fake ``.faiss`` so it looks like a real index pair."""
import pickle
from langchain_community.docstore.in_memory import InMemoryDocstore
dir_path.mkdir(parents=True, exist_ok=True)
with open(dir_path / f"{index_name}.pkl", "wb") as f:
pickle.dump((InMemoryDocstore({}), index_to_docstore_id), f)
(dir_path / f"{index_name}.faiss").write_bytes(b"fake-vectors")
def test_malformed_idmap_does_not_crash_and_others_processed(cache_root):
# A tampered .pkl whose id-map is a list, not a dict — must not crash the
# whole run (else other users' plaintext would never be removed).
_write_raw_pkl(cache_root / "userA", ["not", "a", "dict"])
_make_legacy_index(
cache_root / "userB",
[uuid.uuid4().hex, uuid.uuid4().hex],
[f"real {SECRET}", "x"],
)
result = migrate_legacy_docstores() # must NOT raise
assert not (cache_root / "userA" / "idx.pkl").exists() # deleted anyway
assert not (cache_root / "userA" / "idx.idmap.json").exists() # no sidecar
assert result["reindex_fallback"] >= 1
# userB was still reached and processed despite userA's bad file.
assert not (cache_root / "userB" / "idx.pkl").exists()
assert (cache_root / "userB" / "idx.idmap.json").exists()
assert result["remaining"] == 0
assert _grep_all(cache_root, SECRET.encode()) == []
def test_non_uuid_values_never_reach_the_sidecar(cache_root):
smuggled = "EXFIL_SECRET_TEXT_LOOKALIKE_zzzzz"
# One smuggled value + one real uuid — a single bad entry rejects the map.
_write_raw_pkl(
cache_root / "userC",
{0: smuggled, 1: "a" * 32},
)
result = migrate_legacy_docstores()
assert not (cache_root / "userC" / "idx.pkl").exists()
assert not (cache_root / "userC" / "idx.idmap.json").exists()
assert result["reindex_fallback"] >= 1
# The smuggled text never lands anywhere on disk.
assert _grep_all(cache_root, smuggled.encode()) == []
def test_non_string_idmap_value_is_rejected_without_str_coercion(cache_root):
# A tampered .pkl whose id-map VALUE is a non-string object (here a nested
# list) must be rejected outright, never coerced via str(). str() on a
# maliciously deep/shared structure is a pickle "billion laughs"
# amplification that could hang/OOM the whole startup migration.
_write_raw_pkl(
cache_root / "userD",
{
0: ["not", "a", ["uuid", "string"]],
1: "b" * 32,
},
)
# Must complete quickly and without raising; the bad entry rejects the map.
result = migrate_legacy_docstores()
assert not (cache_root / "userD" / "idx.pkl").exists() # deleted anyway
assert not (cache_root / "userD" / "idx.idmap.json").exists() # no sidecar
assert result["reindex_fallback"] >= 1
assert result["remaining"] == 0
def test_full_migration_round_trip_no_reembed(cache_root):
"""old LangChain FAISS -> extract sidecar -> re-key to IndexIDMap2 by int id,
without re-embedding; search returns the int ids; orphans dropped."""
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
from local_deep_research.vector_stores import get_vector_store_class
userdir = cache_root / "u"
uuids = [uuid.uuid4().hex for _ in range(4)]
_make_legacy_index(userdir, uuids, [f"t{i}" for i in range(4)])
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json"
migrate_legacy_docstores()
assert sidecar.exists() and not (userdir / "idx.pkl").exists()
# DB map uuid -> DocumentChunk.id; drop uuids[3] to simulate a deleted chunk.
uuid_to_id = {uuids[0]: 100, uuids[1]: 200, uuids[2]: 300}
res = rekey_index_file(
faiss_p,
sidecar,
uuid_to_id,
dimension=2, # _FakeEmbeddings is 2-D
index_type="flat",
metric="cosine",
normalize=True,
)
assert res == {"reconstructed": 4, "kept": 3, "orphaned": 1}
# rekey_index_file no longer deletes the sidecar — the caller does that only
# after recording integrity (crash-consistency), so it survives here.
assert sidecar.exists()
# The re-keyed index is IndexIDMap2 keyed by the int DocumentChunk.ids.
store = get_vector_store_class().load(
faiss_p,
dimension=2,
index_type="flat",
metric="cosine",
normalize=True,
)
assert set(store.live_ids()) == {100, 200, 300}
hits = store.search([0.5, 0.5], 3)
assert all(cid in {100, 200, 300} for cid, _ in hits)
# No plaintext anywhere on disk after the full migration.
assert not any(str(f).endswith(".pkl") for f in cache_root.rglob("*"))
def test_rekey_already_migrated_tolerates_concurrently_deleted_sidecar(
cache_root,
):
"""Cross-process rekey race: a winning worker finalizes (rebuilds the
IndexIDMap2, then deletes the sidecar as its completion signal). A losing
worker re-enters rekey_index_file for the same index; its already-migrated
short-circuit must treat the now-missing sidecar as 'already finalized' and
report success — NOT raise FileNotFoundError, which makes the caller
destructively quarantine a perfectly healthy, just-rekeyed index.
"""
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
userdir = cache_root / "u"
uuids = [uuid.uuid4().hex for _ in range(3)]
_make_legacy_index(userdir, uuids, [f"t{i}" for i in range(3)])
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json"
migrate_legacy_docstores()
uuid_to_id = {uuids[0]: 100, uuids[1]: 200, uuids[2]: 300}
params = dict(
dimension=2, index_type="flat", metric="cosine", normalize=True
)
# Winner: re-key to IndexIDMap2, then delete the sidecar (Phase B signal).
rekey_index_file(faiss_p, sidecar, uuid_to_id, **params)
sidecar.unlink()
assert not sidecar.exists()
# Loser: re-enter with the sidecar already gone — must NOT raise, and must
# report the already-migrated index as kept.
res = rekey_index_file(faiss_p, sidecar, uuid_to_id, **params)
assert res["kept"] == 3
assert res["orphaned"] == 0
def test_rekey_fresh_build_tolerates_concurrent_migration(cache_root, mocker):
"""Fresh-rebuild path cross-process race: worker B loads the OLD-format
index, and before B reads the sidecar, worker A finishes its own rekey
(persists the new IndexIDMap2, deletes the shared sidecar). B's sidecar read
then raises FileNotFoundError; B must re-read the file, see it is already an
IndexIDMap2, and report success — NOT raise (which makes the caller quarantine
a perfectly good, just-migrated index).
"""
import faiss
import numpy as np
import local_deep_research.vector_stores.legacy_cleanup as lc
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
userdir = cache_root / "u"
userdir.mkdir(parents=True, exist_ok=True)
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json" # deliberately never created → gone
raw = faiss.IndexFlatL2(2)
raw.add(np.random.rand(3, 2).astype("float32"))
migrated = faiss.IndexIDMap2(faiss.IndexFlatL2(2))
migrated.add_with_ids(
np.random.rand(3, 2).astype("float32"),
np.array([100, 200, 300], dtype="int64"),
)
# First read_index() (line 444) → the OLD raw index; the except-block re-read
# → the IndexIDMap2 the concurrent worker just wrote.
mocker.patch.object(lc, "read_index", side_effect=[raw, migrated])
res = rekey_index_file(
faiss_p,
sidecar,
{"0": "x"},
dimension=2,
index_type="flat",
metric="l2",
normalize=False,
)
assert res["kept"] == 3
assert res["orphaned"] == 0
def test_rekey_on_plain_indexidmap_raises_not_aborts(cache_root):
"""A plain faiss.IndexIDMap (our format's superclass, but NOT IndexIDMap2)
on disk must raise a CATCHABLE ValueError, never fall through to
reconstruct_n — which aborts the whole process (SIGABRT) uncatchably. If
this test process survives and sees a ValueError, the guard works.
"""
import faiss
import numpy as np
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
userdir = cache_root / "u"
userdir.mkdir(parents=True, exist_ok=True)
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json"
idmap = faiss.IndexIDMap(faiss.IndexFlatL2(2)) # plain IndexIDMap, not *2
idmap.add_with_ids(
np.random.rand(2, 2).astype("float32"),
np.array([10, 20], dtype="int64"),
)
faiss.write_index(idmap, str(faiss_p))
sidecar.write_text(json.dumps({"0": "0" * 32}))
with pytest.raises(ValueError):
rekey_index_file(
faiss_p,
sidecar,
{"0" * 32: 10},
dimension=2,
index_type="flat",
metric="l2",
normalize=False,
)
def test_rekey_rejects_duplicate_ids_from_sidecar(cache_root):
"""Two legacy positions resolving to the SAME DocumentChunk.id must be
refused (would bake an id-collision the set()-based read-back can't see)."""
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
userdir = cache_root / "u"
uuids = [uuid.uuid4().hex, uuid.uuid4().hex]
_make_legacy_index(userdir, uuids, ["a", "b"])
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json"
migrate_legacy_docstores()
# Both uuids resolve to the SAME int id -> duplicate.
with pytest.raises(ValueError, match="duplicate"):
rekey_index_file(
faiss_p,
sidecar,
{uuids[0]: 100, uuids[1]: 100},
dimension=2,
index_type="flat",
metric="cosine",
normalize=True,
)
def test_rekey_on_already_migrated_index_does_not_crash(cache_root):
"""Crash-consistency: if the process died after rekey persisted the new
IndexIDMap2 but BEFORE it deleted the sidecar, the next login re-runs
rekey_index_file on an already-migrated file. That must NOT reconstruct
(which aborts the process via an untranslated C++ faiss exception, or
silently reconstructs wrong vectors) — it must detect the new format,
drop the stale sidecar, and leave the index intact.
"""
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
from local_deep_research.vector_stores import get_vector_store_class
userdir = cache_root / "u"
uuids = [uuid.uuid4().hex for _ in range(3)]
_make_legacy_index(userdir, uuids, [f"t{i}" for i in range(3)])
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json"
uuid_to_id = {uuids[0]: 100, uuids[1]: 200, uuids[2]: 300}
# First rekey converts to IndexIDMap2 (leaves the sidecar for the caller).
migrate_legacy_docstores()
rekey_index_file(
faiss_p,
sidecar,
uuid_to_id,
dimension=2,
index_type="flat",
metric="cosine",
normalize=True,
)
assert sidecar.exists()
# The interrupted-before-finalize window: the FULL original sidecar (all
# three positions) survives next to the already-migrated IndexIDMap2 file,
# so the guard's id cross-check matches the on-disk ids.
sidecar.write_text(
json.dumps({"0": uuids[0], "1": uuids[1], "2": uuids[2]})
)
# Re-run on the already-migrated file: must return cleanly (no process
# abort) and leave the index + sidecar intact (rekey_index_file no longer
# deletes the sidecar — the caller finalizes).
res = rekey_index_file(
faiss_p,
sidecar,
uuid_to_id,
dimension=2,
index_type="flat",
metric="cosine",
normalize=True,
)
assert res["kept"] == 3
assert sidecar.exists()
store = get_vector_store_class().load(
faiss_p,
dimension=2,
index_type="flat",
metric="cosine",
normalize=True,
)
assert set(store.live_ids()) == {100, 200, 300}
def test_rekey_rejects_already_migrated_with_mismatched_ids(cache_root):
"""An already-migrated IndexIDMap2 whose on-disk ids disagree with the
current sidecar+DB resolution (stale / foreign / cross-tenant file) must be
REFUSED — silently adopting it would rehydrate the wrong document's text."""
import faiss
import numpy as np
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
userdir = cache_root / "u"
userdir.mkdir(parents=True, exist_ok=True)
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json"
# On-disk IndexIDMap2 with ids [7777, 8888].
idx = faiss.IndexIDMap2(faiss.IndexFlatL2(2))
idx.add_with_ids(
np.random.rand(2, 2).astype("float32"),
np.array([7777, 8888], dtype="int64"),
)
faiss.write_index(idx, str(faiss_p))
# But the sidecar + uuid_to_id resolve to DIFFERENT ids [1, 2].
u0, u1 = "a" * 32, "b" * 32
sidecar.write_text(json.dumps({"0": u0, "1": u1}))
with pytest.raises(ValueError, match="do not match"):
rekey_index_file(
faiss_p,
sidecar,
{u0: 1, u1: 2},
dimension=2,
index_type="flat",
metric="l2",
normalize=False,
)
def test_rekey_rejects_foreign_faiss_class_without_segfault(cache_root):
"""A non-base faiss class at the index path (disk corruption / tamper /
foreign file) must raise a CATCHABLE ValueError, not reach reconstruct_n —
which segfaults the process uncatchably and, on the login rekey path,
crash-loops the worker. The test completing (not crashing) is the proof."""
import faiss
import numpy as np
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
userdir = cache_root / "u"
userdir.mkdir(parents=True, exist_ok=True)
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json"
# IndexLSH is a valid, serializable faiss index that is neither an
# IndexIDMap family member nor a reconstruct-safe base index.
lsh = faiss.IndexLSH(8, 16)
lsh.add(np.random.rand(2, 8).astype("float32"))
faiss.write_index(lsh, str(faiss_p))
u0, u1 = "a" * 32, "b" * 32
sidecar.write_text(json.dumps({"0": u0, "1": u1}))
with pytest.raises(ValueError, match="reconstruct-safe|foreign"):
rekey_index_file(
faiss_p,
sidecar,
{u0: 1, u1: 2},
dimension=8,
index_type="flat",
metric="cosine",
normalize=True,
)
def test_rekey_rejects_already_migrated_with_duplicate_ids(cache_root):
"""An already-migrated IndexIDMap2 carrying a DUPLICATE id must be refused,
even though its id *set* matches the expected resolution. A set comparison
dedupes and cannot see the duplicate (which double-counts/mis-scores that
chunk in search); the guard must compare multiplicities, mirroring the
fresh-build path's duplicate check."""
import faiss
import numpy as np
from local_deep_research.vector_stores.legacy_cleanup import (
rekey_index_file,
)
userdir = cache_root / "u"
userdir.mkdir(parents=True, exist_ok=True)
faiss_p = userdir / "idx.faiss"
sidecar = userdir / "idx.idmap.json"
# On-disk IndexIDMap2 with ids [5, 5, 6] — id 5 is duplicated. Its *set*
# is {5, 6}, so a set-only comparison would silently adopt this file.
idx = faiss.IndexIDMap2(faiss.IndexFlatL2(2))
idx.add_with_ids(
np.random.rand(3, 2).astype("float32"),
np.array([5, 5, 6], dtype="int64"),
)
faiss.write_index(idx, str(faiss_p))
# Sidecar + uuid_to_id resolve to the SAME set {5, 6} (two uuids -> id 5),
# so only the multiplicity check can catch the corruption.
u0, u1, u2 = "a" * 32, "b" * 32, "c" * 32
sidecar.write_text(json.dumps({"0": u0, "1": u1, "2": u2}))
with pytest.raises(ValueError, match="duplicate"):
rekey_index_file(
faiss_p,
sidecar,
{u0: 5, u1: 5, u2: 6},
dimension=2,
index_type="flat",
metric="l2",
normalize=False,
)