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.

15.7 KB
Raw Blame
"""Index ownership and cleanup regressions using real SQLite and FAISS."""

import json
import threading

import faiss
import numpy as np
import pytest
from local_deep_research.database.models.library import (
    Document,
    DocumentChunk,
    DocumentCollection,
    EmbeddingProvider,
    RagDocumentStatus,
)
from local_deep_research.vector_stores.facade import ChunkInput, VectorIndex

from tests.research_library.services import (
    test_rag_stats_index_scope as harness,
)

shared_world = harness.shared_world
seeded_collection = harness.seeded_collection
faiss.omp_set_num_threads(2)


def report(name, **values):
    print(json.dumps({"scenario": name, **values}))


def make_index(tmp_path):
    return VectorIndex(
        username="synthetic",
        db_password="",
        embeddings=harness._FakeEmbeddings("synthetic", 4),
        embedding_model="synthetic",
        embedding_model_type=EmbeddingProvider.OLLAMA,
        collection_name="collection_synthetic",
        dimension=4,
        path=tmp_path / "rollback.faiss",
        lock=threading.RLock(),
        integrity_record=lambda path: None,
        integrity_verify=lambda path: (True, None),
        index_type="hnsw",
        metric="cosine",
        normalize=True,
    )


def test_shorter_retry_cleans_rollback_orphans(tmp_path, shared_world):
    session = shared_world
    index = make_index(tmp_path)
    index.index(
        source_type="document",
        source_id="one-document",
        chunks=[ChunkInput(text="original")],
        session=session,
    )
    session.commit()
    index.index(
        source_type="document",
        source_id="one-document",
        chunks=[ChunkInput(text=f"uncommitted {i}") for i in range(4)],
        session=session,
    )
    session.rollback()  # The vector file has already been durably replaced.
    index.index(
        source_type="document",
        source_id="one-document",
        chunks=[ChunkInput(text="successful smaller retry")],
        session=session,
    )
    session.commit()
    rows = {row.id for row in session.query(DocumentChunk)}
    live = set(index.live_ids())
    assert len(rows) == 1
    assert live == rows
    report(
        "rollback_then_shorter_retry",
        database_rows=len(rows),
        vectors=len(live),
        unresolved_vectors=len(live - rows),
    )


def test_successful_delete_purges_historical_model_vectors(
    shared_world, seeded_collection
):
    session = shared_world
    collection, document, _ = seeded_collection
    service_a = harness._make_service(
        "model-A", 8, index_type="hnsw", chunk_size=80, chunk_overlap=10
    )
    service_b = harness._make_service(
        "model-B", 8, index_type="hnsw", chunk_size=80, chunk_overlap=10
    )
    for service in [service_a, service_b]:
        result = service.index_document(
            document, collection, force_reindex=True
        )
        assert result["status"] == "success", result
    name = f"collection_{collection}"
    index_a = service_a._get_vector_index(collection, name)
    before = index_a.count()
    removed = service_b.purge_document_vectors(document, collection)
    remaining_rows = (
        session.query(DocumentChunk).filter_by(source_id=document).count()
    )
    assert removed > 0 and before > 0
    assert remaining_rows == 0
    index_a = service_a._get_vector_index(collection, name)
    assert index_a.count() == 0
    report(
        "successful_delete_current_model",
        current_vectors_removed=removed,
        database_rows_remaining=remaining_rows,
        historical_orphan_vectors=index_a.count(),
    )


def test_same_model_configuration_roundtrip_preserves_ownership(
    shared_world, seeded_collection
):
    session = shared_world
    collection, document, _ = seeded_collection
    services = [
        harness._make_service(
            "same-model",
            8,
            index_type="hnsw",
            chunk_size=size,
            chunk_overlap=10,
        )
        for size in [80, 120]
    ]
    name = f"collection_{collection}"
    history = []
    for step, service in enumerate([services[0], services[1], services[0]]):
        result = service.index_document(
            document, collection, force_reindex=True
        )
        assert result["status"] == "success", result
        index = service._get_vector_index(collection, name)
        rows = {
            row.id
            for row in session.query(DocumentChunk).filter_by(
                source_id=document, vector_index_key=index.index_key
            )
        }
        live = set(index.live_ids())
        history.append(
            dict(
                step=step,
                chunk_size=service.chunk_size,
                database_rows=len(rows),
                vectors=len(live),
                orphan_vectors=len(live - rows),
            )
        )
    assert history[-1]["orphan_vectors"] == 0
    assert history[-1]["vectors"] == history[0]["vectors"] > 0
    report("same_model_configuration_A_B_A", history=history)


