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.

27.1 KB
Raw Blame
"""Indexing rejects borrowed sessions without changing caller transactions.

Production callers must therefore leave their own ``get_user_db_session``
blocks before they index; the last tests here pin that.
"""

import ast
import textwrap
import threading
from contextlib import nullcontext
from datetime import UTC, datetime
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest
from sqlalchemy import create_engine, event, inspect
from sqlalchemy.exc import InvalidRequestError
from sqlalchemy.orm import sessionmaker

import local_deep_research
from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentCollection,
    DocumentStatus,
    EmbeddingProvider,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)
from local_deep_research.database.models.queue import TaskMetadata
from local_deep_research.database.session_context import get_user_db_session
from local_deep_research.database.thread_local_session import (
    thread_session_manager,
)
from local_deep_research.research_library.services import (
    library_rag_service as rag_service_module,
)
from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
)

SCOPE_ERROR = "Indexing must run outside an existing database session"


@pytest.fixture
def rig(tmp_path):
    engine = create_engine(
        f"sqlite:///{tmp_path / 'review.sqlite'}", connect_args={"timeout": 0.1}
    )
    Base.metadata.create_all(engine)
    factory = sessionmaker(bind=engine)
    with factory() as setup:
        setup.add_all(
            [
                SourceType(
                    id="source", name="document", display_name="Document"
                ),
                Collection(
                    id="collection",
                    name="Library",
                    collection_type="default_library",
                ),
                Document(
                    id="doc",
                    source_type_id="source",
                    document_hash="hash",
                    title="Original",
                    text_content="Text to index",
                    file_size=13,
                    file_type="txt",
                    status=DocumentStatus.COMPLETED,
                ),
                RAGIndex(
                    id=1,
                    collection_name="collection_collection",
                    embedding_model="test-model",
                    embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
                    embedding_dimension=3,
                    index_path=str(tmp_path / "index"),
                    index_hash="review",
                    chunk_size=500,
                    chunk_overlap=50,
                ),
            ]
        )
        setup.commit()
    session = factory()
    service = LibraryRAGService.__new__(LibraryRAGService)
    service.username = "local_review"
    service._db_password = "test-only"
    service.embedding_model = "test-model"
    service.embedding_provider = "sentence_transformers"
    service.chunk_size = 500
    service.chunk_overlap = 50
    service.splitter_type = "recursive"
    service.text_separators = list(
        rag_service_module.DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS
    )
    service.distance_metric = "cosine"
    service.normalize_vectors = True
    service.index_type = "flat"
    service.rag_index_record = SimpleNamespace(id=1)
    service.text_splitter = MagicMock()
    service.text_splitter.split_documents.side_effect = lambda docs: docs
    service.embedding_manager = MagicMock()
    observations = []

    def embed(texts):
        observations.append(
            {
                "in_transaction": session.in_transaction(),
                "checked_out": engine.pool.checkedout(),
            }
        )
        return [[0.1, 0.2, 0.3] for _ in texts]

    service.embedding_manager.embeddings.embed_documents.side_effect = embed
    vector_index = MagicMock()
    stats = SimpleNamespace(added=1, removed=0, chunks=1)
    vector_index.index_prepared.return_value = stats

    def index(**kwargs):
        service.embedding_manager.embeddings.embed_documents(
            [c.text for c in kwargs["chunks"]]
        )
        return stats

    vector_index.index.side_effect = index
    service._get_vector_index = MagicMock(return_value=vector_index)
    service._get_index_hash = MagicMock(return_value="review")
    service._get_index_path = MagicMock(return_value=tmp_path / "index")

    # Like thread_session_manager, give each thread its own Session. The test
    # thread gets ``session``; indexing pool threads get their own.
    local = threading.local()
    local.session = session
    sessions = [session]
    sessions_lock = threading.Lock()

    def thread_session(_username, _password):
        if getattr(local, "session", None) is None:
            local.session = factory()
            with sessions_lock:
                sessions.append(local.session)
        return local.session

    with patch(
        "local_deep_research.database.thread_local_session.get_metrics_session",
        side_effect=thread_session,
    ):
        yield SimpleNamespace(
            engine=engine,
            factory=factory,
            session=session,
            service=service,
            observations=observations,
        )
    for owned in sessions:
        owned.close()
    engine.dispose()


def add_membership(rig):
    with rig.factory() as session:
        session.add(
            DocumentCollection(
                document_id="doc", collection_id="collection", indexed=False
            )
        )
        session.commit()


