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.

19.3 KB
Raw Blame
"""Text replacement and index persistence serialize across real SQLite sessions."""

from contextlib import nullcontext
from types import SimpleNamespace

import pytest
from sqlalchemy import event
from sqlalchemy.exc import OperationalError

from local_deep_research.database.models.library import (
    Document,
    DocumentChunk,
    DocumentCollection,
    EmbeddingProvider,
    RAGIndex,
    RagDocumentStatus,
)
from local_deep_research.database.models.research import ResearchResource
from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
)
from local_deep_research.vector_stores.facade import VectorIndex
from tests.research_library.services.test_download_text_quality import (
    LONGER,
    ORIGINAL,
    extraction_case as extraction_case,
)


class MemoryVectors:
    """Control vector membership while retaining the real chunk SQL facade."""

    dimension = 2

    def __init__(self, ids):
        self.ids = set(ids)
        self.removed = set()

    def live_ids(self):
        return list(self.ids)

    def iter_id_batches(self):
        ids = self.live_ids()
        for start in range(0, len(ids), 500):
            yield ids[start : start + 500]

    def apply(self, *, add_ids, add_vectors, remove_ids):
        self.ids.difference_update(remove_ids)
        self.ids.update(add_ids)
        self.removed.update(remove_ids)
        return {"added": len(add_ids), "removed": len(remove_ids)}


def indexer_for(case, collection="library"):
    """Build the fake indexer double used by the serialization tests.

    The double is a ``LibraryRAGService`` built with ``__new__`` (no
    ``__init__``): only the collaborators set in ``__dict__`` below are
    faked, and every other method is the production one.

    Premise change (#7557, PR 7759): ``_prepare_document`` used to
    snapshot ``status_exists`` against the coordinator's
    ``rag_index_record``. It now resolves the index through its OWN
    session (the coordinator's ORM record may expire mid-run and a pool
    worker must not refresh it), via the real
    ``_resolve_index_for_config`` -> ``_find_matching_rag_index``, and
    both sides of the prepare/write status comparison measure through the
    real ``_status_row_exists`` against that resolved identity.
    ``_get_index_hash`` maps ``collection_<id>`` to the ``index_hash``
    extraction_case seeds (the collection id), so
    ``_find_matching_rag_index`` returns at its hash lookup and its
    configuration-matching fallback (``_matches_current_index_configuration``)
    never runs. ``_matches_collection_configuration`` is never called
    either: its caller skips it because the seeded collections store no
    ``embedding_model``. So the
    double sets none of the configuration attributes (``chunk_size``,
    ``chunk_overlap``, ``splitter_type``, ``text_separators``,
    ``distance_metric``, ``normalize_vectors``, ``index_type``) that
    ``__init__`` would. ``rag_index_record`` stands in for the record the
    real ``_get_vector_index`` (faked here) assigns. The prepared write's
    persist step reads it after that call. The serial path
    (``_index_document_locked``) also reads it for its own status snapshot
    and gate. The tests' staleness and invalidation premises are unchanged.
    """
    with case.sessions() as session:
        index_id, index_key = (
            session.query(RAGIndex.id, RAGIndex.index_hash)
            .filter_by(collection_name=f"collection_{collection}")
            .one()
        )
        chunk_ids = [
            row[0]
            for row in session.query(DocumentChunk.id).filter_by(
                collection_name=f"collection_{collection}"
            )
        ]
    backend = VectorIndex.__new__(VectorIndex)
    backend.collection_name = f"collection_{collection}"
    backend.index_key = index_key
    backend.require_document_membership = True
    backend.membership_collection_id = collection
    backend.embedding_model = "test"
    backend.embedding_model_type = EmbeddingProvider.SENTENCE_TRANSFORMERS
    backend._store = MemoryVectors(chunk_ids)
    obj = LibraryRAGService.__new__(LibraryRAGService)
    obj.__dict__.update(
        username=case.service.username,
        _db_password=case.service.password,
        rag_index_record=SimpleNamespace(id=index_id),
        embedding_model="test",
        embedding_provider="sentence_transformers",
        _collection_transaction_lock=lambda coll: nullcontext(),
        _get_vector_index=lambda *a, **kw: backend,
        _get_index_hash=lambda coll, *args: coll.removeprefix("collection_"),
        _get_index_path=lambda h: case.service.library_root / h,
        text_splitter=SimpleNamespace(split_documents=lambda docs: docs),
        _embed_texts_cancellable=lambda texts, cancelled: [
            [1.0, 0.0] for _ in texts
        ],
        embedding_manager=SimpleNamespace(
            embeddings=SimpleNamespace(
                embed_documents=lambda texts: [[1.0, 0.0] for _ in texts]
            )
        ),
    )
    return obj, backend


