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.

31.1 KB
Raw Blame
"""Port of ``tests/followup_research/test_routes_coverage.py``.

That file (17 tests, deleted by the Flask->FastAPI migration) drove the
bodies of ``/api/followup/prepare`` and ``/api/followup/start``: the
missing-field 400s, the parent-not-found 404, the populated-context 200,
both routes' 500 handlers, the empty-``llm.model`` pre-flight, the SSRF
pre-flight on ``llm.openai_endpoint.url``, the spawn-failure cleanup, and
the per-user concurrency cap.

What already exists on the branch and is NOT re-ported here:
``tests/web/routers/test_followup_body_contract.py`` (non-object / malformed
bodies), ``tests/web/routers/test_followup_capacity_reject.py`` (the
post-commit race recheck) and ``tests/test_followup_api.py`` (happy path +
session-password 401). None of them touch the SSRF pre-flight, the
empty-model guard, the spawn-failure FAILED flip, the up-front admission
cap, or either 500 handler — hence this file.

Translation notes (plumbing only; the assertions are the originals):

* Flask blueprint app -> the real FastAPI app with ``require_auth``
  dependency-overridden (the idiom of ``test_benchmark_defaults_fence.py``).
* ``followup_research.routes`` -> ``web.routers.followup``.
* Flask ``session["username"]`` + ``db_manager.is_user_connected`` patching
  -> the dependency override.
* ``resp.get_json()`` -> ``resp.json()``; POSTs carry a session CSRF token.
* ``session_password_store`` patching -> patching
  ``web.routers.followup.resolve_user_password``, the helper the FastAPI
  route calls. The Flask ``g.user_password`` / ``temp_auth_store`` legs of
  the old 3-source chain are gone by design (see the module docstring of
  ``web/auth/password_utils.py``), so
  ``test_password_fallback_to_temp_auth`` is dropped as Flask-only
  plumbing. ``test_password_fallback_to_g_user_password`` survives as
  :meth:`TestStartFollowupNoPassword.test_no_password_on_unencrypted_db_still_starts`
  — its observable assertion was "still 200 when the store yields None".
* The FastAPI ``/start`` gained a parent-ownership 404 ahead of the business
  logic, so ``load_parent_research`` must return something truthy in every
  start test (the ``Mock()`` service does, as it did on main).
"""

from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch

import pytest
from fastapi.testclient import TestClient


TEST_USERNAME = "testuser"

MODULE = "local_deep_research.web.routers.followup"
SETTINGS_MANAGER = "local_deep_research.settings.manager.SettingsManager"
DB_SESSION_CTX = (
    "local_deep_research.database.session_context.get_user_db_session"
)
RESEARCH_HISTORY = "local_deep_research.database.models.ResearchHistory"
START_RESEARCH = (
    "local_deep_research.web.services.research_service.start_research_process"
)
RUN_RESEARCH = (
    "local_deep_research.web.services.research_service.run_research_process"
)
RESOLVE_PASSWORD = f"{MODULE}.resolve_user_password"


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _make_app():
    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: TEST_USERNAME
    return app


@pytest.fixture(autouse=True)
def _clear_overrides():
    yield
    from local_deep_research.web.fastapi_app import app
    from local_deep_research.web.dependencies.auth import require_auth

    app.dependency_overrides.pop(require_auth, None)


@pytest.fixture()
def app():
    return _make_app()


_ip_counter = iter(range(1, 60000))


def _authed_post(app, path, json_body):
    """POST as the overridden user, carrying a session CSRF token and a
    per-test X-Forwarded-For so no limiter bucket is shared."""
    client = TestClient(app, raise_server_exceptions=False)
    n = next(_ip_counter)
    client.headers["X-Forwarded-For"] = f"10.78.{n // 250 % 250}.{n % 250}"
    token = client.get("/auth/csrf-token")
    assert token.status_code == 200, token.text
    return client.post(
        path,
        json=json_body,
        headers={"X-CSRFToken": token.json()["csrf_token"]},
    )