def add_second_document(rig):
    with rig.factory() as setup:
        setup.add_all(
            [
                Document(
                    id="doc-2",
                    source_type_id="source",
                    document_hash="hash-2",
                    title="Second",
                    text_content="More text to index",
                    file_size=18,
                    file_type="txt",
                    status=DocumentStatus.COMPLETED,
                ),
                DocumentCollection(
                    document_id="doc-2",
                    collection_id="collection",
                    indexed=False,
                ),
            ]
        )
        setup.commit()


def test_parallel_preparation_uses_worker_owned_index_records(rig):
    """An expired coordinator record must never refresh in a pool worker."""
    add_membership(rig)
    add_second_document(rig)

    shared_record = rig.session.get(RAGIndex, 1)
    control_record = rig.session.get(Collection, "collection")
    rig.session.commit()
    rig.service.rag_index_record = shared_record
    coordinator = threading.current_thread()
    worker_refreshes = []
    coordinator_loads = []

    def reject_worker_refresh(state):
        if threading.current_thread() is not coordinator:
            worker_refreshes.append(state)
            raise InvalidRequestError("Coordinator session used by worker")
        coordinator_loads.append(state.is_column_load)

    event.listen(rig.session, "do_orm_execute", reject_worker_refresh)
    preparing = threading.Barrier(2)

    def embed(texts):
        preparing.wait(timeout=5)
        return [[0.1, 0.2, 0.3] for _ in texts]

    rig.service.embedding_manager.embeddings.embed_documents.side_effect = embed
    try:
        # Positive control: the hook observes a refresh of a commit-expired
        # attribute on this session, so worker_refreshes == [] below cannot
        # pass merely because refreshes stopped reaching the listener.
        assert control_record.name == "Library"
        assert coordinator_loads == [True]
        # End the control's read transaction; shared_record stays expired.
        rig.session.commit()
        assert "id" in inspect(shared_record).expired_attributes
        result = rig.service.index_documents_parallel(
            [("doc", "Original"), ("doc-2", "Second")],
            "collection",
            max_workers=2,
        )
    finally:
        event.remove(rig.session, "do_orm_execute", reject_worker_refresh)

    assert result["failed"] == 0, result["errors"]
    assert result["successful"] == 2
    assert worker_refreshes == []
    with rig.factory() as check:
        assert (
            check.query(DocumentCollection).filter_by(indexed=True).count() == 2
        )


def test_prep_write_status_parity_survives_prior_status_rows(rig):
    """A fresh service must not abort docs whose status row outlived a purge.

    Production shape the worker-ownership test above cannot see: an in-place
    text change (Zotero ``_purge_collection_chunks``) deletes the docs'
    chunks and clears ``link.indexed`` but leaves their
    ``RagDocumentStatus`` rows behind, and the reconciler sweep later picks
    the docs up via ``index_documents_parallel(force_reindex=False)`` on a
    FRESH service whose ``rag_index_record`` stays ``None`` until the first
    write's ``_get_vector_index`` sets it — after that write's Phase-3
    staleness gate, and after any document already in flight took its
    prepare-time snapshot. Both documents here are prepared before either
    is written, so preparation and the write-side re-check must measure the
    status row against the same index identity, or a healthy write aborts
    as "stale" and ``_flag_document_for_reindex`` deletes its surviving
    status row:

    * re-checked against ``rag_index_record`` (still ``None``) after
      preparation resolved the index itself, the FIRST write compares
      True with False — and so does every write after it;
    * measured against ``rag_index_record`` on both sides, the SECOND
      document was snapshotted while it was ``None`` (False) but is
      re-checked after the first write set it (True).
    """
    add_membership(rig)
    add_second_document(rig)
    with rig.factory() as setup:
        setup.add_all(
            [
                RagDocumentStatus(
                    document_id=document_id,
                    collection_id="collection",
                    rag_index_id=1,
                    chunk_count=1,
                    indexed_at=datetime(2026, 10, 1, tzinfo=UTC),
                )
                for document_id in ("doc", "doc-2")
            ]
        )
        setup.commit()

    # The real _get_vector_index sets rag_index_record before the persist
    # writes — but only AFTER the staleness gate above them. Model that so
    # the test cannot accidentally arm the gate's guard itself.
    vector_index = rig.service._get_vector_index.return_value

    def fresh_service_vector_index(*args, **kwargs):
        rig.service.rag_index_record = SimpleNamespace(id=1)
        return vector_index

    rig.service._get_vector_index.side_effect = fresh_service_vector_index
    rig.service.rag_index_record = None
    # Neither document finishes preparing until both took their status
    # snapshot, so both snapshots precede the first write.
    preparing = threading.Barrier(2)

    def embed(texts):
        preparing.wait(timeout=5)
        return [[0.1, 0.2, 0.3] for _ in texts]

    rig.service.embedding_manager.embeddings.embed_documents.side_effect = embed
    result = rig.service.index_documents_parallel(
        [("doc", "Original"), ("doc-2", "Second")],
        "collection",
        force_reindex=False,
        max_workers=2,
    )
    assert result["failed"] == 0, result["errors"]
    assert {
        document_id: outcome["status"]
        for document_id, outcome in result["results"].items()
    } == {"doc": "success", "doc-2": "success"}
    with rig.factory() as check:
        statuses = {
            status.document_id: (status.rag_index_id, status.chunk_count)
            for status in check.query(RagDocumentStatus).filter_by(
                collection_id="collection"
            )
        }
        assert statuses == {"doc": (1, 1), "doc-2": (1, 1)}
        assert (
            check.query(DocumentCollection).filter_by(indexed=True).count() == 2
        )


