Skip to content

~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.

test(rag-cleanup): pin the collector's links union and per-collection isolation (#7552)

Masyarakat AI Indonesia - FMD4d3ddc8112f7
diff --git a/tests/database/test_research_retry_rag_cleanup.py b/tests/database/test_research_retry_rag_cleanup.py
index daf0219f83..467394c0c6 100644
--- a/tests/database/test_research_retry_rag_cleanup.py
+++ b/tests/database/test_research_retry_rag_cleanup.py
@@ -330,3 +330,70 @@ def test_target_discovery_preserves_caller_transaction(database, nested):
with Session(database) as verification:
assert verification.get(ResearchHistory, "owner").query == "q"
assert verification.get(Document, "canonical").title == "Shared paper"
+
+
+def test_rag_targets_include_a_linked_collection_with_no_chunk_rows(database):
+ """A collection the document is linked to still reaches the collector.
+
+ ``collect_research_document_rag_targets`` unions the ``DocumentCollection``
+ links onto the collections named by chunk rows. Without the union a
+ linked-but-chunkless collection is silently skipped, and its vectors and
+ chunks survive the delete (#7504).
+ """
+ seed_family(database, references=False)
+ with Session(database) as session:
+ # "linked" has no chunk rows of its own; only the link to it exists.
+ session.add(Collection(id="linked", name="Linked collection"))
+ session.add(
+ DocumentCollection(document_id="canonical", collection_id="linked")
+ )
+ session.commit()
+
+ assert research_cascade.collect_research_document_rag_targets(
+ session, "owner"
+ ) == [("canonical", ["library", "linked", "selected"])]
+
+
+def test_purge_isolates_per_collection_failures(database, monkeypatch):
+ """One collection's vector purge failing must not skip the next one."""
+ seed_family(database, references=False)
+ # The purge only runs once the research and its document are gone (the
+ # observer checks the committed delete from a fresh connection).
+ with Session(database) as session:
+ session.query(DocumentCollection).filter_by(
+ document_id="canonical"
+ ).delete()
+ session.delete(session.get(Document, "canonical"))
+ session.delete(session.get(ResearchHistory, "owner"))
+ session.commit()
+ events = []
+ observe_actual_purge(monkeypatch, database, events)
+
+ # observe_actual_purge installed its own get_rag_service; wrap whatever it
+ # returns so the first collection's vector purge raises.
+ observed = rag_service_factory.get_rag_service
+ seen = []
+
+ def failing_once(*args, **kwargs):
+ observer = observed(*args, **kwargs)
+ real_vectors = observer.purge_document_vectors
+
+ def purge(document_id, collection_id):
+ seen.append(collection_id)
+ if collection_id == "library":
+ raise RuntimeError("vector store unavailable")
+ return real_vectors(document_id, collection_id)
+
+ observer.purge_document_vectors = purge
+ return observer
+
+ monkeypatch.setattr(rag_service_factory, "get_rag_service", failing_once)
+
+ document_deletion.DocumentDeletionService(
+ "owner"
+ ).purge_deleted_research_documents([("canonical", ["library", "selected"])])
+
+ # The failing collection did not stop the next one.
+ assert seen == ["library", "selected"]
+ assert ("vectors", "canonical", "selected") in events
+ assert ("chunks", "canonical", "selected") in events