~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
"""Tests for the stray-file SWEEP in CascadeHelper.delete_faiss_index_files.
The existing test_cascade_helper_edge_cases.py only covers the primary
.faiss/.pkl pair on None/empty input. This file covers the sweep logic added
alongside the RAG vector-store refactor:
(a) the migration sidecar (<name>.idmap.json) is removed
(b) quarantine files (<name>.faiss.corrupt-<ns>, <name>.pkl.corrupt-<ns>) are
ALWAYS removed regardless of age -- a leftover .pkl.corrupt-* is
quarantined PLAINTEXT, so leaving it behind is a data leak
(c) crash-orphaned .tmp files are age-gated: a FRESH .tmp (a live writer's
not-yet-renamed persist() temp) survives, while a STALE .tmp (older than
_STALE_TMP_AGE_SECONDS) is swept
(d) delete_rag_indices_for_collection's unlink_files=False contract: index
paths are returned but files are NOT unlinked (deferred-unlink, so a
later rollback can't leave the collection pointing at a missing index)
"""
import os
import time
from unittest.mock import MagicMock, patch
from local_deep_research.research_library.deletion.utils.cascade_helper import (
CascadeHelper,
_STALE_TMP_AGE_SECONDS,
)
def _age_file(path, seconds_old):
"""Set a file's mtime/atime to `seconds_old` seconds in the past."""
now = time.time()
target = now - seconds_old
os.utime(path, (target, target))
class TestIdmapSidecarSweep:
"""(a) <name>.idmap.json sidecar is removed."""
def test_idmap_sidecar_removed(self, tmp_path):
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
idmap = base.with_suffix(".idmap.json")
idmap.write_text("{}")
result = CascadeHelper.delete_faiss_index_files(str(base))
assert result is True
assert not idmap.exists()
def test_idmap_sidecar_removed_when_faiss_missing(self, tmp_path):
"""The sidecar can outlive its .faiss during the cutover window."""
base = tmp_path / "index"
idmap = base.with_suffix(".idmap.json")
idmap.write_text("{}")
result = CascadeHelper.delete_faiss_index_files(str(base))
assert result is True
assert not idmap.exists()
class TestQuarantineSweepAlwaysRemoved:
"""(b) .corrupt-<ns> quarantine files are removed regardless of age."""
def test_faiss_corrupt_quarantine_removed_even_if_fresh(self, tmp_path):
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
quarantined = tmp_path / "index.faiss.corrupt-abc123"
quarantined.write_text("corrupted binary blob")
# Fresh mtime -- age must NOT matter for quarantine files.
_age_file(quarantined, 0)
CascadeHelper.delete_faiss_index_files(str(base))
assert not quarantined.exists()
def test_pkl_corrupt_quarantine_removed_even_if_fresh(self, tmp_path):
"""A .pkl.corrupt-* is quarantined PLAINTEXT -- must always be swept."""
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
quarantined = tmp_path / "index.pkl.corrupt-def456"
quarantined.write_text("plaintext chunk text leaked here")
_age_file(quarantined, 0)
CascadeHelper.delete_faiss_index_files(str(base))
assert not quarantined.exists()
def test_pkl_corrupt_quarantine_removed_even_if_old(self, tmp_path):
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
quarantined = tmp_path / "index.pkl.corrupt-old789"
quarantined.write_text("plaintext chunk text leaked here")
_age_file(quarantined, _STALE_TMP_AGE_SECONDS * 10)
CascadeHelper.delete_faiss_index_files(str(base))
assert not quarantined.exists()
def test_idmap_corrupt_quarantine_removed(self, tmp_path):
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
quarantined = tmp_path / "index.idmap.json.corrupt-ghi789"
quarantined.write_text("{}")
_age_file(quarantined, 0)
CascadeHelper.delete_faiss_index_files(str(base))
assert not quarantined.exists()
def test_multiple_quarantine_generations_all_removed(self, tmp_path):
"""Repeated corruption events can leave several quarantine files;
all of them must be swept, not just the newest/oldest."""
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
q1 = tmp_path / "index.pkl.corrupt-gen1"
q2 = tmp_path / "index.pkl.corrupt-gen2"
q1.write_text("plaintext leak 1")
q2.write_text("plaintext leak 2")
_age_file(q1, _STALE_TMP_AGE_SECONDS * 10)
_age_file(q2, 0)
CascadeHelper.delete_faiss_index_files(str(base))
assert not q1.exists()
assert not q2.exists()
class TestTmpSweepAgeGate:
"""(c) fresh .tmp survives (live writer), stale .tmp is swept."""
def test_fresh_tmp_survives(self, tmp_path):
"""A .tmp younger than _STALE_TMP_AGE_SECONDS may be a concurrent
writer's not-yet-renamed persist() temp; deleting it would crash
that write, so it must be left alone."""
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
fresh_tmp = tmp_path / "index.faiss.abc123.tmp"
fresh_tmp.write_text("in-progress write")
_age_file(fresh_tmp, 0)
CascadeHelper.delete_faiss_index_files(str(base))
assert fresh_tmp.exists()
def test_stale_tmp_is_swept(self, tmp_path):
"""A .tmp older than _STALE_TMP_AGE_SECONDS is certainly
crash-orphaned and must be removed."""
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
stale_tmp = tmp_path / "index.faiss.abc123.tmp"
stale_tmp.write_text("orphaned write")
_age_file(stale_tmp, _STALE_TMP_AGE_SECONDS + 60)
result = CascadeHelper.delete_faiss_index_files(str(base))
assert result is True
assert not stale_tmp.exists()
def test_boundary_just_under_threshold_survives(self, tmp_path):
"""One second younger than the threshold must still be treated as
'fresh' -- confirms the age gate uses '<', not '<='."""
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
boundary_tmp = tmp_path / "index.faiss.boundary.tmp"
boundary_tmp.write_text("borderline write")
_age_file(boundary_tmp, _STALE_TMP_AGE_SECONDS - 5)
CascadeHelper.delete_faiss_index_files(str(base))
assert boundary_tmp.exists()
def test_fresh_and_stale_tmp_coexist_only_stale_removed(self, tmp_path):
"""Sanity-check the assertion actually discriminates on age: with
one fresh and one stale .tmp present, only the stale one is gone
and the fresh one remains -- this would fail if the sweep either
removed everything or removed nothing."""
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
fresh_tmp = tmp_path / "index.faiss.fresh.tmp"
stale_tmp = tmp_path / "index.faiss.stale.tmp"
fresh_tmp.write_text("live writer")
stale_tmp.write_text("crash orphan")
_age_file(fresh_tmp, 0)
_age_file(stale_tmp, _STALE_TMP_AGE_SECONDS * 2)
CascadeHelper.delete_faiss_index_files(str(base))
assert fresh_tmp.exists()
assert not stale_tmp.exists()
def test_pkl_tmp_family_also_age_gated(self, tmp_path):
"""The sweep runs per-base (.faiss, .pkl, .idmap.json) -- confirm the
.pkl family's tmp siblings are age-gated the same way."""
base = tmp_path / "index"
base.with_suffix(".faiss").write_text("faiss-data")
base.with_suffix(".pkl").write_text("pkl-data")
fresh_tmp = tmp_path / "index.pkl.fresh.tmp"
stale_tmp = tmp_path / "index.pkl.stale.tmp"
fresh_tmp.write_text("live writer")
stale_tmp.write_text("crash orphan")
_age_file(fresh_tmp, 0)
_age_file(stale_tmp, _STALE_TMP_AGE_SECONDS * 2)
CascadeHelper.delete_faiss_index_files(str(base))
assert fresh_tmp.exists()
assert not stale_tmp.exists()
class TestDeleteRagIndicesForCollectionUnlinkContract:
"""(d) unlink_files=False defers unlinking; unlink_files=True (default)
unlinks immediately."""
def _make_session_with_index(self, tmp_path, base_name="index"):
# Nested under a "rag_indices" dir so it mirrors the real on-disk
# layout (get_cache_directory() / "rag_indices") -- needed because
# the unlink_files=True path now enforces that containment root.
rag_indices_dir = tmp_path / "rag_indices"
rag_indices_dir.mkdir(exist_ok=True)
base = rag_indices_dir / base_name
faiss_file = base.with_suffix(".faiss")
faiss_file.write_text("faiss-data")
rag_index = MagicMock()
rag_index.index_path = str(base)
session = MagicMock()
session.query.return_value.filter_by.return_value.all.return_value = [
rag_index
]
return session, rag_index, faiss_file
def test_unlink_files_false_returns_paths_and_leaves_files(self, tmp_path):
session, rag_index, faiss_file = self._make_session_with_index(tmp_path)
result = CascadeHelper.delete_rag_indices_for_collection(
session, "collection_abc", unlink_files=False
)
assert result["index_paths"] == [str(faiss_file.with_suffix(""))]
assert result["deleted_files"] == 0
assert result["deleted_indices"] == 1
# Deferred-unlink contract: the file must still be on disk.
assert faiss_file.exists()
# DB row is still staged for deletion.
session.delete.assert_called_once_with(rag_index)
def test_unlink_files_true_unlinks_and_omits_paths(self, tmp_path):
session, rag_index, faiss_file = self._make_session_with_index(tmp_path)
# unlink_files=True derives its containment root from
# get_cache_directory() / "rag_indices"; point that at tmp_path so
# the fixture's nested "rag_indices" dir is the allowed root.
with patch(
"local_deep_research.config.paths.get_cache_directory",
return_value=tmp_path,
):
result = CascadeHelper.delete_rag_indices_for_collection(
session, "collection_abc", unlink_files=True
)
assert result["index_paths"] == []
assert result["deleted_files"] == 1
assert result["deleted_indices"] == 1
assert not faiss_file.exists()
def test_default_unlink_files_is_true(self, tmp_path):
"""Confirms the default parameter value matches the True behavior
(the safe/backwards-compatible default), not the deferred one."""
session, rag_index, faiss_file = self._make_session_with_index(tmp_path)
with patch(
"local_deep_research.config.paths.get_cache_directory",
return_value=tmp_path,
):
result = CascadeHelper.delete_rag_indices_for_collection(
session, "collection_abc"
)
assert result["index_paths"] == []
assert not faiss_file.exists()