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.

42.1 KB
Raw Blame
"""
Tests for LibraryRAGService.index_documents_parallel.

This is the new parallel fan-out that backs the six call sites which
previously indexed documents one at a time:

  * ``rag_routes.py::_background_index_worker`` (the user-facing
    "Index" button on /library/collections/<id>)
  * ``rag_routes.py::index_collection`` SSE generator
  * ``rag_routes.py::_auto_index_documents_worker`` (post-upload
    auto-index)
  * ``rag_routes.py::index_all`` SSE generator (replaces the
    pseudo-batch in ``index_documents_batch`` with a real bounded
    pool)
  * ``scheduler/background.py::_reconcile_unindexed_documents`` (two
    branches: in-collection and orphan ingest)

Each ``index_document`` worker opens its own DB session, so this
file mocks at the ``index_document`` boundary rather than mocking
SQLAlchemy, FAISS, embeddings, etc. The behaviour we care about is
that the parallel helper:

  * delegates to ``index_document`` exactly once per doc_id,
    deduplicating repeats in the input list,
  * aggregates per-doc results into the same
    ``{successful, skipped, failed, errors, results}`` shape that
    the SSE generators consume,
  * drives an optional progress callback once per completion in
    completion order (not submission order),
  * polls an optional ``is_cancelled`` callable between completions
    and shuts the executor down with ``wait=False,
    cancel_futures=True`` when cancelled,
  * clamps ``max_workers`` to ``>= 1`` so a bad setting can never
    explode the pool,
  * never lets one doc's failure kill the run.
"""

import threading
from concurrent.futures import ThreadPoolExecutor
from unittest.mock import MagicMock, patch

_MOD = "local_deep_research.research_library.services.library_rag_service"


def _make_service(**overrides):
    """Build a LibraryRAGService with all external deps stubbed.

    ``index_document`` is intentionally NOT mocked here — each test
    monkey-patches it onto the returned service, because the helper
    is what we're testing.
    """
    with (
        patch(f"{_MOD}.LocalEmbeddingManager") as _lem,
        patch(f"{_MOD}.get_user_db_session"),
        patch(f"{_MOD}.FileIntegrityManager") as _fim,
        patch(f"{_MOD}.get_text_splitter") as _gts,
    ):
        _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)


class TestEmptyInput:
    def test_empty_list_returns_zero_counts(self):
        """No docs → all counters at 0, results dict empty, not cancelled."""
        svc = _make_service()
        # Mock index_document so we can also assert it was never called
        # for the empty-input case (the helper short-circuits before the
        # dispatch loop).
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            return_value={"status": "success", "chunk_count": 0}
        )
        result = svc.index_documents_parallel(
            [],
            "coll-1",
            force_reindex=False,
            max_workers=4,
        )
        assert result["successful"] == 0
        assert result["skipped"] == 0
        assert result["failed"] == 0
        assert result["errors"] == []
        assert result["results"] == {}
        assert result["cancelled"] is False
        assert result["total"] == 0
        # Crucially, never even touched the per-doc code path.
        svc.index_document.assert_not_called()  # type: ignore[attr-defined]  # noqa: E501


class TestDispatch:
    def test_calls_index_document_per_doc(self):
        """Each ``(doc_id, title)`` becomes exactly one index_document call."""
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            return_value={"status": "success", "chunk_count": 1}
        )
        doc_info = [
            ("doc-a", "A"),
            ("doc-b", "B"),
            ("doc-c", "C"),
        ]
        svc.index_documents_parallel(
            doc_info, "coll-1", force_reindex=True, max_workers=2
        )
        assert svc.index_document.call_count == 3  # type: ignore[attr-defined]
        # force_reindex is forwarded verbatim.
        for call in svc.index_document.call_args_list:  # type: ignore[attr-defined]
            args, kwargs = call
            assert args[2] is True  # positional: doc_id, coll_id, force_reindex

    def test_duplicate_doc_ids_are_deduplicated(self):
        """Repeated doc_ids in doc_info collapse to one index_document call.

        The underlying ``index_document`` is not idempotent at the DB
        level beyond its own checks; without this guard, a buggy
        caller could double-insert chunks from the same doc.
        """
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            return_value={"status": "success", "chunk_count": 2}
        )
        doc_info = [("doc-a", "A"), ("doc-a", "A2"), ("doc-a", "A3")]
        result = svc.index_documents_parallel(doc_info, "coll-1", max_workers=2)
        assert svc.index_document.call_count == 1  # type: ignore[attr-defined]
        assert (
            result["total"] == 3
        )  # reflects *input* length, not dedup'd count
        assert result["successful"] == 1