def test_serial_rejects_enclosing_write_without_discarding_it(rig):
    add_membership(rig)
    with get_user_db_session("local_review", "test-only") as outer:
        document = outer.query(Document).filter_by(id="doc").one()
        document.title = "Caller edit"
        outer.flush()
        result = rig.service.index_document("doc", "collection")
        assert result == {
            "status": "error",
            "error": "Indexing must run outside an existing database session",
        }
        assert outer.in_transaction()
        assert document.title == "Caller edit"
        assert rig.observations == []
        rig.service._get_index_path.assert_not_called()
        with rig.factory() as observer:
            assert (
                observer.query(Document).filter_by(id="doc").one().title
                == "Original"
            )
        outer.commit()
    with rig.factory() as check:
        assert (
            check.query(Document).filter_by(id="doc").one().title
            == "Caller edit"
        )
    assert (
        rig.service.index_document("doc", "collection")["status"] == "success"
    )


def test_serial_rejects_staged_membership_without_discarding_it(rig):
    with get_user_db_session("local_review", "test-only") as outer:
        outer.add(
            DocumentCollection(
                document_id="doc", collection_id="collection", indexed=False
            )
        )
        outer.flush()
        result = rig.service.index_document("doc", "collection")
        assert result["status"] == "error", result
        assert rig.observations == []
        assert (
            outer.query(DocumentCollection)
            .filter_by(document_id="doc", collection_id="collection")
            .count()
            == 1
        )
        with rig.factory() as observer:
            assert (
                observer.query(DocumentCollection)
                .filter_by(document_id="doc", collection_id="collection")
                .count()
                == 0
            )
        outer.commit()
    with rig.factory() as check:
        assert (
            check.query(DocumentCollection)
            .filter_by(document_id="doc", collection_id="collection")
            .count()
            == 1
        )
    assert (
        rig.service.index_document("doc", "collection")["status"] == "success"
    )


@pytest.mark.parametrize("entry", ["locked", "prepare", "persist"])
def test_internal_indexing_entries_preserve_borrowed_transactions(rig, entry):
    add_membership(rig)
    prepared = None
    if entry == "persist":
        prepared = rig.service._prepare_document("doc", "collection", False)[
            "prepared"
        ]
        rig.observations.clear()
    with get_user_db_session("local_review", "test-only") as outer:
        document = outer.query(Document).filter_by(id="doc").one()
        document.title = "Caller edit"
        outer.flush()
        if entry == "locked":
            result = rig.service._index_document_locked("doc", "collection")
        elif entry == "prepare":
            result = rig.service._prepare_document("doc", "collection", False)
        else:
            result = rig.service._write_prepared_document(prepared)
        assert result["status"] == "error", result
        assert rig.observations == []
        assert outer.in_transaction()
        assert document.title == "Caller edit"
        with rig.factory() as observer:
            assert (
                observer.query(Document).filter_by(id="doc").one().title
                == "Original"
            )
        outer.commit()
    with rig.factory() as check:
        assert (
            check.query(Document).filter_by(id="doc").one().title
            == "Caller edit"
        )


def test_serial_first_time_membership_succeeds(rig):
    result = rig.service.index_document("doc", "collection")
    assert result["status"] == "success", result
    with rig.factory() as check:
        membership = (
            check.query(DocumentCollection)
            .filter_by(document_id="doc", collection_id="collection")
            .one()
        )
        assert membership.indexed is True


