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.

29.8 KB
Raw Blame
"""``POST /api/start_research``: the parameter resolution and failure
branches that lost their only test in the port.

Ported from the Flask-era
``tests/web/routes/test_research_routes_extracted_helpers.py`` and
``tests/web/routes/test_research_routes_start_research_coverage.py``,
both deleted by the FastAPI migration. ``_extract_research_params``,
``_queue_research`` and ``_start_research_sync`` came across essentially
unchanged; the tests did not.

Already superseded on the branch and therefore NOT re-ported here: the
SSRF ``custom_endpoint`` cluster (``test_start_research_ssrf.py``, which
covers strictly more), the encrypted-DB password gate
(``tests/security/test_research_password_gate_fastapi.py``), missing
query / missing model (``tests/security/test_hostile_input_matrix.py``),
the DB-defaults fallbacks (``tests/settings/test_settings_take_effect.py``),
queued/in-progress status and queue position 1
(``tests/web/test_research_lifecycle_states.py``), and provider
normalisation (``test_extract_research_params_provider.py``).

What is recovered:

* the ``search_tool`` alias and ``search_engine``-wins precedence -- a live
  API alias with no test at all;
* boundary validation on ``iterations`` (1..100) and
  ``questions_per_iteration`` (1..20), rejecting non-ints, <= 0, or over cap,
  while ``model_provider``/``model``/``strategy`` guard on truthiness (so ``""``
  falls back). A refactor that flattens both to one style is invisible
  today;
* the ``DEFAULT_OLLAMA_URL`` fallback, and ``ollama_url is None`` for a
  non-ollama provider;
* the exact returned key set, including the three newest egress-policy
  keys;
* ``_queue_research``'s ``max(position) + 1`` (only position 1 is ever
  observed on the branch), the record's field set, the nine parameter
  kwargs it forwards to ``notify_research_queued`` (only
  ``settings_snapshot`` and ``session_id`` are asserted today, so
  dropping any other silently reverts a queued run to DB defaults), and
  both message shapes;
* the ``YYYY-MM-DD`` -> today replacement in the query;
* the four failure branches: settings-snapshot failure -> 500,
  research-creation failure -> 500, spawn failure -> 500 WITH the orphan
  ``UserActiveResearch`` deleted and ``ResearchHistory`` marked FAILED,
  and ``DuplicateResearchError`` -> 409 with that state deliberately left
  alone (it belongs to the live thread);
* the race-condition requeue, which is the only caller of
  ``_queue_research(research=...)``.

Dropped, with reason: ``test_start_research_no_g_db_session`` and
``test_start_research_password_from_temp_auth`` pin Flask-only
mechanisms. The branch has no ``flask.g`` branch (the snapshot block
unconditionally opens ``get_user_db_session``) and the ``temp_auth_store``
leg of the password chain was deliberately removed -- see
``web/auth/password_utils.py``.

The client harness follows ``tests/web/routers/test_start_research_ssrf.py``:
``require_auth`` overridden, every DB/spawn seam patched, so no real
research runs and no encrypted database is created.
"""

from contextlib import ExitStack, contextmanager
from datetime import UTC, datetime
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

ROUTER = "local_deep_research.web.routers.research"

# Imported inside the function body -- patch at the source module.
_SETTINGS_MANAGER = "local_deep_research.settings.SettingsManager"
_SAVE_STRATEGY = (
    "local_deep_research.web.services.research_service.save_research_strategy"
)
_RECLAIM_STALE = (
    "local_deep_research.web.routes.globals.reclaim_stale_user_active_research"
)
_QUEUE_PROCESSOR = "local_deep_research.web.queue.processor_v2.queue_processor"

START_URL = "/api/start_research"


