~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
"""A failed database commit must preserve the previous searchable generation.
Real FAISS files and SQLite transactions exercise both the service retry
backstop and the facade checkpoint. The checkpoint protects prior vectors
independently of the service's conservative reindex flag.
"""
import hashlib
import threading
import uuid
from contextlib import contextmanager
from unittest.mock import MagicMock
import numpy as np
import pytest
from langchain_core.embeddings import Embeddings
from sqlalchemy import create_engine
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,
RAGIndex,
RagDocumentStatus,
SourceType,
)
from local_deep_research.research_library.services.library_rag_service import (
LibraryRAGService,
)
from local_deep_research.vector_stores.facade import ChunkInput, VectorIndex
from local_deep_research.vector_stores.implementations.segmented_faiss import (
SegmentedFaissStore,
)
_RAG_MOD = "local_deep_research.research_library.services.library_rag_service"
_FACADE_MOD = "local_deep_research.vector_stores.facade"
class _FakeEmbeddings(Embeddings):
"""Deterministic, network-free embeddings (same recipe as the canonical
harness ``test_no_plaintext_production_path.py``)."""
def __init__(self, dim: int = 8):
self.dim = dim
def _vec(self, text: str):
rng = np.random.default_rng(abs(hash(text)) % (2**32))
return rng.random(self.dim).tolist()
def embed_documents(self, texts):
return [self._vec(t) for t in texts]
def embed_query(self, text):
return self._vec(text)
def _flaky_commit(session, state):
"""A one-shot failing ``commit`` wrapper: raises exactly once (while
``state["armed"]`` is True), then delegates to the session's real commit
for every subsequent call — so a later backstop/reconcile commit in the
SAME test still durably persists."""
real_commit = session.commit
def _commit():
if state["armed"]:
state["armed"] = False
raise RuntimeError("simulated DB commit failure")
return real_commit()
return _commit, real_commit
# --------------------------------------------------------------------------- #
# (a) LibraryRAGService.index_document self-heal
# --------------------------------------------------------------------------- #
class TestIndexDocumentSelfHeal:
@pytest.fixture
def rag_env(self, tmp_path, monkeypatch):
"""Real in-memory DB + real on-disk cache tree, shared across every
``get_user_db_session`` call site the indexing path touches."""
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
session = sessionmaker(bind=engine)()
@contextmanager
def _shared_session(*_a, **_k):
yield session
monkeypatch.setattr(f"{_RAG_MOD}.get_user_db_session", _shared_session)
monkeypatch.setattr(
f"{_FACADE_MOD}.get_user_db_session", _shared_session
)
# Integrity checks live in the encrypted DB via a real manager that
# needs real key material; stub it (as the canonical harness does) so
# this test stays focused on the store/DB durability-window invariant.
fim = MagicMock()
fim.verify_file.return_value = (True, None)
fim.record_file.return_value = None
monkeypatch.setattr(
f"{_RAG_MOD}.FileIntegrityManager", lambda *a, **k: fim
)
yield session
session.close()
engine.dispose()
@staticmethod
def _seed_document(
session, source_type_id, *, doc_id, coll_id, text, title
):
session.add(Collection(id=coll_id, name=f"Collection {coll_id[:8]}"))
session.add(
Document(
id=doc_id,
source_type_id=source_type_id,
document_hash=hashlib.sha256(text.encode()).hexdigest(),
title=title,
text_content=text,
original_url="http://example.com/doc",
file_size=len(text),
file_type="txt",
status=DocumentStatus.COMPLETED,
)
)
session.commit()
@staticmethod
def _make_service(username):
emb_mgr = MagicMock()
emb_mgr.embeddings = _FakeEmbeddings(dim=8)
return LibraryRAGService(
username=username,
db_password="pw", # gitleaks:allow
embedding_model="fake-model",
embedding_provider="sentence_transformers",
embedding_manager=emb_mgr,
)
@staticmethod
def _source_type(session):
st = SourceType(
id=uuid.uuid4().hex, name="document", display_name="Document"
)
session.add(st)
session.commit()
return st
def test_self_heals_after_commit_failure(self, rag_env, monkeypatch):
session = rag_env
source_type = self._source_type(session)
doc_id = uuid.uuid4().hex
coll_id = uuid.uuid4().hex
text = "The mitochondria is the powerhouse of the cell. " * 20
self._seed_document(
session,
source_type.id,
doc_id=doc_id,
coll_id=coll_id,
text=text,
title="Bio Notes",
)
svc = self._make_service("user-self-heal")
# 1) First index: real success, real search, real DB rows.
result1 = svc.index_document(doc_id, coll_id)
assert result1["status"] == "success", result1
dc = (
session.query(DocumentCollection)
.filter_by(document_id=doc_id, collection_id=coll_id)
.first()
)
assert dc.indexed is True
assert (
session.query(RagDocumentStatus)
.filter_by(document_id=doc_id, collection_id=coll_id)
.first()
is not None
)
hits0 = svc.search("mitochondria powerhouse", coll_id, top_k=3)
assert any("mitochondria" in h.text.lower() for h in hits0)
# 2) Force-reindex with a ONE-SHOT commit failure injected AFTER
# store.apply() has already durably replaced the vectors (real apply()
# runs for real; only the top-level DB commit inside index_document
# is made to fail, exactly the window _flag_document_for_reindex's
# docstring describes).
state = {"armed": True}
flaky, real_commit = _flaky_commit(session, state)
monkeypatch.setattr(session, "commit", flaky)
result2 = svc.index_document(doc_id, coll_id, force_reindex=True)
assert result2["status"] == "error", result2
monkeypatch.setattr(session, "commit", real_commit)
# Backstop ran: NOT left indexed-but-unsearchable.
session.expire_all()
dc2 = (
session.query(DocumentCollection)
.filter_by(document_id=doc_id, collection_id=coll_id)
.first()
)
assert dc2.indexed is False, (
"backstop did not flag the document NOT indexed after the "
"post-apply() commit failure"
)
assert (
session.query(RagDocumentStatus)
.filter_by(document_id=doc_id, collection_id=coll_id)
.first()
is None
), "backstop did not delete the stale RagDocumentStatus row"
# 3) Reconcile: a normal retry, no injected failure -> real self-heal.
result3 = svc.index_document(doc_id, coll_id)
assert result3["status"] == "success", result3
assert result3.get("chunk_count", 0) > 0
hits1 = svc.search("mitochondria powerhouse", coll_id, top_k=3)
assert any("mitochondria" in h.text.lower() for h in hits1), (
"system did not self-heal: search is still empty after the "
"reconcile pass"
)
svc.close()
def test_force_reindex_updates_aggregate_chunk_delta(self, rag_env):
session = rag_env
source_type = self._source_type(session)
doc_id = uuid.uuid4().hex
coll_id = uuid.uuid4().hex
long_text = "\n\n".join(
f"Paragraph {i}: " + "alpha beta gamma " * 100 for i in range(30)
)
self._seed_document(
session,
source_type.id,
doc_id=doc_id,
coll_id=coll_id,
text=long_text,
title="Changing Notes",
)
svc = self._make_service("user-chunk-delta")
first = svc.index_document(doc_id, coll_id)
assert first["status"] == "success", first
old_count = first["chunk_count"]
assert old_count > 1
document = session.query(Document).filter_by(id=doc_id).one()
short_text = "A much shorter replacement document."
document.text_content = short_text
document.document_hash = hashlib.sha256(short_text.encode()).hexdigest()
session.commit()
second = svc.index_document(doc_id, coll_id, force_reindex=True)
assert second["status"] == "success", second
new_count = second["chunk_count"]
assert new_count < old_count
session.expire_all()
rag_index = (
session.query(RAGIndex)
.filter_by(collection_name=f"collection_{coll_id}", is_current=True)
.one()
)
status = (
session.query(RagDocumentStatus)
.filter_by(document_id=doc_id, collection_id=coll_id)
.one()
)
assert status.chunk_count == new_count
assert rag_index.chunk_count == new_count
assert rag_index.total_documents == 1
svc.close()
def test_checkpoint_preserves_search_without_the_reindex_backstop(
self, rag_env, monkeypatch
):
"""Vector rollback protects search even with the retry flag disabled."""
session = rag_env
source_type = self._source_type(session)
doc_id = uuid.uuid4().hex
coll_id = uuid.uuid4().hex
text = "Photosynthesis converts sunlight into chemical energy. " * 20
self._seed_document(
session,
source_type.id,
doc_id=doc_id,
coll_id=coll_id,
text=text,
title="Bio Notes 2",
)
svc = self._make_service("user-control")
# Disable ONLY the backstop for this instance.
monkeypatch.setattr(
svc, "_flag_document_for_reindex", lambda *a, **k: None
)
result1 = svc.index_document(doc_id, coll_id)
assert result1["status"] == "success", result1
state = {"armed": True}
flaky, real_commit = _flaky_commit(session, state)
monkeypatch.setattr(session, "commit", flaky)
result2 = svc.index_document(doc_id, coll_id, force_reindex=True)
assert result2["status"] == "error", result2
monkeypatch.setattr(session, "commit", real_commit)
session.expire_all()
dc = (
session.query(DocumentCollection)
.filter_by(document_id=doc_id, collection_id=coll_id)
.first()
)
assert dc.indexed is True
hits = svc.search(
"photosynthesis sunlight chemical energy", coll_id, top_k=5
)
assert hits and all(hit.source_id == doc_id for hit in hits)
svc.close()
# --------------------------------------------------------------------------- #
# (b) VectorIndex.index -> _finalize_db: DB failure after a durable apply()
# --------------------------------------------------------------------------- #
class TestFacadeFinalizeDbFailureIsConsistent:
def test_finalize_db_failure_propagates_and_db_stays_consistent(
self, tmp_path, monkeypatch
):
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
session = sessionmaker(bind=engine)()
@contextmanager
def _shared_session(*_a, **_k):
yield session
monkeypatch.setattr(
f"{_FACADE_MOD}.get_user_db_session", _shared_session
)
index_path = tmp_path / "finalize.faiss"
vi = VectorIndex(
username="finalize-tester",
db_password="pw", # gitleaks:allow
embeddings=_FakeEmbeddings(dim=8),
embedding_model="fake-model",
embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
collection_name="collection_finalize",
dimension=8,
path=index_path,
lock=threading.Lock(),
integrity_record=lambda p: None,
integrity_verify=lambda p: (True, None),
index_type="flat",
metric="cosine",
normalize=True,
)
# 1) First index: real, durable, commits fine (no session passed ->
# facade owns the session it gets from the monkeypatched shared one).
stats1 = vi.index(
source_type="document",
source_id="doc-b",
chunks=[
ChunkInput(text="alpha body one", metadata={"chunk_index": 0})
],
)
assert stats1.added == 1
row_a = session.query(DocumentChunk).filter_by(source_id="doc-b").one()
chunk_a_id = row_a.id
def _load_store():
return SegmentedFaissStore.load(
index_path,
dimension=8,
index_type="flat",
metric="cosine",
normalize=True,
)
assert chunk_a_id in _load_store().live_ids()
# 2) Replace-reindex with a commit failure injected for the delete
# that _finalize_db issues AFTER store.apply() already durably
# removed chunk A's vector and added chunk B's.
def _boom():
raise RuntimeError("boom-commit")
monkeypatch.setattr(session, "commit", _boom)
with pytest.raises(RuntimeError, match="boom-commit"):
vi.index(
source_type="document",
source_id="doc-b",
chunks=[
ChunkInput(
text="beta body two", metadata={"chunk_index": 0}
)
],
replace=True,
)
# (No manual rollback needed/added here: _finalize_db's except branch
# already calls session.rollback() itself when it owns the session —
# that is exactly the behavior under test.)
# (1) DB stays CONSISTENT: the prior row survived (its delete was
# rolled back) and no duplicate/second row was left half-applied.
rows = session.query(DocumentChunk).filter_by(source_id="doc-b").all()
assert len(rows) == 1, (
"DB left in a half-applied state after the failed commit: "
f"expected exactly the surviving prior row, got {len(rows)}"
)
assert rows[0].id == chunk_a_id
# The checkpoint restores the same vector ID as the rolled-back row.
assert _load_store().live_ids() == [chunk_a_id]
assert vi.search("alpha body one", top_k=1)[0].text == "alpha body one"
session.close()
engine.dispose()