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.

143 KB
Raw Blame
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import (
    JSONResponse,
)
from python_multipart.exceptions import FormParserError
from starlette.exceptions import HTTPException as StarletteHTTPException
from ..dependencies.auth import require_auth
from ..dependencies.rate_limit import (
    _api_exempt,
    _api_user_key,
    api_rate_limit,
    limiter,
    upload_rate_limit_ip,
    upload_rate_limit_user,
)
from ..dependencies.threadpool import (
    WorkerCleanupStreamingResponse,
    run_db_sync,
)
from ..template_config import templates
from ..utils.download_headers import build_content_disposition

# Bounded preview for untrusted request-body values in log lines (#6191).
from .notes import _log_value_preview

import asyncio
import json
import re
from datetime import datetime, UTC
from pathlib import Path

from loguru import logger
from ...settings.exceptions import SettingsValueError
from ...settings.logger import log_settings
from sqlalchemy import case, func
from sqlalchemy.orm import Session
from typing import Optional, Annotated

# Security imports
from ...config.constants import DEFAULT_OLLAMA_URL
from ...exceptions import DuplicateResearchError, SystemAtCapacityError
from ...error_handling.error_messages import get_known_research_error_message
from ...constants import HISTORY_LOGS_HARD_CAP, ResearchStatus
from ...security import (
    FileUploadValidator,
    UnsafeFilenameError,
    filter_research_metadata,
    sanitize_filename,
    strip_settings_snapshot,
)
from ...security.log_sanitizer import redact_and_bound_for_log
from ...security.secure_logging import log_exception_type
from ...security.url_redaction import redact_url_credentials
from ...utilities.type_utils import overlay_runtime_settings, to_bool
from ...utilities.url_utils import is_safe_custom_llm_endpoint


from ...config.paths import get_config_directory

# Services imports
from ..services.pdf_extraction_service import get_pdf_extraction_service
from ..services.pdf_service import (
    MissingPDFDependencyError,
    get_weasyprint_install_instructions,
)

from ...database.models import (
    QueuedResearch,
    ResearchHistory,
    UserActiveResearch,
)
from ..models.database import (
    ResearchLog,
    calculate_duration,
)
from ...database.models.library import Document as Document
from ...database.research_cascade import (
    collect_research_document_rag_targets,
    SharedDownloadIndex,
    delete_research_with_dependents as _delete_research_with_dependents,
)
from ...database.session_context import get_user_db_session
from ...database.sqlcipher_utils import retry_on_db_lock
from ...llm.providers.base import normalize_provider
from ..auth.password_utils import resolve_user_password
from ..services.research_service import (
    _parse_research_metadata,
    clamp_user_max_concurrent,
    export_report_to_memory,
    run_research_process,
    start_research_process,
)

from ..routes.research_validation import (
    ITERATIONS_MAX,
    ITERATIONS_MIN,
    QUESTIONS_PER_ITERATION_MAX,
    QUESTIONS_PER_ITERATION_MIN,
    validate_research_query_length,
    validate_search_overrides,
)
from ..research_state import (
    append_research_log,
    get_active_research_ids,
    get_research_field,
    get_user_research_start_lock,
    is_research_active,
    set_termination_flag,
    user_research_start_gate,
)
from ...constants import (
    DEFAULT_SEARCH_TOOL,
)
from ..dependencies.json_body import (
    is_json_request,
    json_body_error,
    read_json_dict,
)
from ..utils.pagination import OFFSET_LIMIT_ERROR, parse_bounded_offset

# Create the router for the research application
router = APIRouter(tags=["research"])

# NOTE: Routes use username (not .get()) intentionally.
# require_auth guarantees the value is present; direct access fails fast
# if the dependency is ever removed.


# Add static route at the root level
# include_in_schema=False matches the sibling `/static/{path:path}` mount in
# fastapi_app.py. A `:path` converter cannot be represented in OpenAPI, so a
# schema-included route using one fails test_route_contracts.py's
# "every schema-included APIRoute appears in OpenAPI" check. This is a legacy
# shim for bookmarked URLs, not part of the documented API surface,
# so excluding it is correct rather than a workaround.
@router.get("/redirect-static/{path:path}", include_in_schema=False)
def redirect_static(path: str):
    """Serve legacy URLs with the same asset and cache policy as /static/.

    Serving in place removes the user-controlled Location header (CodeQL
    alert #8204). The shared resolver confines files to the static root,
    prefers built assets and rejects symlink escapes and missing files.
    """
    from ..static_files import static_file_response

    path = path.lstrip("/")
    segments = path.split("/")
    if (
        not path
        or "\\" in path
        or any(segment in {"", ".", ".."} for segment in segments)
        or any(
            ord(character) < 32 or ord(character) == 127 for character in path
        )
    ):
        # Raise rather than returning a body: the app's `@app.exception_
        # handler(404)` (fastapi_app.py) is what renders a missing resource,
        # and it serves HTML to browser navigations and JSON only to API
        # callers. This route exists for bookmarked browser URLs, so a
        # hand-rolled JSON envelope here would show a raw JSON body in the
        # browser to the one audience the route has.
        raise HTTPException(status_code=404, detail="Not found")

    response = static_file_response(path)
    if response is None:
        raise HTTPException(status_code=404, detail="Not found")
    return response