@pytest.mark.parametrize("entry", ["parallel", "all", "batch"])
def test_bulk_entries_reject_before_reading_or_embedding(rig, entry):
    """Bulk entry points reject a borrowed scope before doing any work.

    The bulk helpers would read inside a nested scope, whose exception
    cleanup rolls back the caller's Session. The parallel pool workers run
    on other threads, where ``_prepare_document``'s guard cannot see this
    caller's scope, so without an entry check every document would be
    embedded and only its write rejected.
    """
    add_membership(rig)
    opened = []
    real_scope = rag_service_module.get_user_db_session

    def counted_scope(*args, **kwargs):
        opened.append(threading.current_thread().name)
        return real_scope(*args, **kwargs)

    rejected = {"status": "error", "error": SCOPE_ERROR}
    with get_user_db_session("local_review", "test-only") as outer:
        document = outer.query(Document).filter_by(id="doc").one()
        document.title = "Caller edit"
        outer.flush()
        with patch.object(
            rag_service_module, "get_user_db_session", counted_scope
        ):
            if entry == "parallel":
                result = rig.service.index_documents_parallel(
                    [("doc", "Doc")], "collection", max_workers=1
                )
            elif entry == "all":
                result = rig.service.index_all_documents("collection")
            else:
                result = rig.service.index_documents_batch(
                    [("doc", "Doc")], "collection"
                )
        assert opened == []
        assert rig.observations == []
        if entry == "parallel":
            assert result == {
                "successful": 0,
                "skipped": 0,
                "failed": 1,
                "errors": [
                    {"doc_id": "doc", "title": "Doc", "error": SCOPE_ERROR}
                ],
                "results": {"doc": rejected},
                "cancelled": False,
                "total": 1,
            }
        elif entry == "all":
            assert result == {
                **rejected,
                "successful": 0,
                "skipped": 0,
                "failed": 0,
                "errors": [],
            }
        else:
            assert result == {"doc": rejected}
        assert outer.in_transaction()
        assert document.title == "Caller edit"
        with rig.factory() as observer:
            assert (
                observer.query(Document).filter_by(id="doc").one().title
                == "Original"
            )
        outer.commit()
    with rig.factory() as check:
        assert (
            check.query(Document).filter_by(id="doc").one().title
            == "Caller edit"
        )


@pytest.mark.parametrize("has_document", [False, True])
def test_rebuild_preserves_the_callers_uncommitted_transaction(
    rig, has_document
):
    if has_document:
        add_membership(rig)
    documents = [("doc", "Doc")] if has_document else []
    with get_user_db_session("local_review", "test-only") as outer:
        document = outer.get(Document, "doc")
        document.title = "Caller edit"
        outer.flush()
        caught = None
        try:
            rig.service.rebuild_collection(documents, "collection")
        except Exception as exc:
            caught = exc

        assert outer.in_transaction(), (
            "rebuild consumed the caller's transaction"
        )
        assert document.title == "Caller edit"
        with rig.factory() as observer:
            assert observer.get(Document, "doc").title == "Original"
        assert isinstance(caught, RuntimeError)
        assert str(caught) == SCOPE_ERROR
        assert rig.observations == []
        rig.service._get_vector_index.assert_not_called()
        outer.commit()

    with rig.factory() as check:
        assert check.get(Document, "doc").title == "Caller edit"


def test_background_index_worker_indexes_outside_its_setup_scope(rig):
    """The normal collection Index action must not lend its setup scope.

    ``_background_index_worker`` reads the collection inside
    ``get_user_db_session`` and has to leave that block before it calls
    ``index_documents_parallel``. The coordinator runs
    ``_write_prepared_document`` on this same thread, so an open setup scope
    would make the guard fail every document. Every session here really
    enters the scope, as in production, so moving the call back inside the
    block fails this test.
    """
    from local_deep_research.web.routers import rag as rag_router

    add_membership(rig)
    with rig.factory() as setup:
        # Indexed before, so the worker skips first-index metadata capture.
        collection = setup.query(Collection).filter_by(id="collection").one()
        collection.embedding_model = "test-model"
        collection.embedding_model_type = (
            EmbeddingProvider.SENTENCE_TRANSFORMERS
        )
        collection.chunk_size = 500
        collection.chunk_overlap = 50
        setup.add(
            TaskMetadata(
                task_id="task",
                status="processing",
                task_type="indexing",
                progress_current=0,
                progress_total=0,
                metadata_json={"collection_id": "collection"},
            )
        )
        setup.commit()

    service = rig.service
    scopes = {"coordinator": [], "write": []}
    coordinate = service.index_documents_parallel
    write = service._write_prepared_document

    def observed_coordinate(*args, **kwargs):
        scopes["coordinator"].append(thread_session_manager.in_scope())
        return coordinate(*args, **kwargs)

    def observed_write(prepared):
        scopes["write"].append(thread_session_manager.in_scope())
        return write(prepared)

    service.index_documents_parallel = observed_coordinate
    service._write_prepared_document = observed_write
    service.reconcile_collection_index = MagicMock(
        return_value={
            "indexed_documents": 1,
            "indexed_chunks": 1,
            "live_vectors": 1,
            "orphan_vectors": 0,
        }
    )
    with patch.object(
        rag_router,
        "_get_rag_service_for_thread",
        return_value=nullcontext(service),
    ):
        rag_router._background_index_worker(
            "task", "collection", "local_review", "test-only", False, 2
        )

    with rig.factory() as check:
        task = check.query(TaskMetadata).filter_by(task_id="task").one()
        assert task.status == "completed", (
            task.error_message,
            task.metadata_json,
        )
        result = task.metadata_json["result"]
        assert (result["successful"], result["failed"], result["errors"]) == (
            1,
            0,
            [],
        )
        membership = (
            check.query(DocumentCollection)
            .filter_by(document_id="doc", collection_id="collection")
            .one()
        )
        assert membership.indexed is True
    assert scopes == {"coordinator": [False], "write": [False]}
    service._get_vector_index.return_value.index_prepared.assert_called_once()


