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.

16.2 KB
Raw Blame
"""Upgrade and component recovery through real FAISS and integrity checks."""

import pytest

from local_deep_research.database.models.library import (
    Document,
    DocumentChunk,
    DocumentCollection,
)
from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
    _QUARANTINE_KEEP_RECENT,
)
from local_deep_research.vector_stores.base import VectorStoreIntegrityError
from local_deep_research.vector_stores.facade import VectorIndex
from local_deep_research.vector_stores.implementations import segmented_faiss
from local_deep_research.vector_stores.segment_files import segment_directory
from tests.vector_stores import test_integrity_tamper_lifecycle as harness

rig = harness.rig


def test_reconciliation_adopts_legacy_ownership_before_counting_chunks(rig):
    """A different legacy chunking configuration must not disable search."""
    with LibraryRAGService(
        username=rig.svc.username,
        db_password="pw",
        embedding_model=rig.svc.embedding_model,
        embedding_provider=rig.svc.embedding_provider,
        embedding_manager=rig.svc.embedding_manager,
        chunk_size=80,
        chunk_overlap=10,
    ) as alternate:
        alternate._get_vector_index(rig.collection_id, rig.collection_name)
        # Migration 0042 cannot assign rows when two physical configurations
        # exist for the same model. This is its deliberate upgrade result.
        rig.session.query(DocumentChunk).update(
            {DocumentChunk.vector_index_key: None}
        )
        rig.session.commit()
        legacy_ids = {row.id for row in rig.session.query(DocumentChunk).all()}
        result = alternate.index_document(
            rig.document_id, rig.collection_id, force_reindex=True
        )
        assert result["status"] == "success", result
        index = alternate._get_vector_index(
            rig.collection_id, rig.collection_name
        )
        live_ids = set(index.live_ids())
        assert live_ids and live_ids.isdisjoint(legacy_ids)
        assert alternate.integrity_manager.verify_file(index._path, force=True)[
            0
        ]
        assert alternate.has_indexed_documents(rig.collection_id)

        report = alternate.reconcile_collection_index(rig.collection_id)

        assert report["indexed_documents"] == 1
        assert report["indexed_chunks"] == len(live_ids)
        assert report["partial_document_vectors"] == 0
        assert report["orphan_vectors"] == 0
        assert alternate.has_indexed_documents(rig.collection_id)
        link = (
            rig.session.query(DocumentCollection)
            .filter_by(
                document_id=rig.document_id, collection_id=rig.collection_id
            )
            .one()
        )
        assert link.indexed and link.chunk_count == len(live_ids)
        assert alternate.search(harness.SECRET, rig.collection_id, top_k=1)
        # The other configuration's encrypted text survives without guessed
        # ownership. A normal search can still hydrate its legacy vectors.
        assert {
            row.id
            for row in rig.session.query(DocumentChunk).filter(
                DocumentChunk.vector_index_key.is_(None)
            )
        } == legacy_ids
        assert rig.svc.search(harness.SECRET, rig.collection_id, top_k=1)