def test_reconciliation_preserves_valid_partial_document(
    shared_world, seeded_collection
):
    session = shared_world
    collection, document, _ = seeded_collection
    service = harness._make_service(
        "same-model", 8, index_type="hnsw", chunk_size=80, chunk_overlap=10
    )
    result = service.index_document(document, collection, force_reindex=True)
    assert result["status"] == "success", result
    index = service._get_vector_index(collection, f"collection_{collection}")
    ids = index.live_ids()
    assert len(ids) > 1
    index._store.apply(add_ids=[], add_vectors=None, remove_ids=[ids[0]])
    result = service.reconcile_collection_index(collection)
    live = set(index.live_ids())
    rows = {
        row.id
        for row in session.query(DocumentChunk).filter_by(
            source_id=document, vector_index_key=index.index_key
        )
    }
    assert live and live.issubset(rows)
    assert result["orphan_vectors"] == 0
    assert result["partial_document_vectors"] == len(live)
    assert (
        set(
            service._get_vector_index(
                collection, f"collection_{collection}"
            ).live_ids()
        )
        == live
    )
    assert result["indexed_documents"] == 0
    report(
        "partial_document_orphan_metric",
        **result,
        actual_vectors_without_chunk_rows=len(live - rows),
    )


def test_bulk_delete_applies_once_per_historical_index(
    shared_world, seeded_collection, monkeypatch
):
    from local_deep_research.vector_stores.implementations.segmented_faiss import (
        SegmentedFaissStore,
    )

    collection, first, second = seeded_collection
    services = [
        harness._make_service(
            model, 8, index_type="hnsw", chunk_size=80, chunk_overlap=10
        )
        for model in ("model-A", "model-B")
    ]
    for service in services:
        for document in (first, second):
            assert (
                service.index_document(
                    document, collection, force_reindex=True
                )["status"]
                == "success"
            )
    calls = []
    original = SegmentedFaissStore.apply

    def observed(store, **kwargs):
        calls.append(store._path)
        return original(store, **kwargs)

    monkeypatch.setattr(SegmentedFaissStore, "apply", observed)
    assert services[0].purge_documents_vectors([first, second], collection) > 0
    assert len(calls) == len(set(calls)) == 2
    assert shared_world.query(DocumentChunk).count() == 0


def test_cleanup_repairs_orphans_from_older_writers_before_id_reuse(
    tmp_path, shared_world
):
    index = make_index(tmp_path)
    index.index(
        source_type="document",
        source_id="one",
        chunks=[ChunkInput(text="committed")],
    )
    # Simulate an older writer/process exit that persisted IDs with no rows.
    index._store.apply(
        add_ids=[2, 3, 4], add_vectors=np.ones((3, 4), dtype="float32")
    )
    index.index(
        source_type="document",
        source_id="one",
        chunks=[ChunkInput(text="replacement")],
    )
    rows = {row.id for row in shared_world.query(DocumentChunk)}
    assert set(index.live_ids()) == rows
    assert len(rows) == 1


def test_writes_skip_the_full_orphan_scan_between_intervals(
    tmp_path, shared_world, monkeypatch
):
    from local_deep_research.vector_stores.implementations.segmented_faiss import (
        SegmentedFaissStore,
    )

    index = make_index(tmp_path)
    scans = []
    original = SegmentedFaissStore.iter_id_batches

    def counting(store):
        scans.append(True)
        return original(store)

    monkeypatch.setattr(SegmentedFaissStore, "iter_id_batches", counting)
    for number in range(4):
        index.index(
            source_type="document",
            source_id=f"document-{number}",
            chunks=[ChunkInput(text=f"chunk {number}")],
        )
    # An empty index needs no scan and the second write records the first
    # one; later writes only compare IDs against the highest chunk row.
    assert len(scans) == 1
    rows = {row.id for row in shared_world.query(DocumentChunk)}
    assert set(original(index._store).__next__()) == rows


@pytest.mark.parametrize("delete_parent", [False, True])
def test_reconciliation_collects_deleted_or_unlinked_documents(
    shared_world, seeded_collection, delete_parent
):
    session = shared_world
    collection, first, second = seeded_collection
    service = harness._make_service(
        "same-model", 8, index_type="hnsw", chunk_size=80, chunk_overlap=10
    )
    for document in (first, second):
        assert (
            service.index_document(document, collection)["status"] == "success"
        )
    index = service._get_vector_index(collection, f"collection_{collection}")
    original = index.count()
    survivor_ids = {
        row.id
        for row in session.query(DocumentChunk).filter_by(source_id=second)
    }
    # Reproduce the best-effort post-delete purge being unavailable.
    session.query(RagDocumentStatus).filter_by(document_id=first).delete()
    session.query(DocumentCollection).filter_by(
        document_id=first, collection_id=collection
    ).delete()
    if delete_parent:
        session.query(Document).filter_by(id=first).delete()
    session.commit()
    result = service.reconcile_collection_index(collection)
    assert set(index.live_ids()) == survivor_ids
    assert session.query(DocumentChunk).filter_by(source_id=first).count() == 0
    assert result["orphan_vectors"] == 0
    assert result["orphan_vectors_removed"] == original - len(survivor_ids) > 0
    assert result["indexed_documents"] == 1