def _fake_settings_snapshot():
    return {
        "search.search_strategy": {"value": "source-based"},
        "search.iterations": {"value": 2},
        "search.questions_per_iteration": {"value": 4},
        "llm.provider": {"value": "OLLAMA"},
        "llm.model": {"value": "gemma3:12b"},
        "search.tool": {"value": "searxng"},
        "llm.openai_endpoint.url": {"value": None},
    }


def _make_settings_mock(snapshot=None):
    mock_sm = Mock()
    mock_sm.get_all_settings.return_value = (
        snapshot if snapshot is not None else _fake_settings_snapshot()
    )
    return mock_sm


@contextmanager
def _mock_db_session_ctx(mock_db=None):
    """Patch the lazily-imported get_user_db_session to yield a MagicMock."""
    mock_db = MagicMock() if mock_db is None else mock_db

    @contextmanager
    def fake_get_user_db_session(*args, **kwargs):
        yield mock_db

    with patch(DB_SESSION_CTX, side_effect=fake_get_user_db_session):
        yield mock_db


_RESEARCH_PARAMS = {
    "query": "follow-up question",
    "max_iterations": 2,
    "questions_per_iteration": 4,
    "delegate_strategy": "source-based",
    "research_context": {"summary": "context"},
    "parent_research_id": "parent-1",
}


def _service_mock(params=None):
    """A FollowUpResearchService stand-in.

    ``load_parent_research`` returns a truthy Mock by default, which is what
    the FastAPI ``/start`` parent-ownership gate needs to let the request
    through to the logic under test.
    """
    svc = Mock()
    svc.perform_followup.return_value = dict(params or _RESEARCH_PARAMS)
    return svc


class TestFollowupRouteMethods:
    """From ``tests/followup_research/test_routes.py`` (deleted).

    Its blueprint-name/url-prefix assertions were Flask implementation
    detail and its "route exists" + 401 assertions are covered by
    ``tests/test_followup_api.py::
    test_prepare_and_start_are_the_only_followup_routes`` and
    ``tests/security/test_unauthenticated_reachability_census.py``. The one
    property neither of those pins is the HTTP METHOD: the path census
    compares ``route.path`` only, so a route that quietly grew a GET would
    not be noticed. Both follow-up routes mutate state and must stay
    POST-only.
    """

    @pytest.mark.parametrize(
        "path", ["/api/followup/prepare", "/api/followup/start"]
    )
    def test_route_is_post_only(self, path):
        from local_deep_research.web.routers.followup import router

        methods = {
            frozenset(route.methods)
            for route in router.routes
            if getattr(route, "path", None) == path
        }
        assert methods == {frozenset({"POST"})}, methods

    @pytest.mark.parametrize(
        "path", ["/api/followup/prepare", "/api/followup/start"]
    )
    def test_get_is_method_not_allowed(self, app, path):
        client = TestClient(app, raise_server_exceptions=False)
        assert client.get(path, follow_redirects=False).status_code == 405


# ---------------------------------------------------------------------------
# prepare_followup: missing fields
# ---------------------------------------------------------------------------


class TestPrepareFollowupMissingFields:
    """prepare_followup returns 400 when required fields are absent."""

    def test_missing_parent_id_returns_400(self, app):
        resp = _authed_post(app, "/api/followup/prepare", {"question": "why?"})
        assert resp.status_code == 400, resp.text
        data = resp.json()
        assert data["success"] is False
        assert "Missing" in data["error"]

    def test_missing_question_returns_400(self, app):
        resp = _authed_post(
            app, "/api/followup/prepare", {"parent_research_id": "abc-123"}
        )
        assert resp.status_code == 400, resp.text
        data = resp.json()
        assert data["success"] is False
        assert "Missing" in data["error"]


# ---------------------------------------------------------------------------
# prepare_followup: parent not found
# ---------------------------------------------------------------------------


