~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
"""One foreign child must not abort the index-family delete.
Regression pins for the family-deletion invariant: the plaintext ``.pkl``
removal is "not optional" (see ``library_rag_service``'s quarantine path), so
an unexpected entry inside ``<index>.faiss.segments`` — e.g. a ``.DS_Store``
dropped by a file manager into the directory ``docs/library-and-rag.md`` tells
users to back up — must never prevent removal of the rest of the family. The
refusal guards stand: foreign, symlinked or unexpected entries are still never
deleted.
"""
from pathlib import Path
import pytest
from local_deep_research.research_library.deletion.utils.cascade_helper import (
CascadeHelper,
)
from local_deep_research.vector_stores.segment_files import (
checkpoint_path,
delete_segment_files,
segment_directory,
)
def _family(tmp_path):
"""A minimal index family with generated segments beside it."""
base = tmp_path / "rag_indices" / "user_hash" / "index"
base.parent.mkdir(parents=True)
faiss_file = base.with_suffix(".faiss")
pkl_file = base.with_suffix(".pkl")
idmap_file = base.with_suffix(".idmap.json")
faiss_file.write_bytes(b"synthetic manifest")
pkl_file.write_bytes(b"legacy plaintext docstore")
idmap_file.write_text("{}")
segments = segment_directory(faiss_file)
segments.mkdir()
for name in ("a" * 32 + ".faiss", "a" * 32 + ".ids"):
(segments / name).write_bytes(b"generated segment")
return base, faiss_file, pkl_file, idmap_file, segments
def test_foreign_entry_does_not_abort_family_delete(tmp_path):
base, faiss_file, pkl_file, idmap_file, segments = _family(tmp_path)
foreign = segments / ".DS_Store"
foreign.write_bytes(b"file-manager droppings")
result = CascadeHelper.delete_faiss_index_files(str(base))
# Honest reporting: every removable member is gone, but the segment
# cleanup was refused, so the delete reports False (incomplete).
assert result is False
assert not faiss_file.exists()
assert not pkl_file.exists() # plaintext removal is not optional
assert not idmap_file.exists()
# Every generated sibling is retired; only the refused foreign entry
# remains, and it is never deleted.
assert [child.name for child in segments.iterdir()] == [".DS_Store"]
assert foreign.read_bytes() == b"file-manager droppings"
def test_symlinked_entry_is_still_refused_but_family_removed(tmp_path):
base, faiss_file, pkl_file, idmap_file, segments = _family(tmp_path)
outside = tmp_path / "outside"
outside.mkdir()
sentinel = outside / "payload.faiss"
sentinel.write_bytes(b"do not delete me")
link = segments / ("b" * 32 + ".faiss")
link.symlink_to(sentinel)
result = CascadeHelper.delete_faiss_index_files(str(base))
assert result is False # the refusal is contained but still reported
assert not faiss_file.exists()
assert not pkl_file.exists()
assert link.is_symlink() # the refusal guard is not weakened
assert sentinel.read_bytes() == b"do not delete me"
def test_poisoned_quarantine_payload_does_not_abort_plaintext_sweep(tmp_path):
"""The per-stray handler for quarantined ``.segments`` payloads swallows
the refusal: a poisoned quarantine must not strand the plaintext
``.pkl.corrupt-*`` sweep that runs later in the same family delete."""
base = tmp_path / "index"
faiss_file = base.with_suffix(".faiss")
pkl_file = base.with_suffix(".pkl")
faiss_file.write_bytes(b"synthetic manifest")
pkl_file.write_bytes(b"legacy plaintext docstore")
poisoned = tmp_path / "index.faiss.corrupt-111.segments"
poisoned.mkdir()
(poisoned / ("d" * 32 + ".faiss")).write_bytes(b"generated segment")
(poisoned / "surprise.bin").write_bytes(b"user file")
pkl_quarantine = tmp_path / "index.pkl.corrupt-222"
pkl_quarantine.write_bytes(b"quarantined PLAINTEXT")
result = CascadeHelper.delete_faiss_index_files(str(base))
assert result is False # the refusal is contained but still reported
assert not pkl_file.exists()
assert not pkl_quarantine.exists()
assert (poisoned / "surprise.bin").read_bytes() == b"user file"
def test_delete_segment_files_retires_generated_siblings_before_refusing(
tmp_path, monkeypatch
):
"""The refusal raises only after every generated sibling is retired, so
the iteration order of the directory cannot strand generated files behind
a foreign one. The test forces the foreign entry to be seen first."""
faiss_file = tmp_path / "index.faiss"
faiss_file.write_bytes(b"synthetic manifest")
segments = segment_directory(faiss_file)
segments.mkdir()
generated = segments / ("c" * 32 + ".faiss")
generated.write_bytes(b"generated segment")
foreign = segments / "surprise.txt"
foreign.write_bytes(b"user file")
real_iterdir = Path.iterdir
def foreign_first(self):
children = list(real_iterdir(self))
if self == segments:
children.sort(key=lambda child: child.name != "surprise.txt")
return iter(children)
monkeypatch.setattr(Path, "iterdir", foreign_first)
with pytest.raises(ValueError, match="unexpected"):
delete_segment_files(faiss_file)
assert not generated.exists()
assert foreign.read_bytes() == b"user file"
assert segments.is_dir()
@pytest.mark.parametrize(
"unavailable", ["checkpoint", "index", "metadata", "ids", "quarantine"]
)
def test_family_cleanup_continues_after_component_error(
tmp_path, monkeypatch, unavailable
):
base, faiss_file, pkl_file, idmap_file, segments = _family(tmp_path)
checkpoint = checkpoint_path(faiss_file)
checkpoint.write_bytes(b"sample checkpoint")
members = {
"checkpoint": checkpoint,
"index": faiss_file,
"metadata": pkl_file,
"ids": idmap_file,
}
quarantined = pkl_file.with_name(pkl_file.name + ".corrupt-123")
quarantined.write_bytes(b"sample metadata")
members["quarantine"] = quarantined
real_unlink = Path.unlink
def unlink(path, *args, **kwargs):
if path == members[unavailable]:
raise PermissionError("Component unavailable")
return real_unlink(path, *args, **kwargs)
monkeypatch.setattr(Path, "unlink", unlink)
assert CascadeHelper.delete_faiss_index_files(str(base)) is False
assert members[unavailable].is_file()
assert all(
not member.exists()
for name, member in members.items()
if name != unavailable
)
if unavailable == "index":
# A manifest that could not be removed keeps the segments it names,
# so a later write at this path never meets a dangling manifest.
assert sorted(child.name for child in segments.iterdir()) == [
"a" * 32 + ".faiss",
"a" * 32 + ".ids",
]
else:
assert not segments.exists()
def test_unreadable_root_keeps_segments_and_still_sweeps_quarantine(
tmp_path, monkeypatch
):
base, faiss_file, pkl_file, idmap_file, segments = _family(tmp_path)
quarantined = pkl_file.with_name(pkl_file.name + ".corrupt-123")
quarantined.write_bytes(b"sample metadata")
real_unlink = Path.unlink
real_exists = Path.exists
def unlink(path, *args, **kwargs):
if path == faiss_file:
raise PermissionError("Component unavailable")
return real_unlink(path, *args, **kwargs)
def exists(path, *args, **kwargs):
# Python < 3.14 re-raises EACCES from Path.exists().
if path == faiss_file:
raise PermissionError("Permission denied")
return real_exists(path, *args, **kwargs)
monkeypatch.setattr(Path, "unlink", unlink)
monkeypatch.setattr(Path, "exists", exists)
assert CascadeHelper.delete_faiss_index_files(str(base)) is False
assert sorted(child.name for child in segments.iterdir()) == [
"a" * 32 + ".faiss",
"a" * 32 + ".ids",
]
assert not pkl_file.exists()
assert not real_exists(quarantined) # plaintext removal is not optional
@pytest.mark.parametrize("suffix", [".faiss", ".ids"])
def test_family_cleanup_continues_after_segment_error(
tmp_path, monkeypatch, suffix
):
base, faiss_file, pkl_file, idmap_file, segments = _family(tmp_path)
unavailable = segments / ("a" * 32 + suffix)
real_unlink = Path.unlink
real_iterdir = Path.iterdir
def unavailable_first(directory):
children = list(real_iterdir(directory))
if directory == segments:
children.sort(key=lambda child: child != unavailable)
return iter(children)
def unlink(path, *args, **kwargs):
if path == unavailable:
raise PermissionError("Component unavailable")
return real_unlink(path, *args, **kwargs)
monkeypatch.setattr(Path, "iterdir", unavailable_first)
monkeypatch.setattr(Path, "unlink", unlink)
assert CascadeHelper.delete_faiss_index_files(str(base)) is False
assert not faiss_file.exists()
assert not pkl_file.exists()
assert not idmap_file.exists()
assert list(segments.iterdir()) == [unavailable]
assert unavailable.read_bytes() == b"generated segment"
def test_family_cleanup_accepts_a_disappearing_segment(tmp_path, monkeypatch):
base, faiss_file, pkl_file, idmap_file, segments = _family(tmp_path)
disappearing = segments / ("a" * 32 + ".faiss")
real_unlink = Path.unlink
real_iterdir = Path.iterdir
def disappearing_first(directory):
children = list(real_iterdir(directory))
if directory == segments:
children.sort(key=lambda child: child != disappearing)
return iter(children)
def unlink(path, *args, **kwargs):
result = real_unlink(path, *args, **kwargs)
if path == disappearing:
raise FileNotFoundError("Component already removed")
return result
monkeypatch.setattr(Path, "iterdir", disappearing_first)
monkeypatch.setattr(Path, "unlink", unlink)
assert CascadeHelper.delete_faiss_index_files(str(base)) is True
assert not faiss_file.exists()
assert not pkl_file.exists()
assert not idmap_file.exists()
assert not segments.exists()
def test_delete_segment_files_skips_a_child_retired_mid_walk(
tmp_path, monkeypatch
):
"""A segment a concurrent settle retires after the directory is listed
is neither foreign nor an error: the walk completes and the directory
is removed."""
faiss_file = tmp_path / "index.faiss"
faiss_file.write_bytes(b"synthetic manifest")
segments = segment_directory(faiss_file)
segments.mkdir()
kept = segments / ("c" * 32 + ".faiss")
kept.write_bytes(b"generated segment")
retired = segments / ("d" * 32 + ".faiss")
retired.write_bytes(b"generated segment")
real_iterdir = Path.iterdir
def retire_after_listing(self):
children = list(real_iterdir(self))
if self == segments:
retired.unlink()
return iter(children)
monkeypatch.setattr(Path, "iterdir", retire_after_listing)
assert delete_segment_files(faiss_file) is True
assert not segments.exists()