def test_bulk_cleanup_respects_sqlite_parameter_limit(tmp_path, shared_world):
    import sqlite3

    index = make_index(tmp_path)
    index.index(
        source_type="file",
        source_id="batch",
        chunks=[ChunkInput(text=f"chunk {i}") for i in range(1100)],
    )
    for row in shared_world.query(DocumentChunk):
        row.source_id = str(row.id)
    shared_world.commit()
    connection = shared_world.connection().connection.driver_connection
    previous = connection.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, 999)
    try:
        ids = [str(value) for value in index.live_ids()]
        removed = index.delete_sources(source_type="file", source_ids=ids)
        assert removed.rows_deleted == removed.removed == 1100
        assert index.count() == shared_world.query(DocumentChunk).count() == 0
    finally:
        connection.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, previous)


@pytest.mark.parametrize("settle", ["commit", "rollback"])
def test_bulk_purge_waits_for_writer_before_checkpoint_recovery(
    tmp_path, monkeypatch, settle
):
    """An active writer's checkpoint must never look like crash recovery."""
    from local_deep_research.research_library.services import (
        library_rag_service,
    )
    from local_deep_research.security.file_integrity.integrity_manager import (
        FileIntegrityManager,
    )
    from tests.research_library.services.test_collection_rebuild import (
        RebuildWorld,
    )

    world = RebuildWorld(
        tmp_path, monkeypatch, encrypted=True, real_integrity=True
    )
    survivor, deleted = [doc_id for doc_id, _ in world.doc_info]
    attempted = threading.Event()
    writer_settled = threading.Event()
    verification_before_settlement = []
    errors = []
    removed = []
    lock = library_rag_service._get_collection_lock(
        "rebuild-user", world.collection_id, purpose="transaction"
    )
    original_acquire = type(lock).acquire
    original_verify = FileIntegrityManager.verify_file

    def observe_acquire(candidate, *args, **kwargs):
        if candidate is lock and threading.current_thread() is worker:
            attempted.set()
        return original_acquire(candidate, *args, **kwargs)

    def observe_verify(manager, *args, **kwargs):
        if threading.current_thread() is worker:
            verification_before_settlement.append(not writer_settled.is_set())
            try:
                return original_verify(manager, *args, **kwargs)
            finally:
                # Also wake the test if the collection lock is missing.
                attempted.set()
        return original_verify(manager, *args, **kwargs)

    def purge():
        try:
            with world.service() as service:
                removed.append(
                    service.purge_documents_vectors(
                        [deleted], world.collection_id
                    )
                )
        except BaseException as exc:
            errors.append(exc)

    worker = threading.Thread(target=purge)
    monkeypatch.setattr(type(lock), "acquire", observe_acquire)
    monkeypatch.setattr(FileIntegrityManager, "verify_file", observe_verify)
    try:
        with (
            world.service() as service,
            library_rag_service._hold_collection_lock(
                service.username, world.collection_id, purpose="transaction"
            ),
            world.session() as session,
        ):
            index = service._get_vector_index(
                world.collection_id, world.collection_name
            )
            old_ids = {
                row.id
                for row in session.query(DocumentChunk).filter_by(
                    source_id=survivor
                )
            }
            index.index(
                source_type="document",
                source_id=survivor,
                chunks=[ChunkInput(text="Replacement survivor text")],
                session=session,
            )
            new_ids = {
                row.id
                for row in session.query(DocumentChunk).filter_by(
                    source_id=survivor
                )
            }
            assert new_ids and new_ids.isdisjoint(old_ids)
            pending_manifest = index._path.read_bytes()
            worker.start()
            assert attempted.wait(10), "Bulk purge did not start"
            assert not verification_before_settlement
            assert index._path.read_bytes() == pending_manifest
            getattr(session, settle)()
            writer_settled.set()
        worker.join(timeout=10)
        assert not worker.is_alive()
        assert not errors
        assert removed and removed[0] > 0
        assert verification_before_settlement
        assert not any(verification_before_settlement)
        with world.service() as service, world.session() as session:
            rows = {row.id for row in session.query(DocumentChunk)}
            expected = new_ids if settle == "commit" else old_ids
            assert rows == expected
            index = service._get_vector_index(
                world.collection_id, world.collection_name
            )
            assert set(index.live_ids()) == expected
            assert service.integrity_manager.verify_file(index._path)[0]
    finally:
        if worker.ident is not None and worker.is_alive():
            worker.join(timeout=10)
        world.engine.dispose()