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.

39.9 KB
Raw Blame
"""Does every research state transition a user can trigger actually land?

Sibling files cover other axes of the same subsystem and are deliberately
NOT duplicated here:

* ``tests/settings/test_settings_take_effect.py`` — saved settings reaching
  the background worker, and the *start-vs-queue decision* made by
  ``app.max_concurrent_researches``.
* ``tests/security/test_hostile_input_matrix.py`` — hostile request bodies.
* ``tests/security/test_csrf_coverage.py`` — CSRF.
* ``tests/web_search_engines/test_engine_success_path.py`` — engine success.

This module asks the state-machine question instead: a user clicks Stop,
a worker crashes, a run sits in the queue — **does the transition land, and
is it observable afterwards?**  Every assertion is either on the persisted
``ResearchHistory`` row, on what the *research thread itself* observed, or
on what the real HTTP status endpoints return — never on a re-implementation
of production logic.

Harness notes
-------------
* Everything runs over the real HTTP API with a real registered user
  (``authenticated_client``), the real ``start_research_process`` thread
  spawn, the real service layer and the real per-user encrypted DB.
* The only stubbed boundary is the worker body
  (``research_router.run_research_process``) or, where the *real* worker is
  wanted, ``research_service.get_search`` — the same two boundaries the
  settings suite uses. Nothing talks to the network.
* The queue processor thread is **switched off under pytest** by
  ``fastapi_app`` (``if os.getenv("PYTEST_CURRENT_TEST") ... enabled =
  False``). Tests that need dispatch or terminal-status persistence
  therefore call the processor's own per-tick entry points
  (``_process_user_queue`` / ``_drain_pending_operations``) directly: that
  is the exact production code the loop body runs, with only the 10-second
  timer bypassed.
"""

from __future__ import annotations

import threading
import uuid

# ---------------------------------------------------------------------------
# HTTP helpers — every one fails loudly instead of returning a bad response
# ---------------------------------------------------------------------------


def put_setting(client, key: str, value):
    resp = client.put(f"/settings/api/{key}", json={"value": value})
    assert resp.status_code == 200, (
        f"PUT /settings/api/{key} = {value!r} -> {resp.status_code} "
        f"{resp.text[:300]}"
    )


def submit(client, query: str):
    """POST /api/start_research, returning the decoded JSON body."""
    resp = client.post(
        "/api/start_research", json={"query": query, "mode": "quick"}
    )
    assert resp.status_code == 200, (
        f"POST /api/start_research -> {resp.status_code} {resp.text[:300]}"
    )
    return resp.json()


def status_of(client, research_id):
    """GET /api/research/{id}/status (routers/research.py)."""
    return client.get(f"/api/research/{research_id}/status")


def api_status_of(client, research_id):
    """GET /research/api/status/{id} (routers/api.py — the other one)."""
    return client.get(f"/research/api/status/{research_id}")


def terminate(client, research_id):
    """POST /api/terminate/{id} (routers/research.py)."""
    return client.post(f"/api/terminate/{research_id}")


def api_terminate(client, research_id):
    """POST /research/api/terminate/{id} (routers/api.py -> cancel_research)."""
    return client.post(f"/research/api/terminate/{research_id}")


def db_row(username, research_id):
    """Read the persisted row directly. Returns a plain dict or None."""
    from local_deep_research.database.models import ResearchHistory
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )

    with get_user_db_session(username) as session:
        row = session.query(ResearchHistory).filter_by(id=research_id).first()
        if row is None:
            return None
        return {
            "status": row.status,
            "progress": row.progress,
            "completed_at": row.completed_at,
            "meta": dict(row.research_meta or {}),
        }


# ---------------------------------------------------------------------------
# Worker stubs
# ---------------------------------------------------------------------------