def _make_settings_manager(overrides=None):
    """SettingsManager double backed by a lookup table (the Flask suite's
    idiom, kept so the ported assertions read the same)."""
    sm = MagicMock()
    lookup = {
        "llm.provider": "ollama",
        "llm.model": "gpt-4",
        "llm.openai_endpoint.url": None,
        "llm.ollama.url": "http://localhost:11434",
        "search.tool": "searxng",
        "search.iterations": 5,
        "search.questions_per_iteration": 5,
        "search.search_strategy": "source-based",
        "app.max_concurrent_researches": 3,
    }
    if overrides:
        lookup.update(overrides)
    sm.get_setting.side_effect = lambda key, default=None: lookup.get(
        key, default
    )
    sm.get_all_settings.return_value = {"setting_key": "setting_val"}
    # Deliberately NOT a dict, so _precheck_engine_policy short-circuits;
    # the egress precheck has its own suite. Same idiom as
    # test_start_research_ssrf.py.
    sm.get_settings_snapshot.return_value = MagicMock()
    return sm


# ===========================================================================
# _extract_research_params  (pure unit -- no app, no client)
# ===========================================================================


def _extract(data, settings_manager):
    from local_deep_research.web.routers.research import (
        _extract_research_params,
    )

    return _extract_research_params(data, settings_manager)


def test_search_tool_is_accepted_as_an_alias_for_search_engine():
    """``search_tool`` is a live request alias. Delete
    ``or data.get("search_tool")`` and callers using it silently fall
    back to the saved default instead of the engine they asked for."""
    result = _extract(
        {"search_tool": "tavily", "model_provider": "OLLAMA"},
        _make_settings_manager(),
    )
    assert result["search_engine"] == "tavily"


def test_search_engine_takes_precedence_over_search_tool():
    result = _extract(
        {"search_engine": "google", "search_tool": "tavily"},
        _make_settings_manager(),
    )
    assert result["search_engine"] == "google"


def test_ollama_url_falls_back_to_the_default_constant():
    """With no ``llm.ollama.url`` row, the resolved URL is
    ``DEFAULT_OLLAMA_URL`` -- not ``None``, which would send the provider
    to its own library default."""
    from local_deep_research.config.constants import DEFAULT_OLLAMA_URL

    sm = _make_settings_manager()
    inner = sm.get_setting.side_effect
    sm.get_setting.side_effect = lambda key, default=None: (
        default if key == "llm.ollama.url" else inner(key, default)
    )
    result = _extract({"model_provider": "OLLAMA"}, sm)
    assert result["ollama_url"] == DEFAULT_OLLAMA_URL


def test_ollama_url_is_not_resolved_for_a_non_ollama_provider():
    """The settings lookup is gated on the provider; an openai run must
    not carry an ollama URL into the thread."""
    result = _extract({"model_provider": "OPENAI"}, _make_settings_manager())
    assert result["ollama_url"] is None


@pytest.mark.parametrize(
    "bad_val",
    [0, -1, 101, 1000000000, 1.5, True, False, float("inf"), float("nan"), "5"],
)
def test_invalid_iterations_is_rejected(bad_val):
    """iterations must be a finite int between 1 and 100."""
    with pytest.raises(
        ValueError, match="iterations must be an integer between 1 and 100"
    ):
        _extract({"iterations": bad_val}, _make_settings_manager())


@pytest.mark.parametrize("good_val", [1, 5, 100])
def test_valid_iterations_is_preserved(good_val):
    result = _extract(
        {"iterations": good_val},
        _make_settings_manager({"search.iterations": 5}),
    )
    assert result["iterations"] == good_val


@pytest.mark.parametrize(
    "bad_val",
    [0, -1, 21, 100, 1.5, True, False, float("inf"), float("nan"), "5"],
)
def test_invalid_questions_per_iteration_is_rejected(bad_val):
    """questions_per_iteration must be a finite int between 1 and 20."""
    with pytest.raises(
        ValueError,
        match="questions_per_iteration must be an integer between 1 and 20",
    ):
        _extract({"questions_per_iteration": bad_val}, _make_settings_manager())


@pytest.mark.parametrize("good_val", [1, 5, 20])
def test_valid_questions_per_iteration_is_preserved(good_val):
    result = _extract(
        {"questions_per_iteration": good_val},
        _make_settings_manager({"search.questions_per_iteration": 5}),
    )
    assert result["questions_per_iteration"] == good_val