class TestResultAggregation:
    def test_counts_aggregate_successes_skips_failures(self):
        """Counter buckets match the legacy single-doc shape used by SSE."""
        svc = _make_service()
        # Dispatch on doc_id so the per-doc status is deterministic regardless
        # of which worker thread runs first — the previous list-based side_effect
        # was consumed in call order, so parallel execution attributed the
        # statuses to the wrong docs and the ``errors[0]`` ordering was
        # fundamentally flaky (CI flaked on ``assert 'doc-b' == 'doc-c'``).
        by_doc = {
            "doc-a": {"status": "success", "chunk_count": 4},
            "doc-b": {
                "status": "skipped",
                "message": "already",
                "chunk_count": 0,
            },
            "doc-c": {"status": "error", "error": "boom"},
            "doc-d": {"status": "success", "chunk_count": 1},
        }
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            side_effect=lambda doc_id, collection_id, force_reindex=False: (
                by_doc[doc_id]
            )
        )
        result = svc.index_documents_parallel(
            [
                ("doc-a", "A"),
                ("doc-b", "B"),
                ("doc-c", "C"),
                ("doc-d", "D"),
            ],
            "coll-1",
            max_workers=4,
        )
        assert result["successful"] == 2
        assert result["skipped"] == 1
        assert result["failed"] == 1
        assert len(result["errors"]) == 1
        # ``errors`` is in *completion* order (as_completed), which is
        # non-deterministic in a unit test. Pin the per-doc membership on the
        # ``results`` dict instead, which is keyed by doc_id.
        assert result["results"]["doc-c"]["status"] == "error"
        assert result["results"]["doc-c"]["error"] == "boom"
        # Per-doc results exposed for callers that want finer detail.
        assert result["results"]["doc-a"]["status"] == "success"
        assert result["results"]["doc-b"]["status"] == "skipped"
        assert result["results"]["doc-d"]["status"] == "success"

    def test_index_document_exception_is_captured_not_raised(self):
        """A buggy ``index_document`` raises → counted as failed, run continues."""
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            side_effect=[
                RuntimeError("boom"),
                {"status": "success", "chunk_count": 1},
            ]
        )
        result = svc.index_documents_parallel(
            [("doc-x", "X"), ("doc-y", "Y")],
            "coll-1",
            max_workers=4,
        )
        assert result["failed"] == 1
        assert result["successful"] == 1
        assert result["results"]["doc-x"]["status"] == "error"
        assert "RuntimeError" in result["results"]["doc-x"]["error"]


class TestPreparedPipeline:
    def test_preparation_overlaps_and_writes_are_serial(self):
        """Embedding preparation overlaps; durable writes never overlap."""
        import time
        from local_deep_research.research_library.services.library_rag_service import (
            _PreparedDocument,
        )
        import numpy as np

        svc = _make_service()
        prep_active = 0
        prep_peak = 0
        prep_lock = threading.Lock()
        write_active = 0
        write_peak = 0
        write_threads = []

        def prepare(doc_id, collection_id, force_reindex, is_cancelled=None):
            nonlocal prep_active, prep_peak
            with prep_lock:
                prep_active += 1
                prep_peak = max(prep_peak, prep_active)
            time.sleep(0.03)
            with prep_lock:
                prep_active -= 1
            return {
                "status": "prepared",
                "prepared": _PreparedDocument(
                    document_id=doc_id,
                    collection_id=collection_id,
                    chunk_inputs=[],
                    vectors=np.empty((0, 1), dtype="float32"),
                ),
            }

        def write(prepared):
            nonlocal write_active, write_peak
            write_threads.append(threading.current_thread())
            write_active += 1
            write_peak = max(write_peak, write_active)
            time.sleep(0.005)
            write_active -= 1
            return {"status": "success", "chunk_count": 0}

        svc._prepare_document = MagicMock(side_effect=prepare)
        svc._write_prepared_document = MagicMock(side_effect=write)

        result = svc.index_documents_parallel(
            [(f"doc-{i}", str(i)) for i in range(8)],
            "coll-1",
            max_workers=4,
        )

        assert prep_peak > 1
        assert write_peak == 1
        assert all(t is threading.current_thread() for t in write_threads)
        assert result["successful"] == 8


