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.1 KB
Raw Blame
"""A follow-up refused for capacity must actually be refused, with 429.

Both capacity branches in ``_start_followup_sync`` returned Flask's
``jsonify({...}), 429``. ``jsonify`` is not imported in this router, so both
raised ``NameError`` -- and both sit inside ``except Exception:`` blocks:

* The plain at-capacity branch's exception reached the OUTER handler, which
  converted the intended 429 into a generic 500.
* The post-commit recheck branch's exception was caught by an INNER handler
  that only logs a warning, so execution fell through and started the research
  anyway -- moments after that branch had deleted and committed away its
  ``ResearchHistory`` / ``UserActiveResearch`` rows. The per-user cap was
  bypassed in exactly the race it exists to close, and the caller was told
  ``success: True`` for a research with no tracking rows.

`/api/followup/start` is denylisted from the whole-surface smoke test because
it starts real background work, so nothing else covers these branches.

These tests drive the sync worker directly, which is what lets them assert on
the race branch at all.
"""

import threading
from unittest.mock import MagicMock, patch

import pytest
from fastapi.responses import JSONResponse

from local_deep_research.web.routers import followup as followup_mod

MODULE = "local_deep_research.web.routers.followup"
# _start_followup_sync imports its collaborators lazily, inside the function,
# so they never become attributes of the router module -- patch the source
# modules instead.
SESSION_CTX = "local_deep_research.database.session_context.get_user_db_session"
SETTINGS_MGR = "local_deep_research.settings.manager.SettingsManager"
START_RESEARCH = (
    "local_deep_research.web.services.research_service.start_research_process"
)
# The capacity checks sit AFTER the password/session pre-flight, so that has
# to succeed for the branches under test to be reached at all.
RESOLVE_PW = f"{MODULE}.resolve_user_password"


class _FakeQuery:
    def __init__(self, session):
        self._session = session

    def filter_by(self, *a, **k):
        return self

    def filter(self, *a, **k):
        return self

    def count(self):
        return self._session.next_count()

    def delete(self, *a, **k):
        return 1

    def first(self):
        return None

    def all(self):
        return []


class _FakeSession:
    """Serves `counts` to successive .count() calls, in order.

    Only ``.count()`` consumes an entry; ``reclaim_stale_user_active_research``
    issues its own query but ends in ``.all()``, so it does not shift the
    sequence. That keeps the counts list readable as
    [admission_count, recheck_count].
    """

    def __init__(self, counts):
        self._counts = list(counts)
        self.deleted = []
        self.committed = 0

    def query(self, *a, **k):
        return _FakeQuery(self)

    def next_count(self):
        return self._counts.pop(0) if self._counts else 0

    def add(self, obj):
        pass

    def delete(self, obj):
        self.deleted.append(obj)

    def commit(self):
        self.committed += 1

    def rollback(self):
        pass

    def flush(self):
        pass


def _settings(max_concurrent):
    return {
        "app.max_concurrent_researches": {"value": max_concurrent},
        "search.search_strategy": {"value": "source-based"},
        "search.iterations": {"value": 1},
        "search.questions_per_iteration": {"value": 3},
        "llm.provider": {"value": "OLLAMA"},
        "llm.model": {"value": "some-model"},
        "llm.openai_endpoint.url": {"value": None},
    }


@pytest.fixture
def patched(monkeypatch):
    """Patch the router's collaborators; yields a handle to configure counts."""
    state = {
        "session": None,
        "started": [],
        "competing_gate_acquisitions": [],
        "spawn_error": None,
    }

    def _run(counts, max_concurrent):
        session = _FakeSession(counts)
        state["session"] = session

        class _Ctx:
            def __call__(self, *a, **k):
                return self

            def __enter__(self):
                return session

            def __exit__(self, *a):
                return False

        sm = MagicMock()
        sm.get_all_settings.return_value = _settings(max_concurrent)

        svc = MagicMock()
        # Full shape: the spawn path reads these directly, so a partial stub
        # would KeyError and mask the branch under test behind a 500.
        svc.return_value.perform_followup.return_value = {
            "query": "follow-up question",
            "parent_research_id": "parent-1",
            "max_iterations": 1,
            "questions_per_iteration": 3,
            "research_context": {},
        }

        def _start(*a, **k):
            state["started"].append((a, k))
            from local_deep_research.web.research_state import (
                get_user_research_start_lock,
            )

            gate = get_user_research_start_lock("alice")

            def compete_for_gate():
                acquired = gate.acquire(blocking=False)
                state["competing_gate_acquisitions"].append(acquired)
                if acquired:
                    gate.release()

            contender = threading.Thread(target=compete_for_gate)
            contender.start()
            contender.join(timeout=5)
            assert not contender.is_alive()
            if state["spawn_error"] is not None:
                raise state["spawn_error"]

        with (
            patch(SESSION_CTX, _Ctx()),
            patch(SETTINGS_MGR, return_value=sm),
            patch(f"{MODULE}.FollowUpResearchService", svc),
            patch(START_RESEARCH, _start),
            patch(RESOLVE_PW, return_value=("pw", False)),
        ):
            return followup_mod._start_followup_sync(
                {"parent_research_id": "parent-1", "question": "why?"},
                "alice",
            )

    state["run"] = _run
    return state


def test_at_capacity_returns_429_not_500(patched):
    """The plain capacity branch. Previously a NameError -> generic 500."""
    result = patched["run"](counts=[5], max_concurrent=0)

    assert isinstance(result, JSONResponse), f"got {type(result)}"
    assert result.status_code == 429, (
        "at-capacity must surface as 429, not the 500 the NameError produced"
    )
    assert patched["started"] == [], "must not start research when at capacity"


def test_recheck_race_rejects_and_does_not_start_research(patched):
    """The post-commit recheck branch: admission passes, then a concurrent
    start pushes the user over. This is the one that used to fall through and
    start the research anyway, after deleting its own tracking rows."""
    # first count = admission check (under cap), second = recheck (over cap)
    result = patched["run"](counts=[0, 99], max_concurrent=1)

    assert patched["started"] == [], (
        "research was started despite the over-capacity recheck -- the cap is "
        "bypassed in exactly the race it exists to close"
    )
    assert isinstance(result, JSONResponse), f"got {type(result)}"
    assert result.status_code == 429
    assert patched["session"].deleted, (
        "the recheck branch should have rolled back its own tracking row"
    )


@pytest.mark.parametrize("spawn_fails", [False, True])
def test_followup_claim_keeps_gate_through_worker_registration(
    patched, spawn_fails
):
    """Queue replay cannot inspect a new claim before spawn or cleanup."""
    from local_deep_research.web.research_state import (
        get_user_research_start_lock,
    )

    if spawn_fails:
        patched["spawn_error"] = RuntimeError("synthetic spawn failure")

    result = patched["run"](counts=[0, 1], max_concurrent=3)

    assert patched["competing_gate_acquisitions"] == [False]
    assert not get_user_research_start_lock("alice").locked()
    if spawn_fails:
        assert isinstance(result, JSONResponse)
        assert result.status_code == 500
    else:
        assert result["success"] is True


def test_router_module_has_no_jsonify_reference():
    """Cheap structural pin: `jsonify` is a Flask name this module cannot
    resolve, so any reintroduction is a latent NameError."""
    import inspect

    source = inspect.getsource(followup_mod)
    assert "jsonify(" not in source