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.

18.1 KB
Raw Blame
"""Tests for ``LibraryRAGService.reconcile_collection_index``.

Pins the safety invariants added in PR #5235 (review comment 5085604502):

* If ``live_ids()`` is empty but ``DocumentChunk`` rows exist, the
  reconciler must REFUSE to shrink state. It returns
  ``{"reconciliation_skipped": True, ...}`` and the caller surfaces a
  task failure rather than clearing every indexed flag and
  ``RagDocumentStatus`` row.
* ``RagDocumentStatus.indexed_at`` is preserved for documents that
  already had a status row; only newly durable documents get the
  current timestamp.

Also pins a PR #5284 review follow-up: unlike every other test above (which
stubs ``_get_vector_index`` wholesale), ``TestReconcileDimensionDrift`` below
lets the REAL ``_get_vector_index`` / ``_preflight_index_path`` chain run, so
it actually exercises the ``probe_dimension=True`` this call site passes --
reconcile never embeds a query itself, so it has no ``_prepare`` of its own
to catch an embedding-provider dimension drift for it.
"""

from datetime import datetime, UTC
from unittest.mock import MagicMock, patch

import pytest

_MOD = "local_deep_research.research_library.services.library_rag_service"


def _make_service(**overrides):
    with (
        patch(f"{_MOD}.LocalEmbeddingManager") as _lem,
        patch(f"{_MOD}.get_user_db_session"),
        patch(f"{_MOD}.FileIntegrityManager"),
        patch(f"{_MOD}.get_text_splitter"),
    ):
        _lem.return_value.embeddings = MagicMock()
        from local_deep_research.research_library.services.library_rag_service import (
            LibraryRAGService,
        )

        defaults = dict(username="testuser", db_password="pw")
        defaults.update(overrides)
        return LibraryRAGService(**defaults)


def _make_session_ctx(session):
    ctx = MagicMock()
    ctx.__enter__ = MagicMock(return_value=session)
    ctx.__exit__ = MagicMock(return_value=None)
    return ctx


def _build_query_mock(*, all_rows=None, first_row=None):
    """Return a MagicMock suitable for ``session.query(<Model>)`` chains.

    ``all_rows`` is returned by ``.all()``; ``first_row`` is returned by
    ``.first()``. ``.filter_by(...)`` / ``.filter(...)`` simply return the
    same mock so the chain resolves. ``.delete(...)`` returns the mock so
    the bulk-delete call chain resolves.
    """
    q = MagicMock()
    q.filter_by.return_value = q
    q.filter.return_value = q
    q.delete.return_value = q
    if all_rows is not None:
        q.all.return_value = all_rows
        q.first.return_value = all_rows[0] if all_rows else None
    if first_row is not None:
        q.first.return_value = first_row
    return q


def _make_status_row(document_id, indexed_at):
    """Build a mock that supports ``row.document_id`` / ``row.indexed_at``.

    Production code uses both attribute access (the prior-status query
    builds a dict via ``row.document_id``) and tuple unpacking (the
    chunk-row query does ``for chunk_id, source_id in rows``), so the
    mock must support both.
    """
    row = MagicMock()
    row.document_id = document_id
    row.indexed_at = indexed_at
    # Tuple-unpacking support: the chunk-row query does
    # ``for chunk_id, source_id in rows``. Status rows are unpacked the
    # same way, so this is harmless if not used.
    row.__iter__ = lambda self: iter((document_id, indexed_at))
    return row


def _make_chunk_row(chunk_id, source_id):
    """Build a mock that supports ``row.id`` / ``row.source_id`` and tuple
    unpacking (``for chunk_id, source_id in rows``).
    """
    row = MagicMock()
    row.id = chunk_id
    row.source_id = source_id
    row.__iter__ = lambda self: iter((chunk_id, source_id, None))
    return row


@pytest.fixture(autouse=True)
def _reset_locks():
    """Make sure each test sees a clean global lock state."""
    from local_deep_research.research_library.services import (
        library_rag_service as svc,
    )

    with svc._faiss_write_locks_lock:
        svc._faiss_write_locks.clear()
        svc._faiss_active_lock_keys.clear()
    yield
    with svc._faiss_write_locks_lock:
        svc._faiss_write_locks.clear()
        svc._faiss_active_lock_keys.clear()


