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.

83.6 KB
Raw Blame
"""
FastAPI routes for the chat API.

Provides endpoints for:
- Chat page rendering
- Session management (create, list, get, archive, delete)
- Message management (send, list)
- Research triggering from chat

Ported from the pre-FastAPI Flask blueprint ``chat/routes.py``: Flask
``session``/``@login_required`` became ``Depends(require_auth)``,
``request.get_json`` became ``await request.json()``, ``jsonify(...), N``
became ``JSONResponse(..., status_code=N)``, and the blocking DB/service
work runs in the threadpool via ``run_db_sync`` so it never blocks the
uvicorn event loop. The framework-agnostic helpers and all concurrency /
cleanup logic are preserved verbatim.
"""

import threading
import unicodedata
import uuid
from datetime import datetime, timedelta, UTC
from urllib.parse import quote

from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import JSONResponse
from loguru import logger
from sqlalchemy import update as sa_update
from sqlalchemy.exc import IntegrityError, SQLAlchemyError

from ...chat.service import (
    ArchiveBlockedError,
    AttemptInProgress,
    AttemptNotFound,
    ChatCursorError,
    ChatService,
    ChatSessionNotFound,
    DB_EXCEPTIONS,
)
from ...chat.context import ChatContextManager
from ...constants import ResearchStatus
from ...database.models import (
    ChatMessage,
    ChatSession,
    ChatSessionStatus,
    ResearchHistory,
    UserActiveResearch,
)
from ...database.session_context import get_user_db_session
from ...exceptions import DuplicateResearchError, SystemAtCapacityError
from ...llm.providers.base import normalize_provider
from ...settings.manager import SettingsManager
from ...text_optimization.citation_formatter import (
    strip_appended_sources_sentinel,
)
from ...utilities.formatting import short_id
from ...utilities.url_utils import is_safe_custom_llm_endpoint
from ..auth.password_utils import get_user_password, resolve_user_password
from ..routes.globals import (
    cleanup_research,
    is_research_thread_alive,
    reclaim_stale_user_active_research,
)
from ..research_state import (
    get_user_research_start_lock,
    user_research_start_gate,
)
from ..services.research_service import (
    clamp_user_max_concurrent,
    run_research_process,
    start_research_process,
)
from ..dependencies.auth import require_auth
from ..dependencies.rate_limit import limiter, _get_client_ip
from ..dependencies.template_helpers import render_template
from ..dependencies.threadpool import run_db_sync
from typing import Annotated

router = APIRouter(tags=["chat"])

# Valid status values for sessions (built from the enum so a typo never
# silently passes validation; the literal "all" sentinel widens the list
# filter to every status without bypassing the whitelist).
VALID_UPDATE_STATUSES = {
    ChatSessionStatus.ACTIVE.value,
    ChatSessionStatus.ARCHIVED.value,
}
VALID_LIST_STATUSES = {*(s.value for s in ChatSessionStatus), "all"}

# Input length limits
MAX_QUERY_LENGTH = 10_000
MAX_TITLE_LENGTH = 500
MAX_MESSAGE_LENGTH = 10_000
# Hard cap on `offset` to prevent server-side DoS: get_session_messages
# fetches `limit + offset` rows from BOTH chat_messages and chat_progress_steps
# tables, so unbounded offset means unbounded SQL LIMIT. With cursor-based
# pagination (`before_created_at`) as the recommended path, offset above a few
# pages is not a normal access pattern.
MAX_OFFSET = 1_000

# Wider exception tuple used by HTTP route handlers (subsumes
# service.DB_EXCEPTIONS plus the attribute/type errors that can escape
# request-shape coercion code). DB_EXCEPTIONS itself is single-sourced
# from chat.service so the two never drift.
ROUTE_EXCEPTIONS = (
    ValueError,
    RuntimeError,
    SQLAlchemyError,
    AttributeError,
    TypeError,
)


def _bad_body() -> JSONResponse:
    """Build the 400 response for a non-JSON / non-object request body.

    Returns a FRESH ``JSONResponse`` each call — a shared module-level
    instance can't be reused safely across concurrent requests. This is the
    FastAPI equivalent of the old ``@require_json_body(error_format="success",
    ...)`` decorator.

    It does NOT reject form-encoded posts, and an earlier version of this
    docstring wrongly claimed it "hardens CSRF" by doing so. Flask's
    ``get_json(silent=True)`` returned ``None`` for a non-JSON Content-Type,
    which had that side effect; ``request.json()`` parses the body regardless
    of Content-Type, so a form-encoded POST carrying a JSON object body is
    accepted here. Verified: ``Content-Type: text/plain`` with a JSON object
    creates a session.

    That is safe, but for a different reason: ``CSRFMiddleware``
    (web/dependencies/csrf.py) is the fail-closed choke point and rejects
    these requests with 403 before they reach any handler. Recording the
    correction because a reviewer could otherwise rely on a guarantee this
    function does not provide.
    """
    return JSONResponse(
        {"success": False, "error": "Request body must be a JSON object"},
        status_code=400,
    )


def _chat_user_key(request: Request) -> str:
    """Per-user rate-limit key (default slowapi key is per-IP).

    Without this, users behind a shared NAT/proxy share one bucket and can
    DoS each other for legitimate chat use. Falls back to the client IP for
    any unauthenticated request that somehow reaches a limited route.

    The username branch is prefixed (``user:<name>``, the shape
    ``_user_key`` uses) so a username shaped like an address is not the
    same bucket as an anonymous caller from that address; the IP branch is
    always a validated address or a fixed token, never ``user:...``.
    """
    username = (
        request.session.get("username") if "session" in request.scope else None
    )
    return f"user:{username}" if username else _get_client_ip(request)


async def _json_object_body(request: Request):
    """Parse the request body as a JSON object, or return a 400 response.

    Returns the parsed dict on success, or a ``JSONResponse`` (400) the
    caller must return as-is. Mirrors the old ``@require_json_body`` gate.
    """
    try:
        data = await request.json()
    except Exception:
        return _bad_body()
    if not isinstance(data, dict):
        return _bad_body()
    return data


def _load_settings(username):
    """Load all settings for a user.

    ``bypass_cache=True`` matches the call pattern in
    ``research_routes.start_research``: a setting changed via the UI
    moments before the user sends a chat message must take effect on the
    next research, not be served from a stale cache.
    """
    with get_user_db_session(username) as db:
        snapshot = SettingsManager(db_session=db).get_all_settings(
            bypass_cache=True
        )
    # Inject the username so snapshot-driven consumers (get_llm, and ADAPTIVE
    # egress-scope resolution for a per-user retriever primary) can resolve it.
    # Without it, chat follow-up summarization builds get_llm() with
    # username=None -> a private-retriever primary is invisible -> scope falls
    # open to BOTH and a cloud LLM summarizes private-corpus turns. Mirrors
    # AdvancedSearchSystem.ensure_snapshot_username. Ported from #5539, which
    # landed on the Flask chat/routes.py this router replaced.
    from ...search_system import ensure_snapshot_username

    return ensure_snapshot_username(snapshot, username)


def _custom_endpoint_from_snapshot(settings_snapshot):
    """The openai_endpoint provider's base URL, or None for any other provider.

    Single source of truth for the ``llm.openai_endpoint.url`` read both
    chat research-start routes share, so the boundary gate and the spawn
    kwargs can never disagree about which endpoint a run would use.
    """
    provider = settings_snapshot.get("llm.provider", {}).get("value", "")
    if normalize_provider(provider) != "openai_endpoint":
        return None
    return settings_snapshot.get("llm.openai_endpoint.url", {}).get("value")


