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.

19.9 KB
Raw Blame
"""
Tests for send_message error-handling and concurrency-cap behavior.

Specifically covers:

* atomic write of user-message + research row in one transaction;
  no orphan ChatMessage when the research insert raises.
* ValueError("not found") from a session-deleted race surfaces
  as HTTP 404, not 500.
* DuplicateResearchError from start_research_process is caught
  explicitly, both side-effect rows are rolled back, response is 409.
* any other spawn failure is cleaned up the same way and answers a
  fixed 500.
* global per-user concurrent-research cap enforced for chat-triggered
  research (via UserActiveResearch + app.max_concurrent_researches).
* the per-user admission gate stays held from the slot recheck until
  start_research_process returns, whatever the spawn does.

tests/chat/test_chat_concurrency_guard.py covers the per-session 409
invariant; these tests cover the remaining error branches.
"""

import json
import threading
from unittest.mock import patch

import pytest

from local_deep_research.constants import ResearchStatus
from local_deep_research.database.models import (
    ChatMessage,
    ChatSession,
    ResearchHistory,
    UserActiveResearch,
)
from local_deep_research.database.session_context import get_user_db_session
from local_deep_research.database.thread_local_session import thread_cleanup
from local_deep_research.exceptions import (
    DuplicateResearchError,
    SystemAtCapacityError,
)
from local_deep_research.web.research_state import (
    get_user_research_start_lock,
)

_SPAWN = "local_deep_research.web.routers.chat.start_research_process"


def _create_session(client, query="Test"):
    resp = client.post(
        "/api/chat/sessions",
        json={"initial_query": query},
        content_type="application/json",
    )
    assert resp.status_code == 200, resp.data
    return json.loads(resp.data)["session_id"]


def _username(client) -> str:
    resp = client.get("/auth/check")
    assert resp.status_code == 200, resp.text
    return resp.json()["username"]


def _send(client, session_id, content):
    return client.post(
        f"/api/chat/sessions/{session_id}/messages",
        json={"content": content, "trigger_research": True},
        content_type="application/json",
    )


def _claim_statuses(username, research_id):
    """Return ``(research status, slot status)``; None for a missing row."""
    with get_user_db_session(username) as db:
        research = db.query(ResearchHistory).filter_by(id=research_id).first()
        slot = (
            db.query(UserActiveResearch)
            .filter_by(username=username, research_id=research_id)
            .first()
        )
        statuses = (
            None if research is None else research.status,
            None if slot is None else slot.status,
        )
        # The session is thread-local and reused: end the read here.
        db.rollback()
    return statuses


def _send_leftovers(username, session_id, research_id, content):
    """Count what one send left behind.

    Returns ``(research rows, slot rows, user messages with this content,
    the session's message_count)``.
    """
    with get_user_db_session(username) as db:
        leftovers = (
            db.query(ResearchHistory).filter_by(id=research_id).count(),
            db.query(UserActiveResearch)
            .filter_by(username=username, research_id=research_id)
            .count(),
            db.query(ChatMessage)
            .filter_by(session_id=session_id, content=content)
            .count(),
            db.query(ChatSession.message_count)
            .filter_by(id=session_id)
            .scalar(),
        )
        db.rollback()
    return leftovers


class TestSessionDeletedRace:
    """When the session row vanishes between the existence-check and the
    UPDATE...RETURNING inside insert_message_in_db, the route must return
    HTTP 404, not a generic 500.
    """

    def test_deleted_session_after_existence_check_returns_404(
        self, authenticated_client
    ):
        session_id = _create_session(authenticated_client)

        # Simulate the race: get_session sees the session, but by the
        # time insert_message_in_db runs the UPDATE...RETURNING the
        # row is gone. We patch the helper to raise the same
        # "not found" ValueError the real code raises when
        # scalar_one_or_none() returns None.
        with patch(
            "local_deep_research.chat.service.ChatService.insert_message_in_db",
            side_effect=ValueError(f"Chat session {session_id} not found"),
        ):
            resp = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/messages",
                json={"content": "hello", "trigger_research": True},
                content_type="application/json",
            )

        assert resp.status_code == 404, resp.data
        body = json.loads(resp.data)
        assert body["success"] is False
        assert "not found" in body["error"].lower()


