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.

9.5 KB
Raw Blame
"""Collection indexing admission across the two streams and background jobs."""

import json
import threading
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import Mock

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.library import Collection, RAGIndex
from local_deep_research.database.models.queue import TaskMetadata
from local_deep_research.web.routers import rag


@pytest.fixture
def admission(tmp_path, monkeypatch):
    engine = create_engine(
        f"sqlite:///{tmp_path / 'admission.db'}",
        connect_args={"check_same_thread": False},
    )
    Base.metadata.create_all(
        engine,
        tables=[
            Collection.__table__,
            RAGIndex.__table__,
            TaskMetadata.__table__,
        ],
    )
    sessions = sessionmaker(bind=engine)

    @contextmanager
    def session_scope(*args, **kwargs):
        with sessions() as session:
            yield session

    with sessions.begin() as session:
        session.add(
            Collection(
                id="collection", name="Existing", embedding_model="model"
            )
        )
    monkeypatch.setattr(
        "local_deep_research.database.session_context.get_user_db_session",
        session_scope,
    )
    monkeypatch.setattr(rag, "_active_sse_indexers", {})
    monkeypatch.setattr(rag, "_active_background_indexers", {})
    monkeypatch.setattr(rag, "_start_bg_index_locks", {})
    settings = Mock()
    settings.get_setting.return_value = 4
    monkeypatch.setattr(rag, "get_settings_manager", lambda *a: settings)
    monkeypatch.setattr(
        "local_deep_research.utilities.db_utils.get_settings_manager",
        lambda *a: settings,
    )
    service = Mock()
    monkeypatch.setattr(rag, "get_rag_service", lambda *a, **k: service)
    monkeypatch.setattr(
        rag,
        "_query_documents_to_index",
        lambda *a: [
            (
                None,
                SimpleNamespace(id="document", title="Doc", filename="doc.txt"),
            )
        ],
    )
    monkeypatch.setattr(
        rag,
        "WorkerCleanupStreamingResponse",
        lambda content, **kwargs: SimpleNamespace(content=content, headers={}),
    )
    started = threading.Event()
    release = threading.Event()
    finished = threading.Event()

    def worker(
        task_id,
        collection_id,
        username,
        password,
        *args,
        settings_snapshot=None,
    ):
        started.set()
        try:
            assert release.wait(10), (
                "test did not release the background worker"
            )
            rag._update_task_status(
                username, password, task_id, status="completed"
            )
        finally:
            finished.set()

    monkeypatch.setattr(rag, "_background_index_worker", worker)
    threads = []
    thread_class = threading.Thread

    def track_thread(*args, **kwargs):
        thread = thread_class(*args, **kwargs)
        threads.append(thread)
        return thread

    monkeypatch.setattr(rag.threading, "Thread", track_thread)
    state = SimpleNamespace(
        sessions=sessions,
        started=started,
        release=release,
        finished=finished,
        threads=threads,
        service=service,
        thread_class=thread_class,
    )
    yield state
    release.set()
    for thread in threads:
        if thread.ident is not None:
            thread.join(timeout=10)
        assert not thread.is_alive()
    engine.dispose()


def _stream(kind, *, force=False, username="user"):
    request = SimpleNamespace(
        session={},
        query_params={
            "collection_id": "collection",
            "force_reindex": str(force).lower(),
        },
    )
    if kind == "bulk":
        return rag.index_all(request, username=username).content
    return rag.index_collection(
        request, "collection", username=username
    ).content


@pytest.mark.parametrize("first", ["single", "bulk", "background"])
@pytest.mark.parametrize("second", ["single", "bulk", "background"])
def test_collection_indexers_exclude_each_other(
    admission, monkeypatch, first, second
):
    stream = None
    if first == "background":
        accepted = rag._start_background_index_sync(
            "collection", "user", None, False
        )
        assert accepted["success"]
        assert admission.started.wait(5)
    else:
        stream = _stream(first)
        assert (
            json.loads(next(stream).removeprefix("data: "))["type"] == "start"
        )

    rebuild = Mock(
        side_effect=AssertionError("a competing run must not rebuild the index")
    )
    monkeypatch.setattr(admission.service, "rebuild_collection", rebuild)
    try:
        if second == "background":
            refused = rag._start_background_index_sync(
                "collection", "user", None, True
            )
            assert refused.status_code == 409
        else:
            contender = _stream(second, force=True)
            try:
                event = json.loads(next(contender).removeprefix("data: "))
                assert event == {
                    "type": "error",
                    "error": "Indexing is already in progress for this collection",
                }
            finally:
                contender.close()
        rebuild.assert_not_called()
        admission.service.index_documents_parallel.assert_not_called()
    finally:
        if stream is not None:
            stream.close()