@pytest.mark.parametrize("suffix", [".faiss", ".ids"])
@pytest.mark.parametrize("damage", ["corrupt", "missing"])
@pytest.mark.parametrize("writer", ["serial", "prepared", "cleared"])
def test_write_recovers_component_damage_after_rollback(
    rig, suffix, damage, writer, mocker
):
    path = rig.index_path()
    original_root = path.read_bytes()
    original_ids = {row.id for row in rig.session.query(DocumentChunk)}
    index = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    entry = index._store._state()[0]
    component = (segment_directory(path) / entry["file"]).with_suffix(suffix)
    if writer == "prepared":
        prepared = rig.svc._prepare_document(
            rig.document_id, rig.collection_id, True
        )
        assert prepared["status"] == "prepared", prepared
    if damage == "missing":
        component.unlink()
    else:
        component.write_bytes(component.read_bytes() + b"synthetic corruption")

    # Reads refuse bad data without altering the root or indexed state.
    with pytest.raises(VectorStoreIntegrityError):
        rig.svc.search(harness.SECRET, rig.collection_id, top_k=1)
    assert path.read_bytes() == original_root
    assert rig.svc.has_indexed_documents(rig.collection_id)
    assert not list(path.parent.glob(path.name + ".corrupt-*"))
    quarantine = rig.svc._quarantine_corrupt_index

    def after_rollback(*args, **kwargs):
        assert not rig.session.info.get("ldr_vector_transactions")
        assert path.read_bytes() == original_root
        assert {
            row.id for row in rig.session.query(DocumentChunk)
        } == original_ids
        return quarantine(*args, **kwargs)

    mocker.patch.object(
        rig.svc, "_quarantine_corrupt_index", side_effect=after_rollback
    )
    if writer == "cleared":
        document = rig.session.get(Document, rig.document_id)
        document.text_content = ""
        rig.session.commit()
    result = (
        rig.svc._write_prepared_document(prepared["prepared"])
        if writer == "prepared"
        else rig.svc.index_document(
            rig.document_id, rig.collection_id, force_reindex=True
        )
    )
    assert result["status"] == (
        "cleared" if writer == "cleared" else "success"
    ), result
    rig.svc._quarantine_corrupt_index.assert_called_once()
    roots = [
        candidate
        for candidate in path.parent.glob(path.name + ".corrupt-*")
        if candidate.is_file()
    ]
    assert len(roots) == 1
    assert segment_directory(roots[0]).is_dir()
    assert roots[0].read_bytes() == original_root
    current = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    rows = {row.id for row in rig.session.query(DocumentChunk)}
    assert set(current.live_ids()) == rows
    assert rows.isdisjoint(original_ids)
    assert rig.svc.integrity_manager.verify_file(path, force=True)[0]
    hits = rig.svc.search(harness.SECRET, rig.collection_id, top_k=1)
    assert bool(hits) == (writer != "cleared")
    assert not rig.session.info.get("ldr_vector_transactions")


def test_component_io_failure_preserves_the_index(rig, monkeypatch, mocker):
    path = rig.index_path()
    original = path.read_bytes()
    rows = {row.id for row in rig.session.query(DocumentChunk)}
    quarantine = mocker.spy(rig.svc, "_quarantine_corrupt_index")

    def unavailable(_path):
        raise OSError("transient component I/O failure")

    with monkeypatch.context() as patch:
        patch.setattr(segmented_faiss, "_digest", unavailable)
        result = rig.svc.index_document(
            rig.document_id, rig.collection_id, force_reindex=True
        )
    assert result == {"status": "error", "error": "Operation failed: OSError"}
    quarantine.assert_not_called()
    assert path.read_bytes() == original
    index = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    assert set(index.live_ids()) == rows
    assert rig.svc.search(harness.SECRET, rig.collection_id, top_k=1)


def test_component_recovery_retries_only_once(rig, monkeypatch, mocker):
    attempts = []

    def corrupt(_index, _session):
        attempts.append(True)
        raise VectorStoreIntegrityError("persistent component failure")

    monkeypatch.setattr(VectorIndex, "_prepare_cleanup", corrupt)
    quarantine = mocker.spy(rig.svc, "_quarantine_corrupt_index")
    result = rig.svc.index_document(
        rig.document_id, rig.collection_id, force_reindex=True
    )
    assert result == {
        "status": "error",
        "error": "Operation failed: VectorStoreIntegrityError",
    }
    assert len(attempts) == 2
    quarantine.assert_called_once()


