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.

52.7 KB
Raw Blame
# allow: no-sut-import — black-box HTTP test; drives the real routers through
# the ASGI test client, with only the LLM/search boundary stubbed.
"""End-to-end behavioural contracts for chat sessions and follow-up research.

Everything here runs over **real HTTP** against the FastAPI app, with real
registered users, real per-user encrypted SQLCipher databases and the real
background research thread. The single seam that is stubbed is the
LLM/search boundary: ``research_service.AdvancedSearchSystem`` is replaced by
a recorder that returns a canned report and remembers exactly what the
research layer handed to the strategy (``strategy_name``,
``research_context``, ...). Nothing else is mocked, so a persisted row here
is a row the production code really wrote.

The question asked throughout is "does the feature work?", not "did the
endpoint return 200":

* ``TestChatSessionLifecycle`` -- create -> send -> answer -> history ->
  rename -> archive -> delete, checking the *persisted transcript* matches
  what was exchanged, in order, and survives a fresh client.
* ``TestFollowUpParentContextHandover`` -- what the strategy boundary
  actually RECEIVED when a follow-up ran on a completed parent.
* ``TestFollowUpParentLinkExposure`` -- is the child linked to its parent
  afterwards, and can any UI-facing API see that link?
* ``TestCrossUserResearchIds`` -- a foreign research id is ABSENT (per-user
  encrypted DBs), not refused; every negative has an owner-side control
  through the identical path.
* ``TestConcurrentSends`` -- two sends racing on one session, and the
  commit-time backstop behind the admission gate, reached through a row a
  test thread writes outside that gate.
* ``TestErrorSurfaces`` -- the LLM boundary raises; is the session still
  usable afterwards?
* ``TestUserDatabaseErrorWrapping`` -- encrypted driver errors must reach
  SQLAlchemy exception handlers, just like errors from ordinary SQLite.

Known-and-filed defects deliberately NOT re-tested here: #5803 (deleting a
non-newest attempt bricks the session) and #5793 (the follow-up router's
configured delegate strategy never reaches the strategy).
"""

import json
import threading
import uuid
from unittest.mock import patch

import pytest

from tests.conftest import generate_unique_test_username


# --------------------------------------------------------------------------
# Recording stand-in for the LLM/search boundary
# --------------------------------------------------------------------------

ANSWER = "STUB_ANSWER: the recorded assistant report body."


class _RecordingSearchSystem:
    """Stands in for ``AdvancedSearchSystem`` inside the research worker.

    Records the *constructor* kwargs -- which is precisely what the research
    layer hands the strategy: ``strategy_name``, ``research_context``,
    ``max_iterations``, ``username``, ... -- then returns a canned report so
    the worker's persistence path runs for real.
    """

    # Class-level so a test can read what the background thread observed.
    constructions: list = []
    analyzed: list = []
    # When set, analyze_topic raises this (simulates an LLM/search failure).
    raise_on_analyze: BaseException | None = None
    # Set by tests that want to observe two workers overlapping.
    gate: threading.Event | None = None

    def __init__(self, **kwargs):
        self.kwargs = kwargs
        self.all_links_of_system = []
        type(self).constructions.append(kwargs)

    @classmethod
    def reset(cls):
        cls.constructions = []
        cls.analyzed = []
        cls.raise_on_analyze = None
        cls.gate = None

    def set_progress_callback(self, callback):
        self._callback = callback

    def analyze_topic(self, query):
        type(self).analyzed.append(query)
        if type(self).gate is not None:
            # Hold the worker open so a second request sees it in flight.
            type(self).gate.wait(timeout=10)
        if type(self).raise_on_analyze is not None:
            raise type(self).raise_on_analyze
        return {
            "findings": [{"phase": "stub", "content": ANSWER}],
            "formatted_findings": ANSWER,
            "current_knowledge": ANSWER,
            "iterations": 1,
            "questions": {},
            "all_links_of_system": [],
        }

    def close(self):
        pass


class _FakeLLM:
    """Minimal LLM: every invoke returns a fixed short string."""

    def invoke(self, *args, **kwargs):
        class _R:
            content = "stub llm output"

        return _R()

    def __call__(self, *args, **kwargs):
        return self.invoke(*args, **kwargs)


@pytest.fixture
def stub_research_boundary():
    """Replace the LLM + search + strategy boundary inside the worker.

    ``research_service`` binds ``get_llm``/``get_search``/
    ``AdvancedSearchSystem`` as module-level names, so they must be patched
    on that module (patching ``config.llm_config.get_llm`` would not affect
    the already-bound reference).
    """
    _RecordingSearchSystem.reset()
    base = "local_deep_research.web.services.research_service"
    with (
        patch(f"{base}.AdvancedSearchSystem", _RecordingSearchSystem),
        patch(f"{base}.get_llm", return_value=_FakeLLM()),
        patch(f"{base}.get_search", return_value=object()),
        patch(
            "local_deep_research.config.llm_config.get_llm",
            return_value=_FakeLLM(),
        ),
    ):
        yield _RecordingSearchSystem
    _RecordingSearchSystem.reset()


# --------------------------------------------------------------------------
# Real users over real HTTP
# --------------------------------------------------------------------------