def test_cancelled_background_worker_keeps_admission_until_it_stops(admission):
    accepted = rag._start_background_index_sync(
        "collection", "user", None, False
    )
    assert admission.started.wait(5)
    rag._do_update_task_status(
        "user", None, accepted["task_id"], status="cancelled"
    )
    assert (
        rag._start_background_index_sync(
            "collection", "user", None, True
        ).status_code
        == 409
    )
    stream = _stream("single", force=True)
    try:
        assert (
            json.loads(next(stream).removeprefix("data: "))["type"] == "error"
        )
    finally:
        stream.close()
    admission.release.set()
    admission.threads[0].join(timeout=5)
    assert not admission.threads[0].is_alive()
    assert not rag._active_background_indexers
    assert rag._start_background_index_sync("collection", "user", None, False)[
        "success"
    ]


def test_failed_thread_start_releases_admission_and_fails_task(
    admission, monkeypatch
):
    def fail_start(self):
        raise RuntimeError("cannot start new thread")

    # The fixture tracks real Thread instances; fail their actual start method.
    monkeypatch.setattr(admission.thread_class, "start", fail_start)
    refused = rag._start_background_index_sync(
        "collection", "user", None, False
    )
    assert refused.status_code == 500
    assert not rag._active_background_indexers
    with admission.sessions() as session:
        task = session.query(TaskMetadata).one()
        assert task.status == "failed"
        assert task.completed_at is not None
        assert (
            task.error_message == "Failed to start indexing. Please try again."
        )
        task_id = task.task_id

    status = rag.get_index_status(
        SimpleNamespace(session={}), "collection", "user"
    )
    assert status["status"] == "failed"
    assert (
        status["error_message"] == "Failed to start indexing. Please try again."
    )

    # Only the exact fixed text is public. An older row containing extra
    # exception detail must not pass merely because it starts the same way.
    with admission.sessions.begin() as session:
        task = session.query(TaskMetadata).filter_by(task_id=task_id).one()
        task.error_message += " [SQL: SELECT private] at /srv/private.db"
    status = rag.get_index_status(
        SimpleNamespace(session={}), "collection", "user"
    )
    assert status["error_message"] == (
        "Indexing failed. Check the server logs for details."
    )
    assert "[SQL:" not in str(status)
    assert "/srv/private.db" not in str(status)


def test_racing_stream_and_background_request_admit_exactly_one(admission):
    start = threading.Barrier(2)
    cancel = threading.Event()

    def stream_request():
        start.wait(timeout=5)
        return rag._register_sse_indexer("user", "collection", None, cancel)

    def background_request():
        start.wait(timeout=5)
        return rag._start_background_index_sync(
            "collection", "user", None, False
        )

    try:
        with ThreadPoolExecutor(max_workers=2) as pool:
            stream = pool.submit(stream_request)
            background = pool.submit(background_request)
            stream_admitted = stream.result(timeout=5)
            background_result = background.result(timeout=5)
        if stream_admitted:
            assert background_result.status_code == 409
        else:
            assert background_result["success"] is True
    finally:
        rag._unregister_sse_indexer("user", "collection", cancel)


def test_other_users_and_collections_can_index(admission):
    cancel = threading.Event()
    assert rag._register_sse_indexer("user", "collection", None, cancel)
    try:
        assert rag._start_background_index_sync(
            "other-collection", "user", None, False
        )["success"]
        assert rag._start_background_index_sync(
            "collection", "other-user", None, False
        )["success"]
    finally:
        rag._unregister_sse_indexer("user", "collection", cancel)