@router.get("/progress/{research_id}")
def progress_page(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Render the research progress page"""
    return templates.TemplateResponse(
        request=request,
        name="pages/progress.html",
        context={"request": request},
    )


@router.get("/details/{research_id}")
def research_details_page(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Render the research details page"""
    return templates.TemplateResponse(
        request=request, name="pages/details.html", context={"request": request}
    )


@router.get("/results/{research_id}")
def results_page(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Render the research results page"""
    return templates.TemplateResponse(
        request=request, name="pages/results.html", context={"request": request}
    )


@router.get("/history")
def history_page(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Render the history page"""
    return templates.TemplateResponse(
        request=request, name="pages/history.html", context={"request": request}
    )


# These request fields name providers, engines and strategies. Null and ""
# remain unset so the saved settings apply; every other value must be a string.
# Validate before dispatch and again for in-process callers such as news runs.
_STRING_REQUEST_FIELDS = (
    "model_provider",
    "search_engine",
    "search_tool",
    "strategy",
)


def _string_field_error(field):
    """Return the client-facing 400 message for a non-string ``field``."""
    return f"{field} must be a string"


def _non_string_request_field(data):
    """Return the first ``_STRING_REQUEST_FIELDS`` name in ``data`` whose
    value is not null and not a ``str``, or None when there is none."""
    for field in _STRING_REQUEST_FIELDS:
        value = data.get(field)
        if value is not None and not isinstance(value, str):
            return field
    return None


def _settings_value_error_response(error):
    """Refuse a research start because a configured setting is invalid.

    This is a server-side configuration error, not a request error, so it
    is a 500 rather than one of the 400 messages for request fields. The
    message names the key, its source and the reason, never the value.
    """
    logger.error("Research start refused: {}", error.diagnostic)
    return JSONResponse(
        {"status": "error", "message": error.diagnostic},
        status_code=500,
    )


def _extract_research_params(data, settings_manager):
    """Extract and resolve research parameters from request data and settings.

    Returns a dict with keys: model_provider, model, custom_endpoint,
    ollama_url, search_engine, max_results, time_period, iterations,
    questions_per_iteration, strategy, policy_egress_scope,
    llm_require_local_endpoint, embeddings_require_local.

    Raises ``ValueError`` when a ``_STRING_REQUEST_FIELDS`` value is neither
    null nor a string (checked first, before any settings lookup), when a
    string ``custom_endpoint`` (from the request or the saved setting) fails
    the SSRF guard, or when ``iterations`` / ``questions_per_iteration`` is
    not an integer in range. ``_start_research_sync`` maps each to a fixed
    400 message. A settings read raises ``SettingsValueError`` (also a
    ``ValueError``) for an invalid configured value; that one is returned
    as a 500 naming the setting instead.
    """
    bad_field = _non_string_request_field(data)
    if bad_field is not None:
        raise ValueError(_string_field_error(bad_field))
    model_provider = data.get("model_provider")
    if not model_provider:
        model_provider = settings_manager.get_setting("llm.provider", "ollama")
        logger.debug(
            f"No model_provider in request, using database setting: {model_provider}"
        )
    else:
        # Raw request-body value: log it through the bounded preview so a
        # wide dict/list posted for it does not cost a full repr per
        # request (#6305, same shape as #6191). Same for ``model`` below.
        logger.opt(lazy=True).debug(
            "Using model_provider from request: {}",
            lambda: _log_value_preview(model_provider),
        )
    # Normalize provider to lowercase canonical form (main fix #3348) —
    # the uppercase comparisons below would otherwise never match the
    # lowercase values stored in settings / sent by the UI, silently
    # skipping the custom_endpoint and ollama_url settings fallbacks.
    model_provider = normalize_provider(model_provider)

    model = data.get("model")
    if not model:
        model = settings_manager.get_setting("llm.model", None)
        logger.debug(f"No model in request, using database setting: {model}")
    else:
        logger.opt(lazy=True).debug(
            "Using model from request: {}", lambda: _log_value_preview(model)
        )

    custom_endpoint = None
    if model_provider == "openai_endpoint":
        custom_endpoint = data.get("custom_endpoint")
        if not custom_endpoint:
            custom_endpoint = settings_manager.get_setting(
                "llm.openai_endpoint.url", None
            )
            logger.debug(
                "No custom_endpoint in request, using database setting: "
                f"{redact_url_credentials(custom_endpoint) if custom_endpoint else None}"
            )

    # SSRF guard: a user-supplied custom_endpoint goes straight to the
    # OpenAI client as base_url. Without validation, an authenticated user
    # could point the LLM client at internal services (cloud metadata,
    # localhost, etc.) and read responses through completions output.
    if custom_endpoint and isinstance(custom_endpoint, str):
        # Delegate to the shared guard so the endpoint is normalized the way
        # the OpenAI-compatible provider normalizes it — a bare
        # ``localhost:11434`` is a legitimate local backend and must not be
        # rejected here. Private IPs stay allowed; cloud-metadata and bad
        # schemes are still blocked.
        if not is_safe_custom_llm_endpoint(custom_endpoint):
            raise ValueError("Invalid custom endpoint URL")

    ollama_url = data.get("ollama_url")
    if not ollama_url and model_provider == "ollama":
        ollama_url = settings_manager.get_setting(
            "llm.ollama.url", DEFAULT_OLLAMA_URL
        )
        logger.debug(
            "No ollama_url in request, using database setting: "
            f"{redact_url_credentials(ollama_url) if ollama_url else None}"
        )

    search_engine = data.get("search_engine") or data.get("search_tool")
    if not search_engine:
        search_engine = settings_manager.get_setting(
            "search.tool", DEFAULT_SEARCH_TOOL
        )

    max_results = data.get("max_results")
    time_period = data.get("time_period")

    iterations = data.get("iterations")
    if iterations is not None:
        if (
            type(iterations) is not int
            or not ITERATIONS_MIN <= iterations <= ITERATIONS_MAX
        ):
            raise ValueError(
                f"iterations must be an integer between {ITERATIONS_MIN} and {ITERATIONS_MAX}"
            )
    else:
        iterations = settings_manager.get_setting("search.iterations", 5)

    questions_per_iteration = data.get("questions_per_iteration")
    if questions_per_iteration is not None:
        if (
            type(questions_per_iteration) is not int
            or not QUESTIONS_PER_ITERATION_MIN
            <= questions_per_iteration
            <= QUESTIONS_PER_ITERATION_MAX
        ):
            raise ValueError(
                f"questions_per_iteration must be an integer between "
                f"{QUESTIONS_PER_ITERATION_MIN} and {QUESTIONS_PER_ITERATION_MAX}"
            )
    else:
        questions_per_iteration = settings_manager.get_setting(
            "search.questions_per_iteration", 5
        )

    strategy = data.get("strategy")
    if not strategy:
        strategy = settings_manager.get_setting(
            "search.search_strategy", "source-based"
        )

    # Egress policy per-research overrides. Mirror the
    # model/search_engine pattern: missing values fall back to saved
    # settings; supplied values override JUST FOR THIS RUN. They do
    # NOT persist to the user's settings DB.
    policy_egress_scope = data.get("policy_egress_scope")
    llm_require_local_endpoint = data.get("llm_require_local_endpoint")
    embeddings_require_local = data.get("embeddings_require_local")

    return {
        "model_provider": model_provider,
        "model": model,
        "custom_endpoint": custom_endpoint,
        "ollama_url": ollama_url,
        "search_engine": search_engine,
        "max_results": max_results,
        "time_period": time_period,
        "iterations": iterations,
        "questions_per_iteration": questions_per_iteration,
        "strategy": strategy,
        "policy_egress_scope": policy_egress_scope,
        "llm_require_local_endpoint": llm_require_local_endpoint,
        "embeddings_require_local": embeddings_require_local,
    }


def _precheck_search_engine_api_key(settings_manager, search_engine, username):
    """Reject an explicit engine choice only when its required key is missing.

    Automatic tool-list availability does not restrict the run's primary.
    The factory still validates credentials when constructing the engine.
    """
    try:
        from ...web_search_engines.search_engines_config import (
            _resolve_api_key,
            search_config,
        )

        snapshot = settings_manager.get_settings_snapshot()
        if not isinstance(snapshot, dict):
            return None
        config = search_config(
            settings_snapshot=snapshot, username=username
        ).get(search_engine, {})
        if not config.get("requires_api_key", False) or _resolve_api_key(
            search_engine, config, snapshot
        ):
            return None
        display_name = config.get("display_name", search_engine)
        return JSONResponse(
            {
                "status": "error",
                "message": (
                    f"{display_name} requires an API key. "
                    "Configure it in Settings before starting research."
                ),
                "reason": "search_api_key_missing",
                "field": "search_engine",
            },
            status_code=400,
        )
    except SettingsValueError as error:
        return _settings_value_error_response(error)
    except Exception as error:
        log_exception_type("Failed to check search engine credentials", error)
        return JSONResponse(
            {
                "status": "error",
                "message": "Could not verify the search engine's API key configuration.",
            },
            status_code=500,
        )


def _precheck_engine_policy(settings_manager, params, search_engine, username):
    """Validate the requested search engine against this run's egress policy.

    Under non-STRICT scopes the requested engine is overlaid as
    ``search.tool`` before resolving the primary, matching the worker,
    so ADAPTIVE scope follows the run engine rather than the saved
    database default. Under STRICT there is no overlay: the saved
    primary is resolved first and a blank/missing saved primary fails
    closed (400), so the request can never choose its own STRICT
    primary.

    Returns a ``JSONResponse`` when the request should be rejected, or
    ``None`` to continue. Falls through (returns None) when there's no
    real dict snapshot to validate or the policy module errors — the
    factory PEP still enforces at engine-instantiation time. An invalid
    configured setting is not such an error: it rejects the request with
    the setting diagnostic instead of skipping the check.
    """
    try:
        from ...security.egress.policy import (
            DEFAULT_EGRESS_SCOPE,
            EgressScope,
            PolicyDeniedError,
            _get_setting_value,
            context_from_snapshot,
            evaluate_engine,
            resolve_run_primary_engine,
        )

        policy_snapshot = settings_manager.get_settings_snapshot()
        # Only validate against a real dict snapshot. A test double or
        # an unavailable settings backend hands back something else;
        # skip rather than misfire.
        if not isinstance(policy_snapshot, dict):
            return None

        # Overlay per-research form overrides so the snapshot reflects
        # what the user picked for THIS run (per-research overrides, not
        # a global settings save).
        # Resolve the primary the SAME way the worker does (single source of
        # truth) so the precheck and the background worker agree on accept vs.
        # refuse — including the fail-closed missing-primary case, which the
        # ValueError handler below maps to a 400. (Previously this substituted
        # searxng and accepted runs the worker then refused.)
        try:
            _apply_policy_overrides(policy_snapshot, params)

            scope_raw = _get_setting_value(
                policy_snapshot, "policy.egress_scope", DEFAULT_EGRESS_SCOPE
            )
            from ...settings.manager import check_env_setting

            env_scope = check_env_setting("policy.egress_scope")
            if env_scope is not None:
                scope_raw = env_scope
            scope_str = str(scope_raw).strip().lower()

            if scope_str == EgressScope.STRICT.value:
                # STRICT must never fall back to the requested engine: a
                # blank/missing saved primary is a wiring error and must
                # fail closed (ValueError -> 400 below), not let the
                # request choose its own primary.
                saved_primary = resolve_run_primary_engine(policy_snapshot)
                primary = saved_primary
            else:
                saved_primary = resolve_run_primary_engine(
                    policy_snapshot, default=search_engine or None
                )
                primary = search_engine or saved_primary

            overlay_runtime_settings(
                policy_snapshot,
                search_engine=search_engine,
                model_provider=params.get("model_provider"),
                model=params.get("model"),
                custom_endpoint=params.get("custom_endpoint"),
                iterations=params.get("iterations"),
                questions_per_iteration=params.get("questions_per_iteration"),
                strategy=params.get("strategy"),
                max_results=params.get("max_results"),
                time_period=params.get("time_period"),
            )
            policy_ctx = context_from_snapshot(
                policy_snapshot,
                primary,
                username=username,
            )
        except PolicyDeniedError as exc:
            return JSONResponse(
                {
                    "status": "error",
                    "message": (
                        f"Egress policy refused this run: {exc.decision.reason}"
                    ),
                },
                status_code=400,
            )
        except SettingsValueError:
            # A ValueError subclass, but not a policy decision: the outer
            # handler reports which setting is invalid.
            raise
        except ValueError as exc:
            # An invalid policy config is unrecoverable. Previously this
            # raised, fell through to the outer ``except Exception``
            # below, and silently returned None — so the run started
            # successfully at the precheck and only failed at a
            # downstream PEP. Surface it here as a 400 instead.
            logger.bind(policy_audit=True).warning(
                "POST /api/start_research policy precheck rejected",
                reason=str(exc),
            )
            # Return a generic reason to the client — the raw ValueError text
            # can carry policy-config internals and is already captured in the
            # policy_audit warning above (CWE-209). Unlike the PolicyDeniedError
            # branch, this path has no curated, user-safe decision reason.
            return JSONResponse(
                {
                    "status": "error",
                    "message": (
                        "Egress policy refused this run due to an "
                        "invalid policy configuration."
                    ),
                },
                status_code=400,
            )

        target_engine = search_engine or primary
        decision = evaluate_engine(
            target_engine,
            policy_ctx,
            settings_snapshot=policy_snapshot,
        )
        if not decision.allowed:
            logger.bind(policy_audit=True).warning(
                "POST /api/start_research search_engine refused",
                engine=target_engine,
                reason=decision.reason,
            )
            # Local import: this module imports the egress policy lazily inside
            # this function to avoid a circular import at load time; keep the
            # guidance import with it.
            from ...security.egress.guidance import denial_guidance

            # Only the scope-mismatch and strict-not-primary denials have a
            # form field the user can toggle to fix the error (the Egress
            # Scope dropdown). For engine_unknown / unclassified the user has
            # to pick a different search engine — flagging the egress-scope
            # dropdown there would be misleading and cause the wrong field to
            # turn red. internal_error / no_snapshot are server-side issues
            # with no form-level fix at all, so we return ``field: null`` and
            # let the frontend render just the alert (no inline field error).
            field_for_reason = {
                "scope_mismatch_private_only": "policy_egress_scope",
                "scope_mismatch_public_only": "policy_egress_scope",
                "strict_not_primary": "policy_egress_scope",
            }.get(decision.reason)

            return JSONResponse(
                {
                    "status": "error",
                    # Clear, actionable message (what + why + how to allow),
                    # plus the raw reason code for support/logs. The
                    # guidance uses ``primary_engine`` to decide whether
                    # Adaptive (default) is a reliable remedy — see
                    # ``_scope_mismatch_how``.
                    "message": denial_guidance(
                        decision.reason,
                        target=f"Search engine '{target_engine}'",
                        # Raw engine id, used by the guidance to decide
                        # whether Adaptive (default) is a reliable
                        # remedy for a scope-mismatch denial (see
                        # ``_scope_mismatch_how`` — kept separate from
                        # ``target`` so the formatted display string
                        # above doesn't break the equality check).
                        target_id=target_engine,
                        primary_engine=saved_primary,
                    ),
                    "reason": decision.reason,
                    # The frontend uses this to highlight the offending
                    # form field with an inline error (FormValidator's
                    # .ldr-field-error + .ldr-field-invalid convention)
                    # and to anchor an alert near the submit button.
                    # ``null`` means "no form field fixes this — show the
                    # alert without an inline field error".
                    "field": field_for_reason,
                },
                status_code=400,
            )

        # Stage C (ADR-0007): enforce the two-axis (sensitivity x exposure)
        # admissibility as defense-in-depth on top of the scope check. This is
        # a fast pre-start 400 for the API path; the same rule is also enforced
        # at the shared run chokepoint in run_research_process, so follow-up /
        # chat / queue runs are covered too. Under the UNPROTECTED escape hatch
        # audit_run evaluates in permissive mode (always allowed), so an
        # explicitly-unprotected run is never blocked. On an internal error
        # audit_run fails CLOSED (reason "audit_error") so the run is refused
        # visibly rather than silently degrading to the scope PEPs.
        #
        # NOTE: this sees the run's PRIMARY engine only. The per-engine scope
        # PEP still runs for expanded engines, but it enforces SCOPE, not the
        # two-axis rule — so a non-primary sensitive engine reaching a cloud
        # sink under a permissive scope (legacy `both`) is not caught here.
        # Widening to the full resolved engine set is a follow-up.
        from ...security.egress.run_classification import (
            audit_run_from_snapshot,
        )

        # Same shared entry point the worker chokepoint uses, so both resolve
        # providers identically (LLM override wins over the snapshot; embeddings
        # only for RAG primaries) and fail closed the same way.
        two_axis = audit_run_from_snapshot(
            policy_snapshot,
            policy_ctx,
            search_engine,
            llm_provider=params.get("model_provider"),
        )
        if not two_axis.allowed:
            logger.bind(policy_audit=True).warning(
                "POST /api/start_research two-axis egress refused",
                reason=two_axis.reason,
                offending=two_axis.offending,
            )
            if two_axis.reason == "audit_error":
                message = (
                    "Egress policy could not verify this run, so it was "
                    "refused (fail-closed). To override, ask the server "
                    "operator to enable the Unprotected escape hatch "
                    "(LDR_POLICY_ALLOW_UNPROTECTED_EGRESS), then select the "
                    "'Unprotected' scope yourself."
                )
            else:
                message = (
                    "Egress policy refused this run: a sensitive source would "
                    f"reach an exposing destination ({two_axis.reason}). Use "
                    "local inference, mark the collection public, or — to "
                    "override — ask the server operator to enable the "
                    "Unprotected escape hatch "
                    "(LDR_POLICY_ALLOW_UNPROTECTED_EGRESS), then set Egress "
                    "Scope to 'Unprotected' yourself."
                )
            return JSONResponse(
                {
                    "status": "error",
                    "message": message,
                    "reason": two_axis.reason,
                },
                status_code=400,
            )
        return None
    except SettingsValueError as error:
        # The snapshot read above validates every effective setting, so one
        # invalid row anywhere lands here. Skipping the check would let the
        # run start without it; refuse and name the setting instead.
        return _settings_value_error_response(error)
    except Exception:
        # Policy module unavailable / internal error → log and fall
        # through; the factory PEP will catch any actual violation.
        logger.exception("egress policy pre-check skipped")
        return None


def _apply_policy_overrides(settings_snapshot, params):
    """Overlay form-supplied egress policy values onto the snapshot.

    Per-research overrides: the user picked these values for this
    specific run and they do NOT persist to the settings DB. Search
    engine / model / iteration overlays live in ``overlay_runtime_settings``.
    """
    if not isinstance(settings_snapshot, dict):
        return
    from ...settings.manager import check_env_setting
    from ...security.egress.policy import (
        parse_user_egress_scope,
    )

    scope_override = params.get("policy_egress_scope")
    if (
        scope_override is not None
        and check_env_setting("policy.egress_scope") is None
    ):
        settings_snapshot["policy.egress_scope"] = parse_user_egress_scope(
            scope_override
        ).value
    if (
        params.get("llm_require_local_endpoint") is not None
        and check_env_setting("llm.require_local_endpoint") is None
    ):
        settings_snapshot["llm.require_local_endpoint"] = to_bool(
            params["llm_require_local_endpoint"]
        )
    if (
        params.get("embeddings_require_local") is not None
        and check_env_setting("embeddings.require_local") is None
    ):
        settings_snapshot["embeddings.require_local"] = to_bool(
            params["embeddings_require_local"]
        )


def _research_not_found(research_id, message="Research not found"):
    """Return a consistent 404 JSON for a missing research.

    Emits ``status``, ``error`` and ``message`` so the body is a strict
    superset of both historical 404 shapes — every frontend reader and
    existing test keeps working without changes:
      - Shape A readers read ``data.error``
      - Shape B readers read ``data.status`` (``== "error"``) and/or
        ``data.message``

    ``research_id`` is used only for a debug log identifying which research
    was missing; it is intentionally never echoed in the response body.
    """
    logger.opt(lazy=True).debug(
        "404 for research {}: {}",
        lambda: _log_value_preview(research_id),
        lambda: message,
    )
    return JSONResponse(
        {"status": "error", "error": message, "message": message},
        status_code=404,
    )


def _queue_research(
    db_session: Session,
    username,
    research_id,
    query,
    mode,
    research_settings,
    params,
    session_id,
    reason="",
    research=None,
):
    """Add research to queue and notify processor. Returns a JSON response.

    Args:
        reason: Optional prefix explaining why the research was queued
                (e.g. "due to concurrent limit").
        research: Optional ResearchHistory object whose status should be set
                  to QUEUED atomically with the queue record insertion.
    """
    max_position = (
        db_session.query(func.max(QueuedResearch.position))
        .filter_by(username=username)
        .scalar()
        or 0
    )

    queued_record = QueuedResearch(
        username=username,
        research_id=research_id,
        query=query,
        mode=mode,
        settings_snapshot=research_settings,
        position=max_position + 1,
    )
    db_session.add(queued_record)
    if research is not None:
        research.status = ResearchStatus.QUEUED  # type: ignore[assignment]
    db_session.commit()
    logger.info(
        f"Queued research {research_id} at position {max_position + 1} for user {username}"
    )

    from ..queue.processor_v2 import queue_processor

    queue_processor.notify_research_queued(
        username,
        research_id,
        session_id=session_id,
        query=query,
        mode=mode,
        settings_snapshot=research_settings,
        model_provider=params["model_provider"],
        model=params["model"],
        custom_endpoint=params["custom_endpoint"],
        search_engine=params["search_engine"],
        max_results=params["max_results"],
        time_period=params["time_period"],
        iterations=params["iterations"],
        questions_per_iteration=params["questions_per_iteration"],
        strategy=params["strategy"],
    )

    position = max_position + 1
    reason_text = f" {reason}" if reason else ""
    message = f"Your research has been queued{reason_text}. Position in queue: {position}"
    return {
        "status": ResearchStatus.QUEUED,
        "research_id": research_id,
        "queue_position": position,
        "message": message,
    }


@router.post("/api/start_research")
# Primary research-submission endpoint — must be rate-limited (per-user,
# via the shared api_rate_limit bucket) or a single account can flood the
# research queue (#3135).
@api_rate_limit
async def start_research(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Async wrapper that does the only legitimate await (parsing the JSON
    body) on the event loop, then offloads the synchronous body — which
    opens four ``get_user_db_session`` blocks (each potentially driving
    PBKDF2 key derivation), threads, and other blocking work — to a
    threadpool. Without this, ``async def`` + sync DB blocks would
    serialise the entire server behind the slowest /start_research call.
    """

    # main's @require_json_body(error_format="status") read the body with
    # get_json(silent=True), which returns None for a non-JSON media type,
    # so a JSON-looking text/plain body got this 400 and started nothing.
    # request.json() ignores Content-Type, so the check is explicit here.
    # POST /research/api/start delegates to this handler and shares it.
    if not is_json_request(request):
        return json_body_error("status")

    # A malformed or non-object JSON body would otherwise reach
    # _start_research_sync's data.get(...) calls and 500 (AttributeError).
    # Reject at the boundary with a clean 400.
    try:
        data = await request.json()
    except Exception:
        return JSONResponse(
            {"status": "error", "message": "Request body must be JSON"},
            status_code=400,
        )
    if not isinstance(data, dict):
        return JSONResponse(
            {
                "status": "error",
                "message": "Request body must be a JSON object",
            },
            status_code=400,
        )
    query = data.get("query")
    if query is not None and not isinstance(query, str):
        return JSONResponse(
            {"status": "error", "message": "query must be a string"},
            status_code=400,
        )
    # The string fields (``_STRING_REQUEST_FIELDS``) are used with str
    # methods after dispatch, so any JSON type other than a string or null
    # is a 400 here, before any database work. Null and "" still count as
    # unset in ``_extract_research_params``.
    bad_field = _non_string_request_field(data)
    if bad_field is not None:
        return JSONResponse(
            {"status": "error", "message": _string_field_error(bad_field)},
            status_code=400,
        )
    metadata = data.get("metadata", {})
    if not isinstance(metadata, dict):
        return JSONResponse(
            {"status": "error", "message": "metadata must be an object"},
            status_code=400,
        )
    # A missing query outranks the override range checks below, so a body
    # with no query and an invalid iterations value reports "Query is
    # required" (the more useful message when both are wrong). Mirrors the
    # early return in _start_research_sync and the api_v1 query-first
    # order. The JSON type checks above (query, the string fields,
    # metadata) still run first, so a wrong-typed field is reported even
    # when the query is missing.
    if not query:
        return JSONResponse(
            {"status": "error", "message": "Query is required"}, status_code=400
        )
    validation_error = validate_search_overrides(data)
    if validation_error is not None:
        return JSONResponse(
            {"status": "error", "message": validation_error},
            status_code=400,
        )
    base_url = str(request.base_url)
    session_id = request.session.get("session_id")
    return await run_db_sync(
        _start_research_sync, data, username, base_url, session_id
    )


def _unrunnable_subscription_response(db_session, metadata):
    """400 response for a run linked to a saved subscription that cannot run.

    ``metadata.subscription_id`` makes the finished run advance that
    subscription's schedule, which needs a runnable saved interval (see
    ``is_runnable_refresh_interval``). The manual-run endpoint and the
    overdue sweep check that before they call in; checking it in this shared
    entry point as well covers a direct ``POST /api/start_research``. An id
    that matches no saved subscription is left alone, as before.

    Any truthy id is looked up as ``str(id)``, the way the completion advance
    (``advance_refresh_schedule_by_id``) resolves it, so a JSON number cannot
    pass this check and still advance a numeric-string row at completion.
    """
    subscription_id = metadata.get("subscription_id")
    if not subscription_id:
        return None
    subscription_id = str(subscription_id)
    from ...database.models.news import NewsSubscription
    from ...news.constants import (
        SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE,
        is_runnable_refresh_interval,
    )

    subscription = (
        db_session.query(NewsSubscription)
        .filter(NewsSubscription.id == subscription_id)
        .first()
    )
    if subscription is None or is_runnable_refresh_interval(
        subscription.refresh_interval_minutes
    ):
        return None
    return JSONResponse(
        {"status": "error", "message": SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE},
        status_code=400,
    )


def _start_research_sync(
    data: dict, username: str, base_url: str, session_id: str | None
):
    # Debug logging to trace model parameter. The key list is request
    # input too (a body may carry any number of keys of any length), so
    # it goes through the same bounded preview as the values below.
    logger.opt(lazy=True).debug(
        "Request data keys: {}", lambda: _log_value_preview(list(data.keys()))
    )

    # Check if this is a news search
    metadata = data.get("metadata", {})
    if metadata.get("is_news_search"):
        logger.info(
            "News search request received: triggered_by={}",
            _log_value_preview(metadata.get("triggered_by", "unknown")),
        )

    query = data.get("query")
    mode = data.get("mode", "quick")

    # Missing query outranks parameter validation: a body with no query
    # and an invalid iterations value reports "Query is required" (the
    # more useful message when both are wrong). The no-query log-preview
    # probe only exercises the two log sites above this return ("Request
    # data keys", news "triggered_by"); the sites below (model/provider/
    # engine/iterations/strategy lines) need a query to run and are pinned
    # by the with-query probe variant. See #6517 review and #6756.
    if not query:
        return JSONResponse(
            {"status": "error", "message": "Query is required"}, status_code=400
        )

    # Replace date placeholders if they exist
    if query and "YYYY-MM-DD" in query:
        # Use local system time
        current_date = datetime.now(UTC).strftime("%Y-%m-%d")

        original_query = query
        query = query.replace("YYYY-MM-DD", current_date)
        logger.info(
            "Replaced date placeholder in query: {} -> {}",
            # (#6938: redacted before it is cut.)
            redact_and_bound_for_log(original_query, 100),
            redact_and_bound_for_log(query, 100),
        )
        logger.info(f"Using date: {current_date}")

        # Update metadata to track the replacement
        if not metadata:
            metadata = {}
        metadata["original_query"] = original_query
        metadata["processed_query"] = query
        metadata["date_replaced"] = current_date
        data["metadata"] = metadata

    # Get parameters from request or use database settings
    from ...settings import SettingsManager

    with get_user_db_session(username) as db_session:
        unrunnable = _unrunnable_subscription_response(db_session, metadata)
        if unrunnable is not None:
            return unrunnable

        settings_manager = SettingsManager(db_session=db_session)
        try:
            params = _extract_research_params(data, settings_manager)
        except SettingsValueError as error:
            # Ahead of the ValueError mapping below: its substring matching
            # would report an invalid saved or environment value as a bad
            # request field (e.g. "iterations must be an integer ...").
            return _settings_value_error_response(error)
        except ValueError as exc:
            # SSRF guard or parameter validation in _extract_research_params
            # rejected the input — return main's clean 400 contract
            # instead of an unhandled 500. Map to explicit error messages to
            # avoid exposing internal exception details (CodeQL py/stack-trace-exposure).
            msg = str(exc)
            # A string-field type error keeps its own message, rebuilt from
            # the constant field list rather than echoed from ``exc``. Only
            # in-process callers that skip start_research's identical check
            # (the news run-now and overdue paths) can reach it.
            bad_field = next(
                (
                    field
                    for field in _STRING_REQUEST_FIELDS
                    if msg == _string_field_error(field)
                ),
                None,
            )
            if bad_field is not None:
                error_msg = _string_field_error(bad_field)
            elif "iterations" in msg:
                error_msg = f"iterations must be an integer between {ITERATIONS_MIN} and {ITERATIONS_MAX}"
            elif "questions_per_iteration" in msg:
                error_msg = (
                    f"questions_per_iteration must be an integer between "
                    f"{QUESTIONS_PER_ITERATION_MIN} and {QUESTIONS_PER_ITERATION_MAX}"
                )
            else:
                error_msg = "Invalid custom endpoint URL"
            return JSONResponse(
                {"status": "error", "message": error_msg},
                status_code=400,
            )

        model_provider = params["model_provider"]
        model = params["model"]
        custom_endpoint = params["custom_endpoint"]
        search_engine = params["search_engine"]
        max_results = params["max_results"]
        time_period = params["time_period"]
        iterations = params["iterations"]
        questions_per_iteration = params["questions_per_iteration"]
        strategy = params["strategy"]

        # Egress policy: server-side check on the requested search
        # engine BEFORE we enqueue the research. The requested engine is
        # the run primary (same overlay the worker applies), so ADAPTIVE
        # scope follows this run's engine rather than the saved DB default.
        # The factory PEP still enforces expanded engines at instantiation.
        policy_error = _precheck_engine_policy(
            settings_manager, params, search_engine, username
        )
        if policy_error is not None:
            return policy_error

        credential_error = _precheck_search_engine_api_key(
            settings_manager, search_engine, username
        )
        if credential_error is not None:
            return credential_error

        # This thread-local session survives the ``with`` block. End its
        # read transaction before the admission gate below; otherwise this
        # request can wait for the gate while retaining a pooled connection
        # (and, under rollback-journal SQLite, a shared database lock).
        db_session.rollback()

    # Debug logging for model parameter specifically. ``model`` is an
    # unvalidated request-body value, so it must not be interpolated
    # eagerly: an f-string builds the full repr on every request
    # regardless of log level. The bounded preview keeps the cost and
    # the logged size capped (#6305, same shape as #6191).
    logger.opt(lazy=True).debug(
        "Extracted model value: {} (type: {})",
        lambda: _log_value_preview(model),
        lambda: type(model).__name__,
    )

    # Log the selections for troubleshooting. These two lines are emitted
    # at the default INFO level, so the request-body values in them go
    # through the same bounded preview. ``max_results`` and ``time_period``
    # are validated at the request boundary (``validate_search_overrides``)
    # and are passed as they are.
    logger.info(
        "Starting research with provider: {}, model: {}, search engine: {}",
        _log_value_preview(model_provider),
        _log_value_preview(model),
        _log_value_preview(search_engine),
    )
    logger.info(
        "Additional parameters: max_results={}, time_period={}, iterations={}, questions={}, strategy={}",
        max_results,
        time_period,
        _log_value_preview(iterations),
        _log_value_preview(questions_per_iteration),
        _log_value_preview(strategy),
    )

    # NOTE: the missing-query 400 lives at the top of this function
    # (before _extract_research_params) so it outranks parameter
    # validation — see the comment there.

    # Cap query length to defend against accidental prompt blowups that would
    # bloat ResearchHistory storage and run up LLM context.  The queue replay
    # path uses the same validator before dispatching a persisted row.
    query_length_error = validate_research_query_length(query)
    if query_length_error is not None:
        return JSONResponse(
            {
                "status": "error",
                "message": query_length_error,
            },
            status_code=400,
        )

    # Validate required parameters based on provider.
    # model_provider is normalize_provider()'d to lowercase in
    # _extract_research_params, so this MUST compare to the lowercase
    # canonical form — the old "OPENAI_ENDPOINT" literal never matched and
    # left this required-field check dead.
    if model_provider == "openai_endpoint" and not custom_endpoint:
        return JSONResponse(
            {
                "status": "error",
                "message": "Custom endpoint URL is required for OpenAI endpoint provider",
            },
            status_code=400,
        )

    # SSRF pre-flight on the user-supplied LLM endpoint: reject metadata /
    # link-local targets at the request boundary, before any research thread
    # is spawned. This is fail-fast defense-in-depth — the OpenAI-compatible
    # provider's assert_base_url_safe re-validates the same URL before the
    # client is built. Private IPs / localhost are permitted so local LLMs
    # (vLLM, Ollama, LM Studio) work, including scheme-less endpoints
    # (the helper normalizes exactly as the provider does).
    if model_provider == "openai_endpoint" and not is_safe_custom_llm_endpoint(
        custom_endpoint
    ):
        return JSONResponse(
            {"status": "error", "message": "Invalid custom endpoint URL"},
            status_code=400,
        )

    if not model:
        logger.error(
            "No model specified or configured. Provider: {}",
            _log_value_preview(model_provider),
        )
        return JSONResponse(
            {
                "status": "error",
                "message": "Model is required. Please configure a model in the settings.",
            },
            status_code=400,
        )

    # Check if the user has too many active researches
    from ...settings import SettingsManager

    should_queue = False
    # Pre-bind: the post-commit race check below reads this, so if the block
    # under try raises before the setting is read (a DB hiccup), that check
    # would raise UnboundLocalError and turn a recoverable failure into a 500.
    max_concurrent_researches = 3
    try:
        # Share one stable per-user admission lock with queue replay. The
        # preliminary count and the post-commit claim below both use this gate;
        # the second guarded section closes any race during snapshot capture.
        with user_research_start_gate(username):
            with get_user_db_session(username) as db_session:
                settings_manager = SettingsManager(db_session)
                # Clamp to the server-wide semaphore ceiling (#5549).
                # app.max_concurrent_researches is a per-user, user-editable
                # setting, but the semaphore it is checked against is global —
                # so without the clamp a user can raise their own limit past
                # the server's and monopolise it.
                max_concurrent_researches = clamp_user_max_concurrent(
                    settings_manager.get_setting(
                        "app.max_concurrent_researches", 3
                    )
                )

                # First, clean up stale entries where the research thread died.
                from ..routes.globals import reclaim_stale_user_active_research

                if reclaim_stale_user_active_research(
                    db_session, username, logger=logger
                ):
                    db_session.commit()

                active_count = (
                    db_session.query(UserActiveResearch)
                    .filter_by(
                        username=username,
                        status=ResearchStatus.IN_PROGRESS,
                    )
                    .count()
                )

                logger.info(
                    f"Active research count for {username}: "
                    f"{active_count}/{max_concurrent_researches}"
                )

                should_queue = active_count >= max_concurrent_researches
                logger.info(f"Should queue new research: {should_queue}")

                # This context reuses a thread-local Session rather than
                # closing it on exit. End the count query's read transaction
                # before releasing the admission gate; otherwise this request
                # can retain a pooled connection and later wait for the gate
                # while a queue tick holds the gate and waits for a connection.
                db_session.rollback()
    except SettingsValueError as error:
        # An invalid concurrency limit must not become "do not queue".
        return _settings_value_error_response(error)
    except Exception:
        logger.exception("Failed to check active researches")
        # Default to not queueing if we can't check
        should_queue = False

    # For non-queued research, verify password is available BEFORE creating DB records
    # (queued research gets password later via queue processor)
    user_password = None
    if not should_queue:
        user_password, session_expired = resolve_user_password(username)
        if session_expired:
            return JSONResponse(
                {
                    "status": "error",
                    "message": "Your session has expired. Please log out and log back in to start research.",
                },
                status_code=401,
            )

    # Create a record in the database with explicit UTC timestamp
    import uuid
    import threading

    created_at = datetime.now(UTC).isoformat()
    research_id = str(uuid.uuid4())

    # Create organized research metadata with settings snapshot
    submission = {
        "model_provider": model_provider,
        "model": model,
        "custom_endpoint": custom_endpoint,
        "search_engine": search_engine,
        "max_results": max_results,
        "time_period": time_period,
        "iterations": iterations,
        "questions_per_iteration": questions_per_iteration,
        "strategy": strategy,
    }
    submission_overrides = []
    if data.get("model_provider"):
        submission_overrides.append("model_provider")
    if data.get("model"):
        submission_overrides.append("model")
    if data.get("custom_endpoint"):
        submission_overrides.append("custom_endpoint")
    if data.get("search_engine") or data.get("search_tool"):
        submission_overrides.append("search_engine")
    if data.get("max_results") is not None:
        submission_overrides.append("max_results")
    if data.get("time_period") is not None:
        submission_overrides.append("time_period")
    if data.get("iterations") is not None:
        submission_overrides.append("iterations")
    if data.get("questions_per_iteration") is not None:
        submission_overrides.append("questions_per_iteration")
    if data.get("strategy"):
        submission_overrides.append("strategy")

    research_settings = {
        # Direct submission parameters
        "submission": submission,
        # Only these request-supplied values remain fixed if queued work is
        # dispatched after operator environment settings change.
        "submission_overrides": submission_overrides,
        # System information
        "system": {
            "timestamp": created_at,
            "user": username,
            "version": "1.0",  # Track metadata version for future migrations
            "server_url": base_url,  # Server URL for link generation
        },
    }

    # Add any additional metadata from request
    additional_metadata = data.get("metadata", {})
    if additional_metadata:
        reserved_metadata_keys = {
            "submission",
            "submission_overrides",
            "system",
            "settings_snapshot",
        }
        research_settings.update(
            {
                key: value
                for key, value in additional_metadata.items()
                if key not in reserved_metadata_keys
            }
        )
    # Get complete settings snapshot for this research
    try:
        from ...settings import SettingsManager

        with get_user_db_session(username) as db_session:
            # Ensure any pending changes are committed
            try:
                db_session.commit()
            except Exception:
                db_session.rollback()
            settings_manager = SettingsManager(db_session, owns_session=False)
            # Get all current settings as a snapshot (bypass cache to ensure fresh data)
            all_settings = settings_manager.get_all_settings(
                bypass_cache=True,
                include_environment_overrides=False,
            )
            # Apply per-research egress policy overrides (form-supplied
            # values override saved settings JUST FOR THIS RUN; they do
            # not persist to the user's settings DB).
            _apply_policy_overrides(all_settings, params)
            overlay_runtime_settings(
                all_settings,
                search_engine=search_engine,
                model_provider=model_provider,
                model=model,
                custom_endpoint=custom_endpoint,
                iterations=iterations,
                questions_per_iteration=questions_per_iteration,
                strategy=strategy,
                max_results=max_results,
                time_period=time_period,
            )

            # Add settings snapshot to metadata
            research_settings["settings_snapshot"] = all_settings
            logger.info(
                f"Captured {len(all_settings)} settings for research {research_id}"
            )
            # As above, release the cached session's read transaction before
            # taking the second admission gate for the durable slot claim.
            db_session.rollback()
    except Exception:
        logger.exception("Failed to capture settings snapshot")
        # Cannot continue without settings snapshot for thread-based research
        return JSONResponse(
            {
                "status": "error",
                "message": "Failed to capture settings for research. Please try again.",
            },
            status_code=500,
        )

    # Maintain the process-wide order used by queue replay and rekey/logout:
    # admission gate before database/session work. The narrower gate below is
    # deliberately retained and re-enters this RLock to document the exact
    # count -> claim section.
    admission_lock = get_user_research_start_lock(username)
    hold_admission_gate_until_spawn = False
    admission_lock.acquire()
    try:
        with get_user_db_session(username) as db_session:
            # Determine initial status based on whether we need to queue
            initial_status = (
                ResearchStatus.QUEUED
                if should_queue
                else ResearchStatus.IN_PROGRESS
            )

            research = ResearchHistory(
                id=research_id,  # Set UUID as primary key
                query=query,
                mode=mode,
                status=initial_status,
                created_at=created_at,
                progress_log=[{"time": created_at, "progress": 0}],
                research_meta=research_settings,
            )
            db_session.add(research)
            db_session.commit()
            logger.info(
                f"Created research entry with UUID: {research_id}, status: {initial_status}"
            )

            if should_queue:
                return _queue_research(
                    db_session,
                    username,
                    research_id,
                    query,
                    mode,
                    research_settings,
                    params,
                    session_id,
                )
            # Atomically claim a user slot against queue replay. A queue tick
            # may have started work while this request captured its settings;
            # inserting and re-counting under the shared gate makes one side
            # demote before either can exceed the user's cap.
            with user_research_start_gate(username):
                import threading

                active_record = UserActiveResearch(
                    username=username,
                    research_id=research_id,
                    status=ResearchStatus.IN_PROGRESS,
                    thread_id=str(threading.current_thread().ident),
                    settings_snapshot=research_settings,
                )
                db_session.add(active_record)
                db_session.commit()
                logger.info(
                    f"Created active research record for user {username}"
                )

                # Double-check after committing to catch starts that landed
                # between the preliminary count and this guarded claim.
                try:
                    final_count = (
                        db_session.query(UserActiveResearch)
                        .filter_by(
                            username=username,
                            status=ResearchStatus.IN_PROGRESS,
                        )
                        .count()
                    )
                    logger.info(
                        f"Final active count after commit: "
                        f"{final_count}/{max_concurrent_researches}"
                    )

                    if final_count > max_concurrent_researches:
                        logger.warning(
                            f"Race condition detected: {final_count} > "
                            f"{max_concurrent_researches}, moving to queue"
                        )
                        db_session.delete(active_record)
                        db_session.commit()

                        return _queue_research(
                            db_session,
                            username,
                            research_id,
                            query,
                            mode,
                            research_settings,
                            params,
                            session_id,
                            reason="due to concurrent limit",
                            research=research,
                        )
                except Exception:
                    logger.warning("Could not recheck active count")

        # The durable claim is not live until start_research_process registers
        # its worker in the in-memory research registry. Queue replay uses that
        # registry to distinguish live rows from stale ones, so releasing the
        # shared gate here would let it reclaim this brand-new claim as FAILED
        # before the worker exists. Keep the gate through registration (and
        # through any spawn-failure cleanup) below.
        hold_admission_gate_until_spawn = True

    except Exception:
        logger.exception("Failed to create research entry")
        return JSONResponse(
            {"status": "error", "message": "Failed to create research entry"},
            status_code=500,
        )
    finally:
        # Queued and failed claims return from the section above and must not
        # retain the gate. A successful direct claim transfers responsibility
        # for releasing it to the prepare/spawn try-finally below.
        if not hold_admission_gate_until_spawn:
            admission_lock.release()

    # Only start the research if not queued
    if not should_queue:
        try:
            # Save the research strategy to the database before starting the
            # thread. This remains inside the admission gate so no queue tick
            # can reclaim the durable claim before registration.
            try:
                from ..services.research_service import save_research_strategy

                save_research_strategy(research_id, strategy, username=username)
            except Exception:
                logger.warning("Could not save research strategy")

            # Debug logging for settings snapshot
            snapshot_data = research_settings.get("settings_snapshot", {})
            log_settings(
                snapshot_data, "Settings snapshot being passed to thread"
            )
            if "search.tool" in snapshot_data:
                logger.debug(
                    f"search.tool in snapshot: {snapshot_data['search.tool']}"
                )
            else:
                logger.debug("search.tool NOT in snapshot")

            # Start the research process with the selected parameters.
            # If the spawn raises, the UserActiveResearch + IN_PROGRESS
            # ResearchHistory rows persisted above would otherwise be
            # permanently orphaned (no thread, no cleanup path). Catch any
            # exception, mark the research FAILED, delete the active row,
            # and return 500 — same contract as the queue processor's
            # terminal-failure branch introduced in #3481.
            research_thread = start_research_process(
                research_id,
                query,
                mode,
                run_research_process,
                username=username,  # Pass username to the thread
                user_password=user_password,  # Pass password for database access
                model_provider=model_provider,
                model=model,
                custom_endpoint=custom_endpoint,
                search_engine=search_engine,
                max_results=max_results,
                time_period=time_period,
                iterations=iterations,
                questions_per_iteration=questions_per_iteration,
                strategy=strategy,
                settings_snapshot=snapshot_data,  # Pass complete settings
            )
        except DuplicateResearchError:
            # A live thread already owns this research_id. Do NOT delete
            # the UserActiveResearch row or mark ResearchHistory FAILED —
            # that state belongs to the live thread, and mutating it
            # would terminate a running research from the user's
            # perspective while it keeps executing. Same contract as the
            # queue processor's dedicated dup branch (#3506).
            logger.warning(
                f"Duplicate live thread detected for {research_id} "
                "on direct submission; leaving state intact"
            )
            return JSONResponse(
                content={
                    "status": "error",
                    "message": "Research is already running.",
                },
                status_code=409,
            )
        except SystemAtCapacityError:
            # System at concurrent-research capacity. Roll back the rows
            # committed above (UserActiveResearch + IN_PROGRESS history)
            # and return 429 so the client can retry shortly.
            logger.warning(
                f"SystemAtCapacityError on direct submission for "
                f"{research_id}; rolling back orphan rows"
            )
            try:
                with get_user_db_session(username) as cleanup_session:
                    stale_active = (
                        cleanup_session.query(UserActiveResearch)
                        .filter_by(username=username, research_id=research_id)
                        .first()
                    )
                    if stale_active:
                        cleanup_session.delete(stale_active)
                    cleanup_session.query(ResearchHistory).filter_by(
                        id=research_id
                    ).delete()
                    cleanup_session.commit()
            except Exception:
                logger.exception(
                    "Cleanup after SystemAtCapacityError raised; "
                    "leaving orphan rows for the reconciler"
                )
            return JSONResponse(
                content={
                    "status": "error",
                    "message": "Server is at research capacity. Please retry shortly.",
                },
                status_code=429,
            )
        except Exception:
            logger.exception(
                f"Failed to prepare or spawn research thread for {research_id}"
            )
            try:
                with get_user_db_session(username) as cleanup_session:
                    stale_active = (
                        cleanup_session.query(UserActiveResearch)
                        .filter_by(username=username, research_id=research_id)
                        .first()
                    )
                    if stale_active:
                        cleanup_session.delete(stale_active)
                    research_row = (
                        cleanup_session.query(ResearchHistory)
                        .filter_by(id=research_id)
                        .first()
                    )
                    if research_row:
                        research_row.status = ResearchStatus.FAILED
                    cleanup_session.commit()
            except Exception:
                logger.exception(
                    "Cleanup after spawn failure raised; leaving "
                    "orphan rows for the reconciler to handle"
                )
            return JSONResponse(
                content={
                    "status": "error",
                    "message": "Failed to start research. Please try again.",
                },
                status_code=500,
            )
        finally:
            # start_research_process registers the worker before returning.
            # On failure, the exception handlers above finish durable cleanup
            # before this runs. Either way, queue replay can now safely inspect
            # the claim after acquiring the shared gate.
            admission_lock.release()

        # Update the active research record with the actual thread ID.
        try:
            with get_user_db_session(username) as thread_session:
                active_record = (
                    thread_session.query(UserActiveResearch)
                    .filter_by(username=username, research_id=research_id)
                    .first()
                )
                if active_record:
                    active_record.thread_id = str(research_thread.ident)
                    thread_session.commit()
        except Exception:
            logger.warning("Could not update thread ID")

    return {"status": "success", "research_id": research_id}


@router.post("/api/terminate/{research_id}")
def terminate_research(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Terminate an in-progress research process"""

    # Check if the research exists and is in progress
    try:
        with get_user_db_session(username) as db_session:
            research = (
                db_session.query(ResearchHistory)
                .filter_by(id=research_id)
                .first()
            )

            if not research:
                return _research_not_found(research_id)

            status = research.status

            # If it's already in a terminal state, return success
            if status in (
                ResearchStatus.COMPLETED,
                ResearchStatus.SUSPENDED,
                ResearchStatus.FAILED,
                ResearchStatus.ERROR,
            ):
                return {
                    "status": "success",
                    "message": f"Research already {status}",
                    "research_status": status,
                }

            # Check if it's in the active_research dict
            if not is_research_active(research_id):
                # The worker may not be registered in _active_research yet: a
                # just-submitted research commits its IN_PROGRESS row before the
                # worker thread registers itself (spawn-grace window). Set the
                # termination flag anyway so a worker that starts right after
                # this still sees it and aborts at its first checkpoint —
                # otherwise the user's Stop is silently ignored and the research
                # runs to completion (overwriting this SUSPENDED status). The
                # flag is harmless if no worker ever starts.
                set_termination_flag(research_id)
                if status == ResearchStatus.QUEUED:
                    claimed_queue_row = (
                        db_session.query(QueuedResearch.id)
                        .filter(
                            QueuedResearch.research_id == research_id,
                            QueuedResearch.is_processing.is_(True),
                        )
                        .exists()
                    )
                    suspended = (
                        db_session.query(ResearchHistory)
                        .filter(
                            ResearchHistory.id == research_id,
                            ResearchHistory.status == ResearchStatus.QUEUED,
                            ~claimed_queue_row,
                        )
                        .update(
                            {ResearchHistory.status: ResearchStatus.SUSPENDED},
                            synchronize_session=False,
                        )
                    )
                    if suspended:
                        from ..queue.lifecycle_cleanup import (
                            cleanup_queued_research_state,
                        )

                        cleanup_queued_research_state(
                            db_session, [research_id], acting_user=username
                        )
                else:
                    research.status = ResearchStatus.SUSPENDED
                db_session.commit()
                return {"status": "success", "message": "Research terminated"}

            # Set the termination flag
            set_termination_flag(research_id)

            # Log the termination request - using UTC timestamp
            timestamp = datetime.now(UTC).isoformat()
            termination_message = "Research termination requested by user"
            current_progress = get_research_field(research_id, "progress", 0)

            # Create log entry
            log_entry = {
                "time": timestamp,
                "message": termination_message,
                "progress": current_progress,
                "metadata": {"phase": "termination"},
            }

            # Add to in-memory log
            append_research_log(research_id, log_entry)

            # Add to database log
            logger.log("MILESTONE", f"Research ended: {termination_message}")

            # Update the log in the database
            if research.progress_log:
                try:
                    if isinstance(research.progress_log, str):
                        current_log = json.loads(research.progress_log)
                    else:
                        current_log = research.progress_log
                except Exception:
                    current_log = []
            else:
                current_log = []

            # The column is a log LIST, but a JSON column accepts any shape
            # and the reader hands one back verbatim: a legacy TEXT scalar
            # ('true') parses to a bool here, and a value the driver already
            # decoded arrives as an int. Appending to either raises
            # AttributeError outside the try above — one malformed scalar
            # would turn a Stop request into a 500. A non-list is not a log;
            # start a fresh one instead of failing the request.
            if not isinstance(current_log, list):
                logger.warning(
                    "Ignoring a non-list progress_log for research {}",
                    research_id,
                )
                current_log = []

            # Assign a new value so SQLAlchemy sees the JSON column change.
            research.progress_log = [*current_log, log_entry]
            research.status = ResearchStatus.SUSPENDED
            db_session.commit()

            # Emit a socket event for the termination request. Must be
            # scoped to subscribers of this research_id — a roomless emit
            # broadcasts the user's SUSPENDED status to every connected
            # client on the default namespace.
            try:
                event_data = {
                    "status": ResearchStatus.SUSPENDED,
                    "message": "Research was suspended by user request",
                }

                from ..services.socketio_asgi import emit_to_subscribers

                emit_to_subscribers(
                    "research_progress",
                    research_id,
                    event_data,
                    owner=username,
                )

            except Exception:
                logger.exception("Socket emit error (non-critical)")

            return {
                "status": "success",
                "message": "Research termination requested",
            }

    except Exception:
        logger.exception("Error terminating research")
        return JSONResponse(
            {"status": "error", "message": "Failed to terminate research"},
            status_code=500,
        )


def _unlink_report_file_if_inside_root(report_path: Optional[str]) -> bool:
    """Delete a stored report file, but only when it is a regular file
    inside the research reports root.

    The path arrives from the ``research_history.report_path`` column, so it
    is treated as untrusted: a corrupted row, or a future buggy writer, could
    point it anywhere on the filesystem.

    Three properties matter:

    * Symlinks are refused outright. Resolving first and unlinking the
      resolved path deletes the *target*, so an in-root symlink would delete
      a file elsewhere and leave a dangling link behind; the library deletion
      path refuses symlinks for the same reason.
    * Containment is decided on the resolved location, which also catches an
      escape through a symlinked *ancestor* directory -- one already swapped in
      before the check. Both sides are resolved with resolve_allow_missing(),
      so a path that cannot be fully resolved (a symlink chain whose resolved
      form outgrows PATH_MAX, a loop) is refused rather than compared
      half-resolved. An ancestor swapped in between the resolve and the
      unlink() can still redirect the unlink, since the unlink re-traverses
      the path; that residual race needs local write access to the reports
      tree mid-request and is outside this guard's threat model.
    * The unlink targets the path as given -- never a resolved copy -- and
      tolerates a missing file, so there is no separate
      exists()-before-unlink() window.

    The refused path is not echoed, mirroring config/paths.py.

    Returns True when a file was removed.
    """
    if not report_path:
        return False
    try:
        from ...config.paths import get_research_outputs_directory
        from ...security.path_validator import resolve_allow_missing

        reports_root = resolve_allow_missing(get_research_outputs_directory())
        path = Path(report_path)

        if path.is_symlink():
            logger.warning(
                "Refusing to unlink a symlinked report path; "
                "the link target may be outside the reports root"
            )
            return False

        try:
            contained = resolve_allow_missing(path).is_relative_to(reports_root)
        except OSError:
            contained = False
        if not contained:
            logger.warning(
                "Refusing to unlink a report path outside the reports root"
            )
            return False

        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Leaf symlinks are refused and the fully resolved path must be inside the reports root before unlink
        path.unlink()
        return True
    except FileNotFoundError:
        # Already gone; nothing to remove.
        return False
    except Exception:
        logger.exception("Error removing report file")
        return False


@router.delete("/api/delete/{research_id}")
def delete_research(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Delete a research record"""

    def delete_once():
        with get_user_db_session(username) as db_session:
            research = (
                db_session.query(ResearchHistory)
                .filter_by(id=research_id)
                .first()
            )

            if not research:
                return _research_not_found(research_id)

            status = research.status
            report_path = research.report_path

            # Atomic guard, part 1: refuse the obvious "row is
            # IN_PROGRESS" case before opening the helper. The helper
            # repeats this check *inside* the parent DELETE so a row
            # that becomes IN_PROGRESS between here and the DELETE
            # wins the race (the same #5074 invariant the old
            # inline-WHERE shape preserved). The two checks together
            # guarantee we never silently delete a row a worker owns.
            if status == ResearchStatus.IN_PROGRESS:
                return JSONResponse(
                    {
                        "status": "error",
                        "message": "Cannot delete research that is in progress",
                    },
                    status_code=400,
                )

            # Atomic guard, part 2: the claimed-queue predicate. Again,
            # the helper repeats this inside the parent DELETE; the
            # pre-check exists only to fast-path the 400 so we don't
            # pay for the cascade on a row we already know is claimed.
            is_claimed = db_session.query(
                db_session.query(QueuedResearch.id)
                .filter(
                    QueuedResearch.research_id == research_id,
                    QueuedResearch.is_processing.is_(True),
                )
                .exists()
            ).scalar()
            if is_claimed:
                return JSONResponse(
                    {
                        "status": "error",
                        "message": "Cannot delete research that is in progress",
                    },
                    status_code=400,
                )

            # Application-level cascade with the #5074 predicates
            # baked into the parent DELETE (see
            # ``_delete_research_with_dependents``).
            rag_targets = collect_research_document_rag_targets(
                db_session, research_id
            )
            deleted = _delete_research_with_dependents(db_session, research_id)
            if not deleted:
                # A concurrent request claimed the row between our
                # pre-check and the helper's parent DELETE — the
                # #5074 invariant holds (the row still exists, the
                # worker is in charge). Surface the same 400 the
                # pre-check would have emitted.
                if db_session.get(ResearchHistory, research_id) is not None:
                    return JSONResponse(
                        {
                            "status": "error",
                            "message": (
                                "Cannot delete research that is in progress"
                            ),
                        },
                        status_code=400,
                    )
                # Concurrent delete instead: the row is already gone,
                # honour the user's intent.
                db_session.commit()
                return {"status": "success"}

            # ``user_active_researches`` is not a CASCADE child of
            # ``research_history.id`` (see ``chat/service.py``'s parallel
            # cleanup), so the helper above does not touch it. Drop any
            # leftover per-user counter row here so deleting a research
            # doesn't leave the slot counted as occupied forever.
            db_session.query(UserActiveResearch).filter(
                UserActiveResearch.research_id == research_id,
            ).delete(synchronize_session=False)

            from ..queue.lifecycle_cleanup import cleanup_queued_research_state

            cleanup_queued_research_state(
                db_session, [research_id], acting_user=username
            )
            db_session.commit()

        # Files cannot roll back with the transaction. Remove the report only
        # after its database deletion commits, retaining the containment guard.
        _unlink_report_file_if_inside_root(report_path)
        # The chunk rows must outlive the parent transaction: vector deletion
        # resolves FAISS IDs from them. Purge only after the guarded delete
        # committed, never after a refusal or rollback.
        if rag_targets:
            from ...research_library.deletion.services.document_deletion import (
                DocumentDeletionService,
            )

            DocumentDeletionService(username).purge_deleted_research_documents(
                rag_targets
            )
        return {"status": "success"}

    try:
        # A concurrent writer can invalidate the cascade's read snapshot.
        # The failed context rolls back; each bounded retry re-reads the row,
        # queue claim and shared references in a new transaction.
        return retry_on_db_lock(delete_once)
    except Exception:
        logger.exception("Error deleting research")
        return JSONResponse(
            {"status": "error", "message": "Failed to delete research"},
            status_code=500,
        )


@router.post("/api/clear_history")
def clear_history(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Clear all research history"""

    def clear_once():
        with get_user_db_session(username) as db_session:
            # Get all research records first to clean up files. Select
            # only id + report_path (the columns the cleanup loop uses)
            # so clearing history doesn't load every report body into
            # memory (#4560).
            research_records = db_session.query(
                ResearchHistory.id,
                ResearchHistory.report_path,
                ResearchHistory.status,
            ).all()

            # Get IDs of currently active research (snapshot)
            protected_ids = set(get_active_research_ids())
            protected_ids.update(
                research.id
                for research in research_records
                if research.status == ResearchStatus.IN_PROGRESS
            )
            deletable_ids = [
                research.id
                for research in research_records
                if research.id not in protected_ids
            ]

            # One scan of research_resources for the whole set instead of
            # one per tracker-owning research. Building the index starts
            # a real DB transaction shared with all deletes. A concurrent
            # WAL write makes a stale snapshot fail closed on mutation;
            # retrying the request builds a fresh index.
            shared_downloads = SharedDownloadIndex.build(
                db_session, deletable_ids
            )
            deleted_ids = []
            rag_targets = []
            for research_id in deletable_ids:
                # The #5074 atomic guard is applied INSIDE the helper's
                # parent DELETE; a row whose queue entry was claimed
                # after this snapshot was taken is observed by SQL and
                # preserved. The pre-check below is purely a fast-path
                # so we don't pay for the cascade on a row we already
                # know is claimed.
                is_claimed = db_session.query(
                    db_session.query(QueuedResearch.id)
                    .filter(
                        QueuedResearch.research_id == research_id,
                        QueuedResearch.is_processing.is_(True),
                    )
                    .exists()
                ).scalar()
                if is_claimed:
                    continue
                # Application-level cascade with the IN_PROGRESS and
                # claimed-queue predicates baked into the parent
                # DELETE; see ``_delete_research_with_dependents``.
                document_targets = collect_research_document_rag_targets(
                    db_session, research_id
                )
                if _delete_research_with_dependents(
                    db_session, research_id, shared_downloads=shared_downloads
                ):
                    deleted_ids.append(research_id)
                    rag_targets.extend(document_targets)

            if deleted_ids:
                # The cascade has no FK path to this per-user counter table.
                # Remove only rows whose guarded parent DELETE succeeded;
                # protected or raced-to-active research keeps its slot.
                db_session.query(UserActiveResearch).filter(
                    UserActiveResearch.research_id.in_(deleted_ids)
                ).delete(synchronize_session=False)

                from ..queue.lifecycle_cleanup import (
                    cleanup_queued_research_state,
                )

                cleanup_queued_research_state(
                    db_session, deleted_ids, acting_user=username
                )

            db_session.commit()

        for research in research_records:
            if research.id in deleted_ids:
                _unlink_report_file_if_inside_root(research.report_path)
        if rag_targets:
            from ...research_library.deletion.services.document_deletion import (
                DocumentDeletionService,
            )

            DocumentDeletionService(username).purge_deleted_research_documents(
                rag_targets
            )
        return {"status": "success"}

    try:
        # Retry the whole batch so its protection snapshot and shared-download
        # index are rebuilt after rollback; never reuse a partially applied plan.
        return retry_on_db_lock(clear_once)
    except Exception:
        logger.exception("Error clearing history")
        return JSONResponse(
            {"status": "error", "message": "Failed to process request"},
            status_code=500,
        )


@router.post("/open_file_location")
def open_file_location(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Open a file location in the system file explorer.

    Security: This endpoint is disabled for server deployments.
    It only makes sense for desktop usage where the server and client are on the same machine.
    """
    return JSONResponse(
        {
            "status": "error",
            "message": "This feature is disabled. It is only available in desktop mode.",
        },
        status_code=403,
    )


# save_raw_config refuses a configuration with any key whose lowercased name
# contains one of these: such keys name a Python module or class to import.
_BLOCKED_KEY_PATTERNS = ("module_path", "class_name", "module", "class")

# Its refusal lists at most this many protected keys, each path cut to at
# most this many characters, so the response and its log line stay small
# however many protected keys a configuration has and however long the names
# above them are. One protected key is enough to refuse the save, so a cut
# list changes only what is listed.
_MAX_REPORTED_BLOCKED_KEYS = 100
_MAX_BLOCKED_KEY_PATH_CHARS = 200


def _blocked_key_path(parts: list[str | int]) -> str:
    """Render *parts* as ``a.b[0].c``, cut to _MAX_BLOCKED_KEY_PATH_CHARS.

    Names join with dots and array indexes go in brackets, as
    save_raw_config has always reported them. Rendering stops once the
    path passes the limit, so a long name is never copied whole, and a cut
    path ends with an ellipsis.
    """
    limit = _MAX_BLOCKED_KEY_PATH_CHARS
    pieces: list[str] = []
    size = 0
    for part in parts:
        if isinstance(part, int):
            # Not f"[{part}]": Semgrep's sql-string-concatenation rule
            # reports that assignment as SQL built from input.
            piece = "".join(("[", str(part), "]"))
        elif size:
            piece = "." + part[: limit + 1]
        else:
            piece = part[: limit + 1]
        pieces.append(piece)
        size += len(piece)
        if size > limit:
            break
    path = "".join(pieces)
    if len(path) > limit:
        path = path[: limit - 1] + "\N{HORIZONTAL ELLIPSIS}"
    return path


def _find_blocked_keys(config: dict) -> tuple[list[str], bool]:
    """Return the paths of the protected keys in a parsed configuration.

    Every table, inline table and array is searched, depth first in the
    parsed configuration's key order, for a key whose lowercased name
    contains one of _BLOCKED_KEY_PATTERNS. The second value is True when
    there are more than _MAX_REPORTED_BLOCKED_KEYS of them: the walk stops
    at the first one past that limit, so the list holds the first
    _MAX_REPORTED_BLOCKED_KEYS it found.

    A path is rendered only for a protected key, by _blocked_key_path, so
    the walk's work does not grow with the length of the names above one.
    The walk keeps an explicit stack instead of recursing: a dotted key in
    an inline table nests up to 32 tables for a few frames of tomllib's own
    recursion, so a configuration within the parsing limits can nest deeper
    than Python's recursion limit.
    """
    found: list[str] = []
    parts: list[str | int] = []
    stack = [iter(config.items())]
    while stack:
        entry = next(stack[-1], None)
        if entry is None:
            stack.pop()
            if stack:
                parts.pop()
            continue
        name, value = entry
        parts.append(name)
        if isinstance(name, str):
            lowered = name.lower()
            if any(pattern in lowered for pattern in _BLOCKED_KEY_PATTERNS):
                if len(found) == _MAX_REPORTED_BLOCKED_KEYS:
                    return found, True
                found.append(_blocked_key_path(parts))
        if isinstance(value, dict):
            stack.append(iter(value.items()))
        elif isinstance(value, list):
            stack.append(enumerate(value))
        else:
            parts.pop()
    return found, False


@router.post("/api/save_raw_config")
async def save_raw_config(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Save raw configuration"""
    # Same media-type check as start_research: main's
    # @require_json_body(error_format="success") refused a non-JSON media
    # type with this 400 before the config file could be written.
    if not is_json_request(request):
        return json_body_error("success", "Request body must be valid JSON")
    data, body_error = await read_json_dict(
        request, "success", "Request body must be valid JSON"
    )
    if body_error is not None:
        return body_error
    raw_config = data.get("raw_config")

    if not raw_config:
        return JSONResponse(
            {"success": False, "error": "Raw configuration is required"},
            status_code=400,
        )

    # Security: Parse and validate the TOML to block dangerous keys
    from ...security import toml_limits

    try:
        # Bounded: the body may be 16 MB (fastapi_app.py's default JSON body
        # limit), and tomllib's cost for one long dotted key grows with the
        # square of its parts. loads_toml_within_limits refuses text over
        # 512 KiB, or with a key or table header of more than 32 dotted parts,
        # before tomllib runs (security/toml_limits.py).
        #
        # Offloaded: within those limits the scan and the parse are still
        # pure-Python CPU work. On the event loop, with the single-worker
        # deployment this ships as, that would stall EVERY request for the
        # duration, for any authenticated user. asyncio.to_thread rather than
        # run_db_sync: this opens no DB session, and run_db_sync's docstring
        # says to prefer to_thread for purely CPU-bound work. The GIL is
        # released periodically during the parse, so the loop interleaves
        # instead of stopping dead.
        #
        # Note this, and the protected-key walk below, run BEFORE the
        # system.allow_config_write gate (enforced inside write_file_verified
        # below), so they are reachable even on deployments that have config
        # writing turned off. Offloading and the limits are what make that
        # ordering safe; they are not an argument for keeping it.
        parsed_config = await asyncio.to_thread(
            toml_limits.loads_toml_within_limits, raw_config
        )
    except toml_limits.TOMLLimitError:
        logger.warning("Refused TOML configuration over the parsing limits")
        return JSONResponse(
            {
                "success": False,
                "error": (
                    "Configuration exceeds the parsing limits: at most "
                    f"{toml_limits.MAX_TOML_BYTES // 1024} KiB, and at most "
                    f"{toml_limits.MAX_TOML_KEY_PARTS} dotted parts in any "
                    "key or table header."
                ),
            },
            status_code=400,
        )
    except Exception:
        logger.warning("Invalid TOML configuration")
        # Don't expose internal exception details to users (CWE-209)
        return JSONResponse(
            {
                "success": False,
                "error": "Invalid TOML syntax. Please check your configuration format.",
            },
            status_code=400,
        )

    # Security: Check for dangerous keys that could enable code execution.
    # Offloaded like the parse: the walk is pure-Python work that grows with
    # the size of the parsed configuration.
    blocked_keys, more_blocked_keys = await asyncio.to_thread(
        _find_blocked_keys, parsed_config
    )
    if blocked_keys or more_blocked_keys:
        logger.warning(
            "Security: Blocked attempt to write config with dangerous keys: {}",
            _log_value_preview(blocked_keys),
        )
        error = "Configuration contains protected keys that cannot be modified"
        if more_blocked_keys:
            error += f"; only the first {len(blocked_keys)} are listed"
        return JSONResponse(
            {
                "success": False,
                "error": error,
                "blocked_keys": blocked_keys,
                "blocked_keys_truncated": more_blocked_keys,
            },
            status_code=403,
        )

    try:
        from ...security.file_write_verifier import write_file_verified

        # Get the config file path (uses centralized path config, respects LDR_DATA_DIR)
        config_dir = get_config_directory()
        config_path = config_dir / "config.toml"

        # Write the configuration to file. Threadpooled: the verifier
        # resolves the allow-flag setting (may open the user's DB session)
        # and does sync disk IO — neither belongs on the event loop.
        await run_db_sync(
            write_file_verified,
            config_path,
            raw_config,
            "system.allow_config_write",
            context="system configuration file",
        )

        return {"success": True}
    except Exception:
        logger.exception("Error saving configuration file")
        return JSONResponse(
            {"success": False, "error": "Failed to process request"},
            status_code=500,
        )


@router.get("/api/history")
def get_history(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Get research history.

    NOTE: Returns a raw list of items. The newer canonical endpoint
    ``GET /history/api`` (in routers/history.py) returns the wrapped
    shape ``{"status": "success", "items": [...]}`` — JS callers
    should prefer that one. Kept here because Puppeteer tests still
    target this path; consolidating to a single endpoint with a
    consistent shape is tracked as a follow-up cleanup.
    """

    # Bound the result set. Without a limit this endpoint loaded every
    # research row (and its research_meta JSON, which can hold a settings
    # snapshot) into memory at once (#4560). Mirrors the clamp used by the
    # symmetric /history/api endpoint.
    try:
        limit = int(request.query_params.get("limit", 200))
    except (TypeError, ValueError):
        limit = 200
    limit = max(1, min(limit, 500))
    try:
        offset = parse_bounded_offset(request.query_params.get("offset"))
    except ValueError:
        return JSONResponse({"error": OFFSET_LIMIT_ERROR}, status_code=400)

    try:
        with get_user_db_session(username) as db_session:
            # Query research history ordered by created_at. Project
            # only the metadata columns the loop below consumes — never
            # the large ``report_content`` Text body — so this listing
            # doesn't pull every report into memory (#4560). This mirrors
            # the projection used by the symmetric /history/api endpoint.
            research_records = (
                db_session.query(
                    ResearchHistory.id,
                    ResearchHistory.title,
                    ResearchHistory.query,
                    ResearchHistory.mode,
                    ResearchHistory.status,
                    ResearchHistory.created_at,
                    ResearchHistory.completed_at,
                    ResearchHistory.research_meta,
                    ResearchHistory.chat_session_id,
                )
                .order_by(ResearchHistory.created_at.desc())
                .limit(limit)
                .offset(offset)
                .all()
            )

            # Pre-compute Document counts in a single GROUP BY query
            # to avoid an N+1 SELECT-COUNT-per-row inside the loop. The
            # symmetric /history/api endpoint in web/routers/history.py
            # already uses an outerjoin + group_by — this brings
            # /api/history to parity for users with deep history.
            research_ids = [r.id for r in research_records]
            if research_ids:
                doc_count_rows = (
                    db_session.query(
                        Document.research_id, func.count(Document.id)
                    )
                    .filter(Document.research_id.in_(research_ids))
                    .group_by(Document.research_id)
                    .all()
                )
                doc_counts = dict(doc_count_rows)
            else:
                doc_counts = {}

            # Build history items while session is active to avoid
            # DetachedInstanceError on ORM attribute access
            history_items = []
            for research in research_records:
                # Calculate duration if completed
                duration_seconds = None
                if research.completed_at and research.created_at:
                    try:
                        duration_seconds = calculate_duration(
                            research.created_at, research.completed_at
                        )
                    except Exception:
                        logger.exception("Error calculating duration")

                # Look up the pre-computed document count.
                doc_count = doc_counts.get(research.id, 0)

                # Create a history item
                item = {
                    "id": research.id,
                    "query": research.query,
                    "mode": research.mode,
                    "status": research.status,
                    "created_at": research.created_at,
                    "completed_at": research.completed_at,
                    "duration_seconds": duration_seconds,
                    "metadata": filter_research_metadata(
                        research.research_meta
                    ),
                    "document_count": doc_count,
                }
                if research.chat_session_id is not None:
                    item["metadata"]["chat_session_id"] = (
                        research.chat_session_id
                    )

                # Add title if it exists
                if hasattr(research, "title") and research.title is not None:
                    item["title"] = research.title

                history_items.append(item)

        return {"status": "success", "items": history_items}
    except Exception:
        logger.exception("Error getting history")
        return JSONResponse(
            {"status": "error", "message": "Failed to process request"},
            status_code=500,
        )


@router.get("/api/research/{research_id}")
def get_research_details(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get full details of a research using ORM"""

    try:
        with get_user_db_session(username) as db_session:
            research = (
                db_session.query(ResearchHistory)
                .filter(ResearchHistory.id == research_id)
                .first()
            )

            if not research:
                return _research_not_found(research_id)

            return {
                "id": research.id,
                "query": research.query,
                "status": research.status,
                "progress": research.progress,
                "progress_percentage": research.progress or 0,
                "mode": research.mode,
                "created_at": research.created_at,
                "completed_at": research.completed_at,
                "metadata": strip_settings_snapshot(research.research_meta),
            }
    except Exception:
        logger.exception("Error getting research details")
        return JSONResponse(
            {"error": "An internal error has occurred"}, status_code=500
        )


@router.get("/api/research/{research_id}/logs")
def get_research_logs(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get logs for a specific research.

    Accepts an optional ``?limit=N`` that bounds the response to ``N`` rows
    (returned oldest-first, matching the default ordering) and is
    clamped to ``[1, HISTORY_LOGS_HARD_CAP]`` so a client cannot force an
    unbounded load — a long langgraph run can persist thousands of rows. When
    ``?limit`` is absent or not a valid integer (``type=int`` yields
    ``None``) the historical contract is preserved: every row is returned. The
    frontend log panel always sends a valid limit and
    ``priority=diagnostic``. That mode does a best-effort newest-first
    selection prioritizing errors/CRITICAL/FATAL, then warnings, then
    milestones/SUCCESS, before filling the remaining window with routine rows.
    If the number of higher-priority rows exceeds the limit, the oldest
    diagnostics in those categories and all lower-priority/routine rows are
    dropped.
    Direct API callers retain newest-N behavior unless they opt in.

    Each ``timestamp`` is a raw ``datetime`` that FastAPI's encoder
    serializes as ISO 8601, matching the streaming ``/logs/export``
    endpoint. (Under Flask this endpoint emitted RFC 822 HTTP-dates via
    ``jsonify`` and the two wire formats disagreed; the migration to
    FastAPI aligned them.)
    """

    _limit_raw = request.query_params.get("limit")
    try:
        limit = int(_limit_raw) if _limit_raw is not None else None
    except (TypeError, ValueError):
        limit = None
    if limit is not None:
        limit = max(1, min(limit, HISTORY_LOGS_HARD_CAP))
    prioritize_diagnostics = (
        request.query_params.get("priority") == "diagnostic"
    )

    try:
        # First check if the research exists
        with get_user_db_session(username) as db_session:
            research = (
                db_session.query(ResearchHistory)
                .filter_by(id=research_id)
                .first()
            )
            if not research:
                return _research_not_found(research_id)

            # Get logs from research_logs table
            log_query = db_session.query(ResearchLog).filter_by(
                research_id=research_id
            )
            if limit is None:
                log_results = log_query.order_by(
                    ResearchLog.timestamp, ResearchLog.id
                ).all()
            elif prioritize_diagnostics:
                normalized_level = func.lower(ResearchLog.level)
                diagnostic_priority = case(
                    (
                        normalized_level.in_(("error", "critical", "fatal")),
                        0,
                    ),
                    (normalized_level == "warning", 1),
                    (
                        normalized_level.in_(("milestone", "success")),
                        2,
                    ),
                    else_=3,
                )
                log_results = (
                    log_query.order_by(
                        diagnostic_priority,
                        ResearchLog.timestamp.desc(),
                        ResearchLog.id.desc(),
                    )
                    .limit(limit)
                    .all()
                )
                log_results.sort(key=lambda row: (row.timestamp, row.id))
            else:
                # Take the newest ``limit`` rows at the SQL layer, then flip
                # back to oldest-first so the response ordering is unchanged.
                # ``id`` is the tie-break: timestamps are not unique, so without
                # it the rows that survive ``.limit()`` at a shared-timestamp
                # boundary would be SQL-undefined.
                log_results = list(
                    reversed(
                        log_query.order_by(
                            ResearchLog.timestamp.desc(),
                            ResearchLog.id.desc(),
                        )
                        .limit(limit)
                        .all()
                    )
                )

            # Extract log attributes while session is active
            # to avoid DetachedInstanceError on ORM attribute access
            logs = []
            for row in log_results:
                logs.append(
                    {
                        "id": row.id,
                        "message": row.message,
                        "timestamp": row.timestamp,
                        "log_type": row.level,
                    }
                )

        return logs

    except Exception:
        logger.exception("Error getting research logs")
        return JSONResponse(
            {"error": "An internal error has occurred"}, status_code=500
        )


def _log_export_exempt(request: Request) -> bool:
    """Keep HEAD pre-flights outside the GET export quota.

    Port of main's ``_is_log_export_rate_limit_exempt`` (#5369). Main's
    version read Flask's ``request`` global, which does not exist here;
    slowapi inspects the callable's signature and passes the Starlette
    ``Request`` when it declares exactly one parameter (see
    ``slowapi/wrappers.py``'s ``_exempt_when_takes_request``), so the
    method is read off the live request instead.

    Still needed after the migration: Starlette answers HEAD on a route
    registered with ``@router.get``, so a browser or proxy pre-flight
    would otherwise burn a slot of the caller's 10/min export budget
    without transferring a byte.
    """
    return request.method == "HEAD" or _api_exempt()


_log_export_limit = limiter.shared_limit(
    "10 per minute",
    scope="log_export",
    key_func=_api_user_key,
    exempt_when=_log_export_exempt,
)


# GET *and* HEAD. The log panel's "Download Logs" button issues a HEAD
# pre-flight (static/js/components/logpanel.js:1854) so the browser never
# saves a JSON error body as a .jsonl file, and it bails out on a non-ok
# response before creating the download anchor. Under Flask that worked
# for free: werkzeug adds HEAD to any rule that serves GET
# (routing/rules.py: `if "HEAD" not in methods and "GET" in methods`).
# FastAPI takes `methods` literally, so a bare @router.get answers HEAD
# with 405 -- the pre-flight fails, the function returns early, and the
# export silently never starts. Serving both verbs restores the Flask
# behaviour; Starlette runs the handler and discards the body for HEAD,
# exactly as werkzeug did.
# Registered as two routes rather than one api_route(methods=["GET","HEAD"]):
# a single route with both verbs makes FastAPI emit two OpenAPI operations
# that share one generated operationId, which
# test_route_contracts.py::test_in_process_openapi_covers_schema_included_routes
# correctly rejects as a duplicate. GET stays the documented operation; the
# HEAD twin is include_in_schema=False because it is an implementation detail
# of the pre-flight, exactly as werkzeug's auto-added HEAD never appeared in
# any schema either.
@router.head("/api/research/{research_id}/logs/export", include_in_schema=False)
@router.get("/api/research/{research_id}/logs/export")
@_log_export_limit
def export_research_logs(
    request: Request,
    research_id: str,
    username: Annotated[str, Depends(require_auth)],
):
    """Stream every persisted log row for a research as newline-delimited JSON.

    This endpoint deliberately bypasses ``HISTORY_LOGS_HARD_CAP``: a long
    langgraph run can persist tens of thousands of rows and the on-screen
    log panel rightly caps at ``?limit=5000`` to bound DOM and parsing
    memory. A *download*, by contrast, is short-lived — the browser's
    download manager streams the response body straight to disk — so the
    cap does not protect anything here and only hides data.

    Memory layout for very large exports:

    * **Server**: ``get_user_db_session`` opens a fresh session inside
      the generator so it survives until the response finishes flushing;
      ``.yield_per(500)`` pulls rows in 500-row batches from the SQLite
      cursor so the full set is never resident in process memory.
    * **Wire**: each yielded row is a single ``\\n``-terminated JSON
      line, so the server flushes incrementally and the browser receives
      chunks as they arrive.
    * **Client**: ``Content-Disposition: attachment`` plus the
      ``log-download-button`` JS trigger a native browser download, so
      the response body streams to disk without first being buffered
      into a JS Blob.

    The NDJSON format is the standard streaming interchange for
    line-oriented records. Each line emits ``id``, ``timestamp``, ``message``,
    ``level`` (log level), ``log_type`` (aligned with ``/logs`` endpoint),
    ``module``, and ``line_no``. Consumers can ``for line in file: json.loads(line)``
    without parsing a wrapper array. ``timestamp`` is serialized explicitly
    with ``datetime.isoformat()`` (ISO 8601), which matches how FastAPI's
    encoder renders the datetimes returned by the sibling ``/logs`` endpoint.
    """
    # Verify the research exists (and belongs to this user, since
    # ``get_user_db_session(username)`` scopes queries to the user's
    # encrypted DB). Failing fast here avoids opening a streaming session
    # for a 404.
    try:
        with get_user_db_session(username) as db_session:
            research = (
                db_session.query(ResearchHistory)
                .filter_by(id=research_id)
                .first()
            )
            if not research:
                return _research_not_found(research_id)
    except Exception:
        logger.exception("Error verifying research for log export")
        return JSONResponse(
            {"error": "An internal error has occurred"}, status_code=500
        )

    def generate():
        # NEVER hold a DB session across a yield: Starlette iterates sync
        # generators via anyio's threadpool, so each next() can land on a
        # different OS thread and a session entered on one thread would be
        # exited on another (see download_bulk for the thread-affinity
        # rationale). Snapshot the ordered id list in one short-lived
        # session, then hydrate rows in per-batch sessions, serializing
        # each batch fully BEFORE yielding it.
        yielded_count = 0
        try:
            with get_user_db_session(username) as db_session:
                ordered_ids = [
                    row_id
                    for (row_id,) in db_session.query(ResearchLog.id)
                    .filter_by(research_id=research_id)
                    .order_by(ResearchLog.timestamp.asc(), ResearchLog.id.asc())
                    .all()
                ]

            batch_size = 500
            for start in range(0, len(ordered_ids), batch_size):
                batch_ids = ordered_ids[start : start + batch_size]
                lines = []
                with get_user_db_session(username) as db_session:
                    rows = (
                        db_session.query(ResearchLog)
                        .filter(ResearchLog.id.in_(batch_ids))
                        .all()
                    )
                    by_id = {row.id: row for row in rows}
                    # Serialize inside the session (ORM attribute access
                    # may lazy-load); emit in snapshot order so the
                    # timestamp-then-id ordering is preserved exactly.
                    for row_id in batch_ids:
                        row = by_id.get(row_id)
                        if row is None:
                            continue
                        yielded_count += 1
                        lines.append(
                            json.dumps(
                                {
                                    "id": row.id,
                                    "timestamp": row.timestamp.isoformat()
                                    if row.timestamp is not None
                                    else None,
                                    "message": row.message,
                                    "level": row.level,
                                    "log_type": row.level,
                                    "module": row.module,
                                    "line_no": row.line_no,
                                },
                                default=str,
                            )
                            + "\n"
                        )
                yield "".join(lines)
        except Exception:
            # If the DB blows up mid-stream we have already sent a 200 +
            # partial body, so we can't recover with a JSON error here.
            # Log and let the iterator end; the client will see a
            # truncated file, which is the best we can do without
            # buffering the whole result.
            logger.exception(
                f"Error streaming logs for export (research_id={research_id}, yielded={yielded_count})"
            )

    safe_id = re.sub(r"[^a-zA-Z0-9_-]", "", research_id)
    filename = f"research_logs_{safe_id or 'export'}.jsonl"
    return WorkerCleanupStreamingResponse(
        generate(),
        media_type="application/x-ndjson",
        headers={
            "Content-Disposition": f'attachment; filename="{filename}"',
            # Disable caching: this is a one-shot download, and a stale
            # partial body from a previous run could otherwise confuse a
            # retry after a mid-stream failure.
            "Cache-Control": "no-store",
            "X-Content-Type-Options": "nosniff",
        },
    )


@router.get("/api/report/{research_id}")
def get_research_report(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get the research report content"""

    try:
        with get_user_db_session(username) as db_session:
            # Query using ORM
            research = (
                db_session.query(ResearchHistory)
                .filter_by(id=research_id)
                .first()
            )

            if research is None:
                return _research_not_found(research_id)

            # Parse metadata if it exists
            metadata = research.research_meta

            # research.report_content holds the answer-only string;
            # rebuild the legacy display shape (answer + Sources from
            # research_resources + Metrics from research_meta) on demand.
            from ..services.report_assembly_service import (
                assemble_full_report,
                get_research_source_links_batch,
            )

            content = assemble_full_report(research, db_session)
            # Only None means "research not found" — guarded above.
            # Empty-but-found rows return "" and are valid responses.
            if content is None:
                return _research_not_found(
                    research_id, message="Report not found"
                )

            # Sources live in the research_resources table, not research_meta.
            # The post-refactor save path never writes the legacy
            # `all_links_of_system` metadata key, so reading it here returned
            # [] for every research created since chat-mode-v2 (#3665). Read
            # the structured table instead — the same source of truth the
            # assembled `content` and the news feed already use. limit=None
            # returns every source (this field was never top-N), matching the
            # full list the assembled `content` renders for the same research.
            sources = get_research_source_links_batch(
                [research.id], db_session, limit=None
            ).get(research.id, [])

            # Return the report data with backwards-compatible fields
            # Examples expect 'summary', 'sources', 'findings' at top level
            safe_metadata = strip_settings_snapshot(metadata)
            return {
                "content": content,
                # Backwards-compatible fields for examples
                "summary": content,  # The markdown report is the summary
                "sources": sources,
                "findings": safe_metadata.get("findings", []),
                "metadata": {
                    "title": research.title if research.title else None,
                    "query": research.query,
                    "mode": research.mode if research.mode else None,
                    "created_at": research.created_at
                    if research.created_at
                    else None,
                    "completed_at": research.completed_at
                    if research.completed_at
                    else None,
                    **safe_metadata,
                },
            }

    except Exception:
        logger.exception("Error getting research report")
        return JSONResponse(
            {"error": "An internal error has occurred"}, status_code=500
        )


@router.post("/api/v1/research/{research_id}/export/{format}")
def export_research_report(
    request: Request,
    research_id,
    format,
    username: Annotated[str, Depends(require_auth)],
):
    """Export research report to different formats (LaTeX, Quarto, RIS, PDF, ODT, etc.)"""
    try:
        # Use the exporter registry to validate format
        from ...exporters import ExporterRegistry

        if not ExporterRegistry.is_format_supported(format):
            available = ExporterRegistry.get_available_formats()
            return JSONResponse(
                {
                    "error": f"Invalid format. Available formats: {', '.join(available)}"
                },
                status_code=400,
            )

        # Get research from database

        try:
            with get_user_db_session(username) as db_session:
                research = (
                    db_session.query(ResearchHistory)
                    .filter_by(id=research_id)
                    .first()
                )
                if not research:
                    return _research_not_found(research_id)

                # Build the full assembled report (answer + Sources +
                # Metrics) so exporters get the same shape they did
                # before the report_content refactor.
                from ..services.report_assembly_service import (
                    assemble_full_report,
                )

                report_content = assemble_full_report(research, db_session)
                if report_content is None:
                    return _research_not_found(
                        research_id, message="Report content not found"
                    )

                # Export to requested format (all in memory)
                try:
                    # Use title or query for the PDF title
                    pdf_title = research.title or research.query

                    # Generate export content in memory
                    export_content, filename, mimetype = (
                        export_report_to_memory(
                            report_content, format, title=pdf_title
                        )
                    )

                    # Send the file directly from memory.
                    #
                    # A plain ``Response``, not ``StreamingResponse``:
                    # ``export_content`` is an in-memory bytes object that is
                    # already fully materialised, and iterating a ``BytesIO``
                    # yields one chunk per ``0x0A`` byte — for binary PDF/ODT
                    # payloads that is thousands of tiny ASGI sends, each a
                    # threadpool dispatch, and it also suppresses
                    # ``Content-Length`` so the browser gets no download
                    # progress. Same fix already applied to library.py's PDF
                    # route; this is its sibling call site.
                    from fastapi.responses import Response

                    return Response(
                        content=export_content,
                        media_type=mimetype,
                        headers={
                            "Content-Disposition": build_content_disposition(
                                filename
                            ),
                        },
                    )
                except MissingPDFDependencyError:
                    logger.exception(
                        "PDF export failed: WeasyPrint unavailable"
                    )
                    return JSONResponse(
                        {"error": get_weasyprint_install_instructions()},
                        status_code=500,
                    )
                except Exception:
                    logger.exception("Error exporting report")
                    return JSONResponse(
                        {
                            "error": f"Failed to export to {format}. Please try again later."
                        },
                        status_code=500,
                    )

        except Exception:
            logger.exception("Error in export endpoint")
            return JSONResponse(
                {"error": "An internal error has occurred"}, status_code=500
            )

    except Exception:
        logger.exception("Unexpected error in export endpoint")
        return JSONResponse(
            {"error": "An internal error has occurred"}, status_code=500
        )


@router.get("/api/research/{research_id}/status")
@limiter.exempt
def get_research_status(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get the status of a research process"""

    try:
        with get_user_db_session(username) as db_session:
            research = (
                db_session.query(ResearchHistory)
                .filter_by(id=research_id)
                .first()
            )

            if research is None:
                return _research_not_found(research_id)

            status = research.status
            progress = research.progress
            completed_at = research.completed_at
            metadata = _parse_research_metadata(research.research_meta)
            filtered_metadata = strip_settings_snapshot(metadata)

            # Extract and format error information for better UI display
            error_info = {}
            if metadata.get("error"):
                error_msg = str(metadata["error"])
                known_message = get_known_research_error_message(error_msg)
                error_type = "unknown"

                # Detect specific error types
                if "timeout" in error_msg.lower():
                    error_type = "timeout"
                    error_info = {
                        "type": "timeout",
                        "message": "LLM service timed out during synthesis. This may be due to high server load or connectivity issues.",
                        "suggestion": "Try again later or use a smaller query scope.",
                    }
                elif (
                    "token limit" in error_msg.lower()
                    or "context length" in error_msg.lower()
                ):
                    error_type = "token_limit"
                    error_info = {
                        "type": "token_limit",
                        "message": "The research query exceeded the AI model's token limit during synthesis.",
                        "suggestion": "Try using a more specific query or reduce the research scope.",
                    }
                elif (
                    "final answer synthesis fail" in error_msg.lower()
                    or "llm error" in error_msg.lower()
                ):
                    error_type = "llm_error"
                    error_info = {
                        "type": "llm_error",
                        "message": "The AI model encountered an error during final answer synthesis.",
                        "suggestion": "Check that your LLM service is running correctly or try a different model.",
                    }
                elif "ollama" in error_msg.lower():
                    error_type = "ollama_error"
                    error_info = {
                        "type": "ollama_error",
                        "message": "The Ollama service is not responding properly.",
                        "suggestion": "Make sure Ollama is running with 'ollama serve' and the model is downloaded.",
                    }
                elif "connection" in error_msg.lower():
                    error_type = "connection"
                    error_info = {
                        "type": "connection",
                        "message": "Connection error with the AI service.",
                        "suggestion": "Check your internet connection and AI service status.",
                    }
                elif metadata.get("solution"):
                    # Use the solution provided in metadata if available
                    error_info = {
                        "type": error_type,
                        "message": filtered_metadata["error"],
                        "suggestion": str(metadata.get("solution")),
                    }
                else:
                    # Legacy rows can contain str(exception), including
                    # absolute report paths. The stored detail stays private.
                    error_info = {
                        "type": error_type,
                        "message": filtered_metadata["error"],
                        "suggestion": "Try again with a different query or check the application logs.",
                    }

                # The polling UI displays message alone. Preserve the worker's
                # exact authored classification instead of replacing e.g. a
                # missing-model message with a generic Ollama-service error.
                if known_message is not None:
                    error_info["message"] = known_message
                    if metadata.get("solution"):
                        error_info["suggestion"] = str(metadata["solution"])

            # Get the latest milestone log for this research
            latest_milestone = None
            try:
                milestone_log = (
                    db_session.query(ResearchLog)
                    .filter_by(research_id=research_id, level="MILESTONE")
                    # id tie-breaks equal timestamps so "latest" is
                    # deterministic (the most recently inserted milestone).
                    .order_by(
                        ResearchLog.timestamp.desc(), ResearchLog.id.desc()
                    )
                    .first()
                )
                if milestone_log:
                    latest_milestone = {
                        "message": milestone_log.message,
                        "time": milestone_log.timestamp.isoformat()
                        if milestone_log.timestamp
                        else None,
                        "type": "MILESTONE",
                    }
                    logger.debug(
                        f"Found latest milestone for research {research_id}: {milestone_log.message}"
                    )
                else:
                    logger.debug(
                        f"No milestone logs found for research {research_id}"
                    )
            except Exception:
                logger.warning("Error fetching latest milestone")

            if error_info:
                filtered_metadata["error_info"] = error_info

            response_data = {
                "status": status,
                "progress": progress,
                "completed_at": completed_at,
                "metadata": filtered_metadata,
            }

            # Include latest milestone as a log_entry for frontend compatibility
            if latest_milestone:
                response_data["log_entry"] = latest_milestone

            return response_data
    except Exception:
        logger.exception("Error getting research status")
        return JSONResponse(
            {"error": "Error checking research status"}, status_code=500
        )


@router.get("/api/queue/status")
def get_queue_status(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Get the current queue status for the user"""

    from ..queue import QueueManager

    try:
        queue_items = QueueManager.get_user_queue(username)

        return {
            "status": "success",
            "queue": queue_items,
            "total": len(queue_items),
        }
    except Exception:
        logger.exception("Error getting queue status")
        return JSONResponse(
            {"status": "error", "message": "Failed to process request"},
            status_code=500,
        )


@router.get("/api/queue/{research_id}/position")
def get_queue_position(
    request: Request,
    research_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Get the queue position for a specific research"""

    from ..queue import QueueManager

    try:
        position = QueueManager.get_queue_position(username, research_id)

        if position is None:
            return _research_not_found(
                research_id, message="Research not found in queue"
            )

        return {"status": "success", "position": position}
    except Exception:
        logger.exception("Error getting queue position")
        return JSONResponse(
            {"status": "error", "message": "Failed to process request"},
            status_code=500,
        )


@router.get("/api/config/limits")
def get_upload_limits(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Get file upload configuration limits.

    Returns the backend's authoritative limits for file uploads,
    allowing the frontend to stay in sync without hardcoding values.

    Static constants only, but auth-gated to match the pre-migration
    Flask route (@login_required) — no endpoint enumeration for free.
    """
    return {
        "max_file_size": FileUploadValidator.MAX_FILE_SIZE,
        "max_files": FileUploadValidator.MAX_FILES_PER_REQUEST,
        "allowed_mime_types": list(FileUploadValidator.ALLOWED_MIME_TYPES),
    }


@router.post("/api/upload/pdf")
@upload_rate_limit_user
@upload_rate_limit_ip
async def upload_pdf(
    request: Request,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Upload and extract text from PDF files with comprehensive security validation.

    Security features:
    - Rate limiting per user and per IP (``security.rate_limit_upload_user``
      and ``security.rate_limit_upload_ip``)
    - File size validation (``FileUploadValidator.MAX_FILE_SIZE``,
      configurable via ``security.upload_max_file_size_mb`` /
      ``LDR_SECURITY_UPLOAD_MAX_FILE_SIZE_MB``)
    - File count validation (``FileUploadValidator.MAX_FILES_PER_REQUEST``)
    - PDF structure validation
    - MIME type validation

    Performance improvements:
    - Single-pass PDF processing (text + metadata)
    - Optimized extraction service
    """
    # Starlette's form parser yields STARLETTE UploadFile instances;
    # fastapi.UploadFile is a SUBCLASS of it on fastapi>=0.113, so
    # `isinstance(f, fastapi.UploadFile)` is False for every real
    # upload and the filter below silently dropped all files
    # (every upload 400'd with "No files provided").
    from starlette.datastructures import UploadFile

    try:
        # Early request size validation (before reading any files)
        # This prevents memory exhaustion from chunked encoding attacks
        max_request_size = (
            FileUploadValidator.MAX_FILES_PER_REQUEST
            * FileUploadValidator.MAX_FILE_SIZE
        )
        content_length = request.headers.get("content-length")
        if content_length and int(content_length) > max_request_size:
            return JSONResponse(
                {
                    "error": f"Request too large. Maximum size is {max_request_size // (1024 * 1024)}MB"
                },
                status_code=413,
            )

        # Parse multipart form data — FastAPI's UploadFile parameter binding
        # doesn't work cleanly with the `files: list` shape we want, so we
        # parse the form manually and pull out everything named "files".
        #
        # Nothing in the signature asks FastAPI for the body, so this is
        # the first parse of the request, and leaving the block closes
        # every spooled upload whether the loop below reached it or not
        # (early returns, rejected names, parts under other field names).
        # FastAPI's form extraction used to register that cleanup.
        async with request.form() as form:
            files: list[UploadFile] = [
                f for f in form.getlist("files") if isinstance(f, UploadFile)
            ]

            if not files:
                return JSONResponse(
                    {"error": "No files provided"}, status_code=400
                )
            if all(not f.filename for f in files):
                return JSONResponse(
                    {"error": "No files selected"}, status_code=400
                )

            # Validate file count
            is_valid, error_msg = FileUploadValidator.validate_file_count(
                len(files)
            )
            if not is_valid:
                return JSONResponse({"error": error_msg}, status_code=400)

            # Get PDF extraction service
            pdf_service = get_pdf_extraction_service()

            extracted_texts = []
            total_files = len(files)
            processed_files = 0
            errors = []

            for file in files:
                if not file or not file.filename:
                    errors.append("Unnamed file: Skipped")
                    continue

                try:
                    filename = sanitize_filename(
                        file.filename, allowed_extensions={".pdf"}
                    )
                except UnsafeFilenameError:
                    errors.append(
                        "Rejected file: invalid or disallowed filename"
                    )
                    continue

                try:
                    # Per-file size guard BEFORE reading the spool into
                    # RAM (GHSA-38fh finding 2): the request-level
                    # precheck keys off Content-Length, which chunked
                    # multipart bodies do not carry, so waiting for the
                    # post-read validator would read the whole part into
                    # RAM before rejecting it. Mirrors rag.py's pre-read
                    # guard. On the pinned Starlette range (1.3.x),
                    # file.size is the byte count request.form() actually
                    # wrote to the part's disk-backed SpooledTemporaryFile
                    # (it starts at 0 and grows per chunk), not a
                    # client-declared value, so it is never None here.
                    # The post-read validator's size check below is
                    # therefore only defence in depth for a size of None,
                    # which this Starlette range never produces.
                    # request.form() has already spooled the whole part
                    # to disk by this point; the guard prevents only the
                    # read into RAM. A part under the cap is still read
                    # fully into RAM, by the cap's design (unchanged by
                    # this guard).
                    size_ok, size_err = FileUploadValidator.validate_file_size(
                        file.size, None
                    )
                    if not size_ok:
                        errors.append(f"{filename}: {size_err}")
                        continue

                    # Read file content (UploadFile spools large bodies to disk)
                    pdf_content = await file.read()

                    # Validation + PDF text extraction are CPU-bound (up to
                    # FileUploadValidator.MAX_FILE_SIZE per file ×
                    # MAX_FILES_PER_REQUEST files) — run them in the
                    # threadpool so they don't freeze the event loop (and
                    # with it every other request and WebSocket) for the
                    # duration of the parse. No DB session is opened here,
                    # so a plain to_thread (not run_db_sync) is correct.
                    def _validate_and_extract(
                        content=pdf_content, name=filename
                    ):
                        ok, err = FileUploadValidator.validate_upload(
                            filename=name,
                            file_content=content,
                            content_length=file.size,
                        )
                        if not ok:
                            return None, err
                        return (
                            pdf_service.extract_text_and_metadata(
                                content, name
                            ),
                            None,
                        )

                    result, error_msg = await asyncio.to_thread(
                        _validate_and_extract
                    )

                    if result is None:
                        errors.append(f"{filename}: {error_msg}")
                        continue

                    if result["success"]:
                        extracted_texts.append(
                            {
                                "filename": result["filename"],
                                "text": result["text"],
                                "size": result["size"],
                                "pages": result["pages"],
                                "truncated": result.get("truncated", False),
                            }
                        )
                        processed_files += 1
                    else:
                        errors.append(f"{filename}: {result['error']}")

                except Exception:
                    logger.exception(f"Error processing {filename}")
                    errors.append(f"{filename}: Error processing file")
                finally:
                    # Close the file stream to release resources
                    try:
                        await file.close()
                    except Exception:
                        logger.debug(
                            "best-effort file stream close", exc_info=True
                        )

            # Prepare response
            response_data = {
                "status": "success",
                "processed_files": processed_files,
                "total_files": total_files,
                "extracted_texts": extracted_texts,
                "combined_text": "\n\n".join(
                    [
                        f"--- From {item['filename']} ---\n"
                        + (
                            "[Partial PDF extraction: only part of this "
                            "document's text is included.]\n"
                            if item["truncated"]
                            else ""
                        )
                        + item["text"]
                        for item in extracted_texts
                    ]
                ),
                "errors": errors,
            }

            if processed_files == 0:
                return JSONResponse(
                    {
                        "status": "error",
                        "message": "No files were processed successfully",
                        "errors": errors,
                    },
                    status_code=400,
                )

            return response_data

    except FormParserError as e:
        # python-multipart could not parse the body (garbage where the
        # boundary should be, a malformed part header, an over-long
        # boundary): the client's fault, and the 400 FastAPI's form
        # handling answered before this handler parsed the form itself.
        # Only the parser's own errors are relabelled; anything else that
        # fails while reading the form (an OSError spooling an upload,
        # say) is the server's and goes to the generic handler below,
        # which logs it.
        raise HTTPException(
            status_code=400, detail="There was an error parsing the body"
        ) from e
    except StarletteHTTPException:
        # Starlette's own multipart limits (missing boundary parameter,
        # oversized field) already arrive as a 400: let it through instead
        # of turning it into the generic 500 below.
        raise
    except Exception:
        logger.exception("Error processing PDF upload")
        return JSONResponse(
            {"error": "Failed to process PDF files"}, status_code=500
        )