class TestDuplicateResearchErrorCleanup:
    """start_research_process can raise DuplicateResearchError,
    which inherits from Exception and is NOT in ROUTE_EXCEPTIONS. It is
    caught explicitly, returns 409, and rolls back the user
    message + research row this request just committed (per the
    DuplicateResearchError docstring's 'do not touch
    UserActiveResearch' rule)."""

    def test_duplicate_research_returns_409_not_500(self, authenticated_client):
        from local_deep_research.exceptions import DuplicateResearchError

        session_id = _create_session(authenticated_client)

        with patch(
            "local_deep_research.web.routers.chat.start_research_process",
            side_effect=DuplicateResearchError("simulated"),
        ):
            resp = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/messages",
                json={"content": "first", "trigger_research": True},
                content_type="application/json",
            )

        assert resp.status_code == 409, resp.data
        body = json.loads(resp.data)
        assert body["success"] is False
        assert "in progress" in body["error"].lower()

    def test_duplicate_research_does_not_orphan_user_message(
        self, authenticated_client
    ):
        """After a DuplicateResearchError-induced 409, the chat must
        not contain the rejected user message (i.e. cleanup ran)."""
        from local_deep_research.exceptions import DuplicateResearchError

        session_id = _create_session(authenticated_client)

        with patch(
            "local_deep_research.web.routers.chat.start_research_process",
            side_effect=DuplicateResearchError("simulated"),
        ):
            blocked = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/messages",
                json={
                    "content": "should-be-cleaned-up",
                    "trigger_research": True,
                },
                content_type="application/json",
            )
            assert blocked.status_code == 409

        # Read messages back; the rejected user message must not be
        # in the durable message list. The ChatSession.message_count
        # would also be off-by-one if the cleanup had failed.
        list_resp = authenticated_client.get(
            f"/api/chat/sessions/{session_id}/messages"
        )
        assert list_resp.status_code == 200
        msgs = json.loads(list_resp.data)["messages"]
        user_contents = [m["content"] for m in msgs if m["role"] == "user"]
        assert "should-be-cleaned-up" not in user_contents


class TestGlobalPerUserCap:
    """send_message must respect app.max_concurrent_researches the
    same way research_routes.start_research does; otherwise multiple
    chat tabs would let a user bypass the cap.

    The end-to-end "saturate UserActiveResearch then verify 429"
    scenario requires injecting rows into the per-user encrypted DB
    from outside the request context, which is awkward. Instead we
    patch the SettingsManager to return cap=0 so any non-negative
    active_count trips the 429 — this exercises the same code path
    (the cap-comparison in send_message) without the encrypted-DB
    plumbing.
    """

    def test_chat_send_blocks_at_429_when_cap_exceeded(
        self, authenticated_client
    ):
        session_id = _create_session(authenticated_client)

        with (
            patch(
                "local_deep_research.settings.manager.SettingsManager.get_setting",
                return_value=0,
            ),
            # send_message now clamps the stored setting through
            # clamp_user_max_concurrent (see #5549), which floors any
            # non-positive value to 1 -- so cap=0 alone no longer trips
            # the 429 (a floored max_concurrent=1 lets active_count=0
            # through). Also clamp the global ceiling itself to 0 so the
            # post-floor min(1, ceiling) still comes out at 0, preserving
            # this test's "cap exceeded with zero active researches"
            # scenario.
            patch(
                "local_deep_research.web.services.research_service."
                "_MAX_GLOBAL_CONCURRENT",
                0,
            ),
            patch(
                "local_deep_research.web.routers.chat.start_research_process"
            ),
        ):
            resp = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/messages",
                json={"content": "blocked-by-cap", "trigger_research": True},
                content_type="application/json",
            )

        assert resp.status_code == 429, resp.data
        body = json.loads(resp.data)
        assert body["success"] is False
        assert "concurrent research limit" in body["error"].lower()

    def test_chat_send_inserts_active_research_so_cap_counts_it(
        self, authenticated_client
    ):
        """A chat research send must insert a UserActiveResearch row so it
        counts toward the per-user cap. With cap=1, a first send (session A)
        succeeds and a second send on a DIFFERENT session (B) is then blocked
        at 429 — which only happens if the first send actually recorded an
        active-research row."""

        def _cap_one(key, default=None, *args, **kwargs):
            # Force the per-user cap to 1; every other setting falls back to
            # the caller-provided default so the send path is otherwise normal.
            if key == "app.max_concurrent_researches":
                return 1
            return default

        session_a = _create_session(authenticated_client)
        session_b = _create_session(authenticated_client)

        with (
            patch(
                "local_deep_research.settings.manager.SettingsManager.get_setting",
                side_effect=_cap_one,
            ),
            # No-op the spawn so the first research stays "in progress"
            # (its UserActiveResearch row is never cleaned up by completion).
            patch(
                "local_deep_research.web.routers.chat.start_research_process"
            ),
            # Treat the first research's (stubbed-spawn) worker as alive.
            # The stub never registers it, so a stall of more than 30
            # seconds between the two posts would age its UserActiveResearch
            # row past the admission grace window. The second send's slot
            # reclaim would then free it: a flaky 200 instead of 429. That
            # reclaim (reclaim_stale_user_active_research) looks the liveness
            # name up in web.research_state. A patch on web.routes.globals
            # reaches neither it nor chat.py, which binds its names at
            # import. The before_request cleanup middleware that also
            # deleted such rows under Flask has no FastAPI successor.
            patch(
                "local_deep_research.web.research_state.is_research_thread_alive",
                return_value=True,
            ),
        ):
            first = authenticated_client.post(
                f"/api/chat/sessions/{session_a}/messages",
                json={"content": "first research", "trigger_research": True},
                content_type="application/json",
            )
            second = authenticated_client.post(
                f"/api/chat/sessions/{session_b}/messages",
                json={"content": "second research", "trigger_research": True},
                content_type="application/json",
            )

        assert first.status_code == 200, first.data
        assert second.status_code == 429, second.data
        body = json.loads(second.data)
        assert body["success"] is False
        assert "concurrent research limit" in body["error"].lower()