class ParkedWorker:
    """Stand-in for ``run_research_process`` that parks the research thread.

    ``start_research_process`` stays REAL: the global semaphore, the
    contextvar copy, ``check_and_start_research`` (which registers the run in
    ``_active_research`` and starts the thread) all execute. This object is
    what that thread runs, so ``flag_seen`` below is what a *real worker at a
    real checkpoint* would have observed.
    """

    def __init__(self):
        self.entered = threading.Event()
        self.release = threading.Event()
        self.exited = threading.Event()
        self.calls = []
        self.observations = []

    def __call__(self, research_id, query, mode, **kwargs):
        from local_deep_research.web.research_state import (
            is_research_active,
            is_termination_requested,
        )

        self.calls.append({"research_id": research_id, "kwargs": kwargs})
        self.entered.set()
        # Park here: the run is genuinely in flight while the test acts.
        self.release.wait(30)
        # This is the production checkpoint predicate, evaluated on the
        # research thread after the user's action has completed.
        self.observations.append(
            {
                "research_id": research_id,
                "flag_seen": is_termination_requested(research_id),
                "still_active": is_research_active(research_id),
                "on_worker_thread": threading.current_thread()
                is not threading.main_thread(),
            }
        )
        # This stub replaces the complete production worker callback. Mirror
        # its final state cleanup after recording the checkpoint observation.
        from local_deep_research.web.research_state import cleanup_research

        cleanup_research(research_id)
        # ``let_go`` relies on this ordering when it inspects global state:
        # the event is visible only after final cleanup has returned.
        self.exited.set()

    # -- test-side driving -------------------------------------------------
    def wait_started(self, timeout=20.0):
        assert self.entered.wait(timeout), (
            "the background research thread never entered the worker"
        )
        self.entered.clear()

    def let_go(self, timeout=20.0):
        """Release the worker and block through its checkpoint and cleanup."""
        self.release.set()
        assert self.exited.wait(timeout), "worker never finished"
        self.exited.clear()
        self.release.clear()

    def observation_for(self, research_id):
        for obs in self.observations:
            if obs["research_id"] == research_id:
                return obs
        raise AssertionError(
            f"worker never recorded an observation for {research_id}: "
            f"{self.observations}"
        )


def install_parked_worker(monkeypatch):
    from local_deep_research.web.routers import research as research_router

    worker = ParkedWorker()
    monkeypatch.setattr(
        research_router, "run_research_process", worker, raising=True
    )
    return worker


def username_from(worker):
    assert worker.calls, "the worker was never called; no username to read"
    name = worker.calls[-1]["kwargs"].get("username")
    assert name, f"worker call carried no username: {worker.calls[-1]}"
    return name


# ---------------------------------------------------------------------------
# 1. Stop on an in-flight run: the flag must reach the worker AND the row
# ---------------------------------------------------------------------------