def test_empty_string_model_provider_falls_back_to_the_setting():
    """The mirror image of the two above: the string fields guard on
    truthiness, so ``""`` must fall back rather than be sent on as an
    empty provider."""
    result = _extract(
        {"model_provider": ""},
        _make_settings_manager({"llm.provider": "ANTHROPIC"}),
    )
    assert result["model_provider"] == "anthropic"


def test_empty_string_strategy_falls_back_to_the_setting():
    result = _extract(
        {"strategy": ""},
        _make_settings_manager({"search.search_strategy": "comprehensive"}),
    )
    assert result["strategy"] == "comprehensive"


def test_max_results_and_time_period_default_to_none():
    """Both are pure passthroughs with no settings fallback; ``None``
    means 'the engine decides'. Resolving them from settings here would
    silently override a per-run omission."""
    result = _extract({}, _make_settings_manager())
    assert result["max_results"] is None
    assert result["time_period"] is None


def test_returns_exactly_the_expected_key_set():
    """The thread and the queue both index this dict by key, so an added
    or dropped key is a KeyError at run time, not at import time."""
    result = _extract({}, _make_settings_manager())
    assert set(result.keys()) == {
        "model_provider",
        "model",
        "custom_endpoint",
        "ollama_url",
        "search_engine",
        "max_results",
        "time_period",
        "iterations",
        "questions_per_iteration",
        "strategy",
        # Per-research egress policy overrides.
        "policy_egress_scope",
        "llm_require_local_endpoint",
        "embeddings_require_local",
    }


# ===========================================================================
# _queue_research  (pure unit -- no app, no client)
# ===========================================================================


def _queue_params():
    return {
        "model_provider": "ollama",
        "model": "llama3",
        "custom_endpoint": None,
        "ollama_url": "http://localhost:11434",
        "search_engine": "searxng",
        "max_results": None,
        "time_period": None,
        "iterations": 5,
        "questions_per_iteration": 5,
        "strategy": "source-based",
    }


def _queue_session(max_position=0):
    session = MagicMock()
    session.query.return_value.filter_by.return_value.scalar.return_value = (
        max_position
    )
    return session


def _call_queue(session, **kwargs):
    from local_deep_research.web.routers.research import _queue_research

    call = {
        "db_session": session,
        "username": "testuser",
        "research_id": "r-123",
        "query": "test query",
        "mode": "quick",
        "research_settings": {"test": True},
        "params": _queue_params(),
        "session_id": "sid-1",
    }
    call.update(kwargs)
    return _queue_research(**call)


def test_queue_record_lands_after_the_current_highest_position():
    """Position is ``max(position) + 1``. Every queue test on the branch
    runs against an empty queue and only ever observes position 1, so an
    off-by-one or a constant 1 here is invisible."""
    with patch(_QUEUE_PROCESSOR):
        session = _queue_session(max_position=2)
        result = _call_queue(session)

    record = session.add.call_args[0][0]
    assert record.position == 3
    assert record.username == "testuser"
    assert record.research_id == "r-123"
    assert record.query == "test query"
    assert record.mode == "quick"
    assert session.commit.called
    assert result["queue_position"] == 3


def test_queue_notifies_the_processor_with_every_research_parameter():
    """The queued run is executed later, from these kwargs alone. Drop
    one and that run silently falls back to the user's saved default
    instead of what they submitted."""
    with patch(_QUEUE_PROCESSOR) as processor:
        _call_queue(_queue_session())

    processor.notify_research_queued.assert_called_once()
    args, kwargs = processor.notify_research_queued.call_args
    assert args == ("testuser", "r-123")
    assert kwargs["session_id"] == "sid-1"
    assert kwargs["query"] == "test query"
    assert kwargs["mode"] == "quick"
    assert kwargs["settings_snapshot"] == {"test": True}
    assert kwargs["model_provider"] == "ollama"
    assert kwargs["model"] == "llama3"
    assert kwargs["custom_endpoint"] is None
    assert kwargs["search_engine"] == "searxng"
    assert kwargs["max_results"] is None
    assert kwargs["time_period"] is None
    assert kwargs["iterations"] == 5
    assert kwargs["questions_per_iteration"] == 5
    assert kwargs["strategy"] == "source-based"