class TestPrepareFollowupParentNotFound:
    def test_parent_not_found_returns_404(self, app):
        """Returns 404 with success=False when parent_research_id has no row.

        Earlier code returned 200 + fabricated placeholder data here, which
        let the frontend silently render dummy context and trigger a
        follow-up LLM call against a ghost parent. The contract now matches
        the rest of the API: 404 means "not found".
        """
        mock_service = Mock()
        mock_service.load_parent_research.return_value = None

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
        ):
            resp = _authed_post(
                app,
                "/api/followup/prepare",
                {"parent_research_id": "missing-id", "question": "follow?"},
            )

        assert resp.status_code == 404, resp.text
        data = resp.json()
        assert data["success"] is False
        assert "Parent research not found" in data["error"]


# ---------------------------------------------------------------------------
# prepare_followup: parent found with resources
# ---------------------------------------------------------------------------


class TestPrepareFollowupParentFound:
    def test_parent_found_with_resources(self, app):
        """Returns parent summary and correct source count when parent exists."""
        mock_service = Mock()
        mock_service.load_parent_research.return_value = {
            "query": "original question",
            "resources": [{"url": "http://a.com"}, {"url": "http://b.com"}],
        }

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
        ):
            resp = _authed_post(
                app,
                "/api/followup/prepare",
                {"parent_research_id": "found-id", "question": "more?"},
            )

        assert resp.status_code == 200, resp.text
        data = resp.json()
        assert data["success"] is True
        assert data["parent_summary"] == "original question"
        assert data["available_sources"] == 2
        assert data["parent_research"]["sources_count"] == 2
        assert data["parent_research"]["id"] == "found-id"
        # The suggested strategy comes from the settings snapshot, not a
        # hard-coded literal.
        assert data["suggested_strategy"] == "source-based"


# ---------------------------------------------------------------------------
# prepare_followup: exception path
# ---------------------------------------------------------------------------


class TestPrepareFollowupException:
    def test_exception_in_service_returns_500(self, app):
        mock_service = Mock()
        mock_service.load_parent_research.side_effect = RuntimeError("boom")

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
        ):
            resp = _authed_post(
                app,
                "/api/followup/prepare",
                {"parent_research_id": "x", "question": "y"},
            )

        assert resp.status_code == 500, resp.text
        data = resp.json()
        assert data["success"] is False
        assert "internal error" in data["error"].lower()


# ---------------------------------------------------------------------------
# start_followup: spawn failure
# ---------------------------------------------------------------------------


class TestStartFollowupSpawnFailure:
    """If start_research_process raises, flip ResearchHistory.status to FAILED
    and return 500 — don't leave the row orphaned as IN_PROGRESS."""

    def test_spawn_failure_marks_research_failed(self, app):
        from local_deep_research.constants import ResearchStatus

        mock_service = _service_mock()

        research_row = Mock()
        mock_db = MagicMock()
        mock_db.query.return_value.filter_by.return_value.first.return_value = (
            research_row
        )
        # Admission check: nothing stale, 0 live researches.
        mock_db.query.return_value.filter_by.return_value.count.return_value = 0
        mock_db.query.return_value.filter.return_value.all.return_value = []

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(mock_db),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH, side_effect=RuntimeError("spawn failed")),
            patch(RUN_RESEARCH),
            patch(RESEARCH_HISTORY),
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 500, resp.text
        data = resp.json()
        assert data["success"] is False
        # ResearchHistory row was flipped to FAILED by the cleanup handler.
        assert research_row.status == ResearchStatus.FAILED
        # Cleanup commit was issued.
        mock_db.commit.assert_called()