class TestProgressCallback:
    def test_callback_fires_once_per_completion_in_completion_order(self):
        """Each completion emits one (completed, total, title, status) tuple."""
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            side_effect=[
                {"status": "success", "chunk_count": 1},
                {"status": "success", "chunk_count": 2},
                {"status": "success", "chunk_count": 3},
            ]
        )
        seen = []
        svc.index_documents_parallel(
            [("doc-a", "Title A"), ("doc-b", "Title B"), ("doc-c", "Title C")],
            "coll-1",
            max_workers=4,
            progress_callback=lambda *args: seen.append(args),
        )
        # Three callbacks, one per completed doc; total is fixed;
        # titles come from the input, not from the worker order.
        assert len(seen) == 3
        for entry in seen:
            completed, total, _title, _status = entry
            assert total == 3
            assert completed in {1, 2, 3}
        # ``completed`` must be monotonically non-decreasing across the run.
        completed_seq = [e[0] for e in seen]
        assert completed_seq == sorted(completed_seq)

    def test_callback_exception_does_not_poison_run(self):
        """A buggy progress callback is logged and ignored, not raised."""
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            side_effect=[
                {"status": "success", "chunk_count": 1},
                {"status": "success", "chunk_count": 1},
            ]
        )

        def _bad_cb(*_a, **_k):
            raise ValueError("oops")

        result = svc.index_documents_parallel(
            [("doc-a", "A"), ("doc-b", "B")],
            "coll-1",
            max_workers=4,
            progress_callback=_bad_cb,
        )
        # Both docs still completed despite the crashing callback.
        assert result["successful"] == 2


