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.

10.6 KB
Raw Blame
"""Route-level pins for the stale-row reclaim in chat admission.

On both send and retry, ``_check_chat_research_slot_locked``
(web/routers/chat.py) reclaims stale rows before it runs the slot checks in
``_enforce_chat_session_research_slot``. A row is stale when its worker is
dead and it is older than the 30-second spawn grace window. Two kinds are
reclaimed:

* an IN_PROGRESS ``ResearchHistory`` row on the chat session, which would
  otherwise trip the one-research-per-session 409;
* the user's ``UserActiveResearch`` slot, which would otherwise count
  toward the per-user cap and return 429.

These tests drive the real routes against the real per-user database. The
first send's worker is stubbed out, so its research never registers. Its
rows are then aged past the grace window. Each test sends the same request
twice: once while the worker is reported alive, as a control that must be
refused, and once while it is reported dead, which must be admitted. Only
the liveness verdict differs, so the admission is the reclaim's doing.
Each test fails if the reclaim it covers is dropped or moved after the slot
checks.
"""

import json
from contextlib import contextmanager
from datetime import UTC, datetime, timedelta
from unittest.mock import patch

from local_deep_research.constants import ResearchStatus
from local_deep_research.database.models import (
    ResearchHistory,
    UserActiveResearch,
)
from local_deep_research.database.session_context import get_user_db_session
from local_deep_research.web import research_state
from local_deep_research.web.routers import chat

# Far past the 30-second grace window in _check_chat_research_slot_locked.
_STALE_AGE = timedelta(minutes=10)


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


def _create_session(client) -> str:
    response = client.post(
        "/api/chat/sessions",
        json={"initial_query": "Stale reclaim"},
        content_type="application/json",
    )
    assert response.status_code == 200, response.data
    return json.loads(response.data)["session_id"]


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


def _retry(client, session_id: str, research_id: str):
    return client.post(
        f"/api/chat/sessions/{session_id}/attempts/{research_id}/retry"
    )


def _start_research_with_dead_worker(client, session_id: str, content: str):
    """Send a research message whose worker never registers."""
    with patch.object(chat, "start_research_process"):
        response = _send(client, session_id, content)
    assert response.status_code == 200, response.data
    research_id = json.loads(response.data)["research_id"]
    assert research_id
    assert research_state.is_research_thread_alive(research_id) is False
    return research_id


def _age_past_grace_window(username: str, research_id: str) -> None:
    """Backdate the research row and its slot past the grace window."""
    stale_at = datetime.now(UTC) - _STALE_AGE
    with get_user_db_session(username) as db:
        aged_research = (
            db.query(ResearchHistory)
            .filter_by(id=research_id)
            .update({ResearchHistory.created_at: stale_at.isoformat()})
        )
        aged_slots = (
            db.query(UserActiveResearch)
            .filter_by(username=username, research_id=research_id)
            .update({UserActiveResearch.started_at: stale_at})
        )
        db.commit()
    assert (aged_research, aged_slots) == (1, 1)