class TestSystemAtCapacityErrorCleanup:
    """start_research_process can also raise
    SystemAtCapacityError when the global semaphore is exhausted AFTER
    the pre-spawn cap check passes (race against a parallel request).
    The branch is symmetric with DuplicateResearchError — both must
    return 429-class status and clean up the user-message + research
    rows the request just committed.
    """

    def test_capacity_returns_429_not_500(self, authenticated_client):
        from local_deep_research.exceptions import SystemAtCapacityError

        session_id = _create_session(authenticated_client)

        with patch(
            "local_deep_research.web.routers.chat.start_research_process",
            side_effect=SystemAtCapacityError("simulated"),
        ):
            resp = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/messages",
                json={
                    "content": "blocked-by-capacity",
                    "trigger_research": True,
                },
                content_type="application/json",
            )

        assert resp.status_code == 429, resp.data
        body = json.loads(resp.data)
        assert body["success"] is False
        # The route surfaces a capacity-related error string; assert on
        # "capacity"/"busy"/"try again" so the exact wording can evolve.
        msg = body["error"].lower()
        assert "capacity" in msg or "busy" in msg or "try again" in msg

    def test_capacity_does_not_orphan_user_message(self, authenticated_client):
        """After a SystemAtCapacityError-induced 429, the rejected
        user message must not appear in the chat's durable message
        list. Mirrors TestDuplicateResearchErrorCleanup — if the
        ``_cleanup_chat_send_rows("capacity")`` branch regresses, the
        user-message and ResearchHistory rows become orphans and
        ``ChatSession.message_count`` is off-by-one.
        """
        from local_deep_research.exceptions import SystemAtCapacityError

        session_id = _create_session(authenticated_client)

        with patch(
            "local_deep_research.web.routers.chat.start_research_process",
            side_effect=SystemAtCapacityError("simulated"),
        ):
            blocked = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/messages",
                json={
                    "content": "should-be-cleaned-up-on-capacity",
                    "trigger_research": True,
                },
                content_type="application/json",
            )
            assert blocked.status_code == 429

        list_resp = authenticated_client.get(
            f"/api/chat/sessions/{session_id}/messages"
        )
        assert list_resp.status_code == 200
        msgs = json.loads(list_resp.data)["messages"]
        user_contents = [m["content"] for m in msgs if m["role"] == "user"]
        assert "should-be-cleaned-up-on-capacity" not in user_contents