class TestCancellation:
    def test_is_cancelled_breaks_dispatch_loop(self):
        """When is_cancelled returns True after N docs, no further submissions run.

        ``is_cancelled`` is polled between completions (i.e., between
        ``as_completed`` yields). We track how many futures were
        actually submitted; the helper may submit the whole batch
        up-front but break out of the consume loop early, so the
        observable signature is ``cancelled=True`` plus the
        aggregate-shape contract (not-cancelled docs end up as
        'skipped' with a 'cancelled' message).
        """
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            side_effect=[
                {"status": "success", "chunk_count": 1},
                {"status": "success", "chunk_count": 2},
                {"status": "success", "chunk_count": 3},
                {"status": "success", "chunk_count": 4},
                {"status": "success", "chunk_count": 5},
            ]
        )
        cancel_after = {"n": 0}

        def _is_cancelled():
            cancel_after["n"] += 1
            # Cancel after the second completion is observed.
            return cancel_after["n"] >= 2

        result = svc.index_documents_parallel(
            [
                ("doc-a", "A"),
                ("doc-b", "B"),
                ("doc-c", "C"),
                ("doc-d", "D"),
                ("doc-e", "E"),
            ],
            "coll-1",
            max_workers=5,
            is_cancelled=_is_cancelled,
        )

        assert result["cancelled"] is True
        # Polled at least once; the loop exits at the cancellation point.
        assert cancel_after["n"] >= 1
        # No fatal exception escaped — count totals reflect the
        # run's observation window.
        assert (
            result["successful"] + result["skipped"] + result["failed"]
            == result["total"]
        )

    def test_no_new_index_document_calls_after_cancel_observed(self):
        """Regression: once ``is_cancelled`` returns True, no new ``index_document``
        calls are admitted.

        Under the previous all-upfront submission model, a window existed where
        queued futures could be picked up by idle workers between the caller's
        cancel signal (e.g. ``_sse_cancel.set()`` in
        ``rag_routes.index_collection``) and the helper's
        ``pool.shutdown(cancel_futures=True)``. The new bounded-submission
        loop polls ``is_cancelled`` BEFORE admitting any new work, so
        post-cancel starts are bounded by what was already in flight — not
        by the full input length.

        With synchronous ``index_document`` and ``max_workers=2`` plus 20 docs:
        the OLD code would have submitted all 20 upfront before any
        ``is_cancelled`` poll. The NEW code submits the first batch of 2, then
        one more as a slot frees (per outer-loop iteration), and stops as
        soon as ``is_cancelled()`` returns True.

        We arrange ``is_cancelled`` to return False for the first 2 polls
        (admitting the initial batch + 1 refill as a slot frees) and True
        thereafter. Total admitted calls is therefore ≤ 3 with bounded
        submission, vs. 20 with the old model.
        """
        svc = _make_service()

        # is_cancelled returns False for the first 2 polls, True thereafter.
        poll_count = {"n": 0}

        def _is_cancelled():
            if threading.current_thread() is threading.main_thread():
                poll_count["n"] += 1
            return poll_count["n"] > 2

        # Synchronous index_document: a worker finishes instantly, freeing a
        # slot for the next admission on the very next outer-loop iteration.
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            return_value={"status": "success", "chunk_count": 1}
        )

        result = svc.index_documents_parallel(
            [(f"doc-{i}", str(i)) for i in range(20)],
            "coll-1",
            max_workers=2,
            is_cancelled=_is_cancelled,
        )

        assert result["cancelled"] is True
        # Bounded submission: with max_workers=2 and cancel observed on poll
        # #3, the loop admits at most ~3 calls (first batch of 2 + 1 refill
        # as a slot frees, then cancel observed → no more submits). The
        # OLD all-upfront model would have admitted all 20.
        #
        # Use ≤4 to leave a small scheduling-tolerance margin; the precise
        # bound is 3 with synchronous workers but pool scheduling can let
        # one extra submit land before the next poll.
        admitted = svc.index_document.call_count  # type: ignore[attr-defined]
        assert admitted <= 4, (
            f"Bounded-submission loop admitted {admitted} index_document "
            f"calls after cancellation; expected ≤4 with max_workers=2 and "
            f"is_cancelled flipping True after 2 polls. The OLD "
            f"all-upfront model would have admitted all 20."
        )
        # And the exact upper bound for completeness: at least 1 admit
        # happened before cancel was observed (otherwise cancel couldn't
        # have been observed via the per-iteration poll).
        assert admitted >= 1, (
            "Helper should admit at least one batch before observing cancel"
        )

    def test_zero_index_document_calls_when_cancel_polled_first(self):
        """When ``is_cancelled`` returns True on the FIRST poll (before any
        submit), zero ``index_document`` calls must be admitted.

        The OLD all-upfront model would have submitted the entire batch
        before ever polling ``is_cancelled``, so it would have admitted N
        calls even though cancel was set immediately. The new bounded-
        submission loop polls BEFORE submit, so cancel-on-first-poll
        admits exactly zero work.
        """
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            return_value={"status": "success", "chunk_count": 1}
        )

        result = svc.index_documents_parallel(
            [(f"doc-{i}", str(i)) for i in range(10)],
            "coll-1",
            max_workers=2,
            is_cancelled=lambda: True,
        )

        assert result["cancelled"] is True
        # Critical assertion: NO work was admitted because cancel was
        # observed before any submit. The OLD code would have submitted
        # all 10 upfront.
        svc.index_document.assert_not_called()  # type: ignore[attr-defined]
        # All 10 docs end up in the aggregate as 'skipped' (cancelled
        # before submission), keeping the contract that the totals match.
        assert (
            result["successful"] + result["skipped"] + result["failed"]
            == result["total"]
        )
        assert result["skipped"] == 10
        assert result["successful"] == 0

    def test_no_starts_after_cancellation_during_fill(self):
        """Test that no worker starts after the cancellation event is set,
        even during the initial pool-filling phase.
        """
        import threading

        svc = _make_service()

        cancel_event = threading.Event()
        doc_0_started = threading.Event()
        doc_0_resume = threading.Event()

        started_calls = []

        def mock_index_document(doc_id, collection_id, force_reindex=False):
            # Record the doc_id and whether the cancel_event was set at start time.
            started_calls.append((doc_id, cancel_event.is_set()))
            if doc_id == "doc-0":
                doc_0_started.set()
                doc_0_resume.wait(timeout=5)
            return {"status": "success", "chunk_count": 1}

        svc.index_document = MagicMock(side_effect=mock_index_document)  # type: ignore[method-assign]

        is_cancelled_call_count = {"n": 0}

        def _is_cancelled():
            if threading.current_thread() is threading.main_thread():
                is_cancelled_call_count["n"] += 1
                # Call 3 is the poll right before submitting doc-1
                if is_cancelled_call_count["n"] == 3:
                    # Wait until doc-0 has actually started running in the worker thread
                    doc_0_started.wait(timeout=5)
                    # Trigger the cancellation event
                    cancel_event.set()
                    # Allow doc-0 to resume and complete
                    doc_0_resume.set()
            return cancel_event.is_set()

        result = svc.index_documents_parallel(
            [("doc-0", "Zero"), ("doc-1", "One"), ("doc-2", "Two")],
            "coll-1",
            max_workers=3,
            is_cancelled=_is_cancelled,
        )

        assert result["cancelled"] is True
        # doc-0 should have started when cancel_event was not set.
        # doc-1 and doc-2 should never have started/submitted.
        assert len(started_calls) == 1
        assert started_calls[0] == ("doc-0", False)

        # doc-1 and doc-2 are in results as skipped
        assert result["results"]["doc-1"]["status"] == "skipped"
        assert result["results"]["doc-2"]["status"] == "skipped"

    def test_cancellation_preserved_when_set_during_last_inflight_worker(self):
        """Regression: if the cancel event is set WHILE the final
        in-flight worker is running (and that worker completes
        successfully), the aggregate must still report
        ``cancelled=True``.

        The reviewer's concern was that "the helper can finish without
        another poll and return cancelled=False, allowing the
        background task to overwrite cancellation with completed".
        The fix is the final ``is_cancelled()`` poll in the ``finally``
        block: even if the last worker completes with status=success,
        if ``is_cancelled()`` returns True at any point during the
        run, the helper preserves ``cancelled=True`` in the aggregate.
        """
        svc = _make_service()

        cancel_event = threading.Event()
        worker_started = threading.Event()
        resume_worker = threading.Event()

        def mock_index_document(doc_id, collection_id, force_reindex=False):
            if doc_id == "doc-0":
                worker_started.set()
                # Wait until the test signals us to resume (after it
                # has set the cancel event).
                resume_worker.wait(timeout=5)
            return {"status": "success", "chunk_count": 1}

        svc.index_document = MagicMock(  # type: ignore[method-assign]
            side_effect=mock_index_document
        )

        def _is_cancelled():
            return cancel_event.is_set()

        def _set_cancel_during_last_worker():
            assert worker_started.wait(timeout=5), "doc-0 worker never started"
            cancel_event.set()
            resume_worker.set()

        threading.Thread(
            target=_set_cancel_during_last_worker, daemon=True
        ).start()

        result = svc.index_documents_parallel(
            [("doc-0", "Zero")],
            "coll-1",
            max_workers=1,
            is_cancelled=_is_cancelled,
        )

        # doc-0 completed successfully (it was already running when
        # cancel was set), BUT the helper must still report
        # cancelled=True because the cancel event was observed during
        # the run (via the finally block's final poll).
        assert result["cancelled"] is True, (
            "Helper returned cancelled=False despite the cancel event "
            "being set during the in-flight worker. The finally-block "
            "final poll must preserve cancelled=True."
        )
        assert result["results"]["doc-0"]["status"] == "success"