class TestStartFollowupSuccess:
    def test_start_success_with_session_password(self, app):
        """Successful start; the resolved password reaches the spawn."""
        mock_service = _service_mock()
        mock_db = MagicMock()
        mock_db.query.return_value.filter_by.return_value.count.return_value = 0
        mock_db.query.return_value.filter.return_value.all.return_value = []

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(mock_db),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH) as mock_start,
            patch(RUN_RESEARCH),
            patch(RESEARCH_HISTORY),
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 200, resp.text
        data = resp.json()
        assert data["success"] is True
        assert "research_id" in data
        assert data["message"] == "Follow-up research started"
        assert (
            mock_start.call_args.kwargs["user_password"] == "secret-password"
        )  # gitleaks:allow
        # The snapshot stores "OLLAMA"; every provider comparison in the
        # codebase uses the lowercase canonical form, so the route must
        # pass the value through normalize_provider before spawning. The
        # news router lost exactly this wrapper at one call site (#5974)
        # while the helper survived elsewhere — assert the normalized
        # value, not just that a provider was passed.
        assert mock_start.call_args.kwargs["model_provider"] == "ollama"


class TestStartFollowupModelRequired:
    def test_empty_model_returns_400_before_spawn(self, app):
        """Empty ``llm.model`` returns HTTP 400 with an actionable message
        *before* any ResearchHistory row is written or worker thread spawned,
        mirroring research_routes.start_research. Without this guard the
        worker thread dies and leaves an orphan IN_PROGRESS row.
        """
        mock_service = _service_mock()
        snapshot = _fake_settings_snapshot()
        snapshot["llm.model"] = {"value": ""}

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock(snapshot)),
            _mock_db_session_ctx(),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH) as mock_start,
            patch(RUN_RESEARCH) as mock_run,
            patch(RESEARCH_HISTORY) as mock_history,
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 400, resp.text
        data = resp.json()
        assert data["success"] is False
        assert "Model is required" in data["error"]
        # No orphan ResearchHistory row and no worker thread were created.
        mock_history.assert_not_called()
        mock_start.assert_not_called()
        mock_run.assert_not_called()