def replace_text(case, *, text=LONGER, immediate=False):
    with case.sessions() as writer:
        if immediate:
            writer.connection().exec_driver_sql("PRAGMA busy_timeout=0")
        resource = writer.get(ResearchResource, case.resource_id)
        case.service._save_text_with_db(
            resource, text, writer, "html_extraction", "arxiv_html", "canonical"
        )
        writer.commit()


def persist(indexer, mode):
    if mode == "prepared":
        prep = LibraryRAGService._prepare_document(
            indexer, "canonical", "library", True
        )
        assert prep["status"] == "prepared", prep
        return LibraryRAGService._write_prepared_document(
            indexer, prep["prepared"]
        )
    return LibraryRAGService._index_document_locked(
        indexer, "canonical", "library", True
    )


def assert_pending(case, collection="library"):
    with case.sessions() as session:
        assert session.get(Document, "canonical").text_content == LONGER
        assert (
            not session.query(DocumentCollection)
            .filter_by(document_id="canonical", collection_id=collection)
            .one()
            .indexed
        )
        assert session.get(RagDocumentStatus, ("canonical", collection)) is None


@pytest.mark.parametrize("mode", ["prepared", "serial"])
def test_reconciliation_preserves_invalidation_until_replacement(
    extraction_case, mode
):
    """Old vectors stay invalidated until the replacement is indexed.

    Premise note (#7557, PR 7759): mode="prepared" used to snapshot status
    existence against the coordinator's ``rag_index_record``; preparation
    now resolves the index through its own worker-owned session (the
    coordinator's ORM record may expire mid-run and must not be refreshed
    by a worker), so the ``indexer_for`` double provides that seam. The
    premise tested here — a text replacement keeps the old chunks
    invalidated until the replacement commit lands — is unchanged.
    """
    case = extraction_case(quality="low", incoming=LONGER)
    indexer, backend = indexer_for(case)
    old_ids = backend._store.ids.copy()
    assert case.service.download_resource(case.resource_id) == (True, None)
    assert_pending(case)
    result = LibraryRAGService.reconcile_collection_index(indexer, "library")
    assert result["indexed_documents"] == 0
    assert_pending(case)
    with case.sessions() as session:
        chunk = (
            session.query(DocumentChunk)
            .filter_by(collection_name="collection_library")
            .one()
        )
        assert chunk.chunk_text == ORIGINAL
        assert chunk.id in old_ids
    assert persist(indexer, mode)["status"] == "success"
    assert backend._store.removed == old_ids
    assert old_ids.isdisjoint(backend._store.ids)
    result = LibraryRAGService.reconcile_collection_index(indexer, "library")
    assert result["indexed_documents"] == 1
    with case.sessions() as session:
        assert (
            session.query(DocumentChunk)
            .filter_by(collection_name="collection_library")
            .one()
            .chunk_text
            == LONGER
        )
        assert (
            session.query(DocumentCollection)
            .filter_by(document_id="canonical", collection_id="library")
            .one()
            .indexed
        )
        status = session.get(RagDocumentStatus, ("canonical", "library"))
        index = session.get(RAGIndex, status.rag_index_id)
        assert (
            index.total_documents
            == index.chunk_count
            == status.chunk_count
            == 1
        )


@pytest.mark.parametrize("mode", ["prepared", "serial"])
def test_text_change_after_validation_is_detected_before_vector_writes(
    extraction_case, mode
):
    """A text change between validation and vector writes aborts as stale.

    Premise note (#7557, PR 7759): the prepared path's status snapshot now
    goes through worker-session index resolution instead of the
    coordinator's ``rag_index_record`` (see ``indexer_for``) because
    preparation must use worker-owned sessions. Staleness detection
    itself — what this test forces into the validation-to-write window —
    is unchanged.
    """
    case = extraction_case(quality="low")
    indexer, backend = indexer_for(case)
    before = backend._store.ids.copy()

    def initialize(*args, **kwargs):
        # Real index initialization can commit the shared session. A claim
        # taken before this call would not protect subsequent persistence.
        case.shared.commit()
        replace_text(case)
        return backend

    indexer._get_vector_index = initialize
    result = persist(indexer, mode)
    assert result["status"] == "stale", result
    assert_pending(case)
    assert backend._store.ids == before
    assert not backend._store.removed