class TestWorkerBounds:
    def test_max_workers_zero_clamped_to_one(self):
        """A zero or negative max_workers never produces an empty pool."""
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            return_value={"status": "success", "chunk_count": 1}
        )

        # Patch ThreadPoolExecutor so we can capture max_workers without
        # spawning real workers (deterministic for the clamp check).
        with patch(
            f"{_MOD}.ThreadPoolExecutor", wraps=ThreadPoolExecutor
        ) as tpe_cls:
            svc.index_documents_parallel(
                [("doc-a", "A")],
                "coll-1",
                max_workers=0,
            )
            assert tpe_cls.call_args.kwargs["max_workers"] == 1

    def test_max_workers_one_runs_serially(self):
        """max_workers=1 must not break ordering; results match input order.

        We can't easily *prove* serial execution in a unit test without
        wall-clock tricks, but we can pin the public contract: every
        doc still gets indexed and the result dict is intact.
        """
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            side_effect=[
                {"status": "success", "chunk_count": 1},
                {"status": "success", "chunk_count": 2},
                {"status": "success", "chunk_count": 3},
            ]
        )
        result = svc.index_documents_parallel(
            [("doc-a", "A"), ("doc-b", "B"), ("doc-c", "C")],
            "coll-1",
            max_workers=1,
        )
        assert result["successful"] == 3
        assert set(result["results"].keys()) == {"doc-a", "doc-b", "doc-c"}