_INDEXING_METHODS = frozenset(
    {
        "index_document",
        "index_documents_parallel",
        "index_all_documents",
        "index_documents_batch",
        "rebuild_collection",
        "_index_one",
        "_index_document_locked",
        "_prepare_document",
        "_write_prepared_document",
    }
)


def _called_name(node):
    target = node.func if isinstance(node, ast.Call) else node
    if isinstance(target, ast.Name):
        return target.id
    return getattr(target, "attr", None)


def indexing_calls_inside_scope(tree):
    """``(lineno, method)`` for each indexing call made inside a scope.

    A scope is the body of ``with get_user_db_session(...)`` or of a function
    decorated with ``with_user_database``. A nested function starts outside
    any scope: it runs when it is called, not where it is defined.
    """
    found = []

    def visit(node, scoped):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            scoped = any(
                _called_name(decorator) == "with_user_database"
                for decorator in node.decorator_list
            )
        elif isinstance(node, ast.Lambda):
            scoped = False
        elif isinstance(node, (ast.With, ast.AsyncWith)):
            for item in node.items:
                visit(item, scoped)
            inner = scoped or any(
                isinstance(item.context_expr, ast.Call)
                and _called_name(item.context_expr) == "get_user_db_session"
                for item in node.items
            )
            for statement in node.body:
                visit(statement, inner)
            return
        elif (
            scoped
            and isinstance(node, ast.Call)
            and isinstance(node.func, ast.Attribute)
            and node.func.attr in _INDEXING_METHODS
        ):
            found.append((node.lineno, node.func.attr))
        for child in ast.iter_child_nodes(node):
            visit(child, scoped)

    visit(tree, False)
    return found


def test_scope_scanner_flags_only_calls_made_inside_a_scope():
    tree = ast.parse(
        textwrap.dedent(
            """\
            def worker(service):
                with get_user_db_session(user, password) as db:
                    doc_info = load(db)
                    service.index_documents_parallel(doc_info, "c")
                service.index_documents_parallel(doc_info, "c")
                with get_user_db_session(user, password) as db:
                    def later():
                        service.index_document("d", "c")
                later()

            @with_user_database
            def decorated(db, service):
                service.index_all_documents("c")
            """
        )
    )
    assert indexing_calls_inside_scope(tree) == [
        (4, "index_documents_parallel"),
        (13, "index_all_documents"),
    ]


def test_no_production_caller_indexes_inside_a_session_scope():
    """A call written inside a scope would fail every document at run time.

    The guard rejects indexing inside a ``get_user_db_session`` block on the
    calling thread. The background Index worker, both scheduler sweep
    batches and the bulk helpers had their indexing calls inside their setup
    scopes and had to move them out; this keeps every call site out.
    """
    package = Path(local_deep_research.__file__).resolve().parent
    violations = []
    for path in sorted(package.rglob("*.py")):
        source = path.read_text(encoding="utf-8")
        if (
            "get_user_db_session" not in source
            and "with_user_database" not in source
        ):
            continue
        if not any(method in source for method in _INDEXING_METHODS):
            continue
        violations.extend(
            f"{path.relative_to(package)}:{lineno} {method}()"
            for lineno, method in indexing_calls_inside_scope(
                ast.parse(source, filename=str(path))
            )
        )
    assert violations == [], (
        "Indexing called inside a get_user_db_session block; the guard "
        "rejects it. Collect plain values inside the block, leave it, then "
        "index:\n" + "\n".join(violations)
    )