def _statuses(username: str, research_id: str):
    """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


@contextmanager
def _worker_liveness(*, alive: bool):
    """Report every worker as alive or dead to both reclaim passes.

    The session-row pass reads the name chat.py bound at import, so it is
    stubbed on the chat module. A patch on ``web.routes.globals`` would not
    reach it. ``reclaim_stale_user_active_research`` reads the name from
    ``web.research_state``.
    """
    with (
        patch.object(
            chat, "is_research_thread_alive", return_value=alive
        ) as session_alive,
        patch.object(
            research_state, "is_research_thread_alive", return_value=alive
        ) as slot_alive,
    ):
        yield session_alive, slot_alive


def test_send_reclaims_a_dead_session_research_past_the_grace_window(
    authenticated_client,
):
    """A crashed research on this chat must not 409 the next send.

    Covers the session-row reclaim. Dropping the slot reclaim also fails
    it, through the stale slot's final status.
    """
    client = authenticated_client
    username = _username(client)
    session_id = _create_session(client)
    stale_id = _start_research_with_dead_worker(
        client, session_id, "first question"
    )
    _age_past_grace_window(username, stale_id)

    # Control: the same aged rows with a live worker still hold the slot.
    with (
        _worker_liveness(alive=True),
        patch.object(chat, "start_research_process") as spawn,
    ):
        blocked = _send(client, session_id, "second question")
    assert blocked.status_code == 409, blocked.data
    assert json.loads(blocked.data)["active_research_id"] == stale_id
    spawn.assert_not_called()
    assert _statuses(username, stale_id) == (
        ResearchStatus.IN_PROGRESS,
        ResearchStatus.IN_PROGRESS,
    )

    with (
        _worker_liveness(alive=False) as (session_alive, _slot_alive),
        patch.object(chat, "start_research_process") as spawn,
    ):
        admitted = _send(client, session_id, "second question")
    assert admitted.status_code == 200, admitted.data
    new_id = json.loads(admitted.data)["research_id"]
    assert new_id not in (None, stale_id)
    spawn.assert_called_once()
    assert spawn.call_args.args[0] == new_id
    session_alive.assert_any_call(stale_id)
    assert _statuses(username, stale_id) == (
        ResearchStatus.FAILED,
        ResearchStatus.FAILED,
    )
    assert _statuses(username, new_id) == (
        ResearchStatus.IN_PROGRESS,
        ResearchStatus.IN_PROGRESS,
    )


def test_send_reclaims_a_dead_slot_past_the_grace_window(authenticated_client):
    """A stale slot from another chat must not hold the user's last slot.

    Covers the slot reclaim alone: the other chat has no research row of
    its own, so the session-row reclaim finds nothing there.
    """
    client = authenticated_client
    username = _username(client)
    busy_session = _create_session(client)
    other_session = _create_session(client)

    with patch.object(chat, "clamp_user_max_concurrent", return_value=1):
        stale_id = _start_research_with_dead_worker(
            client, busy_session, "first question"
        )
        _age_past_grace_window(username, stale_id)

        # Control: with a live worker the stale-aged slot still counts.
        with (
            _worker_liveness(alive=True),
            patch.object(chat, "start_research_process") as spawn,
        ):
            blocked = _send(client, other_session, "question elsewhere")
        assert blocked.status_code == 429, blocked.data
        assert "concurrent research limit" in (
            json.loads(blocked.data)["error"].lower()
        )
        spawn.assert_not_called()
        assert _statuses(username, stale_id)[1] == ResearchStatus.IN_PROGRESS

        with (
            _worker_liveness(alive=False) as (_session_alive, slot_alive),
            patch.object(chat, "start_research_process") as spawn,
        ):
            admitted = _send(client, other_session, "question elsewhere")

    assert admitted.status_code == 200, admitted.data
    new_id = json.loads(admitted.data)["research_id"]
    assert new_id not in (None, stale_id)
    spawn.assert_called_once()
    assert spawn.call_args.args[0] == new_id
    slot_alive.assert_any_call(stale_id)
    assert _statuses(username, stale_id)[1] == ResearchStatus.FAILED
    assert _statuses(username, new_id) == (
        ResearchStatus.IN_PROGRESS,
        ResearchStatus.IN_PROGRESS,
    )


def test_retry_reclaims_its_own_dead_attempt_past_the_grace_window(
    authenticated_client,
):
    """Retrying a crashed attempt must not be refused as still running.

    Covers both reclaims on the retry route: without the session-row one
    the retry gets 409, and under a cap of one it gets 429 without the slot
    reclaim.
    """
    client = authenticated_client
    username = _username(client)
    session_id = _create_session(client)

    with patch.object(chat, "clamp_user_max_concurrent", return_value=1):
        stale_id = _start_research_with_dead_worker(
            client, session_id, "crashed question"
        )
        _age_past_grace_window(username, stale_id)

        # Control: a live worker still owns the attempt, so nothing is
        # deleted and the retry is refused.
        with (
            _worker_liveness(alive=True),
            patch.object(chat, "start_research_process") as spawn,
        ):
            blocked = _retry(client, session_id, stale_id)
        assert blocked.status_code == 409, blocked.data
        assert json.loads(blocked.data)["active_research_id"] == stale_id
        spawn.assert_not_called()
        assert _statuses(username, stale_id) == (
            ResearchStatus.IN_PROGRESS,
            ResearchStatus.IN_PROGRESS,
        )

        with (
            _worker_liveness(alive=False) as (session_alive, _slot_alive),
            patch.object(chat, "start_research_process") as spawn,
        ):
            retried = _retry(client, session_id, stale_id)

    assert retried.status_code == 200, retried.data
    new_id = json.loads(retried.data)["research_id"]
    assert new_id not in (None, stale_id)
    spawn.assert_called_once()
    assert spawn.call_args.args[:2] == (new_id, "crashed question")
    session_alive.assert_any_call(stale_id)
    # The reclaimed attempt was replaced, not left behind.
    assert _statuses(username, stale_id) == (None, None)
    assert _statuses(username, new_id) == (
        ResearchStatus.IN_PROGRESS,
        ResearchStatus.IN_PROGRESS,
    )