def test_queue_default_message_reports_the_position():
    with patch(_QUEUE_PROCESSOR):
        result = _call_queue(_queue_session(max_position=0))
    assert (
        result["message"]
        == "Your research has been queued. Position in queue: 1"
    )
    assert result["queue_position"] == 1


def test_queue_message_carries_the_reason_when_one_is_given():
    """The race-condition requeue passes a reason; it has to reach the
    user, otherwise a run that was demoted to the queue looks identical
    to one that was queued normally."""
    with patch(_QUEUE_PROCESSOR):
        result = _call_queue(
            _queue_session(max_position=1), reason="due to concurrent limit"
        )
    assert "due to concurrent limit" in result["message"]
    assert "Position in queue: 2" in result["message"]


def test_queue_position_starts_at_one_when_scalar_returns_none():
    """An empty ``queued_research`` table makes ``max()`` return ``None``;
    the ``or 0`` fallback is what keeps the first position at 1 rather
    than raising."""
    session = _queue_session()
    session.query.return_value.filter_by.return_value.scalar.return_value = None
    with patch(_QUEUE_PROCESSOR):
        result = _call_queue(session)
    assert result["queue_position"] == 1


def test_queue_sets_the_research_status_when_a_research_is_passed():
    """The ``research=`` argument exists solely so the race-condition
    requeue flips ResearchHistory to QUEUED atomically with the queue
    insert. It has exactly one caller, so it dies quietly."""
    from local_deep_research.constants import ResearchStatus

    research = MagicMock()
    with patch(_QUEUE_PROCESSOR):
        session = _queue_session()
        _call_queue(session, research=research)

    assert research.status == ResearchStatus.QUEUED
    assert session.commit.called


# ===========================================================================
# Route-level branches
# ===========================================================================


@pytest.fixture
def client():
    """FastAPI client authenticated as ``testuser``. Every DB touch in
    these tests is patched, so overriding the dependency is cheaper and
    more deterministic than a real register/login."""
    from local_deep_research.web.dependencies.auth import require_auth
    from local_deep_research.web.fastapi_app import app

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


def _post_start(client, payload):
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    return client.post(START_URL, json=payload, headers={"X-CSRFToken": token})


def _session_returning(session):
    @contextmanager
    def _ctx(*_args, **_kwargs):
        yield session

    return patch(f"{ROUTER}.get_user_db_session", _ctx)


def _flow_session(active_count=0):
    session = MagicMock()
    chain = session.query.return_value.filter_by.return_value
    chain.count.return_value = active_count
    chain.scalar.return_value = 0
    chain.first.return_value = MagicMock()
    return session


@contextmanager
def _start_mocks(sm, session, spawn=None):
    """Patch every seam of ``_start_research_sync`` and yield the
    ``start_research_process`` mock."""
    fake_thread = MagicMock()
    fake_thread.ident = 99
    with ExitStack() as stack:
        stack.enter_context(_session_returning(session))
        stack.enter_context(patch(_SETTINGS_MANAGER, return_value=sm))
        stack.enter_context(
            patch(f"{ROUTER}.resolve_user_password", return_value=("pw", False))
        )
        spawn_mock = stack.enter_context(
            patch(
                f"{ROUTER}.start_research_process",
                return_value=fake_thread,
                **({"side_effect": spawn} if spawn is not None else {}),
            )
        )
        stack.enter_context(patch(f"{ROUTER}.log_settings"))
        stack.enter_context(patch(f"{ROUTER}.ResearchHistory"))
        stack.enter_context(patch(f"{ROUTER}.UserActiveResearch"))
        stack.enter_context(patch(_SAVE_STRATEGY))
        stack.enter_context(patch(_RECLAIM_STALE, return_value=False))
        yield spawn_mock