class TestReconcileRefuseToShrink:
    """When ``live_ids()`` is empty but chunk rows exist, refuse to clear."""

    def test_empty_live_ids_with_chunk_rows_returns_skipped(self, tmp_path):
        svc = _make_service()
        # Provide a rag_index_record so the reconciler can read its id.
        svc.rag_index_record = MagicMock()
        svc.rag_index_record.id = 7

        # Stub the vector index to report ZERO live ids (transient store
        # fault — quarantine rebuilt the index but the in-memory handle
        # was empty).
        vindex = MagicMock()
        vindex.live_ids.return_value = []
        with patch.object(svc, "_get_vector_index", return_value=vindex):
            # Stub the DB session: DocumentChunk returns rows so the
            # chunk-set is non-empty (the "would clear" trigger).
            chunk_q = _build_query_mock(
                all_rows=[
                    _make_chunk_row(1, "doc-a"),
                    _make_chunk_row(2, "doc-a"),
                    _make_chunk_row(3, "doc-b"),
                ]
            )
            # The reconciler returns early BEFORE the prior-status /
            # document-collection / rag-index queries fire, so we don't
            # need to wire those up — the chunk query above is the only
            # session.query() call that runs.
            session = MagicMock()
            session.query.return_value = chunk_q
            with patch(
                f"{_MOD}.get_user_db_session",
                return_value=_make_session_ctx(session),
            ):
                result = svc.reconcile_collection_index("coll-1")

        assert result["reconciliation_skipped"] is True
        assert result["indexed_documents"] == 0
        assert result["indexed_chunks"] == 0
        assert result["live_vectors"] == 0
        assert "refusing to clear" in result["reason"].lower()
        # The refuse-to-shrink invariant: the early return path MUST NOT
        # have committed anything (no commit, no bulk delete).
        assert not session.commit.called
        assert not session.delete.called

    def test_empty_live_ids_with_no_chunk_rows_proceeds_normally(
        self, tmp_path
    ):
        """A genuinely empty collection (no chunks) is NOT a transient
        fault — the reconciler proceeds and returns a zeroed result.
        """
        svc = _make_service()
        svc.rag_index_record = MagicMock()
        svc.rag_index_record.id = 7

        vindex = MagicMock()
        vindex.live_ids.return_value = []
        with patch.object(svc, "_get_vector_index", return_value=vindex):
            # No chunk rows; the empty result still triggers the
            # follow-up queries, but they all return empty too.
            empty_q = _build_query_mock(all_rows=[])
            session = MagicMock()
            session.query.return_value = empty_q
            with patch(
                f"{_MOD}.get_user_db_session",
                return_value=_make_session_ctx(session),
            ):
                result = svc.reconcile_collection_index("coll-1")

        assert "reconciliation_skipped" not in result
        assert result["indexed_documents"] == 0
        assert result["live_vectors"] == 0


class TestReconcilePreservesIndexedAt:
    """``RagDocumentStatus.indexed_at`` is preserved for durable docs."""

    def test_durable_doc_keeps_prior_indexed_at(self):
        svc = _make_service()
        svc.rag_index_record = MagicMock()
        svc.rag_index_record.id = 7

        vindex = MagicMock()
        vindex.live_ids.return_value = [10, 11, 12]
        with patch.object(svc, "_get_vector_index", return_value=vindex):
            # Two chunk rows for doc-a, one for doc-b. Both durable.
            chunk_q = _build_query_mock(
                all_rows=[
                    _make_chunk_row(10, "doc-a"),
                    _make_chunk_row(11, "doc-a"),
                    _make_chunk_row(12, "doc-b"),
                ]
            )
            # doc-a had an existing status row with indexed_at=t0
            t0 = datetime(2024, 1, 1, tzinfo=UTC)
            prior_status_q = _build_query_mock(
                all_rows=[_make_status_row("doc-a", t0)]
            )
            empty_q = _build_query_mock(all_rows=[])
            rag_index_q = _build_query_mock()
            session = MagicMock()
            # Order of session.query() calls in production:
            #   0. Current generation lookup for the collection lock
            #   1. RAGIndex no-op write claim (rag_index_q)
            #   2. DocumentChunk rows (chunk_q)
            #   3. RagDocumentStatus prior indexed_at (prior_status_q)
            #   4. DocumentCollection links (empty_q)
            #   5. RagDocumentStatus bulk delete (empty_q — same shape)
            #   6. RAGIndex row (rag_index_q)
            session.query.side_effect = [
                _build_query_mock(first_row=MagicMock(index_hash="generation")),
                rag_index_q,  # SQLite write claim before reading chunks
                chunk_q,
                prior_status_q,
                empty_q,
                empty_q,
                rag_index_q,
            ]
            with patch(
                f"{_MOD}.get_user_db_session",
                return_value=_make_session_ctx(session),
            ):
                result = svc.reconcile_collection_index("coll-1")

        assert result["indexed_documents"] == 2
        assert result["indexed_chunks"] == 3

        # Inspect the RagDocumentStatus rows that were added to the
        # session: doc-a must keep indexed_at=t0, doc-b must use ``now``.
        added = []
        for call in session.add.call_args_list:
            if not call.args:
                continue
            obj = call.args[0]
            if getattr(obj, "document_id", None) in ("doc-a", "doc-b"):
                added.append(obj)
        by_doc = {row.document_id: row for row in added}
        assert "doc-a" in by_doc, "doc-a must have a status row added"
        assert "doc-b" in by_doc, "doc-b must have a status row added"
        assert by_doc["doc-a"].indexed_at == t0, (
            "doc-a must keep its original indexed_at"
        )
        assert by_doc["doc-b"].indexed_at != t0, (
            "doc-b had no prior row, so it must get the current timestamp"
        )