class TestGenericSpawnFailureCleanup:
    """start_research_process can fail for reasons other than a duplicate
    or a capacity rejection, for example a RuntimeError from Thread.start.
    The route must undo the message, research row and per-user slot it
    committed, and answer a fixed 500. Without that branch the RuntimeError
    falls through to the route-wide handler. That is still a 500, but the
    leftover IN_PROGRESS row then refuses the next send on this chat with
    409 until the 30-second grace window has passed, and the slot counts
    toward the per-user cap until a later sweep reclaims it.
    """

    def test_spawn_failure_returns_500_and_removes_the_committed_rows(
        self, authenticated_client
    ):
        client = authenticated_client
        username = _username(client)
        session_id = _create_session(client)
        content = "should-be-cleaned-up-on-spawn-failure"
        before = _send_leftovers(username, session_id, "none", content)
        assert before[:3] == (0, 0, 0)
        spawned = []

        def fail_spawn(research_id, *_args, **_kwargs):
            spawned.append(research_id)
            raise RuntimeError("synthetic private worker details")

        with patch(_SPAWN, side_effect=fail_spawn):
            resp = _send(client, session_id, content)

        assert resp.status_code == 500, resp.data
        body = json.loads(resp.data)
        assert body["success"] is False
        assert b"synthetic" not in resp.data
        assert len(spawned) == 1
        assert (
            _send_leftovers(username, session_id, spawned[0], content) == before
        )

        # Nothing is left inside the grace window to refuse the next send.
        with patch(_SPAWN) as spawn:
            again = _send(client, session_id, "send again after the failure")
        assert again.status_code == 200, again.data
        spawn.assert_called_once()


class TestAdmissionGateHeldThroughRegistration:
    """send_message holds the per-user admission gate from its authoritative
    slot recheck through the claim commit and start_research_process.

    A queue tick or a login reconcile that took the gate between the commit
    and the worker's registration would find a committed claim with no live
    worker and reclaim it as failed. While the spawn runs, another thread
    reads the committed claim and tries a non-blocking acquire of the same
    gate, which must fail. Narrowing the hold back to a ``with`` block
    around the recheck alone lets that acquire succeed. Once the route has
    answered, the gate must be free again, whatever the spawn did.
    """

    @pytest.mark.parametrize(
        ("spawn_error", "expected_status"),
        [
            (None, 200),
            (RuntimeError("synthetic worker start failure"), 500),
            (SystemAtCapacityError("synthetic capacity failure"), 429),
            (DuplicateResearchError("synthetic duplicate failure"), 409),
        ],
        ids=["registered", "spawn-error", "capacity", "duplicate"],
    )
    def test_competing_acquire_fails_until_the_spawn_returns(
        self, authenticated_client, spawn_error, expected_status
    ):
        client = authenticated_client
        username = _username(client)
        session_id = _create_session(client)
        gate = get_user_research_start_lock(username)
        seen_by_contender = []

        @thread_cleanup
        def contend(research_id):
            acquired = gate.acquire(blocking=False)
            if acquired:
                gate.release()
            seen_by_contender.append(
                (_claim_statuses(username, research_id), acquired)
            )

        def at_registration(research_id, *_args, **_kwargs):
            # Record, do not assert, here: the route turns an exception
            # raised by the spawn into a 500, which would hide the failure.
            contender = threading.Thread(target=contend, args=(research_id,))
            contender.start()
            contender.join(timeout=10)
            if spawn_error is not None:
                raise spawn_error

        with patch(_SPAWN, side_effect=at_registration) as spawn:
            resp = _send(client, session_id, "gate held through the spawn")

        assert resp.status_code == expected_status, resp.data
        spawn.assert_called_once()
        # The claim was committed and visible to another thread, yet that
        # thread could not take the gate.
        assert seen_by_contender == [
            ((ResearchStatus.IN_PROGRESS, ResearchStatus.IN_PROGRESS), False)
        ]
        assert not gate.locked()