class TestIndexOneSeam:
    """``_index_one`` is the explicit dispatch seam for parallel indexing.

    The previous ``"index_document" in self.__dict__`` check only saw
    instance-attribute patches and silently bypassed class-level subclass
    overrides — see PR #5235 review comment 5085604502. These tests pin
    the new contract: both the new ``_index_one`` seam AND the legacy
    ``index_document`` instance-patch pattern must be honoured.
    """

    def test_instance_patch_on_index_document_still_routes_through(self):
        """Legacy seam: ``svc.index_document = MagicMock(...)`` must still
        be invoked by the parallel runner.
        """
        svc = _make_service()
        svc.index_document = MagicMock(  # type: ignore[method-assign]
            return_value={"status": "success", "chunk_count": 1}
        )

        result = svc.index_documents_parallel(
            [("doc-a", "A"), ("doc-b", "B")],
            "coll-1",
        )

        assert result["successful"] == 2
        assert svc.index_document.call_count == 2

    def test_instance_patch_on_index_one_is_honored(self):
        """New seam: ``svc._index_one = MagicMock(...)`` must be invoked
        by the parallel runner and short-circuit the prepared pipeline.
        """
        svc = _make_service()
        svc._index_one = MagicMock(  # type: ignore[method-assign]
            return_value={"status": "success", "chunk_count": 7}
        )
        # If the seam isn't honoured, the runner would call the real
        # ``_prepare_document`` (which needs DB+embedding, mocked here
        # to raise) and we'd see an error result instead of success.
        svc._prepare_document = MagicMock(  # type: ignore[method-assign]
            side_effect=AssertionError("prepared pipeline must be skipped")
        )

        result = svc.index_documents_parallel(
            [("doc-a", "A")],
            "coll-1",
        )

        assert result["successful"] == 1
        assert svc._index_one.call_count == 1
        assert result["results"]["doc-a"]["chunk_count"] == 7

    def test_subclass_override_of_index_one_is_honored(self):
        """Class-level subclass overrides of ``_index_one`` must be honoured.

        Regression for the ``"index_document" in self.__dict__`` check that
        silently bypassed subclass overrides.
        """
        from local_deep_research.research_library.services.library_rag_service import (
            LibraryRAGService,
        )

        sentinel = {"status": "success", "chunk_count": 42}

        class Subclass(LibraryRAGService):
            def _index_one(self, doc_id, collection_id, force_reindex):
                return dict(sentinel, doc_id=doc_id)

        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()
            svc = Subclass(username="u", db_password="pw")
            # Sanity-check the seam is the override, not the base impl.
            assert svc._index_one.__func__ is not LibraryRAGService._index_one

            result = svc.index_documents_parallel(
                [("doc-x", "X")],
                "coll-1",
            )

        assert result["successful"] == 1
        assert result["results"]["doc-x"]["chunk_count"] == 42
        assert result["results"]["doc-x"]["doc_id"] == "doc-x"

    def test_subclass_override_of_index_document_is_honored(self):
        """Class-level subclass overrides of the legacy ``index_document``
        extension point must be honoured.

        Regression for the PR #5235 follow-up review comment: ``_run_one``
        detected class-level ``_index_one`` overrides and instance patches
        of either name, but a subclass overriding ``index_document`` at
        class level (the old extension point this seam replaced) silently
        fell through to the prepared pipeline. Pin the new check that
        ``type(self).index_document is not LibraryRAGService.index_document``
        routes the override into the bypass path.
        """
        from local_deep_research.research_library.services.library_rag_service import (
            LibraryRAGService,
        )

        class Subclass(LibraryRAGService):
            def index_document(
                self, doc_id, collection_id, force_reindex=False
            ):
                return {
                    "status": "success",
                    "chunk_count": 5,
                    "doc_id": doc_id,
                }

        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()
            svc = Subclass(username="u", db_password="pw")
            # Sanity-check the override is the subclass impl, not the base.
            assert svc.index_document.__func__ is not (
                LibraryRAGService.index_document
            )

            # If the seam isn't honoured, the runner would call the real
            # ``_prepare_document`` (which needs DB+embedding, mocked here
            # to raise) and we'd see an error result instead of success.
            svc._prepare_document = MagicMock(  # type: ignore[method-assign]
                side_effect=AssertionError(
                    "prepared pipeline must be skipped when index_document "
                    "is overridden at class level"
                )
            )

            result = svc.index_documents_parallel(
                [("doc-y", "Y")],
                "coll-1",
            )

        assert result["successful"] == 1
        assert result["results"]["doc-y"]["chunk_count"] == 5
        assert result["results"]["doc-y"]["doc_id"] == "doc-y"