def test_start_research_rejects_invalid_iterations(client):
    response = _post_start(
        client,
        {"query": "boundary test", "model": "llama3", "iterations": 0},
    )
    assert response.status_code == 400
    assert (
        "iterations must be an integer between 1 and 100"
        in response.json()["message"]
    )


def test_start_research_rejects_invalid_questions_per_iteration(client):
    response = _post_start(
        client,
        {
            "query": "boundary test",
            "model": "llama3",
            "questions_per_iteration": 25,
        },
    )
    assert response.status_code == 400
    assert (
        "questions_per_iteration must be an integer between 1 and 20"
        in response.json()["message"]
    )


def test_date_placeholder_in_the_query_is_replaced_with_today(client):
    """``YYYY-MM-DD`` is the documented placeholder for news
    subscriptions and the news page's canned queries. The replacement
    happens once, at submission, and the *replaced* query is what the
    research thread receives -- nothing on the branch pins either half.
    """
    captured = {}

    def _spawn(research_id, query, *_args, **_kwargs):
        captured["query"] = query
        thread = MagicMock()
        thread.ident = 99
        return thread

    sm = _make_settings_manager()
    with _start_mocks(sm, _flow_session(), spawn=_spawn):
        response = _post_start(
            client,
            {"query": "What happened on YYYY-MM-DD?", "model": "llama3"},
        )

    assert response.status_code == 200, response.text[:300]
    assert response.json()["status"] == "success"
    today = datetime.now(UTC).strftime("%Y-%m-%d")
    assert "query" in captured, "the research thread was never spawned"
    assert "YYYY-MM-DD" not in captured["query"]
    assert today in captured["query"]


def test_settings_snapshot_failure_answers_500(client):
    """The snapshot IS the run's configuration -- a thread started
    without one would silently use whatever the worker's own defaults
    are. The route must refuse rather than continue."""
    sm = _make_settings_manager()
    sm.get_all_settings.side_effect = RuntimeError("DB exploded")

    with _start_mocks(sm, _flow_session()) as spawn:
        response = _post_start(
            client, {"query": "Snapshot failure test", "model": "llama3"}
        )

    assert response.status_code == 500, response.text[:300]
    body = response.json()
    assert body["status"] == "error"
    assert "settings" in body["message"].lower()
    spawn.assert_not_called()


def test_research_creation_failure_answers_500(client):
    """Writing the ResearchHistory row is the point of no return; if it
    fails the caller must be told, not handed a research_id for a row
    that does not exist."""
    sm = _make_settings_manager()
    session = _flow_session()

    with ExitStack() as stack:
        stack.enter_context(_session_returning(session))
        stack.enter_context(patch(_SETTINGS_MANAGER, return_value=sm))
        stack.enter_context(
            patch(f"{ROUTER}.resolve_user_password", return_value=("pw", False))
        )
        spawn = stack.enter_context(patch(f"{ROUTER}.start_research_process"))
        stack.enter_context(
            patch(
                f"{ROUTER}.ResearchHistory",
                side_effect=RuntimeError("creation failed"),
            )
        )
        stack.enter_context(patch(_RECLAIM_STALE, return_value=False))
        response = _post_start(
            client, {"query": "test query", "model": "llama3"}
        )

    assert response.status_code == 500, response.text[:300]
    assert "failed to create" in response.json()["message"].lower()
    spawn.assert_not_called()


def _cleanup_aware_session():
    """A session whose ``query(Model).filter_by(...).first()`` returns a
    distinct, identifiable object per model, so the cleanup branch's
    effects on each can be asserted. Returns ``(session, stale_active,
    research_row)``."""
    session = MagicMock()
    stale_active = MagicMock()
    research_row = MagicMock()

    def _query(model, *_rest):
        q = MagicMock()
        q.filter_by.return_value = q
        name = getattr(model, "__name__", "")
        if name == "UserActiveResearch":
            q.first.return_value = stale_active
        elif name == "ResearchHistory":
            q.first.return_value = research_row
        else:
            q.first.return_value = MagicMock()
        q.count.return_value = 0
        q.scalar.return_value = 0
        return q

    session.query.side_effect = _query
    return session, stale_active, research_row


