~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
"""Real SQLite/FAISS tests for publishing complete collection generations."""
import ast
import hashlib
import os
import sqlite3
import subprocess
import sys
import threading
from contextlib import contextmanager
from pathlib import Path
from types import MethodType, SimpleNamespace
from unittest.mock import MagicMock
import numpy as np
import pytest
from langchain_core.embeddings import Embeddings
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
Collection,
Document,
DocumentChunk,
DocumentCollection,
DocumentStatus,
EmbeddingProvider,
RagDocumentStatus,
RAGIndex,
SourceType,
)
from local_deep_research.research_library.services import library_rag_service
from local_deep_research.vector_stores import facade
from local_deep_research.vector_stores.implementations.faiss_store import (
FaissVectorStore,
)
class LocalEmbeddings(Embeddings):
def __init__(self, model):
self.model = model
self.fail_on = None
def embed_query(self, text):
seed = int.from_bytes(
hashlib.sha256((self.model + text).encode()).digest()[:4], "big"
)
return np.random.default_rng(seed).random(8).tolist()
def embed_documents(self, texts):
if self.fail_on and any(self.fail_on in text for text in texts):
raise RuntimeError("Embedding backend unavailable")
return [self.embed_query(text) for text in texts]
class RebuildWorld:
collection_id = "rebuild-collection"
collection_name = "collection_rebuild-collection"
doc_info = [("document-0", "First"), ("document-1", "Second")]
def __init__(
self,
root,
monkeypatch,
*,
seed=True,
encrypted=False,
real_integrity=False,
):
self.root = Path(root)
monkeypatch.setenv("LDR_DATA_DIR", str(root))
engine_kwargs = {}
if encrypted:
from local_deep_research.database.sqlcipher_compat import (
get_sqlcipher_module,
)
engine_kwargs["module"] = get_sqlcipher_module().dbapi2
self.engine = create_engine(
f"sqlite:///{root}/rebuild.sqlite", **engine_kwargs
)
@event.listens_for(self.engine, "connect")
def foreign_keys(connection, _record):
if encrypted:
connection.execute(
"PRAGMA key = 'test-password'"
) # gitleaks:allow
connection.execute("PRAGMA kdf_iter = 1000")
connection.execute("PRAGMA foreign_keys=ON")
self.make_session = sessionmaker(
bind=self.engine, expire_on_commit=False
)
self.local = threading.local()
monkeypatch.setattr(
library_rag_service, "get_user_db_session", self.session
)
monkeypatch.setattr(facade, "get_user_db_session", self.session)
if real_integrity:
from local_deep_research.security.file_integrity import (
integrity_manager,
)
from local_deep_research.database import thread_local_session
monkeypatch.setattr(
integrity_manager, "get_user_db_session", self.session
)
monkeypatch.setattr(
thread_local_session,
"get_current_thread_session",
lambda: getattr(self.local, "session", None),
)
else:
integrity = MagicMock()
integrity.verify_file.return_value = (True, None)
monkeypatch.setattr(
library_rag_service,
"FileIntegrityManager",
lambda *a, **k: integrity,
)
if seed:
Base.metadata.create_all(self.engine)
with self.session() as session:
session.add(
SourceType(
id="source", name="document", display_name="Document"
)
)
session.add(
Collection(
id=self.collection_id,
name="Rebuild test",
embedding_model="model-A",
embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
embedding_dimension=8,
chunk_size=100,
chunk_overlap=0,
splitter_type="recursive",
text_separators=["\n", " "],
distance_metric="cosine",
normalize_vectors=True,
index_type="flat",
)
)
for doc_id, title in self.doc_info:
body = (
f"Original text for {doc_id}. Searchable before rebuilding. "
* 5
)
session.add(
Document(
id=doc_id,
source_type_id="source",
document_hash=hashlib.sha256(
body.encode()
).hexdigest(),
title=title,
filename=f"{doc_id}.txt",
file_size=len(body.encode()),
file_type="txt",
text_content=body,
status=DocumentStatus.COMPLETED,
)
)
session.add(
DocumentCollection(
document_id=doc_id,
collection_id=self.collection_id,
indexed=False,
)
)
session.commit()
with self.service() as service:
for doc_id, _title in self.doc_info:
assert (
service.index_document(doc_id, self.collection_id)[
"status"
]
== "success"
)
@contextmanager
def session(self, *_args, **_kwargs):
existing = getattr(self.local, "session", None)
if existing is not None:
yield existing
return
with self.make_session() as session:
self.local.session = session
try:
yield session
except BaseException:
session.rollback()
raise
finally:
self.local.session = None
def service(self, model="model-A"):
return library_rag_service.LibraryRAGService(
username="rebuild-user",
db_password="test-password", # gitleaks:allow
embedding_manager=SimpleNamespace(
embeddings=LocalEmbeddings(model)
),
embedding_model=model,
embedding_provider="sentence_transformers",
chunk_size=100,
chunk_overlap=0,
text_separators=["\n", " "],
)
def snapshot(self):
with self.session() as session:
collection = session.get(Collection, self.collection_id)
indices = (
session.query(RAGIndex)
.filter_by(collection_name=self.collection_name)
.all()
)
return {
"model": collection.embedding_model,
"indices": sorted(
(
r.id,
r.index_hash,
r.is_current,
r.chunk_count,
r.total_documents,
)
for r in indices
),
"files": {
r.index_path: Path(r.index_path).read_bytes()
for r in indices
},
"chunks": [
(r.id, r.source_id, r.chunk_text)
for r in session.query(DocumentChunk)
.filter_by(collection_name=self.collection_name)
.order_by(DocumentChunk.id)
],
"status": [
(r.document_id, r.rag_index_id, r.chunk_count)
for r in session.query(RagDocumentStatus)
.filter_by(collection_id=self.collection_id)
.order_by(RagDocumentStatus.document_id)
],
"links": [
(r.document_id, r.indexed, r.chunk_count)
for r in session.query(DocumentCollection)
.filter_by(collection_id=self.collection_id)
.order_by(DocumentCollection.document_id)
],
}
def pending_rebuild_since(self):
with self.session() as session:
return session.get(
Collection, self.collection_id
).rebuild_pending_since
def assert_searchable(self, model="model-A"):
with self.service(model) as service:
hits = service.search("Searchable", self.collection_id, 100)
assert {hit.source_id for hit in hits} == {
doc_id for doc_id, _title in self.doc_info
}
def assert_no_staging(self):
with self.session() as session:
assert (
session.query(RAGIndex)
.filter(RAGIndex.collection_name != self.collection_name)
.count()
== 0
)
assert (
session.query(DocumentChunk)
.filter(DocumentChunk.collection_name != self.collection_name)
.count()
== 0
)
@pytest.fixture
def world(tmp_path, monkeypatch):
result = RebuildWorld(tmp_path, monkeypatch)
yield result
result.engine.dispose()
@pytest.mark.parametrize("model", ["model-A", "model-B"])
def test_complete_rebuild_switches_generation_only_after_all_documents(
world, model
):
before = world.snapshot()
observed = []
def progress(completed, *_args):
assert world.snapshot() == before
world.assert_searchable()
observed.append(completed)
with world.service(model) as service:
result = service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=2,
progress_callback=progress,
)
assert result["successful"] == 2
assert result["failed"] == 0
assert sorted(observed) == [1, 2]
after = world.snapshot()
assert after["model"] == model
assert after["indices"][0][0] != before["indices"][0][0]
assert all(not Path(path).exists() for path in before["files"])
assert all(
not Path(path + ".segments").exists() for path in before["files"]
)
world.assert_searchable(model)
world.assert_no_staging()
# Subsequent ordinary writes must reopen the published generation path.
with world.service(model) as service:
assert (
service.index_document(
"document-0", world.collection_id, force_reindex=True
)["status"]
== "success"
)
world.assert_searchable(model)
def test_embedding_failure_after_partial_preparation_preserves_old_index(world):
before = world.snapshot()
with world.service("model-B") as service:
service.embedding_manager.embeddings.fail_on = "document-1"
with pytest.raises(RuntimeError, match="previous index was preserved"):
service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
assert world.snapshot() == before
assert world.pending_rebuild_since() is not None
world.assert_searchable()
world.assert_no_staging()
@pytest.mark.parametrize("cancel_after", [1, 2])
def test_cancellation_preserves_the_published_generation(world, cancel_after):
before = world.snapshot()
stop = threading.Event()
def progress(completed, *_args):
if completed == cancel_after:
stop.set()
with world.service("model-B") as service:
result = service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
progress_callback=progress,
is_cancelled=stop.is_set,
)
assert result["cancelled"] is True
assert world.snapshot() == before
assert world.pending_rebuild_since() is not None
world.assert_searchable()
world.assert_no_staging()
def test_cancelled_rebuild_notice_survives_incremental_indexing_until_rebuilt(
world, loguru_caplog
):
"""An automatic incremental run cannot finish a cancelled model change."""
before = world.snapshot()
stop = threading.Event()
def cancel_after_preparation(*_args):
stop.set()
with (
loguru_caplog.at_level("WARNING"),
world.service("model-B") as replacement,
):
cancelled = replacement.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
progress_callback=cancel_after_preparation,
is_cancelled=stop.is_set,
)
assert cancelled["cancelled"]
pending_since = world.pending_rebuild_since()
assert pending_since is not None and pending_since.utcoffset() is not None
assert "Automatic indexing does not retry full rebuilds" in (
loguru_caplog.text
)
assert world.snapshot() == before
# Both the immediate auto-indexer and scheduled sweep use this ordinary
# path with the collection's existing configuration. Neither applies the
# requested replacement to documents that are already indexed.
with world.service("model-A") as incremental:
aggregate = incremental.index_documents_parallel(
world.doc_info,
world.collection_id,
force_reindex=False,
max_workers=1,
)
assert aggregate["successful"] == aggregate["failed"] == 0
assert aggregate["skipped"] == len(world.doc_info)
incremental.reconcile_collection_index(world.collection_id)
assert world.pending_rebuild_since() == pending_since
assert world.snapshot() == before
world.assert_searchable()
world.assert_no_staging()
# Only a complete replacement clears the durable notice and changes the
# collection's configuration and generation together.
with world.service("model-B") as replacement:
aggregate = replacement.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
assert aggregate["failed"] == 0 and not aggregate["cancelled"]
assert world.pending_rebuild_since() is None
assert world.snapshot()["model"] == "model-B"
assert world.snapshot()["indices"][0][0] != before["indices"][0][0]
world.assert_searchable("model-B")
world.assert_no_staging()
def test_failed_stage_persist_preserves_the_published_generation(
world, monkeypatch
):
before = world.snapshot()
original = FaissVectorStore.persist
writes = []
def fail_second_write(store, path):
assert str(path) not in before["files"]
writes.append(path)
if len(writes) == 2:
raise OSError("Disk full")
return original(store, path)
monkeypatch.setattr(FaissVectorStore, "persist", fail_second_write)
with (
world.service("model-B") as service,
pytest.raises(OSError, match="Disk full"),
):
service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
assert len(writes) == 2
assert world.snapshot() == before
world.assert_searchable()
world.assert_no_staging()
def test_cancellation_stops_rebuild_embedding_after_the_current_batch(
world, monkeypatch
):
before = world.snapshot()
with world.session() as session:
session.get(Document, "document-0").text_content = (
"Replacement text that requires many embedding batches. " * 200
)
session.commit()
stop = threading.Event()
batch_sizes = []
with world.service("model-B") as service:
embeddings = service.embedding_manager.embeddings
original = embeddings.embed_documents
def cancel_after_this_batch(texts):
batch_sizes.append(len(texts))
stop.set()
return original(texts)
monkeypatch.setattr(
embeddings, "embed_documents", cancel_after_this_batch
)
result = service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
is_cancelled=stop.is_set,
)
assert batch_sizes == [32]
assert result["cancelled"] is True
assert world.snapshot() == before
world.assert_searchable()
world.assert_no_staging()
def test_publication_commit_failure_restores_rows_and_keeps_old_files(world):
before = world.snapshot()
old_id = before["indices"][0][0]
failed = []
def fail_publication(session):
replacement = (
session.query(RAGIndex)
.filter(
RAGIndex.collection_name == world.collection_name,
RAGIndex.id != old_id,
)
.first()
)
if replacement is not None and not failed:
failed.append(replacement.id)
raise RuntimeError("Injected commit failure")
event.listen(world.make_session, "before_commit", fail_publication)
try:
with (
world.service("model-B") as service,
pytest.raises(RuntimeError, match="Injected commit failure"),
):
service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
finally:
event.remove(world.make_session, "before_commit", fail_publication)
assert failed
assert world.snapshot() == before
assert world.pending_rebuild_since() is not None
world.assert_searchable()
world.assert_no_staging()
@pytest.mark.parametrize("damage", ["missing", "corrupt"])
def test_publication_requires_durable_vectors(world, damage):
before = world.snapshot()
def progress(completed, *_args):
if completed != len(world.doc_info):
return
with world.session() as session:
stage = (
session.query(RAGIndex)
.filter(RAGIndex.collection_name != world.collection_name)
.one()
)
path = Path(stage.index_path)
if damage == "missing":
path.unlink()
else:
path.write_bytes(b"truncated FAISS data")
with world.service("model-B") as service, pytest.raises(RuntimeError):
service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
progress_callback=progress,
)
assert world.snapshot() == before
world.assert_searchable()
world.assert_no_staging()
def test_edit_during_rebuild_rejects_stale_preparation(world):
before = world.snapshot()
def progress(completed, *_args):
if completed == 1:
with world.session() as session:
session.get(
Document, "document-0"
).text_content = "New text while rebuilding"
session.commit()
with (
world.service("model-B") as service,
pytest.raises(ValueError, match="documents changed"),
):
service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
progress_callback=progress,
)
assert world.snapshot() == before
world.assert_searchable()
world.assert_no_staging()
@pytest.mark.parametrize("prepare_before_publication", [False, True])
def test_prepared_worker_cannot_overwrite_a_new_collection_configuration(
world, prepare_before_publication
):
with world.service() as original:
prepared = (
original._prepare_document("document-0", world.collection_id, True)[
"prepared"
]
if prepare_before_publication
else None
)
with world.service("model-B") as replacement:
replacement.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
published = world.snapshot()
if prepared is None:
prepared = original._prepare_document(
"document-0", world.collection_id, True
)["prepared"]
result = original._write_prepared_document(prepared)
assert result["status"] == "error"
assert "configuration changed" in result["error"]
assert world.snapshot() == published
world.assert_searchable("model-B")
@pytest.mark.parametrize("operation", ["reindex", "unindexed", "empty"])
def test_direct_worker_cannot_overwrite_a_new_collection_configuration(
world, operation
):
with world.service() as original:
with world.service("model-B") as replacement:
replacement.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
with world.session() as session:
if operation == "unindexed":
session.query(DocumentCollection).filter_by(
document_id="document-0", collection_id=world.collection_id
).one().indexed = False
elif operation == "empty":
session.get(Document, "document-0").text_content = ""
session.commit()
published = world.snapshot()
original.embedding_manager.embeddings.fail_on = "Original"
result = original.index_document(
"document-0",
world.collection_id,
force_reindex=operation != "unindexed",
)
assert result["status"] == "error"
assert "configuration changed" in result["error"]
assert world.snapshot() == published
world.assert_searchable("model-B")
@pytest.mark.parametrize("change", ["configuration", "membership", "index"])
def test_concurrent_collection_changes_are_not_overwritten(world, change):
changed = []
def progress(completed, *_args):
if completed != len(world.doc_info):
return
with world.session() as session:
if change == "configuration":
session.get(Collection, world.collection_id).chunk_size = 200
elif change == "membership":
session.query(DocumentCollection).filter_by(
document_id="document-1", collection_id=world.collection_id
).delete()
else:
index = (
session.query(RAGIndex)
.filter_by(collection_name=world.collection_name)
.one()
)
index.total_documents += 1
session.commit()
changed.append(world.snapshot())
with (
world.service("model-B") as service,
pytest.raises(ValueError, match="changed"),
):
service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
progress_callback=progress,
)
assert changed and world.snapshot() == changed[0]
world.assert_no_staging()
@pytest.mark.parametrize("remove_after", [1, 2])
def test_document_removed_from_search_during_rebuild_stays_removed(
world, monkeypatch, remove_after
):
from local_deep_research.web_search_engines.engines import (
local_embedding_manager,
)
monkeypatch.setattr(
local_embedding_manager, "get_user_db_session", world.session
)
removed = []
def remove_from_search(completed, *_args):
if completed != remove_after:
return
with world.service() as remover:
manager = remover.embedding_manager
manager.username = remover.username
manager.db_password = remover.db_password
manager._delete_chunks_from_db = MethodType(
local_embedding_manager.LocalEmbeddingManager._delete_chunks_from_db,
manager,
)
result = remover.remove_document_from_rag(
"document-0", world.collection_id
)
assert result["status"] == "success"
assert result["deleted_count"] > 0
removed.append(world.snapshot())
with (
world.service("model-B") as service,
pytest.raises(ValueError, match="indexing state changed"),
):
service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
progress_callback=remove_from_search,
)
assert removed and world.snapshot() == removed[0]
with world.service() as reader:
hits = reader.search("Searchable", world.collection_id, 100)
assert {hit.source_id for hit in hits} == {"document-1"}
world.assert_no_staging()
@pytest.mark.parametrize("empty_collection", [False, True])
def test_successful_empty_rebuild_clears_the_old_index(world, empty_collection):
before = world.snapshot()
with world.session() as session:
if empty_collection:
session.query(DocumentCollection).filter_by(
collection_id=world.collection_id
).delete()
else:
session.query(Document).update({"text_content": ""})
session.commit()
with world.service("model-B") as service:
result = service.rebuild_collection(
[] if empty_collection else world.doc_info,
world.collection_id,
max_workers=1,
)
assert result["failed"] == 0
assert world.pending_rebuild_since() is None
after = world.snapshot()
assert after["model"] == "model-B"
assert not after["indices"] and not after["chunks"] and not after["status"]
assert all(
not indexed and count == 0 for _doc, indexed, count in after["links"]
)
assert all(not Path(path).exists() for path in before["files"])
world.assert_no_staging()
def test_retired_files_are_deleted_only_after_publication_commits(
world, monkeypatch
):
from local_deep_research.research_library.deletion.utils.cascade_helper import (
CascadeHelper,
)
before = world.snapshot()
original = CascadeHelper.delete_faiss_index_files
removed = []
def inspect_committed_publication(path, **kwargs):
# A separate session observes only committed state, even if a caller
# mistakenly invokes cleanup from inside the publication transaction.
with world.make_session() as session:
assert (
session.get(Collection, world.collection_id).embedding_model
== "model-B"
)
indices = (
session.query(RAGIndex)
.filter_by(collection_name=world.collection_name)
.all()
)
assert (
len(indices) == 1 and indices[0].id != before["indices"][0][0]
)
assert str(path) not in {row.index_path for row in indices}
removed.append(str(path))
return original(path, **kwargs)
monkeypatch.setattr(
CascadeHelper, "delete_faiss_index_files", inspect_committed_publication
)
with world.service("model-B") as service:
service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
assert set(removed) == set(before["files"])
world.assert_searchable("model-B")
def test_callback_error_after_commit_keeps_the_published_generation(world):
failed = []
def mark_publication(session):
collection = session.get(Collection, world.collection_id)
session.info["publishing"] = collection.embedding_model == "model-B"
def fail_after_publication(session):
if session.info.pop("publishing", False):
failed.append(True)
raise RuntimeError("Post-commit callback failed")
event.listen(world.make_session, "before_commit", mark_publication)
event.listen(world.make_session, "after_commit", fail_after_publication)
try:
with world.service("model-B") as service:
result = service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
finally:
event.remove(world.make_session, "before_commit", mark_publication)
event.remove(world.make_session, "after_commit", fail_after_publication)
assert failed and result["failed"] == 0
assert world.snapshot()["model"] == "model-B"
world.assert_searchable("model-B")
world.assert_no_staging()
def _crash_after_staging(root, phase):
with pytest.MonkeyPatch.context() as monkeypatch:
world = RebuildWorld(root, monkeypatch, seed=False)
if phase == "published":
from local_deep_research.research_library.services.collection_rebuild import (
CollectionRebuild,
)
monkeypatch.setattr(
CollectionRebuild,
"_unlink",
staticmethod(lambda _paths: os._exit(73)),
)
def crash(completed, *_args):
if completed == 1 and phase == "preparing":
os._exit(73)
with world.service("model-B") as service:
service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
progress_callback=crash,
)
@pytest.mark.parametrize("phase", ["preparing", "published"])
def test_process_interruption_leaves_a_complete_generation_searchable(
world, phase
):
before = world.snapshot()
worker = (
"import runpy, sys; "
"ns = runpy.run_path(sys.argv[1]); "
"ns['_crash_after_staging'](sys.argv[2], sys.argv[3])"
)
child = subprocess.run(
[sys.executable, "-c", worker, __file__, str(world.root), phase],
capture_output=True,
text=True,
timeout=60,
)
assert child.returncode == 73, child.stderr
# The exit bypasses finally/cleanup on either side of publication.
if phase == "preparing":
with world.session() as session:
assert (
session.query(RAGIndex)
.filter(RAGIndex.collection_name != world.collection_name)
.count()
== 1
)
assert world.snapshot() == before
world.assert_searchable()
else:
assert world.snapshot()["model"] == "model-B"
assert all(Path(path).exists() for path in before["files"])
world.assert_searchable("model-B")
world.assert_no_staging()
# A later attempt can still replace the published generation. The
# abandoned generation stays unselected; deleting unknown stages here
# could destroy a rebuild still running in another process.
with world.service("model-B") as service:
result = service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
assert result["failed"] == 0
world.assert_searchable("model-B")
def test_encrypted_rebuild_with_real_integrity_records(tmp_path, monkeypatch):
world = RebuildWorld(
tmp_path, monkeypatch, encrypted=True, real_integrity=True
)
try:
before = world.snapshot()
with world.service("model-B") as service:
service.embedding_manager.embeddings.fail_on = "document-1"
with pytest.raises(
RuntimeError, match="previous index was preserved"
):
service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
assert world.snapshot() == before
world.assert_searchable()
with world.service("model-B") as service:
result = service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=2
)
assert result["successful"] == 2
world.assert_searchable("model-B")
world.assert_no_staging()
assert (
not (tmp_path / "rebuild.sqlite")
.read_bytes()
.startswith(b"SQLite format 3")
)
assert all(
b"Original text" not in payload
for payload in world.snapshot()["files"].values()
)
finally:
world.engine.dispose()
def test_cancellation_during_publication_publishes_the_whole_generation(world):
"""Cancellation is decided before the publication transaction opens.
A cancel that arrives while the switch is being written cannot stop the
transaction part way through: it publishes the complete generation.
"""
stop = threading.Event()
before = world.snapshot()
def cancel_on_configuration_switch(
_collection, value, _old_value, _initiator
):
if value == "model-B":
stop.set()
event.listen(
Collection.embedding_model, "set", cancel_on_configuration_switch
)
try:
with world.service("model-B") as service:
result = service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
is_cancelled=stop.is_set,
)
finally:
event.remove(
Collection.embedding_model, "set", cancel_on_configuration_switch
)
assert stop.is_set() and not result["cancelled"]
assert result["successful"] == 2 and result["failed"] == 0
assert world.snapshot()["model"] == "model-B"
assert all(not Path(path).exists() for path in before["files"])
world.assert_searchable("model-B")
world.assert_no_staging()
class TaskCancelCheck:
"""The background worker's cancel check over production session scopes.
``get_user_db_session`` gives every scope on a thread the same
thread-local Session, and rolls that Session back when a scope raises,
even a nested one. ``RebuildWorld.session`` does neither, so these tests
run the real function over one Session per thread. The check is the real
``_is_task_cancelled``, as ``_background_index_worker`` passes it.
"""
task_id = "rebuild-task"
def __init__(self, world, monkeypatch):
from local_deep_research.database import (
session_context,
thread_local_session,
)
from local_deep_research.database.models.queue import TaskMetadata
from local_deep_research.web.routers import rag as rag_routes
self.world = world
self.routes = rag_routes
self.TaskMetadata = TaskMetadata
self.local = threading.local()
self.sessions = []
self.sessions_lock = threading.Lock()
# Whether a get_user_db_session scope was already open on the
# polling thread, for every poll of the check.
self.polls = []
monkeypatch.setattr(
thread_local_session, "get_metrics_session", self.thread_session
)
for module in (library_rag_service, facade):
monkeypatch.setattr(
module,
"get_user_db_session",
session_context.get_user_db_session,
)
with world.session() as session:
session.add(
TaskMetadata(
task_id=self.task_id,
status="processing",
task_type="indexing",
)
)
session.commit()
def thread_session(self, _username, _password):
session = getattr(self.local, "session", None)
if session is None:
session = self.local.session = self.world.make_session()
with self.sessions_lock:
self.sessions.append(session)
return session
def check(self, service):
from local_deep_research.database.thread_local_session import (
thread_session_manager,
)
def is_cancelled():
self.polls.append(thread_session_manager.in_scope())
return self.routes._is_task_cancelled(
service.username, service.db_password, self.task_id
)
return is_cancelled
def cancel(self):
"""Record a cancel request through another connection."""
with self.world.make_session() as session:
session.get(self.TaskMetadata, self.task_id).status = "cancelled"
session.commit()
@contextmanager
def failing_task_reads(self):
"""Fail every task-row statement in the driver, like an I/O error."""
def fail(_cursor, statement, _parameters, _context):
if "task_metadata" in statement:
raise sqlite3.OperationalError("disk I/O error")
return False
event.listen(self.world.engine, "do_execute", fail)
try:
yield
finally:
event.remove(self.world.engine, "do_execute", fail)
def close(self):
for session in self.sessions:
session.close()
@pytest.fixture
def task_check(world, monkeypatch):
result = TaskCancelCheck(world, monkeypatch)
yield result
result.close()
def test_database_cancel_before_publication_preserves_the_index(
world, task_check, monkeypatch
):
"""The check runs before the publication scope opens, never inside it."""
from local_deep_research.research_library.services.collection_rebuild import (
CollectionRebuild,
)
before = world.snapshot()
validate_vectors = CollectionRebuild._validate_vectors
def cancel_once_staged(rebuild):
expected_chunks = validate_vectors(rebuild)
task_check.cancel()
return expected_chunks
monkeypatch.setattr(
CollectionRebuild, "_validate_vectors", cancel_once_staged
)
with world.service("model-B") as service:
result = service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
is_cancelled=task_check.check(service),
)
assert result["cancelled"] is True
assert task_check.polls and not any(task_check.polls), task_check.polls
assert world.snapshot() == before
world.assert_searchable()
world.assert_no_staging()
def test_cancel_check_after_validation_cannot_publish_unvalidated_rows(
world, task_check, monkeypatch
):
"""A rollback after validation must not let the publication commit.
The publication used to check cancellation here. When that check's read
fails, get_user_db_session rolls back the shared Session, which releases
SQLite's writer lock and the validated snapshot, and _is_task_cancelled
swallows the error. A writer can then commit a change that the
publication's writes, in a new transaction, would overwrite.
"""
from local_deep_research.research_library.services.collection_rebuild import (
CollectionRebuild,
)
before = world.snapshot()
validate_sources = CollectionRebuild._validate_sources
def validate_then_check(rebuild, session, collection):
validate_sources(rebuild, session, collection)
with task_check.failing_task_reads():
assert not task_check.routes._is_task_cancelled(
rebuild.service.username,
rebuild.service.db_password,
task_check.task_id,
)
# The rollback released the writer lock: remove a document from
# search through another connection, as a concurrent writer could.
with world.make_session() as other:
other.query(DocumentCollection).filter_by(
document_id="document-1", collection_id=world.collection_id
).update({"indexed": False, "chunk_count": 0})
other.query(RagDocumentStatus).filter_by(
document_id="document-1", collection_id=world.collection_id
).delete()
other.commit()
monkeypatch.setattr(
CollectionRebuild, "_validate_sources", validate_then_check
)
with (
world.service("model-B") as service,
pytest.raises(RuntimeError, match="lost its transaction"),
):
service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)
expected = dict(
before,
links=[
("document-1", False, 0) if row[0] == "document-1" else row
for row in before["links"]
],
status=[row for row in before["status"] if row[0] != "document-1"],
)
assert world.snapshot() == expected
world.assert_no_staging()
def test_failing_cancel_check_cannot_lose_the_published_index(
world, task_check
):
"""A cancel check whose read fails must not empty the publication.
_is_task_cancelled swallows the error after get_user_db_session rolled
back this thread's Session. Inside the publication that rollback used to
discard every publication write. The empty commit then counted as a
publication, and the previous generation's files were deleted while its
rows stayed current.
"""
before = world.snapshot()
with (
world.service("model-B") as service,
task_check.failing_task_reads(),
):
result = service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
is_cancelled=task_check.check(service),
)
assert result["cancelled"] is False
assert result["successful"] == 2 and result["failed"] == 0
assert world.snapshot()["model"] == "model-B"
assert all(not Path(path).exists() for path in before["files"])
world.assert_searchable("model-B")
world.assert_no_staging()
def test_search_finishes_on_the_old_generation_before_publication(
world, monkeypatch
):
before = world.snapshot()
searching = threading.Event()
resume = threading.Event()
staged = threading.Event()
completed = threading.Event()
hits = []
errors = []
original = facade.VectorIndex.search
def pause(index, query, *args, **kwargs):
if query == "pause":
searching.set()
assert resume.wait(10), "Test did not release the search"
return original(index, query, *args, **kwargs)
monkeypatch.setattr(facade.VectorIndex, "search", pause)
def reader():
try:
with world.service() as service:
hits.extend(service.search("pause", world.collection_id, 100))
except BaseException as exc:
errors.append(exc)
def progress(count, *_args):
if count == len(world.doc_info):
staged.set()
def publisher():
try:
with world.service("model-B") as service:
service.rebuild_collection(
world.doc_info,
world.collection_id,
max_workers=1,
progress_callback=progress,
)
completed.set()
except BaseException as exc:
errors.append(exc)
reader_thread = threading.Thread(target=reader)
publisher_thread = threading.Thread(target=publisher)
reader_thread.start()
try:
assert searching.wait(10)
publisher_thread.start()
assert staged.wait(10), (
"Embedding must continue while the old index is searched"
)
assert not completed.is_set()
assert world.snapshot() == before
finally:
resume.set()
reader_thread.join(timeout=10)
if publisher_thread.ident is not None:
publisher_thread.join(timeout=10)
assert not reader_thread.is_alive() and not publisher_thread.is_alive()
assert not errors and completed.is_set()
assert {hit.source_id for hit in hits} == {
doc_id for doc_id, _ in world.doc_info
}
world.assert_searchable("model-B")
world.assert_no_staging()
def test_index_clear_then_reindex_cannot_lose_recreated_file(
world, monkeypatch
):
from local_deep_research.research_library.deletion.services import (
collection_deletion,
)
from local_deep_research.research_library.services.collection_rebuild import (
CollectionRebuild,
)
monkeypatch.setattr(
collection_deletion, "get_user_db_session", world.session
)
old_path = Path(next(iter(world.snapshot()["files"])))
retiring = threading.Event()
release = threading.Event()
observed = threading.Event()
written = threading.Event()
admission = []
errors = []
original_unlink = CollectionRebuild._unlink
def pause_retirement(paths):
if str(old_path) in {str(path) for path in paths}:
retiring.set()
assert release.wait(30)
original_unlink(paths)
monkeypatch.setattr(
CollectionRebuild, "_unlink", staticmethod(pause_retirement)
)
def publish():
try:
with world.service("model-A") as service:
assert (
service.rebuild_collection(
world.doc_info, world.collection_id, max_workers=1
)["failed"]
== 0
)
except BaseException as exc:
errors.append(exc)
def clear_then_write():
try:
result = collection_deletion.CollectionDeletionService(
"rebuild-user"
).delete_collection_index_only(world.collection_id)
assert result["deleted"], result
with world.service("model-A") as service:
# Observe the production writer's existing lock. The model
# reset above is performed by the real public delete service.
lock = library_rag_service._get_collection_lock(
service.username,
world.collection_id,
purpose="transaction",
)
acquired = lock.acquire(blocking=False)
admission.append(acquired)
observed.set()
if not acquired:
assert lock.acquire()
try:
for document_id, _ in world.doc_info:
result = service.index_document(
document_id, world.collection_id, force_reindex=True
)
assert result["status"] == "success", result
assert old_path.is_file()
finally:
lock.release()
written.set()
except BaseException as exc:
errors.append(exc)
observed.set()
publisher = threading.Thread(target=publish)
writer = threading.Thread(target=clear_then_write)
try:
publisher.start()
assert retiring.wait(30), errors
writer.start()
assert observed.wait(30), errors
if admission == [True]:
assert written.wait(30), errors
finally:
release.set()
publisher.join(30)
if writer.ident is not None:
writer.join(30)
assert not publisher.is_alive() and not writer.is_alive()
assert not errors, errors
assert written.is_set()
assert old_path.is_file(), "retirement deleted the newly written index"
world.assert_searchable("model-A")
_ADVANCED_COLUMNS = (
"splitter_type",
"text_separators",
"distance_metric",
"normalize_vectors",
"index_type",
)
def test_legacy_collection_without_stored_settings_keeps_indexing(
world, monkeypatch
):
"""Index a collection created before #1054 through the real factory.
Migration 0032 adds the advanced columns NULL, and get_rag_service fills
them from the user's current settings. The configuration check must
resolve them the same way, or every ordinary index call fails once the
user has customised one of them (here the text separators).
"""
from local_deep_research.research_library.services import (
rag_service_factory,
)
with world.session() as session:
collection = session.get(Collection, world.collection_id)
for column in _ADVANCED_COLUMNS:
setattr(collection, column, None)
session.commit()
settings = {
"local_search_embedding_model": "model-A",
"local_search_embedding_provider": "sentence_transformers",
"local_search_chunk_size": 100,
"local_search_chunk_overlap": 0,
"local_search_text_separators": ["\n", " "],
}
manager = MagicMock()
manager.get_setting.side_effect = lambda key, default=None: settings.get(
key, default
)
real_service = library_rag_service.LibraryRAGService
def service_with_local_embeddings(**kwargs):
return real_service(
embedding_manager=SimpleNamespace(
embeddings=LocalEmbeddings(kwargs["embedding_model"])
),
**kwargs,
)
monkeypatch.setattr(
rag_service_factory, "get_settings_manager", lambda **_kw: manager
)
monkeypatch.setattr(
rag_service_factory, "get_user_db_session", world.session
)
monkeypatch.setattr(
rag_service_factory, "LibraryRAGService", service_with_local_embeddings
)
with rag_service_factory.get_rag_service(
"rebuild-user", world.collection_id
) as service:
assert service.text_separators == ["\n", " "]
result = service.index_document(
"document-0", world.collection_id, force_reindex=True
)
assert result["status"] == "success", result
aggregate = service.index_documents_parallel(
world.doc_info,
world.collection_id,
force_reindex=True,
max_workers=1,
)
assert aggregate["successful"] == 2 and aggregate["failed"] == 0, aggregate
world.assert_searchable()
def test_configuration_check_resolves_every_unset_column(world):
"""Each column the factory fills from settings resolves alike here."""
from local_deep_research.research_library.services.library_rag_service import (
_COLLECTION_INDEX_SETTINGS,
resolve_collection_index_settings,
)
legacy = Collection(id="legacy", name="Legacy", embedding_model="model-A")
with world.service() as service:
own = {
name: getattr(service, name) for name in _COLLECTION_INDEX_SETTINGS
}
assert resolve_collection_index_settings(legacy, own) == own
assert service._matches_collection_configuration(legacy)
# A stored value, such as one a rebuild published, still differs.
legacy.chunk_size = service.chunk_size + 1
assert not service._matches_collection_configuration(legacy)
def test_publish_writes_exactly_the_snapshotted_config_fields():
"""Pin the publish writes to the fields ``collection_configuration`` reads.
``collection_configuration`` is read-only: it snapshots ``_CONFIG_FIELDS``
with ``getattr``. ``CollectionRebuild._publish_rows`` is the only write
path for those fields in ``collection_rebuild.py``, and it hardcodes one
assignment per field so no dynamic ``setattr`` reaches the ORM object.
The two sets must stay identical: every snapshotted configuration field
must have a direct publish assignment, and every published configuration
field must be included in the snapshot. This guard checks field-name
parity, not assigned values or execution paths. The publish path also
clears the ``rebuild_pending_since`` notice, which is bookkeeping, not
configuration.
"""
from local_deep_research.research_library.services import collection_rebuild
source = Path(collection_rebuild.__file__).read_text(encoding="utf-8")
publish = next(
node
for node in ast.walk(ast.parse(source))
if isinstance(node, ast.FunctionDef) and node.name == "_publish_rows"
)
written = {
target.attr
for node in ast.walk(publish)
if isinstance(node, (ast.Assign, ast.AnnAssign))
for target in (
node.targets if isinstance(node, ast.Assign) else [node.target]
)
if isinstance(target, ast.Attribute)
and isinstance(target.value, ast.Name)
and target.value.id == "collection"
}
config_writes = written - {"rebuild_pending_since"}
snapshotted = set(collection_rebuild._CONFIG_FIELDS)
drifted = [
f"{name} is in _CONFIG_FIELDS but the publish path never writes it"
for name in sorted(snapshotted - config_writes)
] + [
f"{name} is written by the publish path but missing from _CONFIG_FIELDS"
for name in sorted(config_writes - snapshotted)
]
assert not drifted, drifted
@pytest.mark.parametrize("cancel", ["before_check", "after_check"])
def test_background_rebuild_status_reports_what_publication_did(
world, task_check, monkeypatch, cancel
):
"""A cancel after publish()'s check cannot stop the publication.
Its task must then report the new index as in use, not stay
``cancelled``, which reads as "previous index kept". A cancel recorded
before that check still cancels.
"""
from contextlib import nullcontext
from local_deep_research.research_library.services.collection_rebuild import (
CollectionRebuild,
)
before = world.snapshot()
routes = task_check.routes
check = CollectionRebuild._check_cancelled
with world.service("model-B") as service:
def request_cancel():
# What the cancel endpoint writes.
routes._do_update_task_status(
service.username,
service.db_password,
task_check.task_id,
status="cancelled",
progress_message="Cancellation requested...",
)
def check_around_a_cancel(is_cancelled):
if cancel == "before_check":
request_cancel()
check(is_cancelled)
if cancel == "after_check":
request_cancel()
monkeypatch.setattr(
CollectionRebuild,
"_check_cancelled",
staticmethod(check_around_a_cancel),
)
monkeypatch.setattr(
routes,
"_get_rag_service_for_thread",
lambda *_args, **_kwargs: nullcontext(service),
)
service.reconcile_collection_index = MagicMock(
return_value={
"indexed_documents": 2,
"indexed_chunks": 4,
"live_vectors": 4,
"orphan_vectors": 0,
}
)
routes._background_index_worker(
task_check.task_id,
world.collection_id,
service.username,
service.db_password,
True,
1,
)
with world.session() as session:
task = session.get(task_check.TaskMetadata, task_check.task_id)
status, message = task.status, task.progress_message
result = (task.metadata_json or {}).get("result")
if cancel == "before_check":
assert status == "cancelled", (status, message)
assert world.snapshot() == before
world.assert_no_staging()
return
assert status == "completed", (status, message)
assert message.endswith(routes._LATE_CANCEL_NOTE), message
assert result["durable_indexed_documents"] == 2, result
assert world.snapshot()["model"] == "model-B"
world.assert_searchable("model-B")
def test_lost_transaction_in_an_unchanged_empty_rebuild_is_not_published(
world, task_check, monkeypatch
):
"""The guard fails before the target configuration is recorded.
With no chunks, no index rows and an unchanged configuration, the state
after the guard's rollback equals the target. A target recorded first
let _is_published() report the refused publication as committed.
"""
from local_deep_research.research_library.services.collection_rebuild import (
CollectionRebuild,
)
with world.session() as session:
session.query(DocumentCollection).filter_by(
collection_id=world.collection_id
).delete()
session.commit()
with world.service("model-A") as service:
# Clears the index and stores this service's configuration.
assert (
service.rebuild_collection([], world.collection_id)["failed"] == 0
)
validate_sources = CollectionRebuild._validate_sources
def validate_then_lose_the_transaction(rebuild, session, collection):
validate_sources(rebuild, session, collection)
with task_check.failing_task_reads():
assert not task_check.routes._is_task_cancelled(
rebuild.service.username,
rebuild.service.db_password,
task_check.task_id,
)
monkeypatch.setattr(
CollectionRebuild,
"_validate_sources",
validate_then_lose_the_transaction,
)
with (
world.service("model-A") as service,
pytest.raises(RuntimeError, match="lost its transaction"),
):
service.rebuild_collection([], world.collection_id)
assert world.pending_rebuild_since() is not None
world.assert_no_staging()
def test_empty_rebuild_commit_failure_keeps_notice_until_a_successful_retry(
world,
):
"""Unchanged empty contents cannot disguise a rolled-back publication."""
with world.session() as session:
session.query(DocumentCollection).filter_by(
collection_id=world.collection_id
).delete()
session.commit()
with world.service() as service:
service.rebuild_collection([], world.collection_id)
before = world.snapshot()
assert world.pending_rebuild_since() is None
requested = []
failed = []
def fail_the_publication(session):
collection = session.get(Collection, world.collection_id)
if collection.rebuild_pending_since is not None:
requested.append(collection.rebuild_pending_since)
elif requested and not failed:
failed.append(True)
raise RuntimeError("Injected empty publication failure")
event.listen(world.make_session, "before_commit", fail_the_publication)
try:
with (
world.service() as service,
pytest.raises(RuntimeError, match="empty publication failure"),
):
service.rebuild_collection([], world.collection_id)
finally:
event.remove(world.make_session, "before_commit", fail_the_publication)
assert failed and requested
assert world.pending_rebuild_since() == requested[0]
assert world.snapshot() == before
world.assert_no_staging()
with world.service() as service:
aggregate = service.rebuild_collection([], world.collection_id)
assert aggregate["failed"] == 0
assert world.pending_rebuild_since() is None
assert world.snapshot() == before
def _abandon_a_staged_rebuild(world, service):
"""Stage one document as a rebuild does, then skip close() like an exit."""
from local_deep_research.research_library.services.collection_rebuild import (
CollectionRebuild,
)
rebuild = CollectionRebuild(
service,
world.collection_id,
world.doc_info,
session_factory=world.session,
)
rebuild.write(rebuild.prepare("document-0")["prepared"])
with world.session() as session:
stage = (
session.query(RAGIndex)
.filter_by(collection_name=rebuild.stage_name)
.one()
)
stage_path = Path(stage.index_path)
assert (
session.query(DocumentChunk)
.filter_by(collection_name=rebuild.stage_name)
.count()
)
assert stage_path.is_file()
return rebuild, stage_path
@pytest.mark.parametrize(
"operation", ["delete_collection", "delete_collection_index_only"]
)
def test_deleting_a_collection_or_its_index_removes_unfinished_rebuilds(
world, monkeypatch, operation
):
"""Staged chunk rows hold document text; nothing else removes them."""
from local_deep_research.research_library.deletion.services import (
collection_deletion,
)
monkeypatch.setattr(
collection_deletion, "get_user_db_session", world.session
)
with world.service("model-B") as service:
_rebuild, stage_path = _abandon_a_staged_rebuild(world, service)
assert world.pending_rebuild_since() is not None
deletion = collection_deletion.CollectionDeletionService("rebuild-user")
result = (
deletion.delete_collection(
world.collection_id, delete_orphaned_documents=False
)
if operation == "delete_collection"
else deletion.delete_collection_index_only(world.collection_id)
)
assert result["deleted"], result
if operation == "delete_collection_index_only":
assert world.pending_rebuild_since() is None
world.assert_no_staging()
assert not stage_path.exists()
def test_clearing_an_index_leaves_a_running_rebuild_to_clean_itself(
world, monkeypatch
):
"""A rebuild in this process holds the rebuild lock and owns its stage."""
from local_deep_research.research_library.deletion.services import (
collection_deletion,
)
monkeypatch.setattr(
collection_deletion, "get_user_db_session", world.session
)
with world.service("model-B") as service:
rebuild, stage_path = _abandon_a_staged_rebuild(world, service)
holding = threading.Event()
release = threading.Event()
def hold_the_rebuild_lock():
with library_rag_service._hold_collection_lock(
"rebuild-user", world.collection_id, purpose="rebuild"
):
holding.set()
release.wait(30)
holder = threading.Thread(target=hold_the_rebuild_lock)
holder.start()
try:
assert holding.wait(30)
result = collection_deletion.CollectionDeletionService(
"rebuild-user"
).delete_collection_index_only(world.collection_id)
finally:
release.set()
holder.join(30)
assert result["deleted"], result
with world.session() as session:
assert (
session.query(RAGIndex)
.filter_by(collection_name=rebuild.stage_name)
.count()
== 1
)
assert stage_path.is_file()