def test_stop_reaches_the_worker_and_persists_suspended(
    authenticated_client, monkeypatch
):
    """POST /api/terminate on a running research.

    Two runs go through the IDENTICAL path; only one is stopped.

    CONTROL (run B): no terminate call — the worker must observe
    ``is_termination_requested`` False at the very same checkpoint and the
    row must still read ``in_progress``. That rules out a harness that
    reports "terminated" for everything.
    """
    worker = install_parked_worker(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")

    # --- subject: started, then stopped while parked -----------------
    rid_a = submit(authenticated_client, "lifecycle stop A")["research_id"]
    worker.wait_started()
    username = username_from(worker)

    before = status_of(authenticated_client, rid_a).json()
    assert before["status"] == "in_progress", (
        f"run did not reach in_progress before the stop: {before}"
    )

    resp = terminate(authenticated_client, rid_a)
    assert resp.status_code == 200, f"terminate -> {resp.text[:300]}"
    assert resp.json()["status"] == "success", resp.json()

    worker.let_go()
    obs_a = worker.observation_for(rid_a)

    # --- control: identical path, no stop ----------------------------
    rid_b = submit(authenticated_client, "lifecycle stop B control")[
        "research_id"
    ]
    worker.wait_started()
    worker.let_go()
    obs_b = worker.observation_for(rid_b)

    assert obs_a["on_worker_thread"] and obs_b["on_worker_thread"], (
        "the worker ran on the main thread; the hand-off never happened and "
        f"this test would prove nothing: {worker.observations}"
    )
    assert (obs_a["flag_seen"], obs_b["flag_seen"]) == (True, False), (
        "the termination flag did not discriminate: stopped run saw "
        f"{obs_a['flag_seen']}, un-stopped control saw {obs_b['flag_seen']}"
    )

    row_a = db_row(username, rid_a)
    row_b = db_row(username, rid_b)
    assert (row_a["status"], row_b["status"]) == ("suspended", "in_progress"), (
        f"persisted rows disagree with the actions taken: stopped={row_a}, "
        f"control={row_b}"
    )

    # The status endpoint must agree with the row it claims to report.
    http_a = status_of(authenticated_client, rid_a).json()
    http_b = api_status_of(authenticated_client, rid_a).json()
    assert http_a["status"] == http_b["status"] == row_a["status"], (
        "the two status endpoints and the DB row disagree: "
        f"/api/research/../status={http_a['status']!r}, "
        f"/research/api/status/..={http_b['status']!r}, row={row_a['status']!r}"
    )


# ---------------------------------------------------------------------------
# 2. The OTHER stop endpoint: /research/api/terminate -> cancel_research
#
# routers/api.py exposes a second terminate endpoint that delegates to
# ``research_service.cancel_research``. For an ACTIVE research that function
# does:
#
#     set_termination_flag(research_id)
#     if is_research_active(research_id):
#         handle_termination(research_id, username)   # -> cleanup_research()
#         return True
#
# The HTTP path must remove the active slot immediately while preserving the
# flag until the still-running worker observes it. The worker's own cleanup
# then removes the flag.
# ---------------------------------------------------------------------------


def test_cancel_research_endpoint_leaves_the_flag_for_the_worker(
    authenticated_client, monkeypatch
):
    """The user pressed Stop; the worker must see it at its next checkpoint.

    CONTROL: an identical run that is NOT cancelled must observe the flag as
    False, so a passing assertion here cannot come from a flag that is always
    set.
    """
    worker = install_parked_worker(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")

    rid_a = submit(authenticated_client, "cancel_research subject")[
        "research_id"
    ]
    worker.wait_started()

    resp = api_terminate(authenticated_client, rid_a)
    assert resp.status_code == 200, resp.text[:300]
    assert resp.json()["result"] is True, (
        f"cancel_research reported failure for an active run: {resp.json()}"
    )

    worker.let_go()
    obs_a = worker.observation_for(rid_a)

    rid_b = submit(authenticated_client, "cancel_research control")[
        "research_id"
    ]
    worker.wait_started()
    worker.let_go()
    obs_b = worker.observation_for(rid_b)

    assert obs_b["flag_seen"] is False, (
        "CONTROL BROKEN: an un-cancelled run already sees the termination "
        f"flag: {obs_b}"
    )
    assert obs_a["flag_seen"] is True, (
        "the worker never saw the termination flag after "
        f"POST /research/api/terminate/{rid_a}: {obs_a} — the cancelled run "
        "keeps running"
    )


def test_cancel_research_does_not_persist_suspended_until_the_queue_drains(
    authenticated_client, monkeypatch
):
    """Characterises what ``cancel_research`` actually leaves behind.

    ``cancel_research`` returns True for an active run *before* it ever opens
    the user's database: ``handle_termination`` only pushes an ``error_update``
    into ``queue_processor.pending_operations``. Until the processor's next
    tick drains that dict, the persisted row — and therefore both status
    endpoints — still say ``in_progress`` even though the API answered
    "Research terminated".

    Under pytest the processor thread is not started at all (fastapi_app
    disables it when ``PYTEST_CURRENT_TEST`` is set), so the drain here is an
    explicit call to the processor's own per-tick method.

    CONTROLS: (a) a second run that is never cancelled must stay
    ``in_progress`` across the very same drain, so the drain is not simply
    suspending everything; (b) the cancelled row is read before and after the
    drain, so the transition itself is observed rather than assumed.
    """
    from local_deep_research.web.queue.processor_v2 import queue_processor

    worker = install_parked_worker(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")

    rid_a = submit(authenticated_client, "drain subject")["research_id"]
    worker.wait_started()
    username = username_from(worker)

    rid_b = submit(authenticated_client, "drain control")["research_id"]
    worker.wait_started()

    resp = api_terminate(authenticated_client, rid_a)
    assert resp.json()["result"] is True, resp.json()

    before = {
        "row": db_row(username, rid_a)["status"],
        "http": status_of(authenticated_client, rid_a).json()["status"],
    }
    assert before["row"] == before["http"], (
        f"status endpoint disagreed with the row it reports: {before}"
    )

    # The exact call the processor loop makes every tick.
    queue_processor._drain_pending_operations()

    after = db_row(username, rid_a)["status"]
    control_after = db_row(username, rid_b)["status"]

    worker.release.set()  # let both parked threads go

    assert before["row"] == "in_progress", (
        "cancel_research persisted a terminal status synchronously — this "
        f"test's premise no longer holds: {before}"
    )
    assert after == "suspended", (
        "after the queue processor drained its pending operations the "
        f"cancelled research still reads {after!r}, not 'suspended'"
    )
    assert control_after == "in_progress", (
        "CONTROL BROKEN: the drain also changed a research nobody "
        f"cancelled: {control_after!r}"
    )


# ---------------------------------------------------------------------------
# 3. The queue: position, cancellation, and dispatch
#
# NOTE: the *decision* to queue (app.max_concurrent_researches) is already
# covered by tests/settings/test_settings_take_effect.py. What follows is
# about what happens to a research AFTER it lands in the queue.
# ---------------------------------------------------------------------------


def install_queue_worker(monkeypatch):
    """Park the worker for BOTH spawn sites.

    A directly submitted research runs ``routers.research.run_research_process``;
    a queue-dispatched one runs ``queue.processor_v2.run_research_process``
    (imported into that module at import time). Both are replaced with the
    same recorder so a dispatch is observable no matter which path spawned it.
    """
    from local_deep_research.web.queue import processor_v2
    from local_deep_research.web.routers import research as research_router

    worker = ParkedWorker()
    monkeypatch.setattr(
        research_router, "run_research_process", worker, raising=True
    )
    monkeypatch.setattr(
        processor_v2, "run_research_process", worker, raising=True
    )
    return worker


def record_queue_notifications(monkeypatch):
    """Wrap (not replace) notify_research_queued to learn the session id.

    ``_process_user_queue`` needs ``(username, session_id)`` — the same pair
    the production loop keeps in ``_users_to_check``. Reading it off the real
    call keeps the test from inventing its own session handling.
    """
    from local_deep_research.web.queue.processor_v2 import queue_processor

    real = queue_processor.notify_research_queued
    calls = []

    def _wrapper(username, research_id, **kwargs):
        calls.append(
            {
                "username": username,
                "research_id": research_id,
                "session_id": kwargs.get("session_id"),
            }
        )
        return real(username, research_id, **kwargs)

    monkeypatch.setattr(
        queue_processor, "notify_research_queued", _wrapper, raising=True
    )
    return calls


def queue_total(client):
    resp = client.get("/api/queue/status")
    assert resp.status_code == 200, resp.text[:200]
    body = resp.json()
    return body["total"], [item for item in body["queue"]]


def test_queued_research_reports_its_position_and_can_be_cancelled(
    authenticated_client, monkeypatch
):
    """A queued run is visible, correctly positioned, and Stop removes it.

    CONTROLS: (a) the running research A is asserted to be in_progress
    throughout, so "suspended" is not simply what every row says; (b) the
    queue endpoints are read BEFORE the cancel (position 1, total 1) and
    after (404, total 0), so the transition itself is observed.
    """
    worker = install_queue_worker(monkeypatch)
    record_queue_notifications(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")
    put_setting(authenticated_client, "app.max_concurrent_researches", 1)

    try:
        rid_a = submit(authenticated_client, "queue holder")["research_id"]
        worker.wait_started()
        username = username_from(worker)

        body_b = submit(authenticated_client, "queued and cancelled")
        rid_b = body_b["research_id"]
        assert body_b["status"] == "queued", (
            f"second submission was not queued under cap=1: {body_b}"
        )
        assert body_b["queue_position"] == 1, (
            f"queue position reported at submit time is wrong: {body_b}"
        )

        # Position endpoint and the api.py status endpoint must agree.
        pos = authenticated_client.get(f"/api/queue/{rid_b}/position")
        assert pos.status_code == 200, pos.text[:200]
        assert pos.json()["position"] == 1, pos.json()

        st = api_status_of(authenticated_client, rid_b).json()
        assert (st["status"], st.get("queue_position")) == ("queued", 1), (
            f"/research/api/status disagrees about the queued run: {st}"
        )

        total_before, items = queue_total(authenticated_client)
        assert total_before == 1 and any(
            i.get("research_id") == rid_b for i in items
        ), f"queued research missing from /api/queue/status: {items}"

        assert db_row(username, rid_b)["status"] == "queued"
        assert db_row(username, rid_a)["status"] == "in_progress"

        # --- the transition under test ---------------------------------
        resp = terminate(authenticated_client, rid_b)
        assert resp.status_code == 200, resp.text[:300]

        assert db_row(username, rid_b)["status"] == "suspended", (
            "cancelling a QUEUED research did not persist SUSPENDED: "
            f"{db_row(username, rid_b)}"
        )
        assert db_row(username, rid_a)["status"] == "in_progress", (
            "CONTROL BROKEN: cancelling the queued run also disturbed the "
            "running one"
        )

        pos_after = authenticated_client.get(f"/api/queue/{rid_b}/position")
        assert pos_after.status_code == 404, (
            "a cancelled research is still reported as holding a queue "
            f"position: {pos_after.status_code} {pos_after.text[:200]}"
        )
        total_after, _ = queue_total(authenticated_client)
        assert total_after == 0, (
            f"queue still holds {total_after} item(s) after the cancel"
        )
    finally:
        worker.release.set()


def test_queued_research_starts_when_the_processor_ticks(
    authenticated_client, monkeypatch
):
    """The queue survives and actually dispatches once the slot is free.

    The processor thread is disabled under pytest, so the test calls the
    method its loop body calls — ``_process_user_queue`` — after releasing
    the holder. Everything inside (claim, IN_PROGRESS commit, real
    ``start_research_process`` spawn) is production code.

    CONTROL: state is read immediately BEFORE the tick (queued, in the
    queue, worker never called for it) and immediately after, so the tick is
    demonstrably the cause.
    """
    from local_deep_research.web.queue.processor_v2 import queue_processor

    worker = install_queue_worker(monkeypatch)
    queued_calls = record_queue_notifications(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")
    put_setting(authenticated_client, "app.max_concurrent_researches", 1)

    try:
        rid_a = submit(authenticated_client, "holder to be released")[
            "research_id"
        ]
        worker.wait_started()
        username = username_from(worker)

        rid_b = submit(authenticated_client, "waiting in queue")["research_id"]
        assert queued_calls and queued_calls[-1]["research_id"] == rid_b, (
            f"the queue processor was never notified: {queued_calls}"
        )
        session_id = queued_calls[-1]["session_id"]
        assert session_id, (
            "no session_id reached notify_research_queued; the production "
            "loop could not dispatch this research either"
        )

        # Free the slot: release the holder and wait for its thread to exit.
        worker.release.set()
        assert worker.exited.wait(20), "holder worker never exited"

        before = {
            "row": db_row(username, rid_b)["status"],
            "queue_total": queue_total(authenticated_client)[0],
            "worker_seen": [c["research_id"] for c in worker.calls],
        }
        assert before["row"] == "queued", before
        assert before["queue_total"] == 1, before
        assert rid_b not in before["worker_seen"], before

        # --- one production tick ---------------------------------------
        queue_processor._process_user_queue(username, session_id)

        after = {
            "row": db_row(username, rid_b)["status"],
            "queue_total": queue_total(authenticated_client)[0],
            "worker_seen": [c["research_id"] for c in worker.calls],
        }

        assert rid_b in after["worker_seen"], (
            "the queued research never reached a worker thread after the "
            f"processor tick: before={before} after={after}"
        )
        assert after["row"] == "in_progress", (
            f"dispatched research did not move out of 'queued': {after}"
        )
        assert after["queue_total"] == 0, (
            f"the queue row survived a successful dispatch: {after}"
        )
        assert db_row(username, rid_a)["status"] == "in_progress", (
            "CONTROL BROKEN: the tick also mutated the already-running research"
        )
    finally:
        worker.release.set()


def test_queue_tick_does_not_start_a_second_run_past_the_cap(
    authenticated_client, monkeypatch
):
    """With cap=1 and one run in flight, a tick must not dispatch the queued one.

    CONTROL: the holder is asserted to be genuinely in flight (its worker
    thread is parked inside the worker and its row reads in_progress) at the
    moment of the tick, so a "still queued" result could not come from an
    idle system.
    """
    from local_deep_research.web.queue.processor_v2 import queue_processor

    worker = install_queue_worker(monkeypatch)
    queued_calls = record_queue_notifications(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")
    put_setting(authenticated_client, "app.max_concurrent_researches", 1)

    try:
        rid_a = submit(authenticated_client, "cap holder")["research_id"]
        worker.wait_started()
        username = username_from(worker)

        rid_b = submit(authenticated_client, "must stay queued")["research_id"]
        session_id = queued_calls[-1]["session_id"]

        # CONTROL: the single slot really is occupied right now.
        assert db_row(username, rid_a)["status"] == "in_progress", (
            "CONTROL BROKEN: the holder is not running, so the cap is not "
            "actually saturated"
        )
        assert not worker.exited.is_set(), (
            "CONTROL BROKEN: the holder's worker already returned"
        )
        assert db_row(username, rid_b)["status"] == "queued"

        queue_processor._process_user_queue(username, session_id)

        row_b = db_row(username, rid_b)["status"]
        started = [c["research_id"] for c in worker.calls]
        assert row_b == "queued" and rid_b not in started, (
            "the queued research was dispatched while the only slot was "
            f"still occupied: row={row_b!r}, workers started={started}"
        )
    finally:
        worker.release.set()


# ---------------------------------------------------------------------------
# 4. Stopping things that cannot be stopped
# ---------------------------------------------------------------------------


def test_terminating_an_unknown_research_id_is_a_404(
    authenticated_client, monkeypatch
):
    """POST /api/terminate/<unknown> must not claim to have stopped anything.

    CONTROL: the very same endpoint, in the same test, returns 200 +
    ``status=success`` for a research that really exists — so the 404 is a
    property of the id, not of the endpoint being broken.
    """
    worker = install_parked_worker(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")

    unknown = str(uuid.uuid4())
    missing = terminate(authenticated_client, unknown)
    assert missing.status_code == 404, (
        f"terminate on an unknown id -> {missing.status_code} "
        f"{missing.text[:200]}"
    )
    assert missing.json()["status"] == "error", missing.json()

    try:
        rid = submit(authenticated_client, "real research for the control")[
            "research_id"
        ]
        worker.wait_started()
        username = username_from(worker)

        present = terminate(authenticated_client, rid)
        assert present.status_code == 200, present.text[:200]
        assert present.json()["status"] == "success", present.json()
        assert db_row(username, rid)["status"] == "suspended", (
            "CONTROL BROKEN: the 200 did not actually change any state"
        )
    finally:
        worker.release.set()


def test_cancel_of_unknown_ids_does_not_leak_termination_flags(
    authenticated_client, monkeypatch
):
    """Cancelling ids that do not exist must not grow process-global state.

    Reads the private ``_termination_flags`` dict directly — there is no
    public accessor for its size, and the leak is only observable there.

    CONTROL: a cancel of a research that DOES exist is measured the same
    way in the same test and must leave the dict size unchanged (the worker
    exit path pops it), so a passing assertion cannot come from a dict that
    never grows for anything.
    """
    from local_deep_research.web import research_state

    worker = install_parked_worker(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")

    try:
        # CONTROL: a legitimate cancel of a live research.
        rid = submit(authenticated_client, "legit cancel")["research_id"]
        worker.wait_started()
        with research_state._lock:
            before_legit = len(research_state._termination_flags)
        api_terminate(authenticated_client, rid)
        worker.let_go()
        with research_state._lock:
            after_legit = len(research_state._termination_flags)
        assert after_legit == before_legit, (
            "CONTROL BROKEN: even a legitimate cancel leaks a flag "
            f"({before_legit} -> {after_legit}); this test cannot "
            "distinguish the unknown-id case"
        )

        # SUBJECT: five ids that never existed.
        with research_state._lock:
            before = len(research_state._termination_flags)
        for _ in range(5):
            resp = api_terminate(authenticated_client, str(uuid.uuid4()))
            assert resp.status_code == 200, resp.text[:200]
        with research_state._lock:
            after = len(research_state._termination_flags)

        assert after == before, (
            "cancelling 5 nonexistent research ids grew the process-global "
            f"termination-flag dict from {before} to {after} entries; the "
            "entries are never reclaimed"
        )
    finally:
        worker.release.set()


def test_stopping_an_already_stopped_research_is_idempotent(
    authenticated_client, monkeypatch
):
    """A second Stop must not resurrect, re-open or corrupt a stopped run.

    CONTROL: the first Stop is verified to have actually moved the row from
    in_progress to suspended, so "unchanged after the second Stop" is not
    trivially true of a row that never moved.
    """
    worker = install_parked_worker(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")

    try:
        rid = submit(authenticated_client, "stop me twice")["research_id"]
        worker.wait_started()
        username = username_from(worker)

        assert db_row(username, rid)["status"] == "in_progress"
        first = terminate(authenticated_client, rid)
        assert first.status_code == 200, first.text[:200]
        assert db_row(username, rid)["status"] == "suspended", (
            "CONTROL BROKEN: the first stop did not move the row"
        )

        second = terminate(authenticated_client, rid)
        assert second.status_code == 200, second.text[:200]
        assert "already" in second.json()["message"].lower(), (
            f"second stop did not report the terminal state: {second.json()}"
        )
        assert db_row(username, rid)["status"] == "suspended", (
            f"a second Stop changed the row: {db_row(username, rid)}"
        )

        # The other endpoint must agree rather than un-stop it.
        third = api_terminate(authenticated_client, rid)
        assert third.status_code == 200, third.text[:200]
        assert db_row(username, rid)["status"] == "suspended", (
            "cancel_research on an already-suspended research changed the "
            f"row: {db_row(username, rid)}"
        )
        http = status_of(authenticated_client, rid).json()
        assert http["status"] == "suspended", http
    finally:
        worker.release.set()


# ---------------------------------------------------------------------------
# 5. Worker failure: does the row reach a terminal state with a usable error?
#
# Here the REAL ``run_research_process`` runs — thread search context,
# snapshot settings context, LLM resolution — and only
# ``research_service.get_search`` is replaced, with something that raises.
# That is the same seam tests/settings/test_settings_take_effect.py uses;
# nothing touches the network.
# ---------------------------------------------------------------------------


class ExplodingSearch:
    """Replacement for ``research_service.get_search`` that raises."""

    def __init__(self):
        self.reached = threading.Event()
        self.message = None

    def __call__(self, **kwargs):
        self.reached.set()
        raise RuntimeError(self.message)


def seed_stub_llm(client, monkeypatch):
    from langchain_core.language_models.fake_chat_models import (
        FakeListChatModel,
    )
    from local_deep_research.llm.providers.implementations import (
        anthropic as anthropic_provider,
    )

    monkeypatch.setattr(
        anthropic_provider,
        "ChatAnthropic",
        lambda **kwargs: FakeListChatModel(responses=["stub"]),
        raising=True,
    )
    put_setting(client, "llm.provider", "anthropic")
    put_setting(client, "llm.model", "claude-3-5-sonnet-20241022")
    put_setting(client, "llm.anthropic.api_key", "sk-ant-test-key")
    put_setting(client, "search.tool", "wikipedia")


def wait_for_pending_error_update(research_id, timeout=60.0):
    """Wait until the worker has queued its terminal status update."""
    import time

    from local_deep_research.web.queue.processor_v2 import queue_processor

    deadline = time.monotonic() + timeout
    while time.monotonic() < deadline:
        with queue_processor._pending_operations_lock:
            for op in queue_processor.pending_operations.values():
                if (
                    op.get("research_id") == research_id
                    and op.get("operation_type") == "error_update"
                ):
                    return dict(op)
        time.sleep(0.1)
    raise AssertionError(
        f"the worker never queued a terminal status update for {research_id}"
    )


def test_worker_failure_lands_a_terminal_failed_row_with_a_usable_error(
    authenticated_client, monkeypatch
):
    """A crashing worker must leave a FAILED row, not a stuck in_progress one.

    Two runs raise two DIFFERENT classified errors through the identical
    path; the persisted message must differ accordingly. That is the CONTROL:
    a harness that reported a constant "failed / unknown error" would fail
    the second half.

    It also pins the *timing*: the worker does not write the terminal status
    itself — it hands an ``error_update`` to the queue processor, so the row
    still reads ``in_progress`` until a processor tick drains it (asserted
    before/after the drain below).
    """
    from local_deep_research.web.queue.processor_v2 import queue_processor
    from local_deep_research.web.services import research_service

    boom = ExplodingSearch()
    monkeypatch.setattr(research_service, "get_search", boom, raising=True)
    seed_stub_llm(authenticated_client, monkeypatch)

    rounds = [
        ("stub blew up (Error type: connection_error)", "connection"),
        ("stub blew up (Error type: openai_timeout)", "timed out"),
        ("stub blew up (Error type: openai_auth)", "authentication"),
    ]
    messages = []
    username = None

    for raw, expected_fragment in rounds:
        boom.message = raw
        boom.reached.clear()
        rid = submit(authenticated_client, f"failing run {expected_fragment}")[
            "research_id"
        ]
        assert boom.reached.wait(90), (
            "the real worker never reached get_search; nothing was exercised"
        )
        pending = wait_for_pending_error_update(rid)
        username = pending["username"]

        before = db_row(username, rid)["status"]
        queue_processor._drain_pending_operations()
        row = db_row(username, rid)

        assert before == "in_progress", (
            "premise changed: the worker now persists its terminal status "
            f"synchronously ({before!r})"
        )
        assert row["status"] == "failed", (
            f"a crashed worker left the row as {row['status']!r} instead of "
            "'failed' after the processor drained its pending operations"
        )
        assert row["completed_at"], (
            f"failed research has no completed_at: {row}"
        )
        error_text = str(row["meta"].get("error", ""))
        assert error_text, f"failed research carries no error text: {row}"
        assert "stub blew up" not in error_text, (
            f"the raw exception text was persisted verbatim: {error_text!r}"
        )
        messages.append(error_text)

        # Known authored messages must survive the API boundary. Legacy raw
        # exception text is masked, but the polling UI needs this safe reason.
        http = status_of(authenticated_client, rid).json()
        assert http["status"] == "failed", http
        assert http["metadata"]["error"] == error_text
        guidance = http["metadata"]["error_info"]
        assert expected_fragment in guidance["message"].lower(), (
            f"status endpoint lost useful error guidance: {http['metadata']}"
        )
        assert expected_fragment in error_text.lower(), (
            f"error for {raw!r} was not classified: {error_text!r}"
        )

    assert messages[0] != messages[1], (
        "both failures produced the identical message, so the persisted "
        f"error carries no information about what happened: {messages}"
    )


# ---------------------------------------------------------------------------
# 6. After the stop: is the slot released, and is there orphaned state?
# ---------------------------------------------------------------------------


def active_row_status(username, research_id):
    """Status of the UserActiveResearch row that holds the concurrency slot."""
    from local_deep_research.database.models import UserActiveResearch
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )

    with get_user_db_session(username) as session:
        row = (
            session.query(UserActiveResearch)
            .filter_by(username=username, research_id=research_id)
            .first()
        )
        return None if row is None else row.status


def test_stopped_research_releases_its_slot_only_once_the_thread_exits(
    authenticated_client, monkeypatch
):
    """Stop -> can the user start research again, and is state left behind?

    Two phases through the identical submit path give the control:

    * while the stopped run's worker thread is still alive, its
      ``UserActiveResearch`` row still holds the user's only slot, so the
      next submission is QUEUED — the Stop alone does not free capacity;
    * once that thread exits, the next submission STARTS, the stale row is
      reclaimed, and the stopped research stays ``suspended`` (it is not
      resurrected by the restart).
    """
    worker = install_queue_worker(monkeypatch)
    record_queue_notifications(monkeypatch)
    put_setting(authenticated_client, "llm.model", "seed-model")
    put_setting(authenticated_client, "app.max_concurrent_researches", 1)

    try:
        rid_a = submit(authenticated_client, "stopped run")["research_id"]
        worker.wait_started()
        username = username_from(worker)

        assert terminate(authenticated_client, rid_a).status_code == 200
        assert db_row(username, rid_a)["status"] == "suspended"

        # Phase 1 — worker thread still parked: the slot is still held.
        assert active_row_status(username, rid_a) == "in_progress", (
            "the stopped research's active-research row vanished before its "
            "thread exited"
        )
        queued_body = submit(authenticated_client, "restart while parked")
        assert queued_body["status"] == "queued", (
            "a stopped-but-still-running research did not hold the user's "
            f"only slot: {queued_body}"
        )

        # Phase 2 — let the worker thread die, then submit again.
        worker.release.set()
        assert worker.exited.wait(20), "worker never exited"

        restarted = submit(authenticated_client, "restart after exit")
        assert restarted["status"] == "success", (
            "the user could not start a new research after the stopped one's "
            f"thread exited: {restarted}"
        )
        assert restarted["research_id"] in [
            c["research_id"] for c in worker.calls
        ], "the restarted research never reached a worker thread"

        assert active_row_status(username, rid_a) != "in_progress", (
            "orphaned state: the stopped research still holds an "
            "IN_PROGRESS UserActiveResearch row after its thread died"
        )
        assert db_row(username, rid_a)["status"] == "suspended", (
            "the stopped research was resurrected by the restart: "
            f"{db_row(username, rid_a)}"
        )
    finally:
        worker.release.set()