def test_spawn_failure_deletes_the_orphan_row_and_marks_the_run_failed(
    client,
):
    """If ``start_research_process`` raises, the UserActiveResearch row
    and the IN_PROGRESS ResearchHistory row committed just above are
    already on disk with no thread and no cleanup path. They must be
    reaped here -- same contract as the queue processor's terminal
    failure branch (#3481)."""
    from local_deep_research.constants import ResearchStatus

    sm = _make_settings_manager()
    session, stale_active, research_row = _cleanup_aware_session()

    with ExitStack() as stack:
        stack.enter_context(_session_returning(session))
        stack.enter_context(patch(_SETTINGS_MANAGER, return_value=sm))
        stack.enter_context(
            patch(f"{ROUTER}.resolve_user_password", return_value=("pw", False))
        )
        stack.enter_context(
            patch(
                f"{ROUTER}.start_research_process",
                side_effect=RuntimeError("boom"),
            )
        )
        stack.enter_context(patch(f"{ROUTER}.log_settings"))
        rh = stack.enter_context(patch(f"{ROUTER}.ResearchHistory"))
        rh.__name__ = "ResearchHistory"
        uar = stack.enter_context(patch(f"{ROUTER}.UserActiveResearch"))
        uar.__name__ = "UserActiveResearch"
        stack.enter_context(patch(_SAVE_STRATEGY))
        stack.enter_context(patch(_RECLAIM_STALE, return_value=False))

        response = _post_start(
            client,
            {"query": "Spawn failure test", "model": "llama3", "mode": "deep"},
        )

    assert response.status_code == 500, response.text[:300]
    assert response.json()["status"] == "error"
    session.delete.assert_called_with(stale_active)
    assert research_row.status == ResearchStatus.FAILED
    assert session.commit.called


def test_duplicate_live_thread_answers_409_and_leaves_the_state_alone(
    client,
):
    """``DuplicateResearchError`` means a live thread already owns this
    research_id. Running the spawn-failure cleanup here would delete the
    active row and mark the run FAILED *while it keeps executing* --
    the user sees a dead research that is still burning tokens. The
    branch must answer 409 and touch nothing (#3506)."""
    from local_deep_research.constants import ResearchStatus
    from local_deep_research.exceptions import DuplicateResearchError

    sm = _make_settings_manager()
    session, _stale_active, research_row = _cleanup_aware_session()

    with ExitStack() as stack:
        stack.enter_context(_session_returning(session))
        stack.enter_context(patch(_SETTINGS_MANAGER, return_value=sm))
        stack.enter_context(
            patch(f"{ROUTER}.resolve_user_password", return_value=("pw", False))
        )
        stack.enter_context(
            patch(
                f"{ROUTER}.start_research_process",
                side_effect=DuplicateResearchError(
                    "research already has a live thread"
                ),
            )
        )
        stack.enter_context(patch(f"{ROUTER}.log_settings"))
        rh = stack.enter_context(patch(f"{ROUTER}.ResearchHistory"))
        rh.__name__ = "ResearchHistory"
        uar = stack.enter_context(patch(f"{ROUTER}.UserActiveResearch"))
        uar.__name__ = "UserActiveResearch"
        stack.enter_context(patch(_SAVE_STRATEGY))
        stack.enter_context(patch(_RECLAIM_STALE, return_value=False))

        response = _post_start(
            client,
            {
                "query": "Duplicate live thread test",
                "model": "llama3",
                "mode": "deep",
            },
        )

    assert response.status_code == 409, response.text[:300]
    assert response.json()["status"] == "error"
    # The two things the spawn-failure branch would have done.
    session.delete.assert_not_called()
    assert research_row.status != ResearchStatus.FAILED