@pytest.mark.parametrize(
    "flip", ["membership", "status_row", "status_row_rekeyed"]
)
def test_status_flip_between_prepare_and_write_aborts_as_stale(
    extraction_case, flip
):
    """A concurrent status flip during embedding must abort the write.

    The Phase-3 staleness gate snapshots the document's indexed state at
    prepare (``initial_indexed`` / ``status_exists``) and re-measures it at
    write time: when a concurrent commit flips that state mid-embed, the
    write must abort as ``stale`` and flag the document for reindex
    instead of silently overwriting the other writer's outcome. Each flip
    shape exercises one arm of the gate: a membership ``indexed`` flip, a
    ``RagDocumentStatus`` row appearing for the prepared index identity,
    and a row that existed throughout but keyed to another index of the
    same collection (an older embedding model's) being re-keyed to the
    prepared index, as a concurrent writer's ``session.merge`` does. Only a
    status predicate that measures the index identity sees that last flip.
    """
    case = extraction_case(quality="low", incoming=LONGER)
    indexer, backend = indexer_for(case)
    assert case.service.download_resource(case.resource_id) == (True, None)
    assert_pending(case)
    before = backend._store.ids.copy()
    with case.sessions() as session:
        # Derived from the seeded rows, not from preparation: extraction_case
        # also seeds the "selected" collection's index, a wrong but valid
        # identity for preparation to resolve.
        library_index_id = (
            session.query(RAGIndex.id)
            .filter_by(collection_name="collection_library")
            .scalar()
        )
        if flip == "status_row_rekeyed":
            old_index = RAGIndex(
                collection_name="collection_library",
                embedding_model="old-model",
                embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
                embedding_dimension=2,
                index_path=str(case.service.library_root / "old-model"),
                index_hash="library-old-model",
                chunk_size=100,
                chunk_overlap=0,
                is_current=False,
            )
            session.add(old_index)
            session.flush()
            session.add(
                RagDocumentStatus(
                    document_id="canonical",
                    collection_id="library",
                    rag_index_id=old_index.id,
                    chunk_count=1,
                )
            )
            session.commit()
            # A lagging coordinator record pointing at the old index: the
            # gate must still measure against prepared.status_index_id.
            indexer.rag_index_record = SimpleNamespace(id=old_index.id)

    prep = LibraryRAGService._prepare_document(
        indexer, "canonical", "library", True
    )
    assert prep["status"] == "prepared", prep
    assert prep["prepared"].status_index_id == library_index_id
    with case.sessions() as session:
        if flip == "membership":
            session.query(DocumentCollection).filter_by(
                document_id="canonical", collection_id="library"
            ).one().indexed = True
        elif flip == "status_row":
            session.add(
                RagDocumentStatus(
                    document_id="canonical",
                    collection_id="library",
                    rag_index_id=library_index_id,
                    chunk_count=1,
                )
            )
        else:
            session.get(
                RagDocumentStatus, ("canonical", "library")
            ).rag_index_id = library_index_id
        session.commit()

    result = LibraryRAGService._write_prepared_document(
        indexer, prep["prepared"]
    )
    assert result == {
        "status": "stale",
        "message": "Document indexed state changed during indexing, "
        "flagged for reindex",
    }
    # The abort's _flag_document_for_reindex undid the flip again.
    assert_pending(case)
    assert backend._store.ids == before
    assert not backend._store.removed


@pytest.mark.parametrize("mode", ["prepared", "serial"])
def test_text_writer_cannot_commit_between_claim_and_index_commit(
    extraction_case, mode
):
    """The claim fence blocks a competing writer until the index commit.

    Premise note (#7557, PR 7759): the prepared path's status snapshot now
    goes through worker-session index resolution instead of the
    coordinator's ``rag_index_record`` (see ``indexer_for``) because
    preparation must use worker-owned sessions. The claim fence this test
    exercises is unchanged.
    """
    case = extraction_case(quality="low")
    indexer, backend = indexer_for(case)
    real_persist = backend.index_prepared
    checked = []

    def write_with_competitor(**kwargs):
        with pytest.raises(OperationalError, match="locked"):
            replace_text(case, immediate=True)
        checked.append(True)
        return real_persist(**kwargs)

    backend.index_prepared = write_with_competitor
    result = persist(indexer, mode)
    assert result["status"] == "success", result
    assert checked == [True]
    # Once indexing commits, the same text update succeeds and invalidates
    # even the newly committed chunks. The claim is transaction-scoped.
    replace_text(case)
    LibraryRAGService.reconcile_collection_index(indexer, "library")
    assert_pending(case)