def _new_user(app, prefix="chatfu"):
    """Register + log in a brand-new user; return (client, username).

    Deliberately does NOT wipe ``encrypted_databases`` (unlike the shared
    ``authenticated_client`` fixture) so several users can coexist in one
    test -- which is the whole point of the cross-user cases.
    """
    from tests.conftest import _make_flask_compat_client

    username = generate_unique_test_username(prefix)
    password = "TestPass123"
    client = _make_flask_compat_client(app)
    # Unique forwarded IP so each user gets their own rate-limit bucket
    # (/auth/register is rate limited per client key).
    client.headers.update(
        {
            "X-Forwarded-For": f"10.{uuid.uuid4().int % 250 + 1}.{uuid.uuid4().int % 250 + 1}.7"
        }
    )

    def _csrf():
        client.get("/auth/login")
        return client.get("/auth/csrf-token").json()["csrf_token"]

    reg = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    assert reg.status_code in (200, 302), (reg.status_code, reg.text[:400])
    login = client.post(
        "/auth/login",
        data={
            "username": username,
            "password": password,
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    assert login.status_code in (200, 302), (
        login.status_code,
        login.text[:400],
    )
    tok = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": tok})
    return client, username


def _relogin(app, username, password="TestPass123"):
    """Fresh client + fresh login for an EXISTING user ("reload the page")."""
    from tests.conftest import _make_flask_compat_client

    client = _make_flask_compat_client(app)
    client.headers.update(
        {
            "X-Forwarded-For": f"10.{uuid.uuid4().int % 250 + 1}."
            f"{uuid.uuid4().int % 250 + 1}.8"
        }
    )
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    resp = client.post(
        "/auth/login",
        data={
            "username": username,
            "password": password,
            "csrf_token": token,
        },
        follow_redirects=False,
    )
    assert resp.status_code in (200, 302), (resp.status_code, resp.text[:300])
    client.headers.update(
        {"X-CSRFToken": client.get("/auth/csrf-token").json()["csrf_token"]}
    )
    return client


def _json(resp):
    return json.loads(resp.content)


def _create_session(client, query="What is quantum error correction?"):
    resp = client.post(
        "/api/chat/sessions",
        json={"initial_query": query},
        content_type="application/json",
    )
    assert resp.status_code == 200, resp.text[:400]
    body = _json(resp)
    assert body["success"] is True
    return body["session_id"]


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


def _await_research(research_id, timeout=25.0):
    """Block until the in-memory active-research registry drops the id.

    Uses the process-local registry rather than polling an HTTP endpoint so a
    wait does not burn connections from the (small) per-user DB pool.
    """
    import time

    from local_deep_research.web.research_state import is_research_active

    deadline = time.monotonic() + timeout
    while time.monotonic() < deadline:
        if not is_research_active(research_id):
            # The worker deregisters before its last commits land in some
            # paths; give the transaction a moment to settle.
            time.sleep(0.15)  # allow: unmarked-sleep
            return True
        time.sleep(0.05)  # allow: unmarked-sleep
    return False


def _messages(client, session_id):
    resp = client.get(f"/api/chat/sessions/{session_id}/messages")
    assert resp.status_code == 200, resp.text[:400]
    body = _json(resp)
    assert body["success"] is True
    return body["messages"]


def _conversation_turns(messages):
    """(role, content) for durable conversation rows only (no step rows)."""
    return [
        (m.get("role"), m.get("content"))
        for m in messages
        if m.get("message_type") != "step"
    ]


# ==========================================================================
# 1. Full chat session lifecycle
# ==========================================================================


class TestChatSessionLifecycle:
    """create -> send -> receive -> history -> rename -> archive -> delete."""

    def test_smoke_research_worker_runs_and_answer_is_persisted(
        self, app, stub_research_boundary
    ):
        """HARNESS CONTROL for every test below.

        Proves the stubbed boundary is really reached by the real background
        worker and that its output lands in the persisted transcript. If this
        test ever fails, no assertion in this file means anything.
        """
        client, _username = _new_user(app)
        session_id = _create_session(client, "What is a surface code?")

        resp = _send(client, session_id, "Explain surface codes.")
        assert resp.status_code == 200, resp.text[:400]
        body = _json(resp)
        research_id = body["research_id"]
        assert research_id, "chat send did not start a research"

        assert _await_research(research_id), "research worker never finished"

        # The stub was constructed by the real worker...
        assert stub_research_boundary.analyzed == ["Explain surface codes."], (
            f"strategy boundary saw {stub_research_boundary.analyzed!r}"
        )
        # ...and its report was persisted as the assistant turn.
        turns = _conversation_turns(_messages(client, session_id))
        assert turns == [
            ("user", "Explain surface codes."),
            ("assistant", ANSWER),
        ], turns

    def test_transcript_is_ordered_and_survives_a_fresh_login(
        self, app, stub_research_boundary
    ):
        """Two full research turns; the persisted transcript must match the
        exchange, in order, and be identical when read by a *new* client
        session for the same user (the "reload the page" case).
        """
        client, username = _new_user(app)
        session_id = _create_session(client, "topic seed")

        first = _json(_send(client, session_id, "First question?"))
        assert _await_research(first["research_id"])
        second = _json(_send(client, session_id, "Second question?"))
        assert _await_research(second["research_id"])

        expected = [
            ("user", "First question?"),
            ("assistant", ANSWER),
            ("user", "Second question?"),
            ("assistant", ANSWER),
        ]
        assert _conversation_turns(_messages(client, session_id)) == expected

        # Each assistant turn must point back at the research that produced
        # it -- otherwise the UI cannot open the report behind an answer.
        answers = [
            m
            for m in _messages(client, session_id)
            if m.get("message_type") == "response"
        ]
        assert [m.get("research_id") for m in answers] == [
            first["research_id"],
            second["research_id"],
        ]

        # "Reload": brand-new client + fresh login as the same user. This
        # re-opens the encrypted database from disk, so it proves the
        # transcript is durable rather than cached in the live session.
        reloaded = _relogin(app, username)
        assert _conversation_turns(_messages(reloaded, session_id)) == expected

        # The session's own record agrees about how many turns happened.
        sess = _json(reloaded.get(f"/api/chat/sessions/{session_id}"))[
            "session"
        ]
        assert sess["message_count"] == 4, sess

    def test_rename_archive_and_delete_are_each_observable(
        self, app, stub_research_boundary
    ):
        """Rename / archive / delete must each change persisted state, and
        the transcript must survive rename+archive but not delete.
        """
        client, _username = _new_user(app)
        session_id = _create_session(client, "lifecycle")
        sent = _json(
            _send(client, session_id, "A question.", trigger_research=False)
        )
        assert sent["success"] is True

        # Rename.
        renamed = client.patch(
            f"/api/chat/sessions/{session_id}",
            json={"title": "My renamed chat"},
            content_type="application/json",
        )
        assert renamed.status_code == 200, renamed.text[:300]
        assert _json(renamed)["session"]["title"] == "My renamed chat"
        # Durable, not just echoed back.
        assert (
            _json(client.get(f"/api/chat/sessions/{session_id}"))["session"][
                "title"
            ]
            == "My renamed chat"
        )

        # Archive: still readable, but closed to new messages.
        archived = client.patch(
            f"/api/chat/sessions/{session_id}",
            json={"status": "archived"},
            content_type="application/json",
        )
        assert archived.status_code == 200, archived.text[:300]
        assert _conversation_turns(_messages(client, session_id)) == [
            ("user", "A question.")
        ]
        blocked = _send(client, session_id, "another", trigger_research=False)
        assert blocked.status_code == 409, blocked.text[:300]
        assert _conversation_turns(_messages(client, session_id)) == [
            ("user", "A question.")
        ], "a rejected send must not append a message"

        # Reactivate -> sends work again (control for the 409 above).
        client.patch(
            f"/api/chat/sessions/{session_id}",
            json={"status": "active"},
            content_type="application/json",
        )
        ok = _send(client, session_id, "another", trigger_research=False)
        assert ok.status_code == 200, ok.text[:300]

        # Delete: the session and its transcript become unreachable.
        deleted = client.delete(f"/api/chat/sessions/{session_id}")
        assert deleted.status_code == 200, deleted.text[:300]
        gone = client.get(f"/api/chat/sessions/{session_id}/messages")
        assert gone.status_code == 404, gone.text[:300]
        assert _json(gone)["error"] == "Session not found"
        listed = _json(client.get("/api/chat/sessions?status=all"))
        assert session_id not in [s["id"] for s in listed["sessions"]], listed


def _set_setting(client, key, value):
    """Write a real user setting through the real settings API."""
    resp = client.put(
        f"/settings/api/{key}",
        json={"value": value},
        content_type="application/json",
    )
    assert resp.status_code == 200, (key, resp.status_code, resp.text[:300])
    # Read it back so a silently-ignored write can't make a later assertion
    # look like a product behaviour.
    check = client.get(f"/settings/api/{key}")
    assert check.status_code == 200, check.text[:300]
    body = _json(check)
    stored = body.get("value", body.get("setting", {}).get("value"))
    assert stored == value, check.text[:300]


# ==========================================================================
# 2. Follow-up: does the parent's context reach the run?
# ==========================================================================


class TestChatFollowUpContextHandover:
    """A chat follow-up turn must hand the prior conversation to the strategy.

    Assertions are on the *constructor kwargs the research worker passed to
    the strategy layer* -- i.e. what the strategy actually received -- not on
    the HTTP status of the send.
    """

    def test_followup_turn_carries_the_prior_exchange_to_the_strategy(
        self, app, stub_research_boundary
    ):
        client, _username = _new_user(app)
        # "full" mode puts the verbatim transcript in the context, so a leak
        # or a loss is unambiguous rather than laundered through an LLM.
        _set_setting(client, "chat.followup_context_mode", "full")

        session_id = _create_session(client, "seed")
        first = _json(_send(client, session_id, "What is a toric code?"))
        assert _await_research(first["research_id"])
        second = _json(_send(client, session_id, "And its threshold?"))
        assert _await_research(second["research_id"])

        assert len(stub_research_boundary.constructions) == 2, (
            stub_research_boundary.constructions
        )
        turn1, turn2 = stub_research_boundary.constructions

        # Turn 1 is not a follow-up.
        assert turn1["strategy_name"] != "enhanced-contextual-followup"
        assert turn1["research_context"].get("is_multi_turn") is False
        assert turn1["research_context"].get("past_findings") == ""

        # Turn 2 is, and it carries the real prior exchange.
        assert turn2["strategy_name"] == "enhanced-contextual-followup"
        assert turn2["research_context"]["is_multi_turn"] is True
        prior = turn2["research_context"]["past_findings"]
        assert "What is a toric code?" in prior, prior[:300]
        assert ANSWER in prior, prior[:300]
        # The conversation's opening question anchors the follow-up prompt.
        assert (
            turn2["research_context"]["original_query"]
            == "What is a toric code?"
        )
        # And the run is bound to the chat session it came from.
        assert turn2["research_context"]["chat_session_id"] == session_id

    def test_context_mode_none_is_the_negative_control(
        self, app, stub_research_boundary
    ):
        """Same path, one setting flipped: the prior exchange must NOT be
        handed over. Proves the positive test above is observing the real
        context-assembly and not something that is always populated.
        """
        client, _username = _new_user(app)
        _set_setting(client, "chat.followup_context_mode", "none")

        session_id = _create_session(client, "seed")
        first = _json(_send(client, session_id, "What is a toric code?"))
        assert _await_research(first["research_id"])
        second = _json(_send(client, session_id, "And its threshold?"))
        assert _await_research(second["research_id"])

        turn2 = stub_research_boundary.constructions[1]
        # Still routed as a follow-up...
        assert turn2["strategy_name"] == "enhanced-contextual-followup"
        assert turn2["research_context"]["is_multi_turn"] is True
        # ...but deliberately carrying nothing.
        assert turn2["research_context"]["past_findings"] == ""
        assert ANSWER not in json.dumps(turn2["research_context"], default=str)


def _completed_parent_research(client, question="What is a toric code?"):
    """Run one real research to completion; return (research_id, session_id)."""
    session_id = _create_session(client, "parent seed")
    started = _json(_send(client, session_id, question))
    research_id = started["research_id"]
    assert _await_research(research_id), "parent research never finished"
    return research_id, session_id


class TestFollowUpEndpointContextHandover:
    """``/api/followup/prepare`` + ``/api/followup/start`` over real HTTP."""

    def test_start_hands_the_parents_report_to_the_strategy(
        self, app, stub_research_boundary
    ):
        client, _username = _new_user(app)
        _set_setting(client, "llm.model", "test-model")

        parent_id, _sid = _completed_parent_research(client)

        # The parent's report really is on disk (control for the context
        # assertions below -- an empty report would make them vacuous).
        report = client.get(f"/history/report/{parent_id}")
        assert report.status_code == 200, report.text[:300]
        assert ANSWER in report.text, report.text[:300]

        # prepare/ sees the parent.
        prep = client.post(
            "/api/followup/prepare",
            json={
                "parent_research_id": parent_id,
                "question": "Why does the threshold matter?",
            },
            content_type="application/json",
        )
        assert prep.status_code == 200, prep.text[:400]
        prep_body = _json(prep)
        assert prep_body["success"] is True
        assert prep_body["parent_research"]["id"] == parent_id
        assert prep_body["parent_summary"] == "What is a toric code?"

        before = len(stub_research_boundary.constructions)
        start = client.post(
            "/api/followup/start",
            json={
                "parent_research_id": parent_id,
                "question": "Why does the threshold matter?",
            },
            content_type="application/json",
        )
        assert start.status_code == 200, start.text[:400]
        child_id = _json(start)["research_id"]
        assert child_id != parent_id
        assert _await_research(child_id), "follow-up research never finished"

        assert len(stub_research_boundary.constructions) == before + 1
        child = stub_research_boundary.constructions[-1]
        assert child["strategy_name"] == "enhanced-contextual-followup"
        ctx = child["research_context"]
        # The parent's identity, question and report all reached the run.
        assert ctx["parent_research_id"] == parent_id
        assert ctx["original_query"] == "What is a toric code?"
        assert ANSWER in ctx["report_content"], ctx["report_content"][:300]
        # And the query the strategy analysed is the follow-up question.
        assert (
            stub_research_boundary.analyzed[-1]
            == "Why does the threshold matter?"
        )


# ==========================================================================
# 3. Is the follow-up linked to its parent, and can the UI see the link?
# ==========================================================================


class TestFollowUpParentLinkExposure:
    def test_completed_followup_still_points_at_its_parent(
        self, app, stub_research_boundary
    ):
        """The link must survive the worker rewriting research_meta on
        completion, and must be readable through a UI-facing API.

        The completion path replaces ``research_meta`` wholesale
        (``research.research_meta = metadata``), so "the row still knows its
        parent AFTER the run finished" is a real question, not a tautology.
        """
        client, _username = _new_user(app)
        _set_setting(client, "llm.model", "test-model")
        parent_id, _sid = _completed_parent_research(client)

        start = client.post(
            "/api/followup/start",
            json={
                "parent_research_id": parent_id,
                "question": "Why does the threshold matter?",
            },
            content_type="application/json",
        )
        assert start.status_code == 200, start.text[:400]
        child_id = _json(start)["research_id"]
        assert _await_research(child_id)

        # Completed, not merely started.
        status = client.get(f"/research/api/status/{child_id}")
        assert status.status_code == 200, status.text[:300]
        status_body = _json(status)
        assert status_body["status"] == "completed", status_body

        # ...and it still names its parent.
        submission = status_body["metadata"]["submission"]
        assert submission["parent_research_id"] == parent_id, status_body
        assert submission["strategy"] == "contextual-followup"

        # The same link is visible on the detail endpoint the UI uses.
        detail = _json(client.get(f"/api/research/{child_id}"))
        assert (
            detail["metadata"]["submission"]["parent_research_id"] == parent_id
        ), detail

        # CONTROL: the parent itself is NOT a follow-up, so it carries no
        # parent pointer through the identical endpoint. Without this, the
        # assertions above would pass against an API that stamped every
        # research with the same key.
        parent_detail = _json(client.get(f"/api/research/{parent_id}"))
        assert "parent_research_id" not in parent_detail["metadata"].get(
            "submission", {}
        ), parent_detail

    def test_history_list_deliberately_hides_the_parent_link(
        self, app, stub_research_boundary
    ):
        """Recorded, not complained about: ``/history/api`` runs metadata
        through an allowlist (``filter_research_metadata``) that keeps only
        ``is_news_search``, so the history LIST cannot render a
        "follow-up of ..." badge even though the per-research endpoints can.
        Pinned so a future widening of that allowlist is a deliberate,
        reviewed change rather than an accidental secret-leak regression.
        """
        client, _username = _new_user(app)
        _set_setting(client, "llm.model", "test-model")
        parent_id, _sid = _completed_parent_research(client)
        child_id = _json(
            client.post(
                "/api/followup/start",
                json={"parent_research_id": parent_id, "question": "why?"},
                content_type="application/json",
            )
        )["research_id"]
        assert _await_research(child_id)

        items = _json(client.get("/history/api"))["items"]
        by_id = {i["id"]: i for i in items}
        # CONTROL: both researches really are in the list, so a missing key
        # below is about filtering and not about a missing row.
        assert parent_id in by_id and child_id in by_id, sorted(by_id)
        assert set(by_id[child_id]["metadata"]) == {"is_news_search"}, by_id[
            child_id
        ]["metadata"]


# ==========================================================================
# 4. Cross-user: a foreign id is ABSENT, not refused
# ==========================================================================


class TestCrossUserChatAndFollowUp:
    def test_another_users_chat_session_is_invisible_and_unwritable(
        self, app, stub_research_boundary
    ):
        alice, _a = _new_user(app, "alice")
        bob, _b = _new_user(app, "bob")

        alice_sid = _create_session(alice, "alice topic")
        assert (
            _json(
                _send(alice, alice_sid, "alice message", trigger_research=False)
            )["success"]
            is True
        )

        # CONTROL: the owner reaches it through the identical path.
        assert alice.get(f"/api/chat/sessions/{alice_sid}").status_code == 200
        assert _conversation_turns(_messages(alice, alice_sid)) == [
            ("user", "alice message")
        ]

        # Bob cannot see it...
        seen = bob.get(f"/api/chat/sessions/{alice_sid}")
        assert seen.status_code == 404, seen.text[:300]
        assert _json(seen)["error"] == "Session not found"
        msgs = bob.get(f"/api/chat/sessions/{alice_sid}/messages")
        assert msgs.status_code == 404, msgs.text[:300]

        # ...and cannot write into it.
        wrote = _send(bob, alice_sid, "bob injection", trigger_research=False)
        assert wrote.status_code == 404, wrote.text[:300]
        assert _json(wrote)["error"] == "Session not found"

        # CONTROL for the 404 reason: the route is not simply refusing every
        # id for Bob -- his own session works through the very same calls.
        bob_sid = _create_session(bob, "bob topic")
        assert bob.get(f"/api/chat/sessions/{bob_sid}").status_code == 200
        assert (
            _send(
                bob, bob_sid, "bob message", trigger_research=False
            ).status_code
            == 200
        )

        # Alice's transcript is untouched by any of Bob's attempts.
        assert _conversation_turns(_messages(alice, alice_sid)) == [
            ("user", "alice message")
        ]
        assert bob_sid != alice_sid

    def test_followup_prepare_404s_on_a_foreign_parent_because_it_is_absent(
        self, app, stub_research_boundary
    ):
        alice, _a = _new_user(app, "alice")
        bob, _b = _new_user(app, "bob")
        _set_setting(alice, "llm.model", "test-model")
        _set_setting(bob, "llm.model", "test-model")

        alice_parent, _sid = _completed_parent_research(
            alice, "alice secret question"
        )

        body = {"parent_research_id": alice_parent, "question": "tell me more"}
        # CONTROL: the owner gets a real 200 on this exact id.
        owner = alice.post(
            "/api/followup/prepare", json=body, content_type="application/json"
        )
        assert owner.status_code == 200, owner.text[:300]
        assert _json(owner)["parent_summary"] == "alice secret question"

        # Bob gets 404 -- and specifically the "parent not found" 404, i.e.
        # the row is absent from HIS database, not an auth refusal.
        foreign = bob.post(
            "/api/followup/prepare", json=body, content_type="application/json"
        )
        assert foreign.status_code == 404, foreign.text[:300]
        assert _json(foreign)["error"] == "Parent research not found"

        # CONTROL for the 404 reason: Bob's own parent works identically.
        bob_parent, _bsid = _completed_parent_research(bob, "bob question")
        ok = bob.post(
            "/api/followup/prepare",
            json={"parent_research_id": bob_parent, "question": "more"},
            content_type="application/json",
        )
        assert ok.status_code == 200, ok.text[:300]
        assert _json(ok)["parent_summary"] == "bob question"

    def test_followup_start_on_a_foreign_parent_is_refused(
        self, app, stub_research_boundary
    ):
        """``/start`` refuses a parent the caller does not own, with 404.

        This test previously asserted the OPPOSITE -- that ``/start`` did not
        404 and instead ran with an empty context -- and pinned the
        consequence (nothing of the other user's research reached the
        strategy). main's #5600 closed that gap: ``/api/followup/start`` now
        applies the same ownership check ``/api/followup/prepare`` always had,
        so no research thread is spawned at all. The assertion is inverted
        here rather than deleted, because the interesting property is not
        "404" but "NOTHING HAPPENED": no strategy construction, no child row.
        """
        alice, _a = _new_user(app, "alice")
        bob, _b = _new_user(app, "bob")
        _set_setting(bob, "llm.model", "test-model")

        alice_parent, _sid = _completed_parent_research(
            alice, "alice secret question"
        )

        before = len(stub_research_boundary.constructions)
        start = bob.post(
            "/api/followup/start",
            json={
                "parent_research_id": alice_parent,
                "question": "what did alice find?",
            },
            content_type="application/json",
        )
        assert start.status_code == 404, start.text[:400]
        # 404 for the RIGHT reason -- the same message /prepare uses -- not an
        # incidental routing or lookup miss.
        assert _json(start)["error"] == "Parent research not found"

        # The point of the test: no research thread was started for Bob.
        assert len(stub_research_boundary.constructions) == before, (
            "a follow-up run was spawned for a parent the caller does not own"
        )

        # Control: Alice's parent is still hers and still reachable, so the
        # 404 above is about ownership rather than a vanished row.
        assert (
            alice.get(f"/research/api/status/{alice_parent}").status_code == 200
        )
        assert (
            bob.get(f"/research/api/status/{alice_parent}").status_code == 404
        )


def _race_past_the_fast_fail(client, session_id, stub):
    """Let request B start and commit while request A waits between checks.

    Request A is started on a thread and parked inside the real
    ``_load_settings`` -- after the route's short gated fast-fail, before
    its authoritative recheck under the per-user admission gate. The
    wrapper calls the genuine function and only delays the caller; no
    product behaviour is faked. Request B then runs to completion (its
    checks see nothing in progress, so it commits and registers its
    worker), after which A is released and runs its recheck.

    Returns ``({"a": <A response>, "b": <B response>}, <A response>)``.
    """
    import local_deep_research.web.routers.chat as chat_router

    real_load_settings = chat_router._load_settings
    parked = threading.Event()
    release = threading.Event()

    def _timing_hook(username):
        settings = real_load_settings(username)
        if not parked.is_set():
            parked.set()
            release.wait(timeout=20)
        return settings

    outcome: dict = {}

    def _run_a():
        outcome["a"] = _send(client, session_id, "racer A")

    # Hold the worker inside analyze_topic so B stays IN_PROGRESS while A
    # runs its recheck.
    stub.gate = threading.Event()
    thread = threading.Thread(target=_run_a)
    try:
        with patch.object(chat_router, "_load_settings", _timing_hook):
            thread.start()
            assert parked.wait(timeout=20), "request A never reached the hook"
            outcome["b"] = _send(client, session_id, "racer B")
            release.set()
            thread.join(timeout=30)
    finally:
        release.set()
        stub.gate.set()
        thread.join(timeout=30)

    assert set(outcome) == {"a", "b"}, outcome
    return outcome, outcome["a"]


def _collide_on_commit(client, username, session_id):
    """Drive one send into a COMMIT-time collision, deterministically.

    Two sends in this process no longer meet at COMMIT. The per-user
    admission gate is held from the authoritative recheck through the
    commit, so the later send is refused by its recheck instead (see
    ``_race_past_the_fast_fail``). The partial unique index
    ``ux_research_history_chat_session_in_progress`` and the route's
    ``except IntegrityError`` remain the backstop for a writer that does
    not take this process's gate. To reach it, the send is parked inside
    the real ``ChatService.insert_message_in_db`` -- after its gated recheck
    passed, before its atomic write touches the database -- while another
    thread, standing in for such a writer, commits an IN_PROGRESS research
    row on the same chat. The send is then released and its commit hits
    the index. The wrapper calls the genuine method and only delays the
    caller; no product behaviour is faked.

    Returns ``(<send response>, <competing research id>)``.
    """
    from datetime import UTC, datetime

    import local_deep_research.web.routers.chat as chat_router
    from local_deep_research.constants import ResearchStatus
    from local_deep_research.database.models import ResearchHistory
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )
    from local_deep_research.database.thread_local_session import (
        thread_cleanup,
    )

    # The class the route instantiates (the chat conftest rebinds the
    # genuine one before every test).
    service_cls = chat_router.ChatService
    real_insert = service_cls.insert_message_in_db
    competing_id = str(uuid.uuid4())
    started: list = []
    committed: list = []

    @thread_cleanup
    def _ungated_writer():
        with get_user_db_session(username) as db:
            db.add(
                ResearchHistory(
                    id=competing_id,
                    query="written outside the admission gate",
                    mode="quick",
                    status=ResearchStatus.IN_PROGRESS.value,
                    created_at=datetime.now(UTC).isoformat(),
                    chat_session_id=session_id,
                )
            )
            db.commit()
        committed.append(competing_id)

    def _timing_hook(self, db, *args, **kwargs):
        if not started:
            started.append(True)
            writer = threading.Thread(target=_ungated_writer)
            writer.start()
            writer.join(timeout=20)
        return real_insert(self, db, *args, **kwargs)

    with patch.object(service_cls, "insert_message_in_db", _timing_hook):
        response = _send(client, session_id, "racer A")

    assert committed == [competing_id], "the competing row was never written"
    return response, competing_id


# ==========================================================================
# 5. Concurrency on a single chat session
# ==========================================================================


class TestConcurrentSends:
    def test_send_while_previous_research_is_still_running_is_refused(
        self, app, stub_research_boundary
    ):
        """The second send must be refused, must name the running research,
        and must not leave a half-written turn behind.
        """
        client, _username = _new_user(app)
        session_id = _create_session(client, "concurrency")

        stub_research_boundary.gate = threading.Event()
        try:
            first = _json(_send(client, session_id, "long running question"))
            running_id = first["research_id"]

            # Wait until the worker is genuinely inside the strategy, so the
            # 409 below is about a live research and not a timing accident.
            import time

            deadline = time.monotonic() + 10
            while (
                not stub_research_boundary.analyzed
                and time.monotonic() < deadline
            ):
                time.sleep(0.02)  # allow: unmarked-sleep
            assert stub_research_boundary.analyzed == [
                "long running question"
            ], stub_research_boundary.analyzed

            second = _send(client, session_id, "impatient second question")
            assert second.status_code == 409, second.text[:400]
            body = _json(second)
            assert body["active_research_id"] == running_id, body

            # The refused send left NO user message behind.
            assert _conversation_turns(_messages(client, session_id)) == [
                ("user", "long running question")
            ]
            # ...and only one research was ever handed to the strategy.
            assert len(stub_research_boundary.constructions) == 1
        finally:
            stub_research_boundary.gate.set()

        assert _await_research(running_id)
        # CONTROL: once it finishes, the same send succeeds -- the 409 was a
        # live-research guard, not a permanently broken session.
        retry = _send(client, session_id, "impatient second question")
        assert retry.status_code == 200, retry.text[:400]
        assert _await_research(_json(retry)["research_id"])
        assert _conversation_turns(_messages(client, session_id)) == [
            ("user", "long running question"),
            ("assistant", ANSWER),
            ("user", "impatient second question"),
            ("assistant", ANSWER),
        ]

    def test_send_that_raced_past_the_fast_fail_is_refused_by_the_recheck(
        self, app, stub_research_boundary
    ):
        """A send that passed the fast-fail while another send started is
        refused by its recheck, never reaching its atomic write.

        Request A is parked in ``_load_settings`` while request B commits
        and registers its worker. A's authoritative recheck runs after that
        wait, under the per-user admission gate that a passing recheck keeps
        through the commit and the worker registration. It sees B's
        IN_PROGRESS row and answers with the per-session guard's 409, which
        names B's research. This interleaving used to collide with B at COMMIT,
        because the route checked only once, before ``_load_settings``. The
        loser then got the commit handler's shorter message and no research
        id. The commit-time collision is now reached only by a writer that
        bypasses the gate; see ``_collide_on_commit``.
        """
        client, username = _new_user(app)
        session_id = _create_session(client, "fast-fail race")

        outcome, loser = _race_past_the_fast_fail(
            client, session_id, stub_research_boundary
        )

        winner = outcome["b"]
        assert winner.status_code == 200, winner.text[:300]
        winner_id = _json(winner)["research_id"]
        assert loser.status_code == 409, (loser.status_code, loser.text[:300])
        assert _json(loser)["error"] == (
            "Research already in progress on this chat session. "
            "Stop it before sending a new message."
        )
        assert _json(loser)["active_research_id"] == winner_id
        assert _await_research(winner_id)

        # Exactly one research reached the strategy...
        assert stub_research_boundary.analyzed == ["racer B"], (
            stub_research_boundary.analyzed
        )
        # ...exactly one turn pair is persisted (the loser left no orphan
        # user message behind)...
        assert _conversation_turns(_messages(client, session_id)) == [
            ("user", "racer B"),
            ("assistant", ANSWER),
        ]
        # ...and the loser left no phantom concurrency accounting either.
        from local_deep_research.database.models import (
            ResearchHistory,
            UserActiveResearch,
        )
        from local_deep_research.database.session_context import (
            get_user_db_session,
        )
        from local_deep_research.web.research_state import (
            get_user_research_start_lock,
        )

        with get_user_db_session(username) as db:
            assert [r.id for r in db.query(ResearchHistory).all()] == [
                winner_id
            ]
            # The winner's concurrency row was cleaned up when it
            # finished; the loser never left one behind either.
            assert db.query(UserActiveResearch).all() == []
        # The refusal released the gate it was holding.
        assert not get_user_research_start_lock(username).locked()

    def test_commit_collision_keeps_the_session_consistent(
        self, app, stub_research_boundary
    ):
        """Force the *commit-time* collision (not the SELECT guard).

        Parking request A inside ``_load_settings`` until request B has
        committed used to schedule this collision. A now rechecks under the
        admission gate after that wait, so that interleaving is refused by
        the recheck instead and never reaches COMMIT
        (``test_send_that_raced_past_the_fast_fail_is_refused_by_the_recheck``
        above). The partial unique index
        ``ux_research_history_chat_session_in_progress`` still catches a
        writer outside this process's gate, so ``_collide_on_commit`` has
        such a writer commit an IN_PROGRESS row while the send sits between
        its recheck and its atomic write. This test pins the resulting
        STATE. The response is covered by
        ``test_commit_collision_should_be_409_not_500`` below.
        """
        client, username = _new_user(app)
        session_id = _create_session(client, "commit race")

        loser, competing_id = _collide_on_commit(client, username, session_id)
        assert loser.status_code == 409, (loser.status_code, loser.text[:300])

        # The losing send never reached the strategy...
        assert stub_research_boundary.constructions == []
        # ...left no orphan user message behind, which is the invariant
        # the atomic write exists to protect...
        assert _conversation_turns(_messages(client, session_id)) == []
        # ...and left no phantom concurrency accounting either.
        from local_deep_research.database.models import (
            ChatSession,
            ResearchHistory,
            UserActiveResearch,
        )
        from local_deep_research.database.session_context import (
            get_user_db_session,
        )
        from local_deep_research.web.research_state import (
            get_user_research_start_lock,
        )

        with get_user_db_session(username) as db:
            assert [r.id for r in db.query(ResearchHistory).all()] == [
                competing_id
            ]
            assert db.query(UserActiveResearch).all() == []
            # The rollback also undid the message_count increment.
            assert (
                db.query(ChatSession.message_count)
                .filter_by(id=session_id)
                .scalar()
                == 0
            )
        # The collision released the gate the send was holding.
        assert not get_user_research_start_lock(username).locked()

    def test_commit_collision_should_be_409_not_500(
        self, app, stub_research_boundary
    ):
        """The commit-time collision answers 409, not an opaque 500.

        This drove two real sends into the collision until the admission
        gate was held from the recheck through the commit. That pair is now
        refused by the recheck with the per-session guard's longer message
        (pinned by
        ``test_send_that_raced_past_the_fast_fail_is_refused_by_the_recheck``),
        so the collision is reached through a writer outside the gate.
        """
        client, username = _new_user(app)
        session_id = _create_session(client, "commit race")
        loser, _competing_id = _collide_on_commit(client, username, session_id)
        assert loser.status_code == 409, (loser.status_code, loser.text[:300])
        assert _json(loser)["error"] == (
            "Research already in progress on this chat session."
        )


class TestUserDatabaseErrorWrapping:
    """SQLAlchemy wraps encrypted DBAPI errors for application handlers."""

    def test_plain_sqlite_wraps_a_unique_violation_positive_control(
        self, tmp_path
    ):
        """CONTROL: on an ordinary SQLAlchemy sqlite engine the very same
        duplicate insert DOES raise ``sqlalchemy.exc.IntegrityError`` -- so
        the encrypted-engine test exercises the same SQLAlchemy contract
        with the production driver.
        """
        import sqlalchemy.exc
        from sqlalchemy import create_engine
        from sqlalchemy.orm import sessionmaker

        from local_deep_research.database.models import Base, ResearchHistory

        engine = create_engine(f"sqlite:///{tmp_path}/plain.db")
        Base.metadata.create_all(engine)
        maker = sessionmaker(bind=engine)
        stamp = "2026-01-01T00:00:00+00:00"
        with maker() as db:
            db.add(
                ResearchHistory(
                    id="dup",
                    query="q",
                    mode="quick",
                    status="completed",
                    created_at=stamp,
                )
            )
            db.commit()
        with pytest.raises(sqlalchemy.exc.IntegrityError):
            with maker() as db:
                db.add(
                    ResearchHistory(
                        id="dup",
                        query="q2",
                        mode="quick",
                        status="completed",
                        created_at=stamp,
                    )
                )
                db.commit()

    def test_encrypted_unique_violation_is_wrapped(self, app):
        import sqlalchemy.exc

        from local_deep_research.database.models import ResearchHistory
        from local_deep_research.database.session_context import (
            get_user_db_session,
        )

        _client, username = _new_user(app)
        stamp = "2026-01-01T00:00:00+00:00"
        with get_user_db_session(username) as db:
            db.add(
                ResearchHistory(
                    id="dup",
                    query="q",
                    mode="quick",
                    status="completed",
                    created_at=stamp,
                )
            )
            db.commit()

        with pytest.raises(sqlalchemy.exc.IntegrityError):
            with get_user_db_session(username) as db:
                db.add(
                    ResearchHistory(
                        id="dup",
                        query="q2",
                        mode="quick",
                        status="completed",
                        created_at=stamp,
                    )
                )
                db.commit()


# ==========================================================================
# 6. Error surfaces: the LLM/search boundary blows up
# ==========================================================================


class TestErrorSurfaces:
    def test_failed_research_leaves_the_session_usable(
        self, app, stub_research_boundary
    ):
        """When the strategy raises, the user must (a) be told in the chat,
        (b) not be left with a phantom "thinking" research, and (c) be able
        to send again.

        The worker cannot write the FAILED status itself (it has no request
        context), so it hands the terminal update to
        ``queue_processor.queue_error_update`` and the processor's background
        loop drains it. That loop is started from the ASGI lifespan, which a
        bare TestClient does not run, so the drain is invoked explicitly
        here -- the production call, not a re-implementation. Everything
        before the drain is asserted too, because that IS what the user sees
        during the up-to-``check_interval`` window.
        """
        from local_deep_research.web.queue.processor_v2 import queue_processor

        client, _username = _new_user(app)
        session_id = _create_session(client, "errors")

        stub_research_boundary.raise_on_analyze = RuntimeError(
            "simulated LLM meltdown"
        )
        failed = _json(_send(client, session_id, "question that explodes"))
        failed_id = failed["research_id"]
        assert _await_research(failed_id)

        # (a) The chat carries an explanation rather than an eternal spinner.
        turns = _conversation_turns(_messages(client, session_id))
        assert turns[0] == ("user", "question that explodes")
        assert len(turns) == 2, turns
        assert turns[1][0] == "assistant"
        assert turns[1][1].startswith("Sorry, the research failed:"), turns[1]

        # Before the drain the row is still IN_PROGRESS, so the session is
        # briefly locked -- pinned so the size of that window is a conscious
        # design point rather than an accident.
        blocked = _send(client, session_id, "too soon")
        assert blocked.status_code == 409, blocked.text[:300]
        assert _json(blocked)["active_research_id"] == failed_id

        # The terminal update really was queued for this user.
        assert queue_processor.pending_operations, "no error update queued"
        queue_processor._drain_pending_operations()

        # (b) Now the research reads as failed and nothing is advertised as
        # running for this chat session.
        status = _json(client.get(f"/research/api/status/{failed_id}"))
        assert status["status"] == "failed", status
        listing = _json(client.get(f"/api/chat/sessions/{session_id}/messages"))
        assert listing["in_progress_research_id"] is None, listing

        # (c) CONTROL: identical call, healthy boundary -> a normal answer,
        # so the assertions above are about the failure and not about a
        # session that never worked in the first place.
        stub_research_boundary.raise_on_analyze = None
        ok = _send(client, session_id, "a question that works")
        assert ok.status_code == 200, ok.text[:400]
        assert _await_research(_json(ok)["research_id"])
        later = _conversation_turns(_messages(client, session_id))
        assert later[-2:] == [
            ("user", "a question that works"),
            ("assistant", ANSWER),
        ], later