class TestFaissWriteLockEvictionRetry:
    """``_hold_faiss_write_lock`` must retry if the canonical entry is
    evicted between ``_get_faiss_write_lock`` and the wrapper's
    ``acquire()`` — otherwise a non-canonical wrapper would be held while
    the next writer created a fresh lock for the same
    ``(username, index_path)``.

    Regression for the residual TOCTOU race flagged in PR #5235 review
    comment 5085604502.
    """

    def _reset_locks(self):
        from local_deep_research.research_library.services import (
            library_rag_service as mod,
        )

        with mod._faiss_write_locks_lock:
            mod._faiss_write_locks.clear()
            mod._faiss_active_lock_keys.clear()

    def test_hold_yields_canonical_wrapper_after_eviction(self, tmp_path):
        """Simulate the pop-during-acquire race deterministically:

        1. Pre-create wrapper A so ``_get_faiss_write_lock`` would return it.
        2. Replace the canonical dict entry with a fresh wrapper B before
           the holder's ``acquire()`` runs.
        3. Wrap ``_TrackedRLock.acquire`` so the first call swaps the
           canonical entry to a fresh wrapper C while A is mid-acquire.
        4. Assert the holder eventually yields the canonical wrapper (C
           or whatever the last swap left) and never A.

        Without the re-check the holder would yield wrapper A and a
        concurrent writer using the canonical wrapper would race A on the
        FAISS file.
        """
        self._reset_locks()
        from local_deep_research.research_library.services import (
            library_rag_service as mod,
        )

        p = str(tmp_path / "race.faiss")
        key = ("u1", str(mod.Path(p).resolve()))

        # Step 1: pre-create wrapper A and put it in the dict as canonical.
        wrapper_a = mod._get_faiss_write_lock("u1", p)
        assert mod._faiss_write_locks.get(key) is wrapper_a

        swap_done = threading.Event()
        original_acquire = mod._TrackedRLock.acquire

        def swapped_acquire(self, *args, **kwargs):
            # While A is mid-acquire, swap the canonical dict entry to a
            # fresh wrapper C. A is now non-canonical from
            # ``_hold_faiss_write_lock``'s perspective.
            if self is wrapper_a and not swap_done.is_set():
                with mod._faiss_write_locks_lock:
                    mod._faiss_write_locks[key] = mod._TrackedRLock(key)
                swap_done.set()
            return original_acquire(self, *args, **kwargs)

        with patch.object(mod._TrackedRLock, "acquire", swapped_acquire):
            with mod._hold_faiss_write_lock("u1", p) as held:
                assert swap_done.is_set(), (
                    "Test setup: eviction swap never fired"
                )
                # The held wrapper MUST be the current canonical entry,
                # not wrapper A.
                with mod._faiss_write_locks_lock:
                    canonical = mod._faiss_write_locks.get(key)
                    assert held is canonical, (
                        "Held wrapper is non-canonical — a concurrent "
                        "writer using the canonical wrapper would race "
                        "this caller on the FAISS file"
                    )
                    assert held is not wrapper_a, (
                        "Held wrapper is the pre-eviction wrapper A — "
                        "the retry path did not run"
                    )
                    # Canonical holder must keep the key active so pop
                    # cannot evict it mid-write.
                    assert key in mod._faiss_active_lock_keys

    def test_non_canonical_release_does_not_discard_active_key(self, tmp_path):
        """A non-canonical wrapper's release must not erase the active key.

        Sequence:
        1. Canonical wrapper C is acquired and marks the key active.
        2. Stale wrapper A (no longer in ``_faiss_write_locks``) is
           acquired and released — the TOCTOU retry path does exactly
           this after detecting eviction.
        3. The key must still be active and C must still survive
           ``pop_faiss_locks_for_user``.

        Without canonical-only active-key tracking, A's release discarded
        the key while C still held the lock, reopening the concurrent
        FAISS-writer race this PR eliminates.
        """
        self._reset_locks()
        from local_deep_research.research_library.services import (
            library_rag_service as mod,
        )

        p = str(tmp_path / "stale-release.faiss")
        key = ("u1", str(mod.Path(p).resolve()))

        wrapper_a = mod._get_faiss_write_lock("u1", p)
        # Evict A from the dict and install a fresh canonical wrapper C
        # without going through pop (simulates the mid-acquire swap).
        with mod._faiss_write_locks_lock:
            wrapper_c = mod._TrackedRLock(key)
            mod._faiss_write_locks[key] = wrapper_c

        wrapper_c.acquire()
        try:
            with mod._faiss_write_locks_lock:
                assert key in mod._faiss_active_lock_keys
                assert wrapper_c._tracks_active_key is True

            # Stale A acquires/releases exactly as the TOCTOU retry path does.
            assert wrapper_a.acquire() is True
            with mod._faiss_write_locks_lock:
                assert wrapper_a._tracks_active_key is False
                assert key in mod._faiss_active_lock_keys
            wrapper_a.release()

            with mod._faiss_write_locks_lock:
                assert key in mod._faiss_active_lock_keys, (
                    "Non-canonical release discarded the active key while "
                    "the canonical holder still owns the lock"
                )

            mod.pop_faiss_locks_for_user("u1")
            assert mod._get_faiss_write_lock("u1", p) is wrapper_c, (
                "Canonical held lock was evicted after a non-canonical "
                "release cleared the active-key set"
            )
        finally:
            wrapper_c.release()

        with mod._faiss_write_locks_lock:
            assert key not in mod._faiss_active_lock_keys


