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.

28.2 KB
Raw Blame
"""
Flask routes for follow-up research functionality.
"""

from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse
from ..dependencies.auth import require_auth
from ..dependencies.threadpool import run_db_sync

from loguru import logger

from ...constants import ResearchStatus
from ...exceptions import DuplicateResearchError, SystemAtCapacityError
from ...llm.providers.base import normalize_provider
from ...followup_research.service import FollowUpResearchService
from ...followup_research.models import FollowUpRequest
from ...utilities.url_utils import is_safe_custom_llm_endpoint
from .notes import _log_value_preview

from ..auth.password_utils import resolve_user_password
from typing import Annotated

# Create the router
router = APIRouter(prefix="/api/followup", tags=["followup"])

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


@router.post("/prepare")
async def prepare_followup(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Prepare a follow-up research by loading parent context.

    Request body:
    {
        "parent_research_id": "uuid",
        "question": "follow-up question"
    }

    Returns:
    {
        "success": true,
        "parent_summary": "...",
        "available_sources": 10,
        "suggested_strategy": "source-based"
    }
    """
    try:
        try:
            data = await request.json()
        except ValueError:
            # Match start_followup: a malformed body is a client error, not
            # a 500 with a logged stack trace.
            return JSONResponse(
                {"success": False, "error": "Request body must be valid JSON"},
                status_code=400,
            )
        if not isinstance(data, dict):
            # Flask's @require_json_body returned 400 for any non-dict body.
            # Without this, a VALID but non-object body (e.g. `[1,2]` or a
            # bare string) parses fine and then `data.get(...)` raises
            # AttributeError, which the outer handler turns into a 500 plus a
            # logged stack trace. The sibling routers kept the gate
            # (chat._json_object_body, notes._notes_json_body); follow-up is
            # the one that lost it.
            return JSONResponse(
                {
                    "success": False,
                    "error": "Request body must be a JSON object",
                },
                status_code=400,
            )
        parent_id = data.get("parent_research_id")
        question = data.get("question")

        if not parent_id or not question:
            return JSONResponse(
                {
                    "success": False,
                    "error": "Missing parent_research_id or question",
                },
                status_code=400,
            )

        from ...settings.manager import SettingsManager
        from ...database.session_context import get_user_db_session

        def _load_sync():
            """Sync work: settings snapshot + parent research load."""
            with get_user_db_session(username) as db_session:
                settings_manager = SettingsManager(db_session=db_session)
                settings_snapshot = settings_manager.get_all_settings()

            strategy_from_settings = settings_snapshot.get(
                "search.search_strategy", {}
            ).get("value", "source-based")

            service = FollowUpResearchService(username=username)
            parent_data = service.load_parent_research(parent_id)
            return strategy_from_settings, parent_data

        strategy_from_settings, parent_data = await run_db_sync(_load_sync)

        if not parent_data:
            # Parent research doesn't exist (wrong ID, deleted, or belongs
            # to another user whose DB we can't read). Return 404 so the
            # caller doesn't submit a follow-up against a ghost parent.
            # (#6938: parent_id is the raw, unchecked body value here.)
            logger.warning(
                "Parent research {} not found for user {}",
                _log_value_preview(parent_id),
                username,
            )
            return JSONResponse(
                {"success": False, "error": "Parent research not found"},
                status_code=404,
            )

        # Prepare response with parent context summary
        return {
            "success": True,
            "parent_summary": parent_data.get("query", ""),
            "available_sources": len(parent_data.get("resources", [])),
            "suggested_strategy": strategy_from_settings,  # Use strategy from settings
            "parent_research": {
                "id": parent_id,
                "query": parent_data.get("query", ""),
                "sources_count": len(parent_data.get("resources", [])),
            },
        }

    except Exception:
        logger.exception("Error preparing follow-up")
        return JSONResponse(
            {"success": False, "error": "An internal error has occurred."},
            status_code=500,
        )


@router.post("/start")
async def start_followup(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Start a follow-up research.

    Request body:
    {
        "parent_research_id": "uuid",
        "question": "follow-up question",
        "strategy": "source-based",  # optional
        "max_iterations": 1,  # optional
        "questions_per_iteration": 3  # optional
    }

    Returns:
    {
        "success": true,
        "research_id": "new-uuid",
        "message": "Follow-up research started"
    }
    """
    # Guard the body parse: the try/except lives in _start_followup_sync, so
    # without this a malformed body escapes this route entirely instead of
    # returning the 400 the Flask original (and prepare_followup) returns.
    try:
        data = await request.json()
    except ValueError:
        return JSONResponse(
            {"success": False, "error": "Request body must be valid JSON"},
            status_code=400,
        )
    if not isinstance(data, dict):
        # See the identical guard in prepare_followup above.
        return JSONResponse(
            {"success": False, "error": "Request body must be a JSON object"},
            status_code=400,
        )
    return await run_db_sync(_start_followup_sync, data, username)


def _start_followup_sync(data, username):
    admission_lock = None
    try:
        from ..services.research_service import (
            start_research_process,
            run_research_process,
            clamp_user_max_concurrent,
        )
        from ..routes.globals import reclaim_stale_user_active_research
        from ..research_state import get_user_research_start_lock
        from ...database.models import UserActiveResearch
        import threading
        import uuid

        # Get username from session

        # Get settings snapshot first to use database values
        from ...settings.manager import SettingsManager
        from ...database.session_context import get_user_db_session

        with get_user_db_session(username) as db_session:
            settings_manager = SettingsManager(db_session=db_session)
            settings_snapshot = settings_manager.get_all_settings()

        # Get strategy from settings snapshot, fallback to source-based if not set
        strategy_from_settings = settings_snapshot.get(
            "search.search_strategy", {}
        ).get("value", "source-based")

        # Get iterations and questions from settings snapshot
        iterations_from_settings = settings_snapshot.get(
            "search.iterations", {}
        ).get("value", 1)
        questions_from_settings = settings_snapshot.get(
            "search.questions_per_iteration", {}
        ).get("value", 3)

        # Create follow-up request using settings values
        followup_request = FollowUpRequest(
            parent_research_id=data.get("parent_research_id"),
            question=data.get("question"),
            strategy=strategy_from_settings,  # Use strategy from settings
            max_iterations=iterations_from_settings,  # Use iterations from settings
            questions_per_iteration=questions_from_settings,  # Use questions from settings
        )

        # Initialize service
        service = FollowUpResearchService(username=username)

        # Resolve the user's password (needed for metrics DB access later) and
        # decide authentication FIRST. An expired encrypted-DB session is a 401,
        # and it must be settled BEFORE the parent-ownership check below so an
        # unauthenticated caller sees "session expired" (401), not the
        # authorization outcome "parent not found" (404). Auth precedes authz.
        user_password, session_expired = resolve_user_password(username)
        if session_expired:
            # success/error keys match the followup API convention (the
            # followup frontend checks data.success and data.error).
            return JSONResponse(
                {
                    "success": False,
                    "error": "Your session has expired. Please log out and log back in to start research.",
                },
                status_code=401,
            )

        # Reject a follow-up naming a parent research the caller does not own,
        # mirroring /api/followup/prepare's 404 contract. Research ids are
        # per-user (a different physical encrypted DB per user), so a parent id
        # that isn't in the caller's DB is not theirs; without this a user could
        # spawn a follow-up referencing another user's research_id (the parent
        # context comes back empty, but a research thread would still start).
        parent_id = data.get("parent_research_id")
        if not parent_id or not service.load_parent_research(parent_id):
            return JSONResponse(
                {"success": False, "error": "Parent research not found"},
                status_code=404,
            )

        # Prepare research parameters
        research_params = service.perform_followup(followup_request)

        # (#6938: the query is raw request input and a wide params dict has
        # unbounded keys, so both go through the bounded preview instead of
        # eager f-strings.)
        params_keys = (
            list(research_params.keys())
            if isinstance(research_params, dict)
            else "Not a dict"
        )
        query_value = (
            research_params.get("query")
            if isinstance(research_params, dict)
            else "N/A"
        )
        logger.info("Research params type: {}", type(research_params))
        logger.info("Research params keys: {}", _log_value_preview(params_keys))
        logger.info("Query value: {}", _log_value_preview(query_value))
        logger.info("Query type: {}", type(query_value))

        # (user_password / session-expired 401 are resolved above, before the
        # parent-ownership check, so auth precedes authz.)

        # Pre-flight: refuse to spawn a research thread (and create an
        # orphan ResearchHistory row) when llm.model is empty. Mirrors the
        # empty-model check in web/routers/research.py's start_research —
        # same contract: HTTP 400 with an actionable message before any DB
        # writes or thread spawning. (This router uses success/error
        # response keys rather than status/message, matching the followup
        # API convention used by the other returns in this function.)
        if not settings_snapshot.get("llm.model", {}).get("value"):
            logger.error(
                "Follow-up research blocked: llm.model is not configured"
            )
            return JSONResponse(
                {
                    "success": False,
                    "error": "Model is required. Please configure a model in the settings.",
                },
                status_code=400,
            )

        # SSRF pre-flight on the LLM endpoint: reject metadata / link-local
        # targets at the request boundary, before any DB row is written.
        # 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 and localhost pass because local LLMs
        # (Ollama / LM Studio / vLLM) live there, including scheme-less
        # endpoints (the helper normalizes exactly as the provider does).
        custom_endpoint = settings_snapshot.get(
            "llm.openai_endpoint.url", {}
        ).get("value")
        if not is_safe_custom_llm_endpoint(custom_endpoint):
            return JSONResponse(
                {
                    "success": False,
                    "error": "Invalid custom endpoint URL",
                },
                status_code=400,
            )

        # Per-user concurrency admission. Follow-ups previously enforced NO
        # per-user cap: only the global research semaphore gated them, so a
        # single authenticated user could fire many rapid
        # /api/followup/start calls and monopolize the entire global
        # research budget, starving other tenants with 429s. Route the
        # follow-up through the SAME admission path
        # research_routes.start_research uses -- reclaim dead-thread rows,
        # count the user's live researches, and reject at the per-user cap.
        # A UserActiveResearch row is created below (alongside the
        # ResearchHistory) so both entry points keep consistent accounting.
        max_concurrent_researches = clamp_user_max_concurrent(
            settings_snapshot.get("app.max_concurrent_researches", {}).get(
                "value", 3
            )
        )

        # The helpers above reuse a thread-local session and may leave a read
        # transaction open. End it before taking the process-wide lock order
        # used by direct starts and queue replay: admission gate, then DB.
        with get_user_db_session(username) as ordering_session:
            ordering_session.rollback()

        # Retain the same stable per-user gate from stale-row reconciliation
        # through durable claim, worker registration, and failure cleanup.
        # Otherwise this route can reclaim a direct start in its pre-register
        # window, or have its own new claim reclaimed by queue replay.
        pending_admission_lock = get_user_research_start_lock(username)
        pending_admission_lock.acquire()
        admission_lock = pending_admission_lock

        # Reclaim stale rows + count active. Mirrors the try/except in
        # research_routes.start_research: if the check itself fails we fall
        # back to allowing the start (the global semaphore is still a
        # backstop) rather than hard-failing the request.
        try:
            with get_user_db_session(username) as admission_session:
                if reclaim_stale_user_active_research(
                    admission_session, username, logger=logger
                ):
                    admission_session.commit()
                active_count = (
                    admission_session.query(UserActiveResearch)
                    .filter_by(
                        username=username,
                        status=ResearchStatus.IN_PROGRESS,
                    )
                    .count()
                )
            at_capacity = active_count >= max_concurrent_researches
        except Exception:
            logger.exception("Failed to check active follow-up researches")
            at_capacity = False

        if at_capacity:
            logger.warning(
                "Follow-up research rejected: user {} at per-user "
                "concurrency cap ({}/{})",
                username,
                active_count,
                max_concurrent_researches,
            )
            return JSONResponse(
                {
                    "success": False,
                    "error": "Server is at research capacity. Please retry shortly.",
                },
                status_code=429,
            )

        # Generate new research ID
        research_id = str(uuid.uuid4())

        # Create database entry (settings_snapshot already captured above)
        from ...database.models import ResearchHistory
        from datetime import datetime, UTC

        created_at = datetime.now(UTC).isoformat()

        with get_user_db_session(username) as db_session:
            # Create the database entry (required for tracking)
            research_meta = {
                "submission": {
                    "parent_research_id": data.get("parent_research_id"),
                    "question": data.get("question"),
                    "strategy": "contextual-followup",
                },
                # Persist the run's settings snapshot so view/export
                # (report_assembly_service) renders the same
                # report.uncited_sources_mode the generation-time render
                # used. Mirrors the main-form creation path; completion
                # merge back-fills when missing.
                "settings_snapshot": settings_snapshot,
            }

            research = ResearchHistory(
                id=research_id,
                query=research_params["query"],
                mode="quick",  # Use 'quick' not 'quick_summary'
                status=ResearchStatus.IN_PROGRESS,
                created_at=created_at,
                progress_log=[{"time": created_at, "progress": 0}],
                research_meta=research_meta,
            )
            db_session.add(research)

            # Record the active-research row in the SAME transaction so the
            # per-user cap accounting matches research_routes.start_research.
            # thread_id is the spawning (request) thread for now; the worker
            # thread id isn't known until start_research_process runs.
            active_record = UserActiveResearch(
                username=username,
                research_id=research_id,
                status=ResearchStatus.IN_PROGRESS,
                thread_id=str(threading.current_thread().ident),
                settings_snapshot=settings_snapshot,
            )
            db_session.add(active_record)
            db_session.commit()
            logger.info(
                f"Created follow-up research entry with ID: {research_id}"
            )

            # Post-commit race recheck. Two concurrent submissions can both
            # pass the up-front admission check before either commits its
            # row. If we are now over the cap, roll back BOTH rows and
            # reject at 429 -- follow-ups have no queue fallback, so unlike
            # research_routes.start_research (which re-queues) we simply ask
            # the client to retry.
            #
            # The DETECTION query is wrapped so that a failure to *ask* never
            # blocks a legitimate start (fail-open is right there: we do not
            # know we are over capacity). The REMEDIATION below is
            # deliberately NOT inside that handler. It used to be, and a
            # failure in the rollback -- e.g. the commit raising under
            # SQLCipher/WAL contention -- was swallowed by the same broad
            # `except`, after which control fell through and started the
            # research anyway, returning success: True for a run whose
            # tracking rows had just been deleted. Once we KNOW we are over
            # capacity, failing to clean up must not become a decision to
            # proceed.
            over_capacity = False
            try:
                final_count = (
                    db_session.query(UserActiveResearch)
                    .filter_by(
                        username=username,
                        status=ResearchStatus.IN_PROGRESS,
                    )
                    .count()
                )
                over_capacity = final_count > max_concurrent_researches
                if over_capacity:
                    logger.warning(
                        "Race detected on follow-up start for {}: "
                        "{} > {}, rolling back and rejecting",
                        username,
                        final_count,
                        max_concurrent_researches,
                    )
            except Exception:
                logger.warning("Could not recheck active follow-up count")

            if over_capacity:
                # Best-effort rollback: if it fails we still reject, and the
                # orphaned IN_PROGRESS row is reclaimed by
                # reclaim_stale_user_active_research on this user's next
                # start (its thread never spawned, so it reads as dead).
                try:
                    db_session.delete(active_record)
                    db_session.query(ResearchHistory).filter_by(
                        id=research_id
                    ).delete()
                    db_session.commit()
                except Exception:
                    from ...database.session_context import safe_rollback

                    logger.exception(
                        "Rollback of the over-capacity follow-up rows failed "
                        "for {}; still rejecting the request",
                        username,
                    )
                    safe_rollback(db_session, "followup over-capacity rollback")
                return JSONResponse(
                    {
                        "success": False,
                        "error": "Server is at research capacity. Please retry shortly.",
                    },
                    status_code=429,
                )

        # Start the research process using the existing infrastructure
        # Use quick_summary mode for follow-ups by default
        logger.info(
            f"Starting follow-up research for query of type: {type(research_params.get('query'))}"
        )

        # Get model and search settings from user's settings
        model_provider = settings_snapshot.get("llm.provider", {}).get(
            "value", "OLLAMA"
        )
        # Normalize provider to lowercase canonical form
        model_provider = normalize_provider(model_provider)
        model = settings_snapshot.get("llm.model", {}).get("value", "")
        search_engine = settings_snapshot.get("search.tool", {}).get(
            "value", "searxng"
        )

        # Spawn the research thread. If the spawn fails, the
        # ResearchHistory row committed above would otherwise be
        # permanently orphaned with status=IN_PROGRESS. Catch any
        # exception, flip the status to FAILED, and return a clear
        # error — same contract as the queue processor's terminal-
        # failure branch (#3481) and the direct-UI spawn-failure path.
        try:
            start_research_process(
                research_id,
                research_params["query"],
                "quick",  # Use 'quick' for quick summary mode
                run_research_process,
                username=username,
                user_password=user_password,  # gitleaks:allow
                model_provider=model_provider,  # Pass model provider
                model=model,  # Pass model name
                search_engine=search_engine,  # Pass search engine
                # llm.openai_endpoint.url is only the client's base URL for
                # the openai_endpoint provider; don't label other providers
                # with it.
                custom_endpoint=(
                    custom_endpoint
                    if model_provider == "openai_endpoint"
                    else None
                ),
                strategy="enhanced-contextual-followup",  # Use enhanced contextual follow-up strategy
                iterations=research_params["max_iterations"],
                questions_per_iteration=research_params[
                    "questions_per_iteration"
                ],
                delegate_strategy=research_params.get(
                    "delegate_strategy", "source-based"
                ),
                research_context=research_params["research_context"],
                parent_research_id=research_params[
                    "parent_research_id"
                ],  # Pass parent research ID
                settings_snapshot=settings_snapshot,
            )
        except DuplicateResearchError:
            # A live thread already owns this research_id. Do NOT delete
            # the row or mark it FAILED — the row belongs to the live
            # thread and mutating it would terminate the running
            # research from the user's perspective. Same contract as
            # research_routes.start_research's duplicate-thread branch.
            logger.warning(
                f"Duplicate live thread detected for follow-up "
                f"{research_id}; leaving state intact"
            )
            return JSONResponse(
                {
                    "success": False,
                    "error": "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.
            logger.warning(
                f"SystemAtCapacityError starting follow-up {research_id}"
            )
            try:
                from ...database.session_context import get_user_db_session
                from ...database.models import (
                    ResearchHistory,
                    UserActiveResearch,
                )

                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 follow-up capacity reject raised"
                )
            return JSONResponse(
                {
                    "success": False,
                    "error": "Server is at research capacity. Please retry shortly.",
                },
                status_code=429,
            )
        except Exception:
            logger.exception(
                f"Failed to spawn follow-up research thread for {research_id}"
            )
            try:
                from ...database.session_context import get_user_db_session
                from ...database.models import (
                    ResearchHistory,
                    UserActiveResearch,
                )

                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 follow-up spawn failure raised")
            return JSONResponse(
                content={
                    "success": False,
                    "error": "Failed to start follow-up research. Please try again.",
                },
                status_code=500,
            )

        return {
            "success": True,
            "research_id": research_id,
            "message": "Follow-up research started",
        }

    except Exception:
        logger.exception("Error starting follow-up")
        return JSONResponse(
            {"success": False, "error": "An internal error has occurred."},
            status_code=500,
        )
    finally:
        if admission_lock is not None:
            admission_lock.release()