def test_reconcile_serializes_its_snapshot_with_text_replacement(
    extraction_case,
):
    case = extraction_case(quality="low")
    indexer, _ = indexer_for(case)
    checked = []

    def replace_after_chunks_read(_conn, _cursor, statement, *_args):
        if not checked and statement.startswith(
            "SELECT rag_document_status.document_id"
        ):
            checked.append(True)
            with pytest.raises(OperationalError, match="locked"):
                replace_text(case, immediate=True)

    event.listen(
        case.shared.bind, "before_cursor_execute", replace_after_chunks_read
    )
    try:
        result = LibraryRAGService.reconcile_collection_index(
            indexer, "library"
        )
    finally:
        event.remove(
            case.shared.bind, "before_cursor_execute", replace_after_chunks_read
        )
    assert checked == [True]
    assert result["indexed_documents"] == 1
    replace_text(case)
    LibraryRAGService.reconcile_collection_index(indexer, "library")
    assert_pending(case)


def test_metadata_recovery_without_text_replacement_still_works(
    extraction_case,
):
    case = extraction_case()
    indexer, _ = indexer_for(case)
    with case.sessions() as session:
        session.query(DocumentCollection).update({"indexed": False})
        session.query(RagDocumentStatus).delete()
        session.commit()
    result = LibraryRAGService.reconcile_collection_index(indexer, "library")
    assert result["indexed_documents"] == 1
    with case.sessions() as session:
        assert (
            session.get(RagDocumentStatus, ("canonical", "library")) is not None
        )


def test_chunk_invalidation_rolls_back_with_text_and_preserves_metadata(
    extraction_case,
):
    case = extraction_case(quality="low")
    with case.sessions() as session:
        for chunk in session.query(DocumentChunk):
            chunk.document_metadata = {"title": "Existing citation context"}
        session.commit()
        resource = session.get(ResearchResource, case.resource_id)
        case.service._save_text_with_db(
            resource,
            LONGER,
            session,
            "html_extraction",
            "arxiv_html",
            "canonical",
        )
        session.flush()
        assert all(
            chunk.document_metadata
            == {
                "title": "Existing citation context",
                "_ldr_text_invalidated": True,
            }
            for chunk in session.query(DocumentChunk)
        )
        session.rollback()
    with case.sessions() as session:
        assert session.get(Document, "canonical").text_content == ORIGINAL
        assert all(
            chunk.document_metadata == {"title": "Existing citation context"}
            for chunk in session.query(DocumentChunk)
        )
    replace_text(case)
    for collection in ["library", "selected"]:
        # Every collection, not just the resource's default one, stays pending.
        indexer, _ = indexer_for(case, collection)
        LibraryRAGService.reconcile_collection_index(indexer, collection)
        assert_pending(case, collection)


@pytest.mark.parametrize("mode", ["prepared", "serial"])
def test_failed_reindex_keeps_old_chunks_invalidated(extraction_case, mode):
    """A failed vector write leaves the old chunks invalidated and pending.

    Premise note (#7557, PR 7759): the prepared path's status snapshot now
    goes through worker-session index resolution instead of the
    coordinator's ``rag_index_record`` (see ``indexer_for``) because
    preparation must use worker-owned sessions. The failure-recovery
    premise — old chunks stay invalidated when the vector write raises —
    is unchanged.
    """
    case = extraction_case(quality="low")
    indexer, backend = indexer_for(case)
    replace_text(case)

    def fail_apply(**kwargs):
        raise RuntimeError("controlled vector failure")

    backend._store.apply = fail_apply
    result = persist(indexer, mode)
    assert result["status"] == "error", result
    LibraryRAGService.reconcile_collection_index(indexer, "library")
    assert_pending(case)
    with case.sessions() as session:
        assert (
            session.query(DocumentChunk)
            .filter_by(collection_name="collection_library")
            .one()
            .chunk_text
            == ORIGINAL
        )