class TestStartFollowupCustomEndpointSSRF:
    """SSRF pre-flight on llm.openai_endpoint.url.

    The endpoint URL is later handed to the OpenAI client (httpx) with no
    SafeSession wrapping, so the route layer is the only place to reject
    cloud-metadata / link-local targets. Like the empty-model guard above,
    the check fires BEFORE any ResearchHistory row is written so a rejected
    request leaves no orphan IN_PROGRESS row.
    """

    _AWS_METADATA = (
        "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
    )

    def _snapshot_with_endpoint(self, url, provider="OPENAI_ENDPOINT"):
        snapshot = _fake_settings_snapshot()
        snapshot["llm.openai_endpoint.url"] = {"value": url}
        snapshot["llm.provider"] = {"value": provider}
        return snapshot

    def test_metadata_endpoint_rejected_before_db_write(self, app):
        """Cloud metadata URLs are blocked before ResearchHistory is created."""
        mock_service = _service_mock()

        with (
            patch(
                SETTINGS_MANAGER,
                return_value=_make_settings_mock(
                    self._snapshot_with_endpoint(self._AWS_METADATA)
                ),
            ),
            _mock_db_session_ctx(),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH) as mock_start,
            patch(RUN_RESEARCH) as mock_run,
            patch(RESEARCH_HISTORY) as mock_history,
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 400, resp.text
        data = resp.json()
        assert data["success"] is False
        assert "endpoint" in data["error"].lower()
        mock_history.assert_not_called()
        mock_start.assert_not_called()
        mock_run.assert_not_called()

    def test_garbage_url_rejected_before_db_write(self, app):
        """Malformed URLs are rejected before ResearchHistory is created."""
        mock_service = _service_mock(
            {
                "query": "q",
                "max_iterations": 1,
                "questions_per_iteration": 1,
                "delegate_strategy": "source-based",
                "research_context": {},
                "parent_research_id": "parent-1",
            }
        )

        with (
            patch(
                SETTINGS_MANAGER,
                return_value=_make_settings_mock(
                    self._snapshot_with_endpoint("not-a-url")
                ),
            ),
            _mock_db_session_ctx(),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH) as mock_start,
            patch(RUN_RESEARCH),
            patch(RESEARCH_HISTORY) as mock_history,
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 400, resp.text
        mock_history.assert_not_called()
        mock_start.assert_not_called()

    def test_localhost_endpoint_accepted(self, app):
        """Local LLM endpoints on localhost pass validation and reach the spawn."""
        mock_service = _service_mock(
            {
                "query": "q",
                "max_iterations": 1,
                "questions_per_iteration": 1,
                "delegate_strategy": "source-based",
                "research_context": {},
                "parent_research_id": "parent-1",
            }
        )
        mock_db = MagicMock()
        mock_db.query.return_value.filter_by.return_value.count.return_value = 0
        mock_db.query.return_value.filter.return_value.all.return_value = []

        with (
            patch(
                SETTINGS_MANAGER,
                return_value=_make_settings_mock(
                    self._snapshot_with_endpoint("http://localhost:11434/v1")
                ),
            ),
            _mock_db_session_ctx(mock_db),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH) as mock_start,
            patch(RUN_RESEARCH),
            patch(RESEARCH_HISTORY),
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 200, resp.text
        mock_start.assert_called_once()
        # The validated endpoint is what gets handed to the worker.
        assert (
            mock_start.call_args.kwargs["custom_endpoint"]
            == "http://localhost:11434/v1"
        )

    def test_endpoint_not_forwarded_for_other_providers(self, app):
        """llm.openai_endpoint.url is only the client's base URL for the
        openai_endpoint provider. For any other provider a stale value
        (default: the OpenRouter URL) must not reach the worker, where it
        would mislabel that provider's endpoint in error messages."""
        mock_service = _service_mock(
            {
                "query": "q",
                "max_iterations": 1,
                "questions_per_iteration": 1,
                "delegate_strategy": "source-based",
                "research_context": {},
                "parent_research_id": "parent-1",
            }
        )
        mock_db = MagicMock()
        mock_db.query.return_value.filter_by.return_value.count.return_value = 0
        mock_db.query.return_value.filter.return_value.all.return_value = []

        with (
            patch(
                SETTINGS_MANAGER,
                return_value=_make_settings_mock(
                    self._snapshot_with_endpoint(
                        "https://openrouter.ai/api/v1", provider="QWEN"
                    )
                ),
            ),
            _mock_db_session_ctx(mock_db),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH) as mock_start,
            patch(RUN_RESEARCH),
            patch(RESEARCH_HISTORY),
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 200, resp.text
        mock_start.assert_called_once()
        assert mock_start.call_args.kwargs["model_provider"] == "qwen"
        assert mock_start.call_args.kwargs["custom_endpoint"] is None


class TestStartFollowupNoPassword:
    def test_no_password_on_unencrypted_db_still_starts(self, app):
        """No password available + unencrypted DB: warn and continue (200).

        On an encrypted DB the same condition is a 401 — that half is
        covered by ``tests/test_followup_api.py::
        test_start_followup_requires_a_live_session_password``. This is the
        unencrypted half, and it also carries the ported assertion that the
        "No password available" warning is emitted (it moved from the route
        into ``web/auth/password_utils.py``).
        """
        mock_service = _service_mock(
            {
                "query": "q",
                "max_iterations": 1,
                "questions_per_iteration": 3,
                "delegate_strategy": "source-based",
                "research_context": {},
                "parent_research_id": "p1",
            }
        )
        mock_db = MagicMock()
        mock_db.query.return_value.filter_by.return_value.count.return_value = 0
        mock_db.query.return_value.filter.return_value.all.return_value = []
        mock_db_mgr = MagicMock(has_encryption=False)

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(mock_db),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH),
            patch(RUN_RESEARCH),
            patch(RESEARCH_HISTORY),
            patch(
                "local_deep_research.web.auth.password_utils.logger"
            ) as mock_logger,
            patch(
                "local_deep_research.database.encrypted_db.db_manager",
                mock_db_mgr,
            ),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "p1", "question": "q?"},
            )

        assert resp.status_code == 200, resp.text
        assert resp.json()["success"] is True
        warning_calls = [str(c) for c in mock_logger.warning.call_args_list]
        assert any("No password available" in call for call in warning_calls)