def _parse_int_param(
    value: str | None,
    default: int,
    min_val: int = 0,
    max_val: int | None = None,
) -> int:
    """Safely parse an integer parameter with bounds checking."""
    try:
        result = int(value) if value is not None else default
        if result < min_val:
            return min_val
        if max_val is not None and result > max_val:
            return max_val
        return result
    except (ValueError, TypeError):
        return default


_INVISIBLE_UNICODE_CATEGORIES = {"Cf", "Zl", "Zp"}


def _validate_title(title) -> tuple[str, int] | None:
    """Return (error_message, http_status) when *title* is invalid, else None.

    A title is invalid when it is not a non-empty string or exceeds
    ``MAX_TITLE_LENGTH``. Callers that allow ``None`` (e.g. create_session
    where omitting the title is fine) should short-circuit on ``None``
    before calling this helper.

    Strips Unicode format / line-separator characters (``Cf``/``Zl``/``Zp``,
    including zero-width spaces U+200B-U+200D and BOM U+FEFF) before the
    emptiness check so an "invisible" title like 500 zero-width chars is
    rejected instead of saving a session that looks blank in the UI.
    """
    if not isinstance(title, str):
        return ("Title cannot be empty", 400)
    visible = "".join(
        c
        for c in title
        if unicodedata.category(c) not in _INVISIBLE_UNICODE_CATEGORIES
    )
    if not visible.strip():
        return ("Title cannot be empty", 400)
    if len(title) > MAX_TITLE_LENGTH:
        return (
            f"Title too long (max {MAX_TITLE_LENGTH} characters)",
            400,
        )
    return None


def _cleanup_chat_send_rows(
    username,
    research_id,
    message_id,
    session_id,
    reason: str,
    *,
    preserve_attempt=False,
) -> None:
    """Undo the user-message + research_history rows committed by send_message
    when ``start_research_process`` rejects the spawn.

    Used by the ``DuplicateResearchError`` (409), ``SystemAtCapacityError``
    (429), and generic spawn-failure (500) paths. Failure to clean up is logged
    at ERROR level so orphan rows + inflated message_count are visible to ops.
    Retry preserves the replacement query as a failed attempt because the old
    attempt has already been removed. Its active claim is still released.
    """
    try:
        with get_user_db_session(username) as cleanup_db:
            if preserve_attempt:
                research = (
                    cleanup_db.query(ResearchHistory)
                    .filter_by(id=research_id, chat_session_id=session_id)
                    .first()
                )
                if research is not None:
                    research.status = ResearchStatus.FAILED.value
                    research.completed_at = datetime.now(UTC).isoformat()
                    # The failed replacement has no assistant response to
                    # expose its ID. Link its retained query directly so the
                    # reloaded timeline can offer Retry and Delete actions.
                    message = (
                        cleanup_db.query(ChatMessage)
                        .filter_by(id=message_id, session_id=session_id)
                        .first()
                    )
                    if message is not None:
                        message.research_id = research_id
            else:
                cleanup_db.query(ResearchHistory).filter_by(
                    id=research_id
                ).delete()
                cleanup_db.query(ChatMessage).filter_by(id=message_id).delete()
            # Drop the per-user-cap tracking row too (the spawn never
            # started, so no live thread owns it). research_id is a fresh
            # UUID, so this only ever matches our own just-inserted row.
            cleanup_db.query(UserActiveResearch).filter_by(
                username=username, research_id=research_id
            ).delete()
            if not preserve_attempt:
                cleanup_db.execute(
                    sa_update(ChatSession)
                    .where(ChatSession.id == session_id)
                    .values(message_count=ChatSession.message_count - 1)
                )
            cleanup_db.commit()
    except DB_EXCEPTIONS:
        logger.exception(
            f"Cleanup after {reason} chat-send rejection FAILED "
            f"for research {short_id(research_id)} in chat "
            f"{short_id(session_id)}; orphan rows + inflated "
            f"message_count may persist until next sweep."
        )


# ============================================================================
# Page Routes
# ============================================================================

# Sec-Fetch-Site values that mean another origin started the navigation.
# "same-site" is still another origin, such as a sibling subdomain or
# another port on the same host.
_FOREIGN_FETCH_SITES = frozenset({"cross-site", "same-site"})


def _drop_foreign_chat_query(request: Request) -> None:
    """Redirect a chat link from another origin to the same page without ``q``.

    chat.js pre-fills the chat box from ``?q=`` and never sends that text
    by itself. The SameSite=Lax session cookie lets a link from another site
    open the page signed in, so when the browser says that another origin
    started the navigation, the text is not even pre-filled. Browsers send
    ``Sec-Fetch-Site`` to HTTPS and localhost origins. ``same-origin`` and
    ``none`` (a typed or bookmarked URL, or a link opened from another app
    such as a mail or chat client) keep ``q``, and so does a request without
    the header. After a login the browser arrives here through this app's
    own redirect, so the login page drops a chat ``q`` from its ``next``
    itself (``routers/auth.py::_drop_chat_query``).

    Declared as ``chat_page``'s first dependency parameter, ahead of
    ``username``. FastAPI solves dependencies in parameter order, so this
    runs before ``require_auth``: a signed-out user is sent on to the login
    page without ``q`` as well.
    """
    if "q" not in request.query_params:
        return
    site = request.headers.get("sec-fetch-site", "").strip().lower()
    if site not in _FOREIGN_FETCH_SITES:
        return
    session_id = request.path_params.get("session_id")
    location = "/chat/" + (quote(session_id, safe="") if session_id else "")
    remaining = request.url.remove_query_params("q").query
    if remaining:
        location += "?" + remaining
    raise HTTPException(status_code=303, headers={"Location": location})


@router.get("/chat/", include_in_schema=False)
@router.get("/chat/{session_id}", include_in_schema=False)
def chat_page(
    request: Request,
    # A parameter, not a decorator ``dependencies=[...]`` entry, so every
    # dependency of this route stays in its signature (where the
    # cross-user isolation census reads auth). It must stay ahead of
    # ``username``: dependencies run in parameter order.
    _foreign_query_dropped: Annotated[None, Depends(_drop_foreign_chat_query)],
    username: Annotated[str, Depends(require_auth)],
    session_id: str | None = None,
):
    """Render the chat page.

    Args:
        session_id: Optional session ID to load existing session
    """
    return render_template(
        request, "pages/chat.html", {"session_id": session_id}
    )


# ============================================================================
# Session API Routes
# ============================================================================