class TestReconcilePartialDurabilityAndOrphans:
    """Reconciler requires ALL doc chunks in live_ids and counts orphan_vectors."""

    def test_partial_durability_marks_doc_not_durable(self):
        """If doc-a has chunks [10, 11] but live_ids only has [10], doc-a is NOT durable."""
        svc = _make_service()
        svc.rag_index_record = MagicMock()
        svc.rag_index_record.id = 7

        vindex = MagicMock()
        vindex.live_ids.return_value = [10]  # Missing chunk 11
        with patch.object(svc, "_get_vector_index", return_value=vindex):
            chunk_q = _build_query_mock(
                all_rows=[
                    _make_chunk_row(10, "doc-a"),
                    _make_chunk_row(11, "doc-a"),
                ]
            )
            empty_q = _build_query_mock(all_rows=[])
            rag_index_q = _build_query_mock()
            session = MagicMock()
            session.query.side_effect = [
                _build_query_mock(first_row=MagicMock(index_hash="generation")),
                rag_index_q,  # SQLite write claim before reading chunks
                chunk_q,
                empty_q,
                empty_q,
                empty_q,
                rag_index_q,
            ]
            with patch(
                f"{_MOD}.get_user_db_session",
                return_value=_make_session_ctx(session),
            ):
                result = svc.reconcile_collection_index("coll-1")

        assert result["indexed_documents"] == 0
        assert result["indexed_chunks"] == 0
        assert result["live_vectors"] == 1

    def test_orphan_vectors_counted_when_live_ids_has_extra_vectors(self):
        """Vectors in live_ids with no matching DocumentChunk DB row are counted as orphans."""
        svc = _make_service()
        svc.rag_index_record = MagicMock()
        svc.rag_index_record.id = 7

        vindex = MagicMock()
        vindex.live_ids.return_value = [10, 999]  # 999 is an orphan
        with patch.object(svc, "_get_vector_index", return_value=vindex):
            chunk_q = _build_query_mock(
                all_rows=[
                    _make_chunk_row(10, "doc-a"),
                ]
            )
            empty_q = _build_query_mock(all_rows=[])
            rag_index_q = _build_query_mock()
            session = MagicMock()
            session.query.side_effect = [
                _build_query_mock(first_row=MagicMock(index_hash="generation")),
                rag_index_q,  # SQLite write claim before reading chunks
                chunk_q,
                empty_q,
                empty_q,
                empty_q,
                rag_index_q,
            ]
            with patch(
                f"{_MOD}.get_user_db_session",
                return_value=_make_session_ctx(session),
            ):
                result = svc.reconcile_collection_index("coll-1")

        assert result["indexed_documents"] == 1
        assert result["indexed_chunks"] == 1
        assert result["live_vectors"] == 2
        assert result["orphan_vectors"] == 1