class TestNeedsSerialWriteFallback:
    """``index_documents_parallel`` falls back to ``index_document`` when preparation returns ``needs_serial_write``."""

    def test_parallel_indexing_falls_back_on_needs_serial_write(self):
        svc = _make_service()
        with (
            patch.object(
                svc,
                "_prepare_document",
                return_value={"status": "needs_serial_write"},
            ),
            patch.object(
                svc,
                "index_document",
                return_value={"status": "success", "chunk_count": 3},
            ) as mock_index_doc,
            patch.object(
                svc,
                "_write_prepared_document",
            ) as mock_write_prep,
        ):
            res = svc.index_documents_parallel(
                doc_info=[("doc-123", "Title 123")],
                collection_id="coll-1",
            )
            assert res["successful"] == 1
            mock_index_doc.assert_called_once_with("doc-123", "coll-1", False)
            assert not mock_write_prep.called


class TestWritePreparedDocumentReindexDelta:
    """``_write_prepared_document`` accurately updates ``rag_index.chunk_count`` on re-index."""

    def test_chunk_count_delta_captures_old_chunks_before_merge(self):
        import hashlib
        import numpy as np
        from local_deep_research.research_library.services.library_rag_service import (
            _PreparedDocument,
        )

        svc = _make_service()
        svc.rag_index_record = MagicMock()
        svc.rag_index_record.id = 1

        mock_existing = MagicMock()
        mock_existing.chunk_count = 5  # 5 old chunks
        mock_existing.rag_index_id = 1

        mock_rag_index = MagicMock()
        mock_rag_index.chunk_count = 10  # 10 total chunks currently in index

        mock_vindex = MagicMock()
        mock_vindex.index_prepared.return_value = MagicMock(added=2)

        def make_q(first_val=None):
            q = MagicMock()
            q.filter_by.return_value = q
            q.first.return_value = first_val
            return q

        body = "delta body"
        body_hash = hashlib.sha256(body.encode()).hexdigest()
        mock_link = MagicMock()
        mock_link.indexed = False
        mock_doc = MagicMock()
        mock_doc.text_content = body

        session = MagicMock()
        # session.query first resolves the transaction lock index, then
        # runs the Phase-3 gate unconditionally:
        # 1. DocumentCollection link (gate)
        # 2. Document existence (gate)
        # 3. RagDocumentStatus existence (gate)
        # 4. Conditional document snapshot claim
        # 5. RagDocumentStatus (existing, for delta)
        # 6. DocumentCollection update (TOCTOU rowcount; MagicMock -> no abort)
        # 7. RAGIndex
        q_link = make_q(first_val=mock_link)
        q_doc = make_q(first_val=mock_doc)
        q_gate_status = make_q(first_val=mock_existing)
        q_claim = make_q()
        q_claim.filter.return_value = q_claim
        q_claim.update.return_value = 1
        q_existing = make_q(first_val=mock_existing)
        q_doc_coll = make_q()
        q_rag_index = make_q(first_val=mock_rag_index)
        index_lookup = make_q(first_val=MagicMock(index_hash="generation"))
        session.query.side_effect = [
            index_lookup,
            q_link,
            q_doc,
            q_gate_status,
            q_claim,
            q_existing,
            q_doc_coll,
            q_rag_index,
        ]

        prepared = _PreparedDocument(
            document_id="doc-1",
            collection_id="coll-1",
            vectors=np.zeros((2, 8), dtype="float32"),
            chunk_inputs=[MagicMock(), MagicMock()],
        )  # 2 new chunks (delta = 2 - 5 = -3).
        prepared.content_hash = body_hash
        prepared.initial_indexed = False
        prepared.status_exists = True
        prepared.had_collection_link = True

        session_ctx = MagicMock()
        session_ctx.__enter__ = MagicMock(return_value=session)
        session_ctx.__exit__ = MagicMock(return_value=None)

        with (
            patch.object(svc, "_get_vector_index", return_value=mock_vindex),
            patch(
                "local_deep_research.research_library.services.library_rag_service.ensure_in_collection"
            ),
            patch(
                "local_deep_research.research_library.services.library_rag_service._hold_faiss_write_lock"
            ),
            patch(
                "local_deep_research.research_library.services.library_rag_service.get_user_db_session",
                return_value=session_ctx,
            ),
        ):
            svc._get_index_hash = MagicMock(return_value="hash123")
            svc._get_index_path = MagicMock(return_value="/path/123.faiss")

            res = svc._write_prepared_document(prepared)

        assert res["status"] == "success"
        # 10 + (2 - 5) = 7
        assert mock_rag_index.chunk_count == 7