def test_race_condition_after_commit_demotes_the_run_to_the_queue():
    """The concurrency cap is enforced twice: once before the write and
    once after, because two submissions can pass the first check
    together. When the recheck finds the cap exceeded, the active row is
    deleted and the run is queued instead of started -- otherwise the
    cap is advisory only under exactly the load it exists for."""
    sm = _make_settings_manager({"app.max_concurrent_researches": 3})
    session = _flow_session()

    counts = iter([2, 4])  # pre-check: under cap. recheck: over cap.
    session.query.return_value.filter_by.return_value.count.side_effect = (
        lambda: next(counts)
    )

    from local_deep_research.web.research_state import (
        get_user_research_start_lock,
        user_research_start_gate,
    )

    admission_lock = get_user_research_start_lock("testuser")
    session_gate_states = []
    session_rollback_counts = []

    @contextmanager
    def observing_session(*_args, **_kwargs):
        session_gate_states.append(admission_lock.locked())
        session_rollback_counts.append(session.rollback.call_count)
        yield session

    with ExitStack() as stack:
        stack.enter_context(
            patch(f"{ROUTER}.get_user_db_session", observing_session)
        )
        stack.enter_context(patch(_SETTINGS_MANAGER, return_value=sm))
        stack.enter_context(
            patch(f"{ROUTER}.resolve_user_password", return_value=("pw", False))
        )
        spawn = stack.enter_context(patch(f"{ROUTER}.start_research_process"))
        stack.enter_context(patch(f"{ROUTER}.ResearchHistory"))
        stack.enter_context(patch(f"{ROUTER}.UserActiveResearch"))
        stack.enter_context(patch(f"{ROUTER}.QueuedResearch"))
        stack.enter_context(patch(_QUEUE_PROCESSOR))
        stack.enter_context(patch(_RECLAIM_STALE, return_value=False))
        gate = stack.enter_context(
            patch(
                f"{ROUTER}.user_research_start_gate",
                wraps=user_research_start_gate,
            )
        )

        from local_deep_research.web.routers.research import (
            _start_research_sync,
        )

        response = _start_research_sync(
            {"query": "Race condition test", "model": "llama3"},
            "testuser",
            "http://testserver/",
            "session-1",
        )

    assert response["status"] == "queued"
    assert "due to concurrent limit" in response["message"]
    assert gate.call_count == 2, (
        "fresh admission must guard both its preliminary count and its "
        "post-commit capacity claim"
    )
    assert session_gate_states[-1] is True, (
        "the durable slot-claim session opened before the admission gate"
    )
    assert session_rollback_counts[-1] == 3, (
        "parameter extraction, preliminary admission, and settings-snapshot "
        "read transactions must all end before the durable claim opens its "
        "session under the admission gate"
    )
    spawn.assert_not_called()


@pytest.mark.parametrize("spawn_fails", [False, True])
def test_direct_claim_keeps_gate_until_worker_registration(spawn_fails):
    """A competing queue/reconciliation consumer cannot enter the claim gap.

    Releasing the gate after the DB commit but before spawn lets the competing
    thread acquire it. Both success and spawn rejection must release it later.
    """
    import threading

    from local_deep_research.web.research_state import (
        get_user_research_start_lock,
    )
    from local_deep_research.web.routers.research import _start_research_sync

    session = _flow_session(active_count=0)
    sm = _make_settings_manager()
    lock = get_user_research_start_lock("testuser")
    observed = []

    def competing_consumer():
        acquired = lock.acquire(blocking=False)
        observed.append(acquired)
        if acquired:
            lock.release()

    def at_registration(*_args, **_kwargs):
        assert session.commit.called
        contender = threading.Thread(target=competing_consumer)
        contender.start()
        contender.join(timeout=5)
        assert not contender.is_alive()
        if spawn_fails:
            raise RuntimeError("synthetic worker start failure")
        return MagicMock()

    with _start_mocks(sm, session, spawn=at_registration) as spawn:
        result = _start_research_sync(
            {"query": "registration interval", "model": "llama3"},
            "testuser",
            "http://testserver/",
            "session-1",
        )

    spawn.assert_called_once()
    assert observed == [False]
    assert not lock.locked()
    if spawn_fails:
        assert result.status_code == 500
    else:
        assert result["status"] == "success"