class TestReconcileDimensionDrift:
    """R3 (PR #5284 review follow-up): ``reconcile_collection_index`` is a
    SECOND ``reset_stale_state=False`` call site (besides ``search()``), but
    unlike ``search()`` it never embeds a query -- it only calls
    ``live_ids()`` -- so it gets none of the free fail-closed dimension
    check ``FaissVectorStore._prepare`` gives a real search. Without
    explicitly opting back in, an embedding-provider dimension drift (e.g.
    repointing an OpenAI-compatible ``base_url`` from a local server to a
    cloud one under the same model name -- the one drift PR #5284's gate
    review found genuinely reachable) would go completely undetected here:
    the stamped ``embedding_dimension`` still matches the stale on-disk
    file, and a non-empty ``live_ids()`` means the refuse-to-shrink guard
    below never fires either. ``_get_vector_index`` now passes
    ``probe_dimension=True`` so ``_preflight_index_path`` runs the SAME
    probe-and-rebuild-on-drift logic a WRITE path uses (see
    ``TestPreflightDimensionMismatch`` in
    ``test_library_rag_service_coverage.py`` for that logic's own direct
    coverage, which this test's setup mirrors).

    Unlike every other test in this module, this one does NOT stub
    ``_get_vector_index`` -- doing so would bypass the very
    ``probe_dimension`` plumbing under test. Only ``_get_or_create_rag_index``
    and the ``VectorIndex`` facade construction are stubbed, exactly as
    ``test_search_skips_dimension_probe_on_read_path`` does for the
    ``search()`` read path.
    """

    def _make_present_index(self, tmp_path, stored_dim=384):
        index_path = tmp_path / "hash.faiss"
        index_path.write_bytes(b"faiss-bytes")
        rag_index = MagicMock(
            id=42,
            index_hash="hash",
            index_path=str(index_path),
            embedding_dimension=stored_dim,
            chunk_count=5,
            total_documents=1,
        )
        return index_path, rag_index

    @patch(f"{_MOD}.get_user_db_session")
    @patch(f"{_MOD}.VectorIndex")
    def test_reconcile_probes_dimension_and_reports_skip_on_drift(
        self, mock_vector_index, mock_session_ctx, tmp_path
    ):
        svc = _make_service()
        svc._get_index_path = MagicMock(return_value=tmp_path / "hash.faiss")
        index_path, rag_index = self._make_present_index(
            tmp_path, stored_dim=384
        )
        svc._get_or_create_rag_index = MagicMock(return_value=rag_index)
        svc.integrity_manager.verify_file.return_value = (True, None)
        svc.embedding_manager = MagicMock()
        # The live embedding provider now returns 768-dim vectors -- drifted
        # from the 384 stamped on the RAGIndex row / stale on-disk file.
        svc.embedding_manager.embeddings.embed_query.return_value = [0.1] * 768

        session = MagicMock()
        mock_session_ctx.return_value.__enter__.return_value = session
        db_row = MagicMock(embedding_dimension=384)
        # Shared query mock: `.first()` serves `_preflight_index_path`'s own
        # RAGIndex dimension update; `.all()` serves reconcile's own
        # DocumentChunk rows query below -- a chunk row still on record for
        # a document that was indexed under the OLD, pre-drift dimension.
        q = session.query.return_value.filter_by.return_value
        q.first.return_value = db_row
        # The chunk query also keeps only rows whose document still exists.
        q.filter.return_value = q
        q.all.return_value = [(101, "doc-1", None)]

        svc._reset_index_state_for_rebuild = MagicMock()
        # After a drift-triggered rebuild, the facade's live_ids() comes
        # back empty (a freshly (re)created index has nothing in it yet).
        mock_vector_index.return_value.live_ids.return_value = []

        result = svc.reconcile_collection_index("col-1")

        # The probe fired and detected drift: the SAME rebuild machinery a
        # WRITE path uses on a dimension mismatch (see
        # test_preflight_dimension_mismatch_rebuilds_and_updates_dimension_on_write_path
        # in test_library_rag_service_coverage.py). These are the
        # falsifying assertions: revert the `probe_dimension=True` plumbing
        # and every one of them fails (the probe never runs).
        svc.embedding_manager.embeddings.embed_query.assert_called_once_with(
            "dimension_check"
        )
        assert not index_path.exists()
        assert db_row.embedding_dimension == 768
        assert rag_index.embedding_dimension == 768
        svc._reset_index_state_for_rebuild.assert_any_call("col-1", rag_index)
        # Reconcile itself refuses to proceed once the rebuilt index comes
        # back empty while chunk rows still exist for the collection --
        # reported the same way a transient store fault is: skipped, not a
        # silent mass-clear of every indexed flag.
        assert result["reconciliation_skipped"] is True