def test_prepared_recovery_reloads_the_source(rig, mocker):
    prepared = rig.svc._prepare_document(
        rig.document_id, rig.collection_id, True
    )
    assert prepared["status"] == "prepared", prepared
    index = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    entry = index._store._state()[0]
    component = segment_directory(rig.index_path()) / entry["file"]
    component.write_bytes(component.read_bytes() + b"corruption")
    reset = rig.svc._reset_index_state_for_rebuild
    replacement = "Updated source text during component recovery"

    def change_after_reset(*args):
        reset(*args)
        rig.session.get(Document, rig.document_id).text_content = replacement
        rig.session.commit()

    mocker.patch.object(
        rig.svc,
        "_reset_index_state_for_rebuild",
        side_effect=change_after_reset,
    )
    result = rig.svc._write_prepared_document(prepared["prepared"])

    assert result["status"] == "success", result
    hits = rig.svc.search(replacement, rig.collection_id, top_k=1)
    assert hits and hits[0].text == replacement
    assert all(
        harness.SECRET not in row.chunk_text
        for row in rig.session.query(DocumentChunk)
    )


def test_empty_live_membership_preserves_unknown_legacy_rows(rig, mocker):
    index = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    rig.session.query(DocumentChunk).update(
        {DocumentChunk.vector_index_key: None}
    )
    rig.session.commit()
    mocker.patch.object(rig.svc, "_get_vector_index", return_value=index)
    mocker.patch.object(index, "live_ids", return_value=[])
    cleanup = mocker.spy(index, "compact_orphans")

    result = rig.svc.reconcile_collection_index(rig.collection_id)

    assert result["reconciliation_skipped"] is True
    cleanup.assert_not_called()
    assert rig.svc.has_indexed_documents(rig.collection_id)
    assert rig.session.query(DocumentChunk).count() > 0
    assert all(
        row.vector_index_key is None for row in rig.session.query(DocumentChunk)
    )


@pytest.mark.parametrize("writer", ["serial", "prepared"])
def test_component_recovery_reloads_metadata_in_encrypted_sessions(
    tmp_path, monkeypatch, writer
):
    from tests.research_library.services.test_collection_rebuild import (
        RebuildWorld,
    )

    world = RebuildWorld(
        tmp_path, monkeypatch, encrypted=True, real_integrity=True
    )
    try:
        with world.service() as service:
            document_id = world.doc_info[0][0]
            index = service._get_vector_index(
                world.collection_id, world.collection_name
            )
            if writer == "prepared":
                prepared = service._prepare_document(
                    document_id, world.collection_id, True
                )
                assert prepared["status"] == "prepared", prepared
            entry = index._store._state()[0]
            component = segment_directory(index._path) / entry["file"]
            component.write_bytes(component.read_bytes() + b"corruption")

            result = (
                service._write_prepared_document(prepared["prepared"])
                if writer == "prepared"
                else service.index_document(
                    document_id, world.collection_id, force_reindex=True
                )
            )

            assert result["status"] == "success", result
            assert service.search("Original text", world.collection_id, top_k=1)
            assert service.integrity_manager.verify_file(
                index._path, force=True
            )[0]
    finally:
        world.engine.dispose()


def _payload(root):
    directory = segment_directory(root)
    directory.mkdir()
    for suffix in (".faiss", ".ids"):
        (directory / ("a" * 32 + suffix)).write_bytes(b"synthetic vectors")
    return directory


def test_quarantine_retention_removes_complete_generations(tmp_path):
    service = LibraryRAGService.__new__(LibraryRAGService)
    path = tmp_path / "index.faiss"
    for _ in range(_QUARANTINE_KEEP_RECENT + 2):
        path.write_bytes(b"synthetic manifest")
        _payload(path)
        service._quarantine_corrupt_index(path, "synthetic review")
    roots = {
        candidate
        for candidate in tmp_path.glob("index.faiss.corrupt-*")
        if candidate.is_file()
    }
    payloads = set(tmp_path.glob("index.faiss.corrupt-*.segments"))
    assert len(roots) == _QUARANTINE_KEEP_RECENT
    assert payloads == {segment_directory(root) for root in roots}