class TestStartFollowupException:
    def test_exception_returns_500(self, app):
        """Returns 500 when an unexpected exception occurs in start_followup."""
        mock_service = Mock()
        mock_service.perform_followup.side_effect = RuntimeError("kaboom")

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(),
            # Auth precedes authz/business logic: give the caller a valid
            # password so the 401 session-expired guard passes and the flow
            # reaches perform_followup (which raises -> the 500 under test).
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "x", "question": "y"},
            )

        assert resp.status_code == 500, resp.text
        data = resp.json()
        assert data["success"] is False
        assert "internal error" in data["error"].lower()


class TestStartFollowupConcurrencyCap:
    """Follow-ups must enforce the SAME per-user concurrency cap as
    research_routes.start_research.

    Before this guard, /api/followup/start created no UserActiveResearch row
    and ran no admission check -- only the global research semaphore gated
    it. A single authenticated user could fire many rapid follow-up starts
    and monopolize the entire global research budget, starving other tenants
    with 429s.
    """

    def test_at_cap_returns_429_before_spawn(self, app):
        """At the per-user cap the follow-up is rejected with HTTP 429 before
        any ResearchHistory row is written or worker thread spawned."""
        mock_service = _service_mock()

        # Default cap is 3 (snapshot has no app.max_concurrent_researches).
        # Report 5 live researches so the admission check trips. reclaim
        # iterates query().filter().all() -> [] (nothing stale to reclaim).
        mock_db = MagicMock()
        mock_db.query.return_value.filter.return_value.all.return_value = []
        mock_db.query.return_value.filter_by.return_value.count.return_value = 5

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(mock_db),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH) as mock_start,
            patch(RUN_RESEARCH) as mock_run,
            patch(RESEARCH_HISTORY) as mock_history,
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 429, resp.text
        data = resp.json()
        assert data["success"] is False
        assert "capacity" in data["error"].lower()
        # Rejected during admission: no history row, no worker thread.
        mock_history.assert_not_called()
        mock_start.assert_not_called()
        mock_run.assert_not_called()

    def test_under_cap_records_active_row_and_spawns(self, app):
        """Under the cap the follow-up records a UserActiveResearch row (so
        the per-user accounting matches research_routes.start_research) and
        proceeds to spawn the worker."""
        mock_service = _service_mock()

        # 0 stale rows to reclaim; 1 live research (< cap of 3), and the
        # post-commit recheck sees the same benign count.
        mock_db = MagicMock()
        mock_db.query.return_value.filter.return_value.all.return_value = []
        mock_db.query.return_value.filter_by.return_value.count.return_value = 1

        added = []
        mock_db.add.side_effect = lambda obj: added.append(obj)

        with (
            patch(SETTINGS_MANAGER, return_value=_make_settings_mock()),
            _mock_db_session_ctx(mock_db),
            patch(
                f"{MODULE}.FollowUpResearchService", return_value=mock_service
            ),
            patch(START_RESEARCH) as mock_start,
            patch(RUN_RESEARCH),
            patch(RESOLVE_PASSWORD, return_value=("secret-password", False)),
        ):
            resp = _authed_post(
                app,
                "/api/followup/start",
                {"parent_research_id": "parent-1", "question": "details?"},
            )

        assert resp.status_code == 200, resp.text
        mock_start.assert_called_once()

        # A UserActiveResearch row was recorded for this research_id -- this
        # is the accounting that lets a later admission check count it.
        from local_deep_research.database.models import UserActiveResearch

        research_id = resp.json()["research_id"]
        assert any(
            isinstance(obj, UserActiveResearch)
            and obj.research_id == research_id
            for obj in added
        ), added