@router.post("/api/chat/sessions")
# Per-user keying (default is per-IP). Without this, users behind a shared
# NAT/proxy share one bucket and can DoS each other for legitimate chat use.
@limiter.limit("20 per minute", key_func=_chat_user_key)
async def create_session(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Create a new chat session."""
    data = await _json_object_body(request)
    if isinstance(data, JSONResponse):
        return data

    def _impl():
        try:
            # Validate input lengths
            initial_query = data.get("initial_query")
            title = data.get("title")

            # Reject non-string initial_query early so len() / downstream
            # string ops don't raise TypeError → 500.
            if initial_query is not None and not isinstance(initial_query, str):
                return JSONResponse(
                    {
                        "success": False,
                        "error": "initial_query must be a string",
                    },
                    status_code=400,
                )

            if initial_query and len(initial_query) > MAX_QUERY_LENGTH:
                return JSONResponse(
                    {
                        "success": False,
                        "error": f"Initial query too long (max {MAX_QUERY_LENGTH} characters)",
                    },
                    status_code=400,
                )

            if title is not None:
                err = _validate_title(title)
                if err is not None:
                    msg, status = err
                    return JSONResponse(
                        {"success": False, "error": msg}, status_code=status
                    )

            settings_snapshot = _load_settings(username)

            service = ChatService(username)
            session_id = service.create_session(
                initial_query=initial_query,
                title=title,
                settings_snapshot=settings_snapshot,
            )

            # Get the created session
            try:
                session_data = service.get_session(session_id)
            except ChatSessionNotFound:
                # Session was just created in this request — getting "not
                # found" here means a delete-race or storage failure.
                logger.exception(
                    "Just-created chat session missing on read-back"
                )
                return JSONResponse(
                    {
                        "success": False,
                        "error": "Failed to load created session",
                    },
                    status_code=500,
                )

            return JSONResponse(
                {
                    "success": True,
                    "session_id": session_id,
                    "session": session_data,
                }
            )

        except ROUTE_EXCEPTIONS:
            logger.exception("Error creating chat session")
            return JSONResponse(
                {
                    "success": False,
                    "error": "Failed to create chat session",
                },
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.post("/api/chat/sessions/{session_id}/generate-title")
# Per-user keying + lower limit than create_session because each call is a
# real LLM round-trip on a server-paid endpoint (vs create_session which is
# zero-LLM DB work). Without per-user keying, shared-IP users share the bucket.
@limiter.limit("10 per minute", key_func=_chat_user_key)
async def generate_session_title(
    request: Request,
    session_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Regenerate the session title using the configured LLM.

    Fire-and-forget endpoint the frontend calls asynchronously right after
    creating a session, so the synchronous POST /api/chat/sessions response
    isn't blocked on an LLM round-trip.
    """
    data = await _json_object_body(request)
    if isinstance(data, JSONResponse):
        return data

    def _impl():
        try:
            query = data.get("query")

            if not query:
                return JSONResponse(
                    {"success": False, "error": "query is required"},
                    status_code=400,
                )
            if not isinstance(query, str) or len(query) > MAX_QUERY_LENGTH:
                return JSONResponse(
                    {
                        "success": False,
                        "error": f"query must be a string up to {MAX_QUERY_LENGTH} chars",
                    },
                    status_code=400,
                )

            service = ChatService(username)
            try:
                service.get_session(session_id)
            except ChatSessionNotFound:
                return JSONResponse(
                    {"success": False, "error": "Session not found"},
                    status_code=404,
                )

            settings_snapshot = _load_settings(username)
            new_title = service.regenerate_title_with_llm(
                session_id, query, settings_snapshot
            )
            if not new_title:
                # LLM disabled, or LLM call failed — keep existing fallback
                return JSONResponse({"success": False, "title": None})

            return JSONResponse({"success": True, "title": new_title})

        except ROUTE_EXCEPTIONS:
            logger.exception("Error regenerating chat title")
            return JSONResponse(
                {"success": False, "error": "Failed to regenerate title"},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.get("/api/chat/sessions")
async def list_sessions(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """List chat sessions for the current user.

    Query params: status (active/archived/deleted/all, default active),
    limit (default 20), offset (default 0).
    """

    def _impl():
        try:
            status = request.query_params.get(
                "status", ChatSessionStatus.ACTIVE.value
            )
            # Validate status parameter
            if status not in VALID_LIST_STATUSES:
                status = ChatSessionStatus.ACTIVE.value
            limit = _parse_int_param(
                request.query_params.get("limit"), 20, min_val=1, max_val=100
            )
            offset = _parse_int_param(
                request.query_params.get("offset"),
                0,
                min_val=0,
                max_val=MAX_OFFSET,
            )

            service = ChatService(username)
            sessions = service.list_sessions(
                status=status, limit=limit, offset=offset
            )

            return JSONResponse({"success": True, "sessions": sessions})

        except ROUTE_EXCEPTIONS:
            logger.exception("Error listing chat sessions")
            return JSONResponse(
                {"success": False, "error": "Failed to list chat sessions"},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.get("/api/chat/sessions/{session_id}")
async def get_session(
    request: Request,
    session_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Get a specific chat session."""

    def _impl():
        try:
            service = ChatService(username)
            try:
                session_data = service.get_session(session_id)
            except ChatSessionNotFound:
                return JSONResponse(
                    {"success": False, "error": "Session not found"},
                    status_code=404,
                )

            return JSONResponse({"success": True, "session": session_data})

        except ROUTE_EXCEPTIONS:
            logger.exception("Error getting chat session")
            return JSONResponse(
                {"success": False, "error": "Failed to get chat session"},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.patch("/api/chat/sessions/{session_id}")
# Per-user keying, like the other state-changing chat routes.
@limiter.limit("30 per minute", key_func=_chat_user_key)
async def update_session(
    request: Request,
    session_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Update a chat session (title, archive, delete)."""
    data = await _json_object_body(request)
    if isinstance(data, JSONResponse):
        return data

    def _impl():
        try:
            # Require at least one valid field
            valid_fields = {"title", "status"}
            if not any(field in data for field in valid_fields):
                return JSONResponse(
                    {
                        "success": False,
                        "error": "Request must include at least one of: title, status",
                    },
                    status_code=400,
                )

            service = ChatService(username)

            try:
                service.get_session(session_id)
            except ChatSessionNotFound:
                return JSONResponse(
                    {"success": False, "error": "Session not found"},
                    status_code=404,
                )

            ops_ok = True

            if "title" in data:
                title = data["title"]
                err = _validate_title(title)
                if err is not None:
                    msg, status = err
                    return JSONResponse(
                        {"success": False, "error": msg}, status_code=status
                    )
                ops_ok = (
                    service.update_session_title(session_id, title) and ops_ok
                )

            if "status" in data:
                new_status = data["status"]
                if new_status not in VALID_UPDATE_STATUSES:
                    return JSONResponse(
                        {"success": False, "error": "Invalid status value"},
                        status_code=400,
                    )
                if new_status == ChatSessionStatus.ACTIVE.value:
                    ops_ok = service.reactivate_session(session_id) and ops_ok
                elif new_status == ChatSessionStatus.ARCHIVED.value:
                    try:
                        ops_ok = service.archive_session(session_id) and ops_ok
                    except ArchiveBlockedError:
                        # Symmetric with send-to-archived (also 409). Hard-coded
                        # message — never echo str(exc) here (CWE-209).
                        return JSONResponse(
                            {
                                "success": False,
                                "error": "Cannot archive: research in_progress. Stop it first.",
                            },
                            status_code=409,
                        )

            try:
                session_data = service.get_session(session_id)
            except ChatSessionNotFound:
                # Session was deleted by a concurrent request between the
                # update above and this read-back. Treat as 404.
                return JSONResponse(
                    {"success": False, "error": "Session not found"},
                    status_code=404,
                )

            if not ops_ok:
                # The read-back above succeeded, so the session still exists,
                # yet an update reported failure — a DB write error swallowed
                # into a False return. Surface it instead of reporting success.
                logger.error(
                    f"Chat session update failed at DB layer for "
                    f"{session_id[:8]}..."
                )
                return JSONResponse(
                    {"success": False, "error": "Failed to update session"},
                    status_code=500,
                )

            return JSONResponse({"success": True, "session": session_data})

        except ROUTE_EXCEPTIONS:
            logger.exception("Error updating chat session")
            return JSONResponse(
                {"success": False, "error": "Failed to update chat session"},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.delete("/api/chat/sessions/{session_id}")
# Per-user keying, like the other state-changing chat routes.
@limiter.limit("30 per minute", key_func=_chat_user_key)
async def delete_session(
    request: Request,
    session_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Delete a chat session permanently."""

    def _impl():
        try:
            service = ChatService(username)
            success = service.delete_session(session_id)

            if not success:
                return JSONResponse(
                    {"success": False, "error": "Session not found"},
                    status_code=404,
                )

            return JSONResponse({"success": True})

        except ROUTE_EXCEPTIONS:
            logger.exception("Error deleting chat session")
            return JSONResponse(
                {"success": False, "error": "Failed to delete chat session"},
                status_code=500,
            )

    return await run_db_sync(_impl)


# ============================================================================
# Message API Routes
# ============================================================================


@router.get("/api/chat/sessions/{session_id}/messages")
async def get_messages(
    request: Request,
    session_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Get messages for a chat session (cursor-paginated, ASC)."""

    def _impl():
        try:
            limit = _parse_int_param(
                request.query_params.get("limit"), 50, min_val=1, max_val=100
            )
            offset = _parse_int_param(
                request.query_params.get("offset"),
                0,
                min_val=0,
                max_val=MAX_OFFSET,
            )
            before_created_at = (
                request.query_params.get("before_created_at") or None
            )
            before_id = request.query_params.get("before_id") or None

            service = ChatService(username)

            try:
                service.get_session(session_id)
            except ChatSessionNotFound:
                return JSONResponse(
                    {"success": False, "error": "Session not found"},
                    status_code=404,
                )

            # Fetch one extra row so we can tell the client whether more
            # older entries exist without a second round-trip.
            peek_limit = limit + 1
            page = service.get_session_messages(
                session_id,
                limit=peek_limit,
                offset=offset,
                before_created_at=before_created_at,
                before_id=before_id,
            )
            has_more = len(page) > limit
            messages = page[-limit:] if has_more else page

            # Legacy response rows retain the report sentinel in storage.
            # Clean only the display payload, never the shared LLM context.
            messages = [
                {
                    **message,
                    "content": strip_appended_sources_sentinel(
                        message["content"]
                    ),
                }
                if message["role"] == "assistant"
                and message["message_type"] == "response"
                else message
                for message in messages
            ]

            # The client (chat.js loadSession) restores the live "thinking"
            # indicator from this field. O(1) via the partial-unique index
            # ux_research_history_chat_session_in_progress.
            in_progress_research_id = service.get_in_progress_research_id(
                session_id
            )

            return JSONResponse(
                {
                    "success": True,
                    "messages": messages,
                    "has_more": has_more,
                    "in_progress_research_id": in_progress_research_id,
                }
            )

        except ChatCursorError:
            return JSONResponse(
                {
                    "success": False,
                    "error": "Invalid pagination cursor. before_created_at must "
                    "include a timezone; before_id must identify an entry in "
                    "this session at before_created_at.",
                },
                status_code=400,
            )
        except ROUTE_EXCEPTIONS:
            logger.exception("Error getting chat messages")
            return JSONResponse(
                {"success": False, "error": "Failed to get chat messages"},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.post("/api/chat/sessions/{session_id}/messages")
# Per-user keying. send_message launches a full research run, so this is the
# heaviest chat endpoint; shared-IP users sharing the bucket would lock each
# other out.
@limiter.limit("10 per minute", key_func=_chat_user_key)
async def send_message(
    request: Request,
    session_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Send a message in a chat session, optionally triggering research.

    Research mode is always "quick" in chat (intentional v1 scope).
    """
    data = await _json_object_body(request)
    if isinstance(data, JSONResponse):
        return data

    def _impl():
        admission_lock = None
        try:
            if not data or not data.get("content"):
                return JSONResponse(
                    {"success": False, "error": "Message content is required"},
                    status_code=400,
                )

            # Reject non-string content before .strip() raises AttributeError
            # → 500. Mirrors the isinstance guard in _validate_title.
            if not isinstance(data["content"], str):
                return JSONResponse(
                    {"success": False, "error": "content must be a string"},
                    status_code=400,
                )

            content = data["content"].strip()

            # Reject whitespace-only content
            if not content:
                return JSONResponse(
                    {"success": False, "error": "Message content is required"},
                    status_code=400,
                )

            if len(content) > MAX_MESSAGE_LENGTH:
                return JSONResponse(
                    {
                        "success": False,
                        "error": f"Message too long (max {MAX_MESSAGE_LENGTH} characters)",
                    },
                    status_code=400,
                )

            # trigger_research is a strict boolean. Reject non-bool values
            # with a 400 instead of silently coercing them to True: a client
            # sending {"trigger_research": "no"} or {"trigger_research": 0}
            # intends to SUPPRESS research, and coercing the truthy-string to
            # True would launch an unwanted (paid) research run against their
            # intent (#4427).
            raw = data.get("trigger_research", True)
            if not isinstance(raw, bool):
                return JSONResponse(
                    {
                        "success": False,
                        "error": "trigger_research must be a boolean",
                    },
                    status_code=400,
                )
            trigger_research = raw

            service = ChatService(username)

            # Verify session exists (informational fast-fail; the
            # UPDATE...RETURNING inside insert_message_in_db is the
            # authoritative check that survives a delete-race).
            try:
                session_data = service.get_session(session_id)
            except ChatSessionNotFound:
                return JSONResponse(
                    {"success": False, "error": "Session not found"},
                    status_code=404,
                )

            # Reject sends to non-active sessions. Archived/deleted sessions
            # are intentionally read-only — users must reactivate first.
            if session_data.get("status") != ChatSessionStatus.ACTIVE.value:
                return JSONResponse(
                    {
                        "success": False,
                        "error": "This chat is archived. Reactivate it to continue.",
                    },
                    status_code=409,
                )

            # Pre-fetch existing messages for context decisions.
            # include_steps=False keeps `limit` spent entirely on genuine
            # ChatMessage conversation turns. Progress-step rows would
            # otherwise dominate this 20-entry window on a research-heavy
            # session and push real conversation history out of it before
            # ChatContextManager ever sees it — and ChatContextManager
            # discards step rows anyway, so there is no reason to spend the
            # window budget on them here.
            messages = service.get_session_messages(
                session_id, limit=20, include_steps=False
            )

            research_id = None
            research_mode = "none"
            message_id = None
            settings_snapshot = None
            research_context = None

            if trigger_research:
                # Always quick mode in chat (intentional v1 scope).
                research_mode = "quick"

                # Verify the DB password is available BEFORE creating any rows
                # or spawning a worker. Chat-triggered research runs on a
                # background thread that writes token/search metrics to the
                # user's encrypted database; without the password every metric
                # write is silently dropped, leaving the metrics dashboard empty
                # while the research still completes (issue #4457). The password
                # is re-fetched at the spawn site below, so discard it here.
                _pw, session_expired = resolve_user_password(username)
                if session_expired:
                    # Use this router's {success, error} shape (the chat
                    # frontend reads `err.error`); a {status, message} body
                    # would surface only a generic "Request failed (401)".
                    return JSONResponse(
                        {
                            "success": False,
                            "error": "Your session has expired. Please log out and log back in to continue.",
                        },
                        status_code=401,
                    )

                # ChatService's reads above reuse a thread-local Session and
                # leave its read transaction open. Release that connection
                # before taking the admission gate: every producer follows
                # gate -> DB, and reversing the order here can deadlock under
                # pool exhaustion with a gate holder waiting for a connection.
                with get_user_db_session(username) as ordering_db:
                    ordering_db.rollback()

                # Short gated fast-fail before the potentially slow context
                # build below. The authoritative check is repeated after that
                # work and retained through claim + worker registration.
                with user_research_start_gate(username):
                    cap_error = _check_chat_research_slot_locked(
                        username, session_id
                    )
                if cap_error is not None:
                    message, status, active_research_id = cap_error
                    payload = {"success": False, "error": message}
                    if active_research_id is not None:
                        payload["active_research_id"] = active_research_id
                    return JSONResponse(payload, status_code=status)

            # Settings + context (read-only — fine to do after the cap
            # check, before the atomic write).
            if trigger_research:
                settings_snapshot = _load_settings(username)
                context_manager = ChatContextManager(
                    session_id,
                    messages,
                    session_data.get("accumulated_context"),
                    settings_snapshot=settings_snapshot,
                )
                # Pass the new user message so prior conversation is condensed
                # into a summary focused on this question.
                research_context = context_manager.build_research_context(
                    current_query=content
                )
                research_id = str(uuid.uuid4())

                # Parse numeric search settings up-front. A malformed value
                # must return a clean 400 HERE — before the atomic write
                # below commits the user message + IN_PROGRESS research row.
                try:
                    iterations = int(
                        settings_snapshot.get("search.iterations", {}).get(
                            "value", 3
                        )
                    )
                    questions = int(
                        settings_snapshot.get(
                            "search.questions_per_iteration", {}
                        ).get("value", 1)
                    )
                except (ValueError, TypeError):
                    return JSONResponse(
                        {
                            "success": False,
                            "error": (
                                "Invalid numeric value in search settings "
                                "(iterations / questions_per_iteration)."
                            ),
                        },
                        status_code=400,
                    )

                # SSRF pre-flight on the custom LLM endpoint: reject
                # metadata / link-local targets at the request boundary,
                # BEFORE the atomic write below commits the user message
                # + IN_PROGRESS research row. Fail-fast defense-in-depth
                # mirroring research.py / followup.py: the
                # OpenAI-compatible provider's assert_base_url_safe
                # re-validates the same URL before the client is built.
                # Private IPs / localhost pass (local LLM backends),
                # including scheme-less endpoints (the helper normalizes
                # exactly as the provider does); None — any provider
                # other than openai_endpoint, or no URL configured — is
                # safe by definition.
                if not is_safe_custom_llm_endpoint(
                    _custom_endpoint_from_snapshot(settings_snapshot)
                ):
                    return JSONResponse(
                        {
                            "success": False,
                            "error": "Invalid custom endpoint URL",
                        },
                        status_code=400,
                    )

                # End the cached read transaction before taking the stable
                # per-user gate (gate -> DB is the process-wide lock order).
                with get_user_db_session(username) as ordering_db:
                    ordering_db.rollback()

                pending_admission_lock = get_user_research_start_lock(username)
                pending_admission_lock.acquire()
                admission_lock = pending_admission_lock

                # Context preparation can take long enough for another start
                # to consume the last slot. Reconcile and recheck under the
                # gate that remains held through commit, registration, and
                # every spawn-failure cleanup return below.
                cap_error = _check_chat_research_slot_locked(
                    username, session_id
                )
                if cap_error is not None:
                    message, status, active_research_id = cap_error
                    payload = {"success": False, "error": message}
                    if active_research_id is not None:
                        payload["active_research_id"] = active_research_id
                    return JSONResponse(payload, status_code=status)

            # ---- Atomic write: user message + research row in ONE txn ----
            # Closes the orphan window: any IntegrityError or
            # concurrent-delete on the research insert rolls back the user
            # message too. The UPDATE...RETURNING inside insert_message_in_db
            # doubles as the authoritative "session-still-exists" check.
            try:
                with get_user_db_session(username) as db_session:
                    message_id = service.insert_message_in_db(
                        db_session,
                        session_id=session_id,
                        role="user",
                        content=content,
                        message_type="query"
                        if len(messages) == 0
                        else "followup",
                    )

                    if trigger_research:
                        created_at = datetime.now(UTC).isoformat()
                        research_meta = {
                            "submission": {
                                "chat_session_id": session_id,
                                "message_id": message_id,
                                "research_mode": research_mode,
                            },
                            # Persist the run's settings snapshot so
                            # view/export (report_assembly_service) renders
                            # the same report.uncited_sources_mode the
                            # generation-time render used. Mirrors the
                            # main-form creation path; completion merge
                            # back-fills when missing.
                            "settings_snapshot": settings_snapshot,
                        }
                        research = ResearchHistory(
                            id=research_id,
                            query=content,
                            mode=research_mode,
                            status=ResearchStatus.IN_PROGRESS.value,
                            created_at=created_at,
                            progress_log=[{"time": created_at, "progress": 0}],
                            research_meta=research_meta,
                            chat_session_id=session_id,
                        )
                        db_session.add(research)

                        # Count this research toward the per-user concurrent
                        # cap. Mirrors research_routes.start_research. Added in
                        # the SAME transaction as the research row so the
                        # IntegrityError rollback below undoes both.
                        db_session.add(
                            UserActiveResearch(
                                username=username,
                                research_id=research_id,
                                status=ResearchStatus.IN_PROGRESS,
                                thread_id=str(threading.current_thread().ident),
                                settings_snapshot=settings_snapshot,
                            )
                        )

                    try:
                        db_session.commit()
                    except IntegrityError:
                        # The admission gate is held from the recheck through
                        # this commit, so no other chat send or retry in this
                        # process can pass the per-session guard meanwhile.
                        # The partial unique index on (chat_session_id) WHERE
                        # status='in_progress' (migration 0010) is the
                        # backstop for a writer outside that gate and catches
                        # the loser here. Rolling back also undoes the
                        # user-message INSERT and the message_count
                        # increment — no orphan.
                        db_session.rollback()
                        logger.warning(
                            f"Concurrent in-progress research race for chat {session_id[:8]}..."
                        )
                        return JSONResponse(
                            {
                                "success": False,
                                "error": "Research already in progress on this chat session.",
                            },
                            status_code=409,
                        )
            except ValueError as exc:
                # `insert_message_in_db` raises ValueError("not found") when
                # the session row was deleted between the existence check and
                # the UPDATE...RETURNING. Map to 404.
                if "not found" in str(exc).lower():
                    return JSONResponse(
                        {"success": False, "error": "Session not found"},
                        status_code=404,
                    )
                raise
            # ---- end atomic write ----

            if trigger_research:
                # Type narrowing: variables initialized to None at the top
                # and assigned inside the matching `if trigger_research:`
                # block above. Real runtime check (not assert) so it survives
                # ``python -O`` (bandit S101).
                if (
                    settings_snapshot is None
                    or research_context is None
                    or research_id is None
                    or message_id is None
                ):  # pragma: no cover — unreachable invariant guard
                    raise RuntimeError(
                        "trigger_research path entered with unset state"
                    )
                # Get user password for metrics
                pw = get_user_password(username)

                # Extract settings values with safe .get() defaults
                model_provider = settings_snapshot.get("llm.provider", {}).get(
                    "value", ""
                )
                model = settings_snapshot.get("llm.model", {}).get("value", "")
                search_engine = settings_snapshot.get("search.tool", {}).get(
                    "value", ""
                )
                # llm.openai_endpoint.url is only the client's base URL for
                # the openai_endpoint provider; passing it for any other
                # provider mislabels that provider's endpoint downstream.
                # (Boundary SSRF pre-flight happened above, before the
                # atomic write.)
                custom_endpoint = _custom_endpoint_from_snapshot(
                    settings_snapshot
                )
                # Defensive fallbacks kept in sync with default_settings.json.
                user_strategy = settings_snapshot.get(
                    "search.search_strategy", {}
                ).get("value", "langgraph-agent")
                # `iterations` and `questions` were parsed + validated up-front
                # (before the atomic write) so a malformed setting returns a
                # clean 400 instead of orphaning a committed research row.

                # For follow-up messages, use the contextual follow-up strategy
                # which wraps the user's preferred strategy as a delegate.
                if research_context.get("is_multi_turn"):
                    strategy = "enhanced-contextual-followup"
                    research_context["delegate_strategy"] = user_strategy
                else:
                    strategy = user_strategy

                # Spawn the worker thread. ``DuplicateResearchError`` inherits
                # from ``Exception`` (not RuntimeError) so it is NOT in
                # ROUTE_EXCEPTIONS and would otherwise escape to a generic 500
                # — leaving the rows we just committed orphaned. Catch here,
                # undo our side effects, and return 409.
                try:
                    start_research_process(
                        research_id,
                        content,
                        research_mode,
                        run_research_process,
                        username=username,
                        user_password=pw,
                        model_provider=model_provider,
                        model=model,
                        search_engine=search_engine,
                        custom_endpoint=custom_endpoint,
                        strategy=strategy,
                        iterations=iterations,
                        questions_per_iteration=questions,
                        research_context=research_context,
                        chat_session_id=session_id,
                        chat_message_id=message_id,
                        settings_snapshot=settings_snapshot,
                    )
                except DuplicateResearchError:
                    logger.warning(
                        f"DuplicateResearchError on chat send_message "
                        f"for {research_id[:8]}... (chat {session_id[:8]}...)"
                    )
                    # Per DuplicateResearchError docstring: do NOT mutate
                    # UserActiveResearch or the existing ResearchHistory row —
                    # those belong to the live thread. Only undo our own rows.
                    _cleanup_chat_send_rows(
                        username,
                        research_id,
                        message_id,
                        session_id,
                        "duplicate",
                    )
                    return JSONResponse(
                        {
                            "success": False,
                            "error": "Research already in progress on this chat session.",
                        },
                        status_code=409,
                    )
                except SystemAtCapacityError:
                    # System at concurrent-research capacity. Undo the rows we
                    # committed above and return 429 so the client can retry.
                    logger.warning(
                        f"SystemAtCapacityError on chat send_message "
                        f"for {research_id[:8]}... (chat {session_id[:8]}...)"
                    )
                    _cleanup_chat_send_rows(
                        username,
                        research_id,
                        message_id,
                        session_id,
                        "capacity",
                    )
                    return JSONResponse(
                        {
                            "success": False,
                            "error": "Server is at research capacity. Please retry shortly.",
                        },
                        status_code=429,
                    )
                except Exception:
                    # Thread construction/registration can fail for reasons
                    # other than duplicate ownership or capacity (for example,
                    # RuntimeError from threading.Thread.start). The durable
                    # message, research, and per-user claim already exist, so
                    # undo all of them before releasing the admission gate.
                    logger.exception(
                        f"Failed to spawn chat research "
                        f"{research_id[:8]}... (chat {session_id[:8]}...)"
                    )
                    _cleanup_chat_send_rows(
                        username,
                        research_id,
                        message_id,
                        session_id,
                        "spawn failure",
                    )
                    return JSONResponse(
                        {
                            "success": False,
                            "error": "Failed to start research. Please try again.",
                        },
                        status_code=500,
                    )

                logger.info(
                    f"Started chat research {research_id[:8]}... for chat {session_id[:8]}..."
                )

            return JSONResponse(
                {
                    "success": True,
                    "message_id": message_id,
                    "session_id": session_id,
                    "research_id": research_id,
                    "research_mode": research_mode,
                }
            )

        except ROUTE_EXCEPTIONS:
            logger.exception("Error sending chat message")
            return JSONResponse(
                {"success": False, "error": "Failed to send message"},
                status_code=500,
            )
        finally:
            if admission_lock is not None:
                admission_lock.release()

    return await run_db_sync(_impl)


def _check_chat_research_slot_locked(
    username: str, session_id: str
) -> tuple[str, int, str | None] | None:
    """Reconcile and check chat admission while the caller holds its gate.

    The caller must acquire ``user_research_start_gate(username)`` before
    entering this helper. Opening the DB here preserves the process-wide
    gate-before-session order. The read transaction is explicitly ended
    before returning so a later gate acquisition cannot form a pool cycle.

    Rows older than the 30-second spawn grace window whose worker is dead
    are reclaimed before the slot checks run: this session's IN_PROGRESS
    research rows first, then the user's ``UserActiveResearch`` slots. Once
    that window has passed, a crashed worker's rows therefore cannot trip
    the per-session 409 or the per-user 429. Returns
    ``(error_message, http_status, active_research_id)`` to reject the
    request, else ``None``.
    """
    grace_cutoff_dt = datetime.now(UTC) - timedelta(seconds=30)
    grace_cutoff_iso = grace_cutoff_dt.isoformat()

    with get_user_db_session(username) as cap_db:
        try:
            stale_chat = (
                cap_db.query(ResearchHistory)
                .filter(
                    ResearchHistory.chat_session_id == session_id,
                    ResearchHistory.status == ResearchStatus.IN_PROGRESS,
                    ResearchHistory.created_at < grace_cutoff_iso,
                )
                .all()
            )
            reclaimed_chat = False
            for row in stale_chat:
                if not is_research_thread_alive(row.id):
                    logger.warning(
                        f"Reclaiming stale chat research {row.id[:8]}... "
                        f"(thread dead) on chat {session_id[:8]}..."
                    )
                    row.status = ResearchStatus.FAILED
                    cleanup_research(row.id)
                    reclaimed_chat = True
            if reclaimed_chat:
                cap_db.commit()

            if reclaim_stale_user_active_research(
                cap_db,
                username,
                grace_cutoff_dt=grace_cutoff_dt,
                logger=logger,
            ):
                cap_db.commit()

            cap_error = _enforce_chat_session_research_slot(
                cap_db, username, session_id
            )
            if cap_error is None:
                return None

            message, status = cap_error
            active_research_id = None
            if status == 409:
                active_row = (
                    cap_db.query(ResearchHistory.id)
                    .filter_by(
                        chat_session_id=session_id,
                        status=ResearchStatus.IN_PROGRESS,
                    )
                    .first()
                )
                if active_row is not None:
                    active_research_id = active_row[0]
            return message, status, active_research_id
        finally:
            cap_db.rollback()


def _enforce_chat_session_research_slot(
    cap_db, username: str, session_id: str
) -> tuple[str, int] | None:
    """Reject send/retry when this chat session or user is at capacity.

    Runs inside the caller's ``cap_db`` transaction after any stale-row
    reclaims, so those writes are visible to the count check below it.

    Returns ``(error_message, http_status)`` when the request should be
    rejected, else ``None``.

    Two checks:
      1. Per-session guard: at most one live research per chat session.
      2. Per-user global cap: at most ``app.max_concurrent_researches``
         researches per user across ALL sessions. Mirrors
         research start_research.

    DOES NOT include the stale-row reclaim sweep. Its only caller,
    ``_check_chat_research_slot_locked``, first reclaims dead rows older
    than its grace window so neither check counts them.
    """
    # Per-session guard. Note: this fires for ANY in-progress research
    # on the session, INCLUDING the target research on a retry path.
    # That is how retry refuses a target whose worker is still live,
    # before anything is deleted. A target whose worker died more than the
    # 30-second grace window ago has already been reclaimed by the caller's
    # sweep, so it cannot block its own retry.
    existing_session_research = (
        cap_db.query(ResearchHistory)
        .filter_by(
            chat_session_id=session_id,
            status=ResearchStatus.IN_PROGRESS,
        )
        .first()
    )
    if existing_session_research:
        return (
            "Research already in progress on this chat session. "
            "Stop it before sending a new message.",
            409,
        )

    active_count = (
        cap_db.query(UserActiveResearch)
        .filter_by(
            username=username,
            status=ResearchStatus.IN_PROGRESS,
        )
        .count()
    )
    # Clamp to the server-wide semaphore ceiling (#5549):
    # app.max_concurrent_researches is user-editable but the semaphore it
    # gates is global, so an unclamped read would let one user raise their
    # own limit past the operator's and starve everyone else.
    max_concurrent = clamp_user_max_concurrent(
        SettingsManager(db_session=cap_db).get_setting(
            "app.max_concurrent_researches", 3
        )
    )
    if active_count >= max_concurrent:
        return (
            f"Concurrent research limit reached ({active_count}/"
            f"{max_concurrent}). Wait for an existing research to finish.",
            429,
        )

    return None


# ============================================================================
# Per-attempt API Routes (delete + retry a single chat turn)
# ============================================================================


@router.delete("/api/chat/sessions/{session_id}/attempts/{research_id}")
# Per-user keying, like the other state-changing chat routes. Caps bulk
# delete attempts that the global limiter alone left under-constrained.
@limiter.limit("30 per minute", key_func=_chat_user_key)
async def delete_attempt(
    request: Request,
    session_id: str,
    research_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Delete a single chat attempt (user message + research + response).

    Refuses with 409 if the target research is IN_PROGRESS and its worker
    thread is alive — the client must Stop it first (or wait for it to
    fail naturally). Stale IN_PROGRESS rows whose thread is dead are
    reclaimed and deleted.
    """

    def _impl():
        try:
            service = ChatService(username)
            try:
                service.delete_attempt(session_id, research_id)
            except AttemptNotFound:
                return JSONResponse(
                    {"success": False, "error": "Attempt not found"},
                    status_code=404,
                )
            except AttemptInProgress:
                return JSONResponse(
                    {
                        "success": False,
                        "error": (
                            "Research is in progress. Stop it before deleting "
                            "the attempt."
                        ),
                        "active_research_id": research_id,
                    },
                    status_code=409,
                )

            return JSONResponse({"success": True})

        except ROUTE_EXCEPTIONS:
            logger.exception("Error deleting chat attempt")
            return JSONResponse(
                {"success": False, "error": "Failed to delete attempt"},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.post("/api/chat/sessions/{session_id}/attempts/{research_id}/retry")
# Same per-minute cap as send_message: retry spawns a full research run,
# so it shares the spawn budget rather than getting a separate one (else
# alternating send/retry would let a user exceed the intended rate).
@limiter.limit("10 per minute", key_func=_chat_user_key)
async def retry_attempt(
    request: Request,
    session_id: str,
    research_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Retry a chat attempt: delete the old turn, re-submit same content.

    Looks up the original user message content via
    ``ChatService.get_original_attempt_query`` (uses
    ``research_meta.submission.message_id`` first, falls back to a
    ChatMessage query for older rows), deletes the old attempt
    atomically, then runs the same spawn path as ``send_message``.

    Returns the SAME shape as ``send_message`` so the client can
    subscribe to the new research_id via its existing post-send flow.
    """

    def _impl():
        admission_lock = None
        service = None
        rag_targets: list[tuple[str, list[str]]] = []
        try:
            service = ChatService(username)

            # Verify session exists + is active BEFORE doing anything
            # destructive. The get_session call also seeds session_data for
            # the context-builder below.
            try:
                session_data = service.get_session(session_id)
            except ChatSessionNotFound:
                return JSONResponse(
                    {"success": False, "error": "Session not found"},
                    status_code=404,
                )

            if session_data.get("status") != ChatSessionStatus.ACTIVE.value:
                return JSONResponse(
                    {
                        "success": False,
                        "error": (
                            "This chat is archived. Reactivate it to continue."
                        ),
                    },
                    status_code=409,
                )

            # Resolve DB password BEFORE any destructive op: the spawn path
            # needs it for metrics writes, and re-checking AFTER
            # delete_attempt would leave the attempt gone on a
            # session-expired 401.
            _pw, session_expired = resolve_user_password(username)
            if session_expired:
                return JSONResponse(
                    {
                        "success": False,
                        "error": (
                            "Your session has expired. Please log out and "
                            "log back in to continue."
                        ),
                    },
                    status_code=401,
                )

            # get_session() leaves the reused thread-local Session in a read
            # transaction. End it before the early gate acquisition so retry
            # follows the same gate -> DB order as every other producer.
            with get_user_db_session(username) as ordering_db:
                ordering_db.rollback()

            # Cheap gated fast-fail before context preparation. If the target
            # is still live this returns 409 before any destructive work.
            with user_research_start_gate(username):
                cap_error = _check_chat_research_slot_locked(
                    username, session_id
                )
            if cap_error is not None:
                message, status, active_research_id = cap_error
                payload = {"success": False, "error": message}
                if active_research_id is not None:
                    payload["active_research_id"] = active_research_id
                return JSONResponse(payload, status_code=status)

            # Fetch original user content BEFORE deleting the old attempt.
            # After delete_attempt the row is gone, so this lookup must
            # succeed first. Raises AttemptNotFound → 404.
            try:
                original_content = service.get_original_attempt_query(
                    session_id, research_id
                )
            except AttemptNotFound:
                return JSONResponse(
                    {"success": False, "error": "Attempt not found"},
                    status_code=404,
                )

            if not original_content.strip():
                # Empty/whitespace-only original — shouldn't happen since
                # send_message rejects these, but guard against a corrupt
                # row before we destroy the only copy.
                return JSONResponse(
                    {
                        "success": False,
                        "error": (
                            "Original attempt has no query content to retry."
                        ),
                    },
                    status_code=400,
                )

            # Build the replacement context before deleting anything, but
            # exclude the target attempt in memory so it cannot become its
            # own prior context. Current rows link the triggering user message
            # through research_meta; legacy rows carry research_id directly.
            original_message_id = None
            with get_user_db_session(username) as attempt_db:
                attempt_row = (
                    attempt_db.query(ResearchHistory)
                    .filter(
                        ResearchHistory.id == research_id,
                        ResearchHistory.chat_session_id == session_id,
                    )
                    .first()
                )
                if attempt_row is None:
                    return JSONResponse(
                        {"success": False, "error": "Attempt not found"},
                        status_code=404,
                    )
                submission = (attempt_row.research_meta or {}).get(
                    "submission", {}
                )
                if isinstance(submission, dict):
                    candidate = submission.get("message_id")
                    if isinstance(candidate, str) and candidate:
                        original_message_id = candidate

            # include_steps=False keeps `limit` spent entirely on genuine
            # ChatMessage conversation turns. Progress-step rows (there can
            # be dozens per attempt, most recently from the very attempt
            # being retried) would otherwise dominate the merged window used
            # by get_session_messages() for client rendering and push real
            # conversation history out of it before this filter ever runs —
            # ChatContextManager discards step rows anyway, so there is no
            # reason to spend the window budget on them here.
            messages = service.get_session_messages(
                session_id, limit=50, include_steps=False
            )
            messages = [
                message
                for message in messages
                if message.get("research_id") != research_id
                and message.get("id") != original_message_id
            ][-20:]

            settings_snapshot = _load_settings(username)
            context_manager = ChatContextManager(
                session_id,
                messages,
                session_data.get("accumulated_context"),
                settings_snapshot=settings_snapshot,
            )
            research_context = context_manager.build_research_context(
                current_query=original_content
            )

            # Parse numeric search settings up-front. A malformed value
            # must return a clean 400 HERE — before the atomic write
            # below commits the user message + IN_PROGRESS research row.
            try:
                iterations = int(
                    settings_snapshot.get("search.iterations", {}).get(
                        "value", 3
                    )
                )
                questions = int(
                    settings_snapshot.get(
                        "search.questions_per_iteration", {}
                    ).get("value", 1)
                )
            except (ValueError, TypeError):
                return JSONResponse(
                    {
                        "success": False,
                        "error": (
                            "Invalid numeric value in search settings "
                            "(iterations / questions_per_iteration)."
                        ),
                    },
                    status_code=400,
                )

            # SSRF pre-flight on the custom LLM endpoint: reject metadata /
            # link-local targets at the request boundary, BEFORE the atomic
            # write below commits the replacement user message + IN_PROGRESS
            # research row. Fail-fast defense-in-depth mirroring research.py
            # / followup.py: the OpenAI-compatible provider's
            # assert_base_url_safe re-validates the same URL before the
            # client is built. Private IPs / localhost pass (local LLM
            # backends), including scheme-less endpoints (the helper
            # normalizes exactly as the provider does); None — any provider
            # other than openai_endpoint, or no URL configured — is safe by
            # definition.
            if not is_safe_custom_llm_endpoint(
                _custom_endpoint_from_snapshot(settings_snapshot)
            ):
                return JSONResponse(
                    {"success": False, "error": "Invalid custom endpoint URL"},
                    status_code=400,
                )

            # End all cached read transactions before taking the stable gate.
            with get_user_db_session(username) as ordering_db:
                ordering_db.rollback()

            pending_admission_lock = get_user_research_start_lock(username)
            pending_admission_lock.acquire()
            admission_lock = pending_admission_lock

            # Nothing destructive has happened yet. Recheck under the gate
            # retained through delete, replacement claim, spawn, and cleanup.
            cap_error = _check_chat_research_slot_locked(username, session_id)
            if cap_error is not None:
                message, status, active_research_id = cap_error
                payload = {"success": False, "error": message}
                if active_research_id is not None:
                    payload["active_research_id"] = active_research_id
                return JSONResponse(payload, status_code=status)

            # Delete only after the authoritative gated recheck, so losing a
            # race cannot destroy the user's old attempt without admitting its
            # replacement. The RAG purge for documents the old attempt owned
            # is deferred until the gate is released (see finally below): it
            # does vector-store IO under FAISS locks, and the queue tick
            # acquires every user's gate in turn.
            try:
                rag_targets = service.delete_attempt_rows(
                    session_id, research_id
                )
            except AttemptInProgress:
                return JSONResponse(
                    {
                        "success": False,
                        "error": (
                            "Research is in progress. Stop it before retrying."
                        ),
                        "active_research_id": research_id,
                    },
                    status_code=409,
                )
            except AttemptNotFound:
                return JSONResponse(
                    {"success": False, "error": "Attempt not found"},
                    status_code=404,
                )

            research_mode = "quick"
            new_research_id = str(uuid.uuid4())

            # ---- Atomic write: user message + research row in ONE txn ----
            # Same pattern as send_message: any IntegrityError rolls back
            # the user message too, so no orphan rows.
            try:
                with get_user_db_session(username) as db_session:
                    new_message_id = service.insert_message_in_db(
                        db_session,
                        session_id=session_id,
                        role="user",
                        content=original_content,
                        message_type="query"
                        if len(messages) == 0
                        else "followup",
                    )

                    created_at = datetime.now(UTC).isoformat()
                    research_meta = {
                        "submission": {
                            "chat_session_id": session_id,
                            "message_id": new_message_id,
                            "research_mode": research_mode,
                        },
                        # Persist the run's settings snapshot so
                        # view/export renders the same uncited-sources
                        # mode as generation. Mirrors the main-form path;
                        # completion merge back-fills when missing.
                        "settings_snapshot": settings_snapshot,
                    }
                    db_session.add(
                        ResearchHistory(
                            id=new_research_id,
                            query=original_content,
                            mode=research_mode,
                            status=ResearchStatus.IN_PROGRESS.value,
                            created_at=created_at,
                            progress_log=[{"time": created_at, "progress": 0}],
                            research_meta=research_meta,
                            chat_session_id=session_id,
                        )
                    )
                    db_session.add(
                        UserActiveResearch(
                            username=username,
                            research_id=new_research_id,
                            status=ResearchStatus.IN_PROGRESS,
                            thread_id=str(threading.current_thread().ident),
                            settings_snapshot=settings_snapshot,
                        )
                    )

                    try:
                        db_session.commit()
                    except IntegrityError:
                        db_session.rollback()
                        logger.warning(
                            f"Concurrent in-progress research race for "
                            f"chat {session_id[:8]}... (retry)"
                        )
                        return JSONResponse(
                            {
                                "success": False,
                                "error": (
                                    "Research already in progress on this "
                                    "chat session."
                                ),
                            },
                            status_code=409,
                        )
            except ValueError as exc:
                if "not found" in str(exc).lower():
                    return JSONResponse(
                        {"success": False, "error": "Session not found"},
                        status_code=404,
                    )
                raise
            # ---- end atomic write ----

            pw = get_user_password(username)

            model_provider = settings_snapshot.get("llm.provider", {}).get(
                "value", ""
            )
            model = settings_snapshot.get("llm.model", {}).get("value", "")
            search_engine = settings_snapshot.get("search.tool", {}).get(
                "value", ""
            )
            # Only meaningful for the openai_endpoint provider (see above;
            # boundary SSRF pre-flight happened before the atomic write).
            custom_endpoint = _custom_endpoint_from_snapshot(settings_snapshot)
            user_strategy = settings_snapshot.get(
                "search.search_strategy", {}
            ).get("value", "langgraph-agent")

            if research_context.get("is_multi_turn"):
                strategy = "enhanced-contextual-followup"
                research_context["delegate_strategy"] = user_strategy
            else:
                strategy = user_strategy

            try:
                start_research_process(
                    new_research_id,
                    original_content,
                    research_mode,
                    run_research_process,
                    username=username,
                    user_password=pw,
                    model_provider=model_provider,
                    model=model,
                    search_engine=search_engine,
                    custom_endpoint=custom_endpoint,
                    strategy=strategy,
                    iterations=iterations,
                    questions_per_iteration=questions,
                    research_context=research_context,
                    chat_session_id=session_id,
                    chat_message_id=new_message_id,
                    settings_snapshot=settings_snapshot,
                )
            except DuplicateResearchError:
                logger.warning(
                    f"DuplicateResearchError on chat retry_attempt "
                    f"for {new_research_id[:8]}... (chat {session_id[:8]}...)"
                )
                _cleanup_chat_send_rows(
                    username,
                    new_research_id,
                    new_message_id,
                    session_id,
                    "duplicate",
                    preserve_attempt=True,
                )
                return JSONResponse(
                    {
                        "success": False,
                        "error": (
                            "Research already in progress on this chat session."
                        ),
                    },
                    status_code=409,
                )
            except SystemAtCapacityError:
                logger.warning(
                    f"SystemAtCapacityError on chat retry_attempt "
                    f"for {new_research_id[:8]}... (chat {session_id[:8]}...)"
                )
                _cleanup_chat_send_rows(
                    username,
                    new_research_id,
                    new_message_id,
                    session_id,
                    "capacity",
                    preserve_attempt=True,
                )
                return JSONResponse(
                    {
                        "success": False,
                        "error": (
                            "Server is at research capacity. Please retry "
                            "shortly."
                        ),
                    },
                    status_code=429,
                )
            except Exception:
                logger.exception(
                    f"Failed to spawn retried chat research "
                    f"{new_research_id[:8]}... "
                    f"(chat {session_id[:8]}...)"
                )
                _cleanup_chat_send_rows(
                    username,
                    new_research_id,
                    new_message_id,
                    session_id,
                    "retry spawn failure",
                    preserve_attempt=True,
                )
                return JSONResponse(
                    {
                        "success": False,
                        "error": "Failed to retry research. Please try again.",
                    },
                    status_code=500,
                )

            logger.info(
                f"Retried chat attempt as research "
                f"{new_research_id[:8]}... for chat {session_id[:8]}..."
            )

            return JSONResponse(
                {
                    "success": True,
                    "session_id": session_id,
                    "research_id": new_research_id,
                    "message_id": new_message_id,
                    "research_mode": research_mode,
                }
            )

        except ROUTE_EXCEPTIONS:
            logger.exception("Error retrying chat attempt")
            return JSONResponse(
                {"success": False, "error": "Failed to retry attempt"},
                status_code=500,
            )
        finally:
            if admission_lock is not None:
                admission_lock.release()
            # The old attempt's rows are committed as deleted whatever
            # happened after, so purge its RAG data now that the gate is
            # free. Best-effort: purge_attempt_rag logs its own failures.
            if rag_targets and service is not None:
                service.purge_attempt_rag(rag_targets)

    return await run_db_sync(_impl)