def test_retention_sweeps_orphan_payloads_and_refuses_symlinks(tmp_path):
    service = LibraryRAGService.__new__(LibraryRAGService)
    path = tmp_path / "index.faiss"
    orphan = _payload(tmp_path / "index.faiss.corrupt-1")
    unrelated = _payload(tmp_path / "other.faiss.corrupt-1")
    outside = tmp_path / "outside"
    outside.mkdir()
    sentinel = outside / ("a" * 32 + ".faiss")
    sentinel.write_bytes(b"unrelated vectors")
    link = tmp_path / "index.faiss.corrupt-2.segments"
    link.symlink_to(outside, target_is_directory=True)
    guarded = _payload(tmp_path / "index.faiss.corrupt-3")
    (guarded / "unrelated.txt").write_text("keep")

    service._prune_old_quarantined_files(path)

    assert not orphan.exists()
    assert unrelated.is_dir()
    assert link.is_symlink()
    assert sentinel.read_bytes() == b"unrelated vectors"
    assert (guarded / "unrelated.txt").read_text() == "keep"


def test_quarantine_completes_when_segment_relocation_is_refused(tmp_path):
    """A ValueError from ``relocate_segments`` (its refusal guard) must not
    abort the quarantine mid-way: the corrupt root is preserved under its
    quarantine name and the plaintext ``.pkl`` removal is still not optional.
    """
    service = LibraryRAGService.__new__(LibraryRAGService)
    path = tmp_path / "index.faiss"
    path.write_bytes(b"synthetic manifest")
    pkl_path = path.with_suffix(".pkl")
    pkl_path.write_bytes(b"legacy plaintext docstore")
    outside = tmp_path / "outside"
    outside.mkdir()
    sentinel = outside / ("a" * 32 + ".faiss")
    sentinel.write_bytes(b"sentinel vectors")
    # A symlinked segments directory trips relocate_segments' refusal guard —
    # the exact ValueError that used to abort the quarantine mid-way.
    link = segment_directory(path)
    link.symlink_to(outside, target_is_directory=True)

    service._quarantine_corrupt_index(path, "synthetic review")

    roots = [
        candidate
        for candidate in tmp_path.glob("index.faiss.corrupt-*")
        if candidate.is_file()
    ]
    assert [root.read_bytes() for root in roots] == [b"synthetic manifest"]
    assert not pkl_path.exists()  # plaintext removal is not optional
    # The refusal guard itself is not weakened: nothing behind the link moved.
    assert link.is_symlink()
    assert sentinel.read_bytes() == b"sentinel vectors"


def test_quarantine_removes_plaintext_when_relocation_destination_exists(
    tmp_path, monkeypatch
):
    """A pre-existing relocation destination trips ``relocate_segments``'
    "Vector segment relocation destination already exists" ValueError — the
    exact escape that used to abort the quarantine mid-way and skip the
    plaintext ``.pkl`` removal ("Plaintext removal is not optional").
    """
    import time as time_module

    service = LibraryRAGService.__new__(LibraryRAGService)
    path = tmp_path / "index.faiss"
    path.write_bytes(b"synthetic manifest")
    pkl_path = path.with_suffix(".pkl")
    pkl_path.write_bytes(b"legacy plaintext docstore")
    segments = segment_directory(path)
    segments.mkdir()
    (segments / ("a" * 32 + ".faiss")).write_bytes(b"generated segment")
    # Freeze the quarantine timestamp so the collision is deterministic: a
    # manually placed destination directory already exists at exactly the
    # name the quarantine would pick.
    monkeypatch.setattr(time_module, "time_ns", lambda: 123456789)
    collision = segment_directory(tmp_path / "index.faiss.corrupt-123456789")
    collision.mkdir()

    service._quarantine_corrupt_index(path, "synthetic review")

    roots = [
        candidate
        for candidate in tmp_path.glob("index.faiss.corrupt-*")
        if candidate.is_file()
    ]
    assert [root.read_bytes() for root in roots] == [b"synthetic manifest"]
    assert not pkl_path.exists()  # plaintext removal is not optional
    # The refused relocation left the segment payloads in place (never lost).
    assert (
        segments / ("a" * 32 + ".faiss")
    ).read_bytes() == b"generated segment"
    assert collision.is_dir()