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.

4.2 KB
Raw Blame
"""Long API v1 research must leave the default auth/DB executor available."""

import asyncio
import threading
from concurrent.futures import ThreadPoolExecutor
from contextvars import ContextVar
from unittest.mock import patch

import pytest

from local_deep_research.config.thread_settings import (
    get_settings_context,
    set_settings_context,
)
from local_deep_research.web.dependencies import threadpool


@pytest.mark.parametrize(
    ("path", "target", "body", "result"),
    [
        (
            "/api/v1/quick_summary",
            "local_deep_research.api.research_functions.quick_summary",
            {"query": "q"},
            {"summary": "ok", "findings": []},
        ),
        (
            "/api/v1/generate_report",
            "local_deep_research.api.research_functions.generate_report",
            {"query": "q"},
            {"content": "ok", "metadata": {}},
        ),
        (
            "/api/v1/analyze_documents",
            "local_deep_research.web.routers.api_v1.analyze_documents",
            {"query": "q", "collection_name": "Library"},
            {"summary": "ok"},
        ),
    ],
)
def test_research_routes_use_the_dedicated_workers(
    authenticated_client, path, target, body, result
):
    observed_threads = []

    def fake_research(*args, **kwargs):
        observed_threads.append(threading.current_thread().name)
        return result

    with patch(target, side_effect=fake_research):
        response = authenticated_client.post(path, json=body)

    assert response.status_code == 200, response.text[:300]
    assert len(observed_threads) == 1
    assert observed_threads[0].startswith("ldr-api-research"), observed_threads


def test_research_does_not_starve_a_single_default_db_worker(monkeypatch):
    entered = threading.Event()
    release = threading.Event()
    research_pool = ThreadPoolExecutor(max_workers=1)
    monkeypatch.setattr(
        threadpool, "_get_api_research_executor", lambda: research_pool
    )

    def hold_research_worker():
        entered.set()
        assert release.wait(timeout=10), "research worker was not released"

    async def scenario():
        loop = asyncio.get_running_loop()
        loop.set_default_executor(ThreadPoolExecutor(max_workers=1))
        research = asyncio.create_task(
            threadpool.run_research_sync(hold_research_worker)
        )
        try:
            for _ in range(200):
                if entered.is_set():
                    break
                await asyncio.sleep(0.01)
            assert entered.is_set(), "research did not start"
            # This models ensure_user_database on an authenticated request.
            assert (
                await asyncio.wait_for(
                    threadpool.run_db_sync(lambda: "auth-ready"), timeout=3
                )
                == "auth-ready"
            )
        finally:
            release.set()
            await asyncio.wait_for(research, timeout=10)

    try:
        asyncio.run(scenario())
    finally:
        release.set()
        research_pool.shutdown(wait=True)


def test_research_dispatch_copies_request_context_and_clears_worker_state(
    monkeypatch,
):
    research_pool = ThreadPoolExecutor(max_workers=1)
    monkeypatch.setattr(
        threadpool, "_get_api_research_executor", lambda: research_pool
    )
    owner = ContextVar("api_research_owner", default=None)

    async def scenario():
        token = owner.set("alice")
        try:

            def first_call():
                set_settings_context({"secret": "alice"})
                return threading.get_ident(), owner.get()

            first_thread, seen_owner = await threadpool.run_research_sync(
                first_call
            )
            second_thread, settings = await threadpool.run_research_sync(
                lambda: (threading.get_ident(), get_settings_context())
            )
            return first_thread, second_thread, seen_owner, settings
        finally:
            owner.reset(token)

    try:
        first_thread, second_thread, seen_owner, settings = asyncio.run(
            scenario()
        )
    finally:
        research_pool.shutdown(wait=True)
    assert first_thread == second_thread
    assert seen_owner == "alice"
    assert settings is None