~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
"""Provider-aware vector cleanup in the library deletion cascade (issue #5765).
The cascade used to be FAISS-only: it deleted the ``RAGIndex`` rows and then,
post-commit, unlinked ``.faiss``/``.pkl``/``.idmap.json`` files. For a
server-backed store that is a silent no-op on files that never existed, and the
remote collection is never dropped.
Worse than a leak: a server-backed collection is named from ``username`` and
``index_hash`` -- illustrated here as ``sha256(f"{username}:{index_hash}")``,
though no such derivation exists yet (``BaseVectorStore.create()`` does not
even receive those inputs; see ``vector_stores/base.py`` and issue #6769) --
and ``index_hash`` lives on the very ``RAGIndex`` row the cascade deletes.
Delete the row first and the vectors become *unaddressable* -- the app can no
longer name them, so it can never clean them up.
So the property under test is an ORDERING one: every remote drop must happen
BEFORE any ``RAGIndex`` row is deleted, and a failed drop must abort the whole
deletion rather than complete it.
These tests use a stub provider registered in the vector-store registry, so
they neither need nor import a real server backend (Milvus lands in #5331) --
and the local-file assertions deliberately avoid importing ``faiss``.
"""
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
import pytest
from local_deep_research.research_library.deletion.utils.cascade_helper import (
CascadeHelper,
RemoteVectorStoreDropError,
remote_drop_failure_message,
)
from local_deep_research.vector_stores import (
BaseVectorStore,
provider_is_local_file,
)
# --------------------------------------------------------------------------- #
# Stub provider plumbing
# --------------------------------------------------------------------------- #
def _make_remote_stub(
events, *, error=None, result=True, implement=True, snapshots=None
):
"""Build a server-backed (``is_local_file = False``) store class.
``drop_collection`` appends to ``events`` so tests can assert it ran, with
what, and -- crucially -- WHEN relative to the row deletes. The
``settings_snapshot`` each call received is appended to ``snapshots``.
``error`` is an exception to raise, or a callable mapping the
``index_hash`` to one (or to None, to succeed).
Never instantiated (``drop_collection`` is a classmethod by design), so the
abstract methods are deliberately left unimplemented.
"""
if snapshots is None:
snapshots = []
namespace = {
"provider_key": "stub_remote",
"provider_name": "Stub Remote",
"is_local_file": False,
}
if implement:
@classmethod
def drop_collection(
cls, *, username, index_hash, settings_snapshot=None
):
events.append(("drop", username, index_hash))
snapshots.append(settings_snapshot)
failure = error(index_hash) if callable(error) else error
if failure is not None:
raise failure
return result
namespace["drop_collection"] = drop_collection
return type("_RemoteStub", (BaseVectorStore,), namespace)
@contextmanager
def _registered_as_default(store_class):
"""Make ``store_class`` the resolved provider for the duration.
Pre-populating ``_PROVIDER_CLASSES`` also stops the registry lazily
importing the FAISS backend (and its native extension).
"""
from local_deep_research.vector_stores import config as vs_config
saved_classes = dict(vs_config._PROVIDER_CLASSES)
saved_default = vs_config._DEFAULT_PROVIDER
vs_config._PROVIDER_CLASSES.clear()
vs_config._PROVIDER_CLASSES[store_class.provider_key] = store_class
vs_config._DEFAULT_PROVIDER = store_class.provider_key
try:
yield
finally:
vs_config._DEFAULT_PROVIDER = saved_default
vs_config._PROVIDER_CLASSES.clear()
vs_config._PROVIDER_CLASSES.update(saved_classes)
def _fake_index(index_hash, index_path="/nowhere/idx"):
index = MagicMock()
index.index_hash = index_hash
index.index_path = index_path
index.collection_name = "collection_abc"
return index
def _session_returning(indices, events=None):
session = MagicMock()
session.query.return_value.filter_by.return_value.all.return_value = indices
if events is not None:
session.delete.side_effect = lambda row: events.append(
("delete_row", row.index_hash)
)
return session
# --------------------------------------------------------------------------- #
# The crux: ordering
# --------------------------------------------------------------------------- #
class TestRemoteDropOrdering:
def test_drops_every_remote_collection_before_deleting_any_row(self):
"""THE fix for #5765.
Mutation check: moving the drop after the ``session.delete`` loop (the
pre-fix ordering, and the natural "clean up after commit" instinct)
flips these assertions.
"""
events = []
indices = [_fake_index("hash-one"), _fake_index("hash-two")]
session = _session_returning(indices, events)
with _registered_as_default(_make_remote_stub(events)):
result = CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
assert events == [
("drop", "alice", "hash-one"),
("drop", "alice", "hash-two"),
("delete_row", "hash-one"),
("delete_row", "hash-two"),
]
# Stated as the invariant too, so a future reordering fails loudly on
# the property and not just on an exact-sequence equality.
last_drop = max(i for i, e in enumerate(events) if e[0] == "drop")
first_row_delete = min(
i for i, e in enumerate(events) if e[0] == "delete_row"
)
assert last_drop < first_row_delete
assert result["deleted_indices"] == 2
def test_remote_store_reports_no_files_to_unlink(self):
"""A server-backed store has no .faiss/.pkl to unlink.
Returning its ``index_path`` would send the caller's post-commit
cleanup after a file that never existed -- the original silent no-op.
"""
events = []
session = _session_returning([_fake_index("hash-one")], events)
with _registered_as_default(_make_remote_stub(events)):
result = CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
assert result["index_paths"] == []
assert result["deleted_files"] == 0
def test_remote_store_does_not_unlink_files_when_unlink_files_true(
self, tmp_path
):
"""Even on the unlink_files=True path, no filesystem work happens.
The stray ``.faiss`` file is placed INSIDE ``get_cache_directory() /
"rag_indices"`` -- which is patched to ``tmp_path`` -- so it sits
squarely within ``delete_faiss_index_files``'s ``allowed_root``
containment check. If the ``is_local_file`` guards were fully
reverted (so the code always tried the FAISS unlink path for any
provider), the containment check alone would NOT refuse this path
and would happily delete it -- so "the file still exists" would pass
for the wrong reason (rescued by containment, not by the
server-backed dispatch this test is meant to cover). ``delete_files``
is a mock (``patch.object`` on ``delete_faiss_index_files``), so the
file is never at risk either way; asserting
``delete_faiss_index_files.assert_not_called()`` checks the actual
dispatch instead of a filesystem side effect that the mock makes
meaningless.
"""
events = []
rag_indices_root = tmp_path / "rag_indices"
rag_indices_root.mkdir()
stray = rag_indices_root / "idx.faiss"
stray.write_text("not really a faiss index")
session = _session_returning(
[_fake_index("hash-one", str(rag_indices_root / "idx"))], events
)
with (
_registered_as_default(_make_remote_stub(events)),
patch(
"local_deep_research.config.paths.get_cache_directory",
return_value=tmp_path,
),
patch.object(
CascadeHelper, "delete_faiss_index_files"
) as delete_files,
):
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=True,
username="alice",
)
delete_files.assert_not_called()
# --------------------------------------------------------------------------- #
# Failure mode: refuse to complete a deletion we cannot make good on
# --------------------------------------------------------------------------- #
class TestRemoteDropFailureAborts:
def test_unreachable_store_aborts_before_any_row_is_deleted(self):
events = []
indices = [_fake_index("hash-one"), _fake_index("hash-two")]
session = _session_returning(indices, events)
stub = _make_remote_stub(events, error=ConnectionError("no route"))
with _registered_as_default(stub):
with pytest.raises(RemoteVectorStoreDropError) as excinfo:
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
session.delete.assert_not_called()
# The message must say the row survives.
assert "NOT deleted" in str(excinfo.value)
assert excinfo.value.retryable is True
assert excinfo.value.cause_type == "ConnectionError"
@pytest.mark.parametrize(
"index_hash, rendered",
[
("hash-one", "hash-one"),
("0123456789ab", "0123456789ab"),
("0123456789abcdef", "0123456789ab..."),
],
)
def test_failure_message_ellipsis_only_marks_a_truncated_hash(
self, index_hash, rendered
):
"""``...`` after the hash must mean the hash was cut (#5583)."""
events = []
session = _session_returning([_fake_index(index_hash)], events)
stub = _make_remote_stub(events, error=ConnectionError("no route"))
with _registered_as_default(stub):
with pytest.raises(RemoteVectorStoreDropError) as excinfo:
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
assert f"for index {rendered}: ConnectionError" in str(excinfo.value)
def test_missing_username_refuses_rather_than_orphaning(self):
"""Half the derived collection name is the username.
Without it the vectors cannot be named, so deleting the rows would
strand them permanently -- refuse instead.
"""
events = []
session = _session_returning([_fake_index("hash-one")], events)
with _registered_as_default(_make_remote_stub(events)):
with pytest.raises(RemoteVectorStoreDropError):
CascadeHelper.delete_rag_indices_for_collection(
session, "collection_abc", unlink_files=False
)
session.delete.assert_not_called()
assert events == []
def test_row_without_index_hash_refuses(self):
events = []
session = _session_returning([_fake_index("")], events)
with _registered_as_default(_make_remote_stub(events)):
with pytest.raises(RemoteVectorStoreDropError):
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
session.delete.assert_not_called()
def test_a_bad_row_after_good_ones_drops_nothing(self):
"""The validation pass must run in full BEFORE any drop_collection
call. A bad row sorted after good ones must not let the loop drop
their collections first and only then abort -- that would destroy
remote collections whose rows survive the rollback, with no way to
re-drop them."""
events = []
session = _session_returning(
[_fake_index("hash-one"), _fake_index("")], events
)
with _registered_as_default(_make_remote_stub(events)):
with pytest.raises(RemoteVectorStoreDropError):
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
assert events == []
session.delete.assert_not_called()
def test_server_backed_store_without_a_drop_implementation_refuses(self):
"""A backend that sets is_local_file=False but never implemented
drop_collection would otherwise orphan every collection it created.
The base class's NotImplementedError is caught and converted."""
events = []
session = _session_returning([_fake_index("hash-one")], events)
stub = _make_remote_stub(events, implement=False)
with _registered_as_default(stub):
with pytest.raises(RemoteVectorStoreDropError):
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
session.delete.assert_not_called()
def test_already_absent_collection_is_not_an_error(self):
"""drop_collection returning False means "nothing was there" -- an
idempotent re-delete must still remove the rows."""
events = []
session = _session_returning([_fake_index("hash-one")], events)
with _registered_as_default(_make_remote_stub(events, result=False)):
result = CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
assert result["deleted_indices"] == 1
session.delete.assert_called_once()
# --------------------------------------------------------------------------- #
# No regression on the FAISS path
# --------------------------------------------------------------------------- #
class TestLocalFilePathUnchanged:
def test_default_provider_is_local_file(self):
"""Guards the dispatch: if this ever flipped, every FAISS deletion
would start trying to drop a remote collection."""
assert provider_is_local_file() is True
def test_local_provider_defers_paths_and_never_drops(self, tmp_path):
"""unlink_files=False must behave exactly as before: rows staged for
deletion, file left on disk, path handed back for the post-commit
unlink. No drop_collection anywhere near it."""
base = tmp_path / "rag_indices" / "idx"
base.parent.mkdir()
faiss_file = base.with_suffix(".faiss")
faiss_file.write_text("faiss-data")
session = _session_returning([_fake_index("hash-one", str(base))])
from local_deep_research.vector_stores import config as vs_config
# A server-backed row is the only thing that looks up a store class
# (to call its drop_collection); a FAISS row never does -- which also
# means the native faiss backend is never imported on this path.
with patch.object(vs_config, "get_vector_store_class") as never:
result = CascadeHelper.delete_rag_indices_for_collection(
session, "collection_abc", unlink_files=False, username="alice"
)
never.assert_not_called()
assert result["index_paths"] == [str(base)]
assert result["deleted_indices"] == 1
assert faiss_file.exists()
def test_local_provider_still_unlinks_on_unlink_files_true(self, tmp_path):
base = tmp_path / "rag_indices" / "idx"
base.parent.mkdir()
faiss_file = base.with_suffix(".faiss")
faiss_file.write_text("faiss-data")
session = _session_returning([_fake_index("hash-one", str(base))])
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, username="alice"
)
assert result["deleted_files"] == 1
assert result["index_paths"] == []
assert not faiss_file.exists()
def test_drop_helper_is_a_noop_for_a_local_provider(self):
"""Called directly (not via the cascade) it must still self-guard."""
assert (
CascadeHelper.drop_remote_vector_collections(
[_fake_index("hash-one")], username="alice"
)
== 0
)
def test_local_file_fast_path_matches_the_class_attribute(self):
"""The registry keeps a cheap _LOCAL_FILE_PROVIDERS set so the deletion
path can branch without importing the native backend. Guard it against
drifting from the authoritative class attribute."""
pytest.importorskip("faiss")
from local_deep_research.vector_stores.config import (
_LOCAL_FILE_PROVIDERS,
)
from local_deep_research.vector_stores.implementations.faiss_store import (
FaissVectorStore,
)
assert (
FaissVectorStore.provider_key in _LOCAL_FILE_PROVIDERS
) is FaissVectorStore.is_local_file
# --------------------------------------------------------------------------- #
# Service-level: a failed drop is reported as a FAILED deletion
# --------------------------------------------------------------------------- #
class TestCollectionDeletionServiceFailureReporting:
@staticmethod
def _patched_session(session):
cm = MagicMock()
cm.__enter__ = Mock(return_value=session)
cm.__exit__ = Mock(return_value=None)
return cm
def test_delete_collection_fails_when_vector_store_unreachable(self):
"""Silently completing is the one option that is off the table: the
rows carry the only handle to the surviving vectors."""
from local_deep_research.research_library.deletion.services.collection_deletion import (
CollectionDeletionService,
)
events = []
session = _session_returning([_fake_index("hash-one")], events)
session.get.return_value = MagicMock(collection_type="custom")
stub = _make_remote_stub(events, error=ConnectionError("no route"))
with patch(
"local_deep_research.research_library.deletion.services."
"collection_deletion.get_user_db_session",
return_value=self._patched_session(session),
):
with _registered_as_default(stub):
result = CollectionDeletionService(
username="testuser"
).delete_collection("col-123")
assert result["deleted"] is False
assert "Vector store unreachable" in result["error"]
assert "the collection was not deleted" in result["error"]
assert result["retryable"] is True
assert result["vector_store_error"] is True
session.rollback.assert_called_once()
session.commit.assert_not_called()
# The username reached the drop -- it is half the collection name.
assert events == [("drop", "testuser", "hash-one")]
def test_delete_index_only_fails_when_vector_store_unreachable(self):
from local_deep_research.research_library.deletion.services.collection_deletion import (
CollectionDeletionService,
)
events = []
session = _session_returning([_fake_index("hash-one")], events)
session.get.return_value = MagicMock()
stub = _make_remote_stub(events, error=ConnectionError("no route"))
with patch(
"local_deep_research.research_library.deletion.services."
"collection_deletion.get_user_db_session",
return_value=self._patched_session(session),
):
with _registered_as_default(stub):
result = CollectionDeletionService(
username="testuser"
).delete_collection_index_only("col-123")
assert result["deleted"] is False
assert "Vector store unreachable" in result["error"]
assert result["retryable"] is True
assert result["vector_store_error"] is True
session.rollback.assert_called_once()
session.commit.assert_not_called()
# --------------------------------------------------------------------------- #
# The production callers must feed the user's settings snapshot through
# --------------------------------------------------------------------------- #
#
# ``_registered_as_default`` above reaches the remote branch by overwriting
# the global default, which is NOT how a user selects a provider: a provider
# setting is read from the user's settings snapshot (that is the shape the
# open Milvus PR, #5331, gives ``resolve_provider``: no snapshot -> the
# default). So the tests below register the stub WITHOUT making it the
# default, stand in a snapshot-reading ``resolve_provider``, and drive the
# real production callers. If any caller stops passing the snapshot, the
# provider resolves to FAISS, no drop happens, and they fail.
_PROVIDER_SETTING = "local_search_vector_store_provider"
def _snapshot_selecting(provider_key):
return {
_PROVIDER_SETTING: {"value": provider_key},
# Connection config travels in the same snapshot.
"local_search_stub_uri": {"value": "http://vector-store.invalid"},
}
def _resolve_from_snapshot(settings_snapshot=None):
if not settings_snapshot:
return "faiss"
raw = settings_snapshot.get(_PROVIDER_SETTING)
if isinstance(raw, dict):
raw = raw.get("value")
return str(raw or "faiss")
@contextmanager
def _registered_behind_a_setting(store_class):
"""Register ``store_class`` as selectable by a snapshot only.
The default stays FAISS; only a snapshot carrying the provider setting
selects the stub.
"""
from local_deep_research.vector_stores import config as vs_config
saved_classes = dict(vs_config._PROVIDER_CLASSES)
vs_config._PROVIDER_CLASSES.clear()
vs_config._PROVIDER_CLASSES[store_class.provider_key] = store_class
try:
with patch.object(
vs_config, "resolve_provider", side_effect=_resolve_from_snapshot
):
yield
finally:
vs_config._PROVIDER_CLASSES.clear()
vs_config._PROVIDER_CLASSES.update(saved_classes)
def _settings_manager_returning(snapshot):
manager = Mock()
manager.get_all_settings.return_value = snapshot
return manager
def _patched_user_session(session):
cm = MagicMock()
cm.__enter__ = Mock(return_value=session)
cm.__exit__ = Mock(return_value=None)
return patch(
"local_deep_research.research_library.deletion.services."
"collection_deletion.get_user_db_session",
return_value=cm,
)
class TestProductionCallersForwardTheSettingsSnapshot:
@pytest.mark.parametrize(
"operation", ["delete_collection", "delete_collection_index_only"]
)
def test_service_drops_the_collection_of_the_configured_provider(
self, operation
):
from local_deep_research.research_library.deletion.services.collection_deletion import (
CollectionDeletionService,
)
events, snapshots = [], []
snapshot = _snapshot_selecting("stub_remote")
session = _session_returning([_fake_index("hash-one")], events)
session.get.return_value = MagicMock(collection_type="custom")
stub = _make_remote_stub(events, snapshots=snapshots)
with (
_patched_user_session(session),
patch(
"local_deep_research.settings.manager.SettingsManager",
return_value=_settings_manager_returning(snapshot),
) as get_manager,
_registered_behind_a_setting(stub),
):
service = CollectionDeletionService(username="testuser")
if operation == "delete_collection":
service.delete_collection(
"col-123", delete_orphaned_documents=False
)
else:
service.delete_collection_index_only("col-123")
# The snapshot is read through the deletion's own session, by a
# manager that must not seed-and-commit defaults mid-deletion.
get_manager.assert_called_once_with(session, seed_defaults=False)
assert ("drop", "testuser", "hash-one") in events, (
f"{operation}: the user's configured server-backed provider "
"was never asked to drop its collection -- the settings "
"snapshot did not reach the cascade"
)
# ...and it reached drop_collection, which needs its connection
# config.
assert snapshots == [snapshot]
# Still ordered: the drop precedes the row delete.
assert events.index(("drop", "testuser", "hash-one")) < events.index(
("delete_row", "hash-one")
)
def test_without_the_setting_the_default_provider_is_used(self):
"""Control for the test above: the same call with a snapshot that
does not select the stub resolves to FAISS and drops nothing -- so
it is the forwarded snapshot, not the stub's registration, that
selects the provider."""
from local_deep_research.research_library.deletion.services.collection_deletion import (
CollectionDeletionService,
)
events = []
session = _session_returning([_fake_index("hash-one")], events)
session.get.return_value = MagicMock(collection_type="custom")
with (
_patched_user_session(session),
patch(
"local_deep_research.settings.manager.SettingsManager",
return_value=_settings_manager_returning({}),
),
_registered_behind_a_setting(_make_remote_stub(events)),
):
CollectionDeletionService(username="testuser").delete_collection(
"col-123", delete_orphaned_documents=False
)
assert [e for e in events if e[0] == "drop"] == []
def test_staged_rebuild_refuses_remote_provider_before_opening_db(self):
"""A remote drop cannot preserve the old index across rollback."""
from local_deep_research.research_library.services.collection_rebuild import (
CollectionRebuild,
)
events = []
session_factory = Mock(side_effect=AssertionError("DB opened"))
with _registered_behind_a_setting(_make_remote_stub(events)):
with pytest.raises(RuntimeError, match="local-file vector stores"):
CollectionRebuild(
Mock(), "abc", [], session_factory=session_factory
)
session_factory.assert_not_called()
assert events == []
# --------------------------------------------------------------------------- #
# Per-row provider, resolved once
# --------------------------------------------------------------------------- #
class TestPerRowProviderResolution:
def test_mixed_provider_rows_are_each_cleaned_up_by_their_own_provider(
self, tmp_path
):
"""Once ``RAGIndex`` records its provider (the open Milvus PR adds
``vector_store_provider``), a user who switched providers holds rows
of both kinds in one collection. Each row must be cleaned up by the
provider that built it: a FAISS row's files are handed back for
unlinking and a server-backed row's collection is dropped --
whatever the current setting says."""
events = []
faiss_row = _fake_index("hash-faiss", str(tmp_path / "idx"))
faiss_row.vector_store_provider = "faiss"
remote_row = _fake_index("hash-remote")
remote_row.vector_store_provider = "stub_remote"
session = _session_returning([faiss_row, remote_row], events)
# No snapshot: the configured provider is FAISS.
with _registered_behind_a_setting(_make_remote_stub(events)):
result = CascadeHelper.delete_rag_indices_for_collection(
session, "collection_abc", unlink_files=False, username="alice"
)
assert [e for e in events if e[0] == "drop"] == [
("drop", "alice", "hash-remote")
]
assert result["index_paths"] == [str(tmp_path / "idx")]
assert result["deleted_indices"] == 2
def test_a_recorded_faiss_row_is_not_dropped_under_a_remote_setting(self):
events = []
faiss_row = _fake_index("hash-faiss")
faiss_row.vector_store_provider = "faiss"
session = _session_returning([faiss_row], events)
with _registered_behind_a_setting(_make_remote_stub(events)):
result = CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
settings_snapshot=_snapshot_selecting("stub_remote"),
)
assert [e for e in events if e[0] == "drop"] == []
assert result["index_paths"] == ["/nowhere/idx"]
def test_the_configured_provider_is_resolved_once_per_call(self):
"""Resolving it separately for the drop and for the delete loop
could flip between the reads -- skipping both the drop and the
unlink while still deleting the rows."""
from local_deep_research.vector_stores import config as vs_config
events = []
session = _session_returning(
[_fake_index("h1"), _fake_index("h2"), _fake_index("h3")], events
)
snapshot = _snapshot_selecting("stub_remote")
with _registered_behind_a_setting(_make_remote_stub(events)):
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
settings_snapshot=snapshot,
)
resolve_calls = vs_config.resolve_provider.call_count
assert resolve_calls == 1
assert [e[0] for e in events] == ["drop"] * 3 + ["delete_row"] * 3
def test_an_unknown_recorded_provider_refuses_permanently(self):
events = []
row = _fake_index("hash-one")
row.vector_store_provider = "no_such_provider"
session = _session_returning([row], events)
with _registered_behind_a_setting(_make_remote_stub(events)):
with pytest.raises(RemoteVectorStoreDropError) as excinfo:
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username="alice",
)
assert excinfo.value.retryable is False
session.delete.assert_not_called()
def test_a_generator_of_rows_is_validated_before_anything_is_dropped(self):
"""``drop_remote_vector_collections`` accepts any iterable. A
generator walked twice (validate, then drop) would be exhausted by
the validation pass; one walked lazily would drop the good row
before reaching the bad one."""
events = []
with _registered_as_default(_make_remote_stub(events)):
with pytest.raises(RemoteVectorStoreDropError):
CascadeHelper.drop_remote_vector_collections(
(row for row in [_fake_index("good"), _fake_index("")]),
username="alice",
)
assert events == []
dropped = CascadeHelper.drop_remote_vector_collections(
(row for row in [_fake_index("one"), _fake_index("two")]),
username="alice",
)
assert dropped == 2
assert events == [("drop", "alice", "one"), ("drop", "alice", "two")]
# --------------------------------------------------------------------------- #
# Transient vs permanent failures, partial drops, and what gets surfaced
# --------------------------------------------------------------------------- #
class _ClientSaysRetryable(Exception):
retryable = True
class _ClientSaysPermanent(ConnectionError):
retryable = False
def _drop_error(stub, *, username="alice", rows=None):
session = _session_returning(rows or [_fake_index("hash-one")])
with _registered_as_default(stub):
with pytest.raises(RemoteVectorStoreDropError) as excinfo:
CascadeHelper.delete_rag_indices_for_collection(
session,
"collection_abc",
unlink_files=False,
username=username,
)
session.delete.assert_not_called()
return excinfo.value
class TestFailureClassification:
@pytest.mark.parametrize(
("error", "retryable"),
[
(ConnectionError("refused"), True),
(ConnectionRefusedError("refused"), True),
(TimeoutError("timed out"), True),
(_ClientSaysRetryable("503 from server"), True),
(PermissionError("forbidden"), False),
(RuntimeError("authentication failed"), False),
(ValueError("bad collection name"), False),
(_ClientSaysPermanent("TLS certificate rejected"), False),
],
ids=lambda value: (
type(value).__name__
if isinstance(value, BaseException)
else str(value)
),
)
def test_only_transient_failures_are_retryable(self, error, retryable):
exc = _drop_error(_make_remote_stub([], error=error))
assert exc.retryable is retryable
assert exc.cause_type == type(error).__name__
def test_a_missing_drop_implementation_is_permanent(self):
exc = _drop_error(_make_remote_stub([], implement=False))
assert exc.retryable is False
assert exc.cause_type == "NotImplementedError"
def test_a_missing_username_is_permanent(self):
assert _drop_error(_make_remote_stub([]), username=None).retryable is (
False
)
def test_a_missing_index_hash_is_permanent(self):
exc = _drop_error(_make_remote_stub([]), rows=[_fake_index("")])
assert exc.retryable is False
def test_the_messages_differ_for_transient_and_permanent(self):
transient = RemoteVectorStoreDropError("x", retryable=True)
permanent = RemoteVectorStoreDropError("x", retryable=False)
assert "Retry once the vector store is available" in (
remote_drop_failure_message(transient, "it was not deleted")
)
permanent_text = remote_drop_failure_message(
permanent, "it was not deleted"
)
assert "Retry once" not in permanent_text
assert "Retrying will not help" in permanent_text
def test_the_service_maps_a_permanent_failure_to_non_retryable(self):
from local_deep_research.research_library.deletion.services.collection_deletion import (
CollectionDeletionService,
)
session = _session_returning([_fake_index("hash-one")], [])
session.get.return_value = MagicMock(collection_type="custom")
stub = _make_remote_stub([], error=PermissionError("forbidden"))
with _patched_user_session(session), _registered_as_default(stub):
result = CollectionDeletionService(
username="testuser"
).delete_collection("col-123")
assert result["deleted"] is False
assert result["retryable"] is False
assert result["vector_store_error"] is True
assert "Retry once" not in result["error"]
session.rollback.assert_called_once()
session.commit.assert_not_called()
class TestPartialDropIsReportedHonestly:
def test_a_failure_after_earlier_drops_counts_them(self):
"""Remote drops cannot be rolled back. When the second of three
fails, the first is already gone -- the error must say so rather
than let callers claim nothing was deleted."""
events = []
stub = _make_remote_stub(
events,
error=lambda index_hash: (
ConnectionError("down") if index_hash == "hash-two" else None
),
)
rows = [_fake_index("hash-one"), _fake_index("hash-two")]
rows.append(_fake_index("hash-three"))
exc = _drop_error(stub, rows=rows)
assert exc.dropped == 1
assert [e for e in events if e[0] == "drop"] == [
("drop", "alice", "hash-one"),
("drop", "alice", "hash-two"),
]
message = remote_drop_failure_message(exc, "the index was not deleted")
assert "nothing was deleted" not in message
assert "1 vector-store collection(s) had already been removed" in (
message
)
assert "force re-index" in message
def test_a_failure_on_the_first_drop_claims_no_removal(self):
exc = _drop_error(_make_remote_stub([], error=ConnectionError("down")))
assert exc.dropped == 0
assert "already been removed" not in remote_drop_failure_message(
exc, "the index was not deleted"
)
class TestBackendErrorTextIsNotSurfaced:
def test_the_cause_text_is_neither_in_the_message_nor_chained(self):
"""A network client's error text can carry the backend URI or a
token. Only its type name may be surfaced, and the cause must not be
chained: a chained cause is printed by any traceback logger."""
secret = "https://admin:[email protected]:19530"
exc = _drop_error(_make_remote_stub([], error=ConnectionError(secret)))
assert "s3cr3t-token" not in str(exc)
assert exc.__cause__ is None
assert exc.__suppress_context__ is True
def test_the_service_logs_no_traceback_for_a_failed_drop(self):
from local_deep_research.research_library.deletion.services import (
collection_deletion,
)
session = _session_returning([_fake_index("hash-one")], [])
session.get.return_value = MagicMock(collection_type="custom")
stub = _make_remote_stub(
[], error=ConnectionError("https://admin:s3cr3t-token@host")
)
with (
_patched_user_session(session),
_registered_as_default(stub),
patch.object(collection_deletion, "logger") as logger,
):
collection_deletion.CollectionDeletionService(
username="testuser"
).delete_collection("col-123")
logger.exception.assert_not_called()
logged = " ".join(str(c) for c in logger.method_calls)
assert "s3cr3t-token" not in logged
# --------------------------------------------------------------------------- #
# Reading the settings snapshot must not write, and must fail closed
# --------------------------------------------------------------------------- #
@pytest.fixture
def empty_settings_session():
"""A real SQLite session over a schema with an EMPTY settings table."""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from local_deep_research.database.models import Base
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
session = sessionmaker(bind=engine)()
try:
yield session
finally:
session.close()
engine.dispose()
class TestSnapshotReadHasNoSideEffects:
def test_an_empty_settings_table_is_read_without_seeding_or_commit(
self, empty_settings_session
):
"""The snapshot is read mid-deletion through the deletion's own
session. A default ``SettingsManager`` seeds an empty settings table
and COMMITS -- outside the deletion's rollback. The deletion's read
must write nothing and still see every default."""
from local_deep_research.database.models import Setting
from local_deep_research.research_library.deletion.services.collection_deletion import (
_user_settings_snapshot,
)
from local_deep_research.settings.manager import SettingsManager
session = empty_settings_session
with patch.object(session, "commit", wraps=session.commit) as commit:
snapshot = _user_settings_snapshot(session)
commit.assert_not_called()
assert not session.new
assert session.query(Setting).count() == 0
# Defaults-only is the CORRECT answer for an empty table.
defaults = SettingsManager().default_settings
assert set(defaults) <= set(snapshot)
for key in ("search.tool", "llm.provider"):
assert snapshot[key]["value"] == defaults[key]["value"]
def test_stored_settings_override_the_defaults(
self, empty_settings_session
):
"""Control: the read-only read still sees the user's own rows."""
from local_deep_research.database.models import Setting
from local_deep_research.research_library.deletion.services.collection_deletion import (
_user_settings_snapshot,
)
session = empty_settings_session
session.add(
Setting(
key="search.tool",
value="user-choice",
type="SEARCH",
name="Search tool",
ui_element="text",
)
)
session.commit()
snapshot = _user_settings_snapshot(session)
assert snapshot["search.tool"]["value"] == "user-choice"
assert session.query(Setting).count() == 1
class _UnreadableSettings:
"""A settings manager whose strict read fails like a stale enum row."""
def __init__(self):
self.calls = []
def get_all_settings(self, strict=False):
self.calls.append(strict)
if strict:
raise LookupError("'CHAT' is not among the defined enum values")
return {"defaults": {"value": True}}
class TestUnreadableSnapshotFailsClosed:
def test_it_degrades_to_defaults_while_only_local_stores_exist(self):
"""FAISS-only: the snapshot cannot change the cleanup, so a broken
settings row must not block a deletion that would succeed."""
from local_deep_research.research_library.deletion.utils.cascade_helper import (
read_vector_cleanup_settings_snapshot,
)
manager = _UnreadableSettings()
assert read_vector_cleanup_settings_snapshot(manager) == {
"defaults": {"value": True}
}
assert manager.calls == [True, False]
def test_it_refuses_once_a_server_backed_store_exists(self):
"""A defaults-only snapshot would resolve the global default
provider and connection config, the drop would miss, and the rows
-- the only handle to the real collection -- would be deleted."""
from local_deep_research.research_library.deletion.utils.cascade_helper import (
read_vector_cleanup_settings_snapshot,
)
manager = _UnreadableSettings()
with _registered_behind_a_setting(_make_remote_stub([])):
with pytest.raises(RemoteVectorStoreDropError) as info:
read_vector_cleanup_settings_snapshot(manager)
assert info.value.retryable is False
assert info.value.cause_type == "LookupError"
assert manager.calls == [True]
def test_the_service_aborts_before_dropping_or_deleting_anything(self):
from local_deep_research.research_library.deletion.services.collection_deletion import (
CollectionDeletionService,
)
events = []
session = _session_returning([_fake_index("hash-one")], events)
session.get.return_value = MagicMock(collection_type="custom")
stub = _make_remote_stub(events)
with (
_patched_user_session(session),
patch(
"local_deep_research.settings.manager.SettingsManager",
return_value=_UnreadableSettings(),
),
_registered_behind_a_setting(stub),
):
result = CollectionDeletionService(
username="testuser"
).delete_collection("col-123", delete_orphaned_documents=False)
assert result["deleted"] is False
assert result["vector_store_error"] is True
assert result["retryable"] is False
assert events == []
session.rollback.assert_called_once()
session.commit.assert_not_called()
class TestDropPrecedesTheFirstWrite:
@pytest.mark.parametrize(
"operation", ["delete_collection", "delete_collection_index_only"]
)
def test_the_remote_drop_runs_before_the_chunk_delete(self, operation):
"""The drop is a network call; made after the chunk delete it would
hold the user's database write lock for its whole duration."""
from local_deep_research.research_library.deletion.services.collection_deletion import (
CollectionDeletionService,
)
events = []
session = _session_returning([_fake_index("hash-one")], events)
session.get.return_value = MagicMock(collection_type="custom")
stub = _make_remote_stub(events)
with (
_patched_user_session(session),
patch(
"local_deep_research.settings.manager.SettingsManager",
return_value=_settings_manager_returning(
_snapshot_selecting("stub_remote")
),
),
patch.object(
CascadeHelper,
"delete_collection_chunks",
side_effect=lambda *a, **k: events.append(("chunks",)) or 0,
),
_registered_behind_a_setting(stub),
):
service = CollectionDeletionService(username="testuser")
if operation == "delete_collection":
service.delete_collection(
"col-123", delete_orphaned_documents=False
)
else:
service.delete_collection_index_only("col-123")
assert ("chunks",) in events
assert events.index(("drop", "testuser", "hash-one")) < events.index(
("chunks",)
)