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.

8.2 KB
Raw Blame
"""Queued-research lifecycle race-safety (#5074), FastAPI port.

Ported from the Flask version main added in #5074 (which registered
``research_bp`` on a bare Flask app). The FastAPI router serves the same
``/api/delete/{id}`` and ``/api/clear_history`` endpoints and the same
``cancel_research`` service function, so these pin the identical invariant:

    a queued/in-progress research CLAIMED by a processing queue row (or still
    in the pre-spawn grace window) must never be deleted/cleared/cancelled out
    from under the worker.

Auth is bypassed with ``dependency_overrides[require_auth]`` and the per-user
DB is patched to a shared in-memory SQLite (StaticPool) so the route's real SQL
runs against seeded rows — mirroring tests/web/routers/test_metrics_star_reviews.
"""

from contextlib import contextmanager

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

from local_deep_research.constants import ResearchStatus
from local_deep_research.database.models import Base
from local_deep_research.database.models.queue import QueueStatus, TaskMetadata
from local_deep_research.database.models.queued_research import QueuedResearch
from local_deep_research.database.models.research import ResearchHistory

RESEARCH_MODULE = "local_deep_research.web.routers.research"
SERVICE_MODULE = "local_deep_research.web.services.research_service"
STATE_MODULE = "local_deep_research.web.research_state"


@pytest.fixture
def client():
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.dependencies.auth import require_auth

    app.dependency_overrides[require_auth] = lambda: "alice"
    try:
        yield TestClient(app, raise_server_exceptions=False)
    finally:
        app.dependency_overrides.pop(require_auth, None)


@pytest.fixture
def db_session():
    engine = create_engine(
        "sqlite://",
        connect_args={"check_same_thread": False},
        poolclass=StaticPool,
    )
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    yield session
    session.close()
    engine.dispose()


def _patch_user_db(monkeypatch, module, db_session):
    @contextmanager
    def _fake(*_, **__):
        yield db_session

    monkeypatch.setattr(f"{module}.get_user_db_session", _fake)


def _csrf(client):
    client.get("/auth/login")
    return client.get("/auth/csrf-token").json().get("csrf_token", "")


def _research(research_id, status):
    return ResearchHistory(
        id=research_id,
        query=research_id,
        mode="quick",
        status=status,
        created_at="2026-07-13T00:00:00+00:00",
    )


def _claimed_queue_row(research_id):
    return QueuedResearch(
        username="alice",
        research_id=research_id,
        query=research_id,
        mode="quick",
        position=1,
        is_processing=True,
    )


def test_delete_does_not_remove_parent_claimed_by_another_session(
    client, db_session, monkeypatch
):
    # A queued parent already claimed before worker registration.
    db_session.add_all(
        [
            _research("claimed", ResearchStatus.QUEUED),
            _claimed_queue_row("claimed"),
            TaskMetadata(
                task_id="claimed", status="queued", task_type="research"
            ),
            QueueStatus(active_tasks=0, queued_tasks=1),
        ]
    )
    db_session.commit()

    _patch_user_db(monkeypatch, RESEARCH_MODULE, db_session)
    # Deletion races with the claim after the active-registry check.
    monkeypatch.setattr(
        f"{RESEARCH_MODULE}.is_research_active", lambda _: False
    )

    token = _csrf(client)
    response = client.delete(
        "/api/delete/claimed", headers={"X-CSRFToken": token}
    )

    # Neither parent nor queue accounting is removed under the live claim.
    assert response.status_code == 400
    assert db_session.query(ResearchHistory).filter_by(id="claimed").one()
    assert (
        db_session.query(QueuedResearch).filter_by(research_id="claimed").one()
    )
    assert db_session.query(TaskMetadata).filter_by(task_id="claimed").one()


def test_clear_history_preserves_in_progress_parent_without_registry_entry(
    client, db_session, monkeypatch
):
    # An in-progress parent that hasn't reached the in-memory registry.
    db_session.add_all(
        [
            _research("spawn-grace", ResearchStatus.IN_PROGRESS),
            _claimed_queue_row("spawn-grace"),
            TaskMetadata(
                task_id="spawn-grace",
                status="processing",
                task_type="research",
            ),
            QueueStatus(active_tasks=1, queued_tasks=0),
        ]
    )
    db_session.commit()

    _patch_user_db(monkeypatch, RESEARCH_MODULE, db_session)
    monkeypatch.setattr(
        f"{RESEARCH_MODULE}.get_active_research_ids", lambda: []
    )

    token = _csrf(client)
    response = client.post("/api/clear_history", headers={"X-CSRFToken": token})

    # The database-level in-progress parent and its accounting survive.
    assert response.status_code == 200
    assert db_session.query(ResearchHistory).filter_by(id="spawn-grace").one()
    assert (
        db_session.query(QueuedResearch)
        .filter_by(research_id="spawn-grace")
        .one()
    )
    assert db_session.query(TaskMetadata).filter_by(task_id="spawn-grace").one()


def test_clear_history_keeps_report_when_conditional_delete_loses_claim_race(
    client, db_session, monkeypatch, tmp_path
):
    report_path = tmp_path / "claimed-report.md"
    report_path.write_text("report", encoding="utf-8")
    research = _research("claimed-report", ResearchStatus.QUEUED)
    research.report_path = str(report_path)
    db_session.add_all(
        [
            research,
            _claimed_queue_row("claimed-report"),
            TaskMetadata(
                task_id="claimed-report",
                status="queued",
                task_type="research",
            ),
            QueueStatus(active_tasks=0, queued_tasks=1),
        ]
    )
    db_session.commit()

    _patch_user_db(monkeypatch, RESEARCH_MODULE, db_session)
    monkeypatch.setattr(
        f"{RESEARCH_MODULE}.get_active_research_ids", lambda: []
    )

    token = _csrf(client)
    response = client.post("/api/clear_history", headers={"X-CSRFToken": token})

    # Both parent and report remain available to the claimed worker.
    assert response.status_code == 200
    assert report_path.exists()
    assert (
        db_session.query(ResearchHistory).filter_by(id="claimed-report").one()
    )


def test_cancel_does_not_cleanup_queued_research_after_claim(
    db_session, monkeypatch
):
    from local_deep_research.web.services.research_service import (
        cancel_research,
    )

    db_session.add_all(
        [
            _research("claimed", ResearchStatus.QUEUED),
            _claimed_queue_row("claimed"),
            TaskMetadata(
                task_id="claimed", status="queued", task_type="research"
            ),
            QueueStatus(active_tasks=0, queued_tasks=1),
        ]
    )
    db_session.commit()

    # cancel_research imports is_research_active/set_termination_flag from
    # research_state at function scope, so patch the source module. (main
    # patches routes.globals; this branch moved that state to research_state.)
    monkeypatch.setattr(f"{STATE_MODULE}.set_termination_flag", lambda _: None)
    monkeypatch.setattr(f"{STATE_MODULE}.is_research_active", lambda _: False)
    # main's #5600 added an ownership gate that opens the user session BEFORE
    # the cancel path re-opens it, and had to switch to a fresh CM per call so
    # a single-use return_value CM is not exhausted on the second enter.
    # _patch_user_db already builds a fresh @contextmanager per call, so it has
    # that property and needs no change.
    _patch_user_db(monkeypatch, SERVICE_MODULE, db_session)

    result = cancel_research("claimed", "alice")

    # The claimed queue state remains for the worker's termination path.
    assert result is True
    assert (
        db_session.query(ResearchHistory).filter_by(id="claimed").one().status
        == ResearchStatus.QUEUED
    )
    assert (
        db_session.query(QueuedResearch).filter_by(research_id="claimed").one()
    )
    assert db_session.query(TaskMetadata).filter_by(task_id="claimed").one()