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.

81.5 KB
Raw Blame
"""
News API endpoints (FastAPI ``APIRouter``, mounted at ``/news/api``).

The module name is a leftover: this code was FastAPI, was converted to a
Flask blueprint to match LDR's then-Flask architecture, and the ASGI port
converted it back. The name was kept across the port so importers and
patch targets stay stable.
"""

import asyncio

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

import uuid
from typing import Any, Annotated

from loguru import logger

from .notes import _log_value_preview

from ...llm.providers.base import normalize_provider
from ...news import api
from ...news.constants import (
    NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES,
    NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES,
    SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE,
    is_runnable_refresh_interval,
    compact_subscription_update_error,
    folder_update_error,
    normalize_compact_subscription_update,
)
from ...news.exceptions import NewsAPIException
from ...news.folder_manager import FolderManager
from ...database.models import SubscriptionFolder

from ...database.session_context import get_user_db_session
from ...settings.env_registry import get_env_setting
from ...utilities.db_utils import get_settings_manager
from ...utilities.url_utils import is_safe_custom_llm_endpoint
from ...security.log_sanitizer import redact_and_bound_for_log
from ..dependencies.json_body import json_body_error, read_json_dict

# Hard ceiling for user-supplied ``limit`` query params on the news
# endpoints in this module. Matches the ``max_value`` of the
# ``news.feed.default_limit`` setting so a direct API caller cannot request a
# larger page than the UI can configure. Shared by the feed and
# subscription-history endpoints here so their caps stay in lockstep.
NEWS_FEED_MAX_LIMIT = 100
# The service currently performs three queries per card ID. Keep the batch
# endpoint bounded to one feed page so a malformed or oversized request cannot
# amplify database work independently of the feed's own pagination ceiling.
NEWS_BATCH_FEEDBACK_MAX_CARD_IDS = NEWS_FEED_MAX_LIMIT

# Subscription scheduling and search controls arrive as untyped JSON. Keep
# their request-boundary limits in one place so the create route and both
# update routes cannot drift apart. One minute and one week are both offered
# by the current UI, so those are the inclusive refresh bounds.
# NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES / MAX_REFRESH_MINUTES live in
# ``news/constants.py`` (imported above) since
# ``normalize_compact_subscription_update`` there needs the same bounds;
# re-exported here (rather than duplicated) so existing
# ``news_flask_api.NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES`` references keep
# working.
NEWS_SUBSCRIPTION_MAX_QUERY_LENGTH = 10_000
NEWS_SUBSCRIPTION_MAX_SEARCH_ITERATIONS = 20
# The subscription form (news-subscription-form.html) advertises max="20"
# for this field, and subscriptions saved with 11-20 must stay editable, so
# the API cap cannot be lowered without a migration path (#6066).
NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION = 20


def _subscription_validation_error(message: str) -> JSONResponse:
    """Return the stable simple-error shape used by the news API."""
    return JSONResponse({"error": message}, status_code=400)


def _validate_subscription_payload(
    data: dict[str, Any],
    *,
    require_query: bool = False,
    query_field: str = "query",
    refresh_field: str = "refresh_minutes",
) -> JSONResponse | None:
    """Validate fields shared by subscription create/update payloads.

    ``bool`` is a subclass of ``int`` in Python, so the numeric checks use an
    exact type comparison. This helper intentionally runs before any DB,
    scheduler, endpoint-resolution, or service work at each call site.
    """
    has_query = query_field in data
    if require_query and not has_query:
        return _subscription_validation_error("query is required")
    if has_query:
        query = data[query_field]
        if query is None and require_query:
            return _subscription_validation_error("query is required")
        if not isinstance(query, str):
            return _subscription_validation_error("query must be a string")
        if not query.strip():
            return _subscription_validation_error("query is required")
        if len(query) > NEWS_SUBSCRIPTION_MAX_QUERY_LENGTH:
            return _subscription_validation_error(
                "query exceeds maximum length of "
                f"{NEWS_SUBSCRIPTION_MAX_QUERY_LENGTH} characters"
            )

    if "is_active" in data and not isinstance(data["is_active"], bool):
        return _subscription_validation_error("is_active must be a boolean")

    for field in ("name", "folder_id", "model_provider"):
        if (
            field in data
            and data[field] is not None
            and not isinstance(data[field], str)
        ):
            return _subscription_validation_error(
                f"{field} must be a string or null"
            )

    integer_ranges = (
        (
            refresh_field,
            NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES,
            NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES,
        ),
        ("search_iterations", 1, NEWS_SUBSCRIPTION_MAX_SEARCH_ITERATIONS),
        (
            "questions_per_iteration",
            1,
            NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION,
        ),
    )
    for field, minimum, maximum in integer_ranges:
        if field not in data:
            continue
        value = data[field]
        if type(value) is not int or not minimum <= value <= maximum:
            return _subscription_validation_error(
                f"{field} must be an integer between {minimum} and {maximum}"
            )

    return None


def require_scheduler_control(request: Request) -> None:
    """Dependency that gates global scheduler control behind a setting.

    The news scheduler is a global singleton — starting, stopping, or
    triggering it affects all users. This checks the
    ``news.scheduler.allow_api_control`` setting (env var
    ``LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL``, default ``false``) and
    raises HTTPException 403 when disabled.
    """
    if not get_env_setting("news.scheduler.allow_api_control", False):
        # Every route using this dependency declares Depends(require_auth)
        # ahead of it, and FastAPI resolves declared dependencies in order,
        # so session["username"] is guaranteed here. Index rather than
        # .get(..., "unknown"): this is an audit line for a blocked
        # privileged action, and a shared "unknown" placeholder silently
        # merges every such attempt into one unattributable entry if that
        # ordering invariant is ever broken. Failing fast surfaces the
        # broken invariant instead of quietly degrading the audit trail.
        # Ported from #5549, which made the same change on main.
        username = request.session["username"]
        remote_addr = request.client.host if request.client else "unknown"
        logger.warning(
            "Scheduler API control blocked (user={}, ip={})",
            _log_value_preview(username),
            _log_value_preview(remote_addr),
        )
        raise HTTPException(
            status_code=403,
            detail=(
                "Scheduler API control is disabled. "
                "Contact your administrator to enable it."
            ),
        )


def safe_error_message(e: Exception, context: str = "") -> str:
    """
    Return a safe error message that doesn't expose internal details.

    Args:
        e: The exception
        context: Optional context about what was being attempted

    Returns:
        A generic error message safe for external users
    """
    # Log the actual error for debugging
    logger.exception(f"Error in {context}")

    # Return generic messages based on exception type
    if isinstance(e, ValueError):
        return "Invalid input provided"
    if isinstance(e, KeyError):
        return "Required data missing"
    if isinstance(e, TypeError):
        return "Invalid data format"
    # Generic message for production
    return f"An error occurred{f' while {context}' if context else ''}"


def _is_job_owned_by_user(job, username, scheduler):
    """Check if an APScheduler job belongs to a specific user."""
    # Primary: all news scheduler jobs pass username as first arg
    if hasattr(job, "args") and job.args and job.args[0] == username:
        return True
    # Fallback: check the tracked scheduled_jobs set
    if hasattr(scheduler, "user_sessions"):
        session_info = scheduler.user_sessions.get(username, {})
        if job.id in session_info.get("scheduled_jobs", set()):
            return True
    return False


# In Flask, this Blueprint had no prefix but was nested under /news.
# In FastAPI, routers are flat, so we add the full prefix.
router = APIRouter(prefix="/news/api", tags=["news_api"])

# Shared rate limits for POST endpoints (port of main's news_routes
# Flask-Limiter shared limits — one bucket per scope).
_news_create_limit = limiter.shared_limit("10 per minute", scope="news_create")
_news_research_limit = limiter.shared_limit(
    "5 per minute", scope="news_research"
)
_news_feedback_limit = limiter.shared_limit(
    "30 per minute", scope="news_feedback"
)
_news_preferences_limit = limiter.shared_limit(
    "10 per minute", scope="news_preferences"
)
# NOTE: Routes use username (not .get()) intentionally.
# Depends(require_auth) guarantees the key exists; direct access fails
# fast if the dependency is ever removed.

# Components are initialized in api.py


def get_user_id(request=None):
    """Get current user ID from the FastAPI request session.

    Pass `request` to read it from the active session. If omitted (legacy
    callers), returns None — these routes should be using
    `Depends(require_auth)` directly instead of this helper.
    """
    if request is None:
        return None
    username = (
        request.session.get("username") if hasattr(request, "session") else None
    )
    if not username:
        # For news, we need authenticated users
        return None

    return username


def _start_research_in_process(request, request_data: dict, username: str):
    """Start a research run by calling the research router's sync entrypoint
    directly, instead of a loopback HTTP POST.

    The two news "run subscription now" / "check overdue" paths used to POST
    to ``<request.base_url>/research/api/start`` via ``safe_post``. Because
    ``request.base_url`` is derived from the client-controlled ``Host``
    header and ``safe_post`` was called with ``allow_localhost=True,
    allow_private_ips=True``, a spoofed ``Host`` turned this into an SSRF: the
    server would issue a request carrying the caller's own session cookie +
    CSRF token to an attacker-named internal host, and ``check-overdue``
    reflected the raw response body back to the client. Calling the handler
    in-process removes the network hop (and the SSRF) entirely — the same
    move main made in #4834.

    Returns the research-start result as a plain dict (``status``,
    ``research_id``, …), normalizing ``_start_research_sync``'s JSONResponse
    error returns into the same dict shape the callers expect.
    """
    import json as _json

    from .research import _start_research_sync

    # base_url is only embedded cosmetically (server_url for link
    # generation) inside _start_research_sync — it drives no network call,
    # so passing the Host-derived value here is inert (matches what the
    # direct /research/api/start endpoint passes).
    base_url = str(request.base_url).rstrip("/")
    session_id = request.session.get("session_id")

    result = _start_research_sync(request_data, username, base_url, session_id)
    if isinstance(result, JSONResponse):
        # _start_research_sync returns a JSONResponse on failure with a
        # meaningful status_code (400 validation, 409 duplicate, 429
        # capacity). Preserve it under `_http_status` so the caller can
        # propagate the real code instead of collapsing everything to 500
        # (the old loopback forwarded response.status_code).
        try:
            body = _json.loads(bytes(result.body).decode("utf-8"))
        except Exception:
            body = {"status": "error", "message": "Failed to start research"}
        body["_http_status"] = result.status_code
        return body
    if isinstance(result, dict):
        # Normalize a ResearchStatus enum status to its str value so the
        # callers' `in ("success", "queued")` check is robust.
        status = result.get("status")
        return {**result, "status": getattr(status, "value", status)}
    return {"status": "error", "message": "Failed to start research"}


async def _reject_custom_endpoint_async(custom_endpoint):
    """Off-loop wrapper for :func:`_reject_custom_endpoint`.

    SSRF validation ends in ``socket.getaddrinfo`` (see
    ``security/ssrf_validator.py``), which is a blocking OS resolver call
    with no timeout parameter — it cannot be bounded in place. Called
    directly from an ``async def`` handler it stalls the event loop, and
    the server runs single-worker, so every other in-flight request stalls
    with it. An authenticated caller supplying a hostname that resolves
    slowly or black-holes would hold the whole process for the resolver's
    full retry budget. Hand it to a worker thread instead.
    """
    return await asyncio.to_thread(_reject_custom_endpoint, custom_endpoint)


def _reject_custom_endpoint(custom_endpoint):
    """Return a 400 JSONResponse if ``custom_endpoint`` fails SSRF
    validation, else None.

    Blocking (DNS). From an ``async def`` handler call
    :func:`_reject_custom_endpoint_async` instead of this directly.

    Rejects cloud-metadata / link-local targets at the request boundary as
    fail-fast defense-in-depth (the OpenAI-compatible provider re-validates
    the same URL via assert_base_url_safe before the client is built).
    Private IPs and localhost are allowed because local LLMs live there;
    scheme-less endpoints are normalized exactly as the provider does.
    """
    if is_safe_custom_llm_endpoint(custom_endpoint):
        return None
    return JSONResponse(
        {"success": False, "error": "Invalid custom endpoint URL"},
        status_code=400,
    )


# The documented sentinel meaning "no subscription filter". Upstream
# (#5603) exempts it from the UUID check on the feed route; without
# this, ?subscription_id=all 400s even though news/api.py still
# implements the "all" branch.
NO_SUBSCRIPTION_FILTER = "all"


def _is_valid_uuid(value: str) -> bool:
    """Return True if ``value`` parses as a UUID, False otherwise.

    Used to validate subscription_id params before they reach the
    LIKE-pattern queries in ``news/api.py``. Without this check, a request
    like ``?subscription_id=%`` would expand the LIKE filter and match
    arbitrary subscriptions (enumeration vector).
    """
    try:
        uuid.UUID(str(value))
    except (ValueError, AttributeError, TypeError):
        return False
    return True


@router.get("/feed")
def get_news_feed(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """
    Get personalized news feed for user.

    Query params:
        user_id: User identifier (default: anonymous)
        limit: Maximum number of cards to return (default: 20)
        use_cache: Whether to use cached news (default: true)
        strategy: Override default recommendation strategy
        focus: Optional focus area for news
    """
    try:
        # Get current user (require_auth ensures we have one)
        user_id = username
        logger.info(f"News feed requested by user: {user_id}")

        # Get query parameters
        with get_user_db_session(username) as _db:
            settings_manager = get_settings_manager(_db, username)
            default_limit = settings_manager.get_setting(
                "news.feed.default_limit"
            )
        try:
            limit = int(request.query_params.get("limit", default_limit))
        except (TypeError, ValueError):
            limit = int(default_limit)
        limit = max(1, min(limit, NEWS_FEED_MAX_LIMIT))
        use_cache = (
            request.query_params.get("use_cache", "true").lower() == "true"
        )
        strategy = request.query_params.get("strategy")
        focus = request.query_params.get("focus")
        subscription_id = request.query_params.get("subscription_id")
        if (
            subscription_id
            and subscription_id != NO_SUBSCRIPTION_FILTER
            and not _is_valid_uuid(subscription_id)
        ):
            return JSONResponse(
                {"success": False, "error": "Invalid subscription_id"},
                status_code=400,
            )

        logger.info(
            f"News feed params: limit={limit}, subscription_id={subscription_id}, focus={_log_value_preview(focus)}"
        )

        # Call the direct API function (now synchronous)
        result = api.get_news_feed(
            user_id=user_id,
            limit=limit,
            use_cache=use_cache,
            focus=focus,
            search_strategy=strategy,
            subscription_id=subscription_id,
        )

        # Check for errors in result
        if "error" in result and result.get("news_items") == []:
            # Sanitize error message before returning to client
            safe_msg = safe_error_message(
                Exception(result["error"]), context="get_news_feed"
            )
            status = 400 if "must be between" in result["error"] else 500
            return JSONResponse(
                {"error": safe_msg, "news_items": []}, status_code=status
            )

        # Debug: Log the result before returning
        logger.info(
            f"API returning {len(result.get('news_items', []))} news items"
        )
        if result.get("news_items"):
            logger.info(
                f"First item ID: {result['news_items'][0].get('id', 'NO_ID')}"
            )

        return result

    except NewsAPIException:
        # api.get_news_feed() raises DatabaseAccessException /
        # NewsFeedGenerationException on failure — let the NewsAPIException
        # handler in fastapi_app.py render exc.to_dict() (error_code,
        # details) instead of downgrading it to a generic {"error": ...}
        # below.
        raise
    except Exception as e:
        return JSONResponse(
            {
                "error": safe_error_message(e, "getting news feed"),
                "news_items": [],
            },
            status_code=500,
        )


@router.post("/subscribe")
@_news_create_limit
async def create_subscription(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """
    Create a new subscription for user.

    JSON body:
        query: Search query or topic
        subscription_type: "search" or "topic" (default: "search")
        refresh_minutes: Refresh interval in minutes (default: from settings)
    """
    # No broad ``except`` around the parse: ``read_json_dict`` answers a
    # malformed body itself, and anything else it raises (the request-size
    # limit's 413, a client disconnect) must reach the middleware intact.
    data, _body_err = await read_json_dict(
        request, "simple", "No JSON data provided"
    )
    if _body_err is not None:
        return _body_err

    try:
        validation_error = _validate_subscription_payload(
            data, require_query=True
        )
        if validation_error is not None:
            return validation_error

        # Get current user
        user_id = username

        # Extract parameters
        query = data.get("query")
        subscription_type = data.get("subscription_type", "search")
        refresh_minutes = data.get(
            "refresh_minutes"
        )  # Will use default from api.py

        # Extract model configuration (optional)
        # Normalised to the lowercase canonical form the rest of the
        # route/service layer compares against (and falsy -> None), so a
        # subscription created with "OLLAMA" still matches later lookups.
        model_provider = normalize_provider(data.get("model_provider"))
        model = data.get("model")
        search_strategy = data.get("search_strategy", "news_aggregation")
        custom_endpoint = data.get("custom_endpoint")

        # Extract additional fields
        name = data.get("name")
        folder_id = data.get("folder_id")
        is_active = data.get("is_active", True)
        search_engine = data.get("search_engine")
        search_iterations = data.get("search_iterations")
        questions_per_iteration = data.get("questions_per_iteration")

        bad_endpoint = await _reject_custom_endpoint_async(custom_endpoint)
        if bad_endpoint is not None:
            return bad_endpoint

        # Sync DB work (SQLCipher session + commit) — threadpool, not loop.
        return await run_db_sync(
            api.create_subscription,
            user_id=user_id,
            query=query,
            subscription_type=subscription_type,
            refresh_minutes=refresh_minutes,
            model_provider=model_provider,
            model=model,
            search_strategy=search_strategy,
            custom_endpoint=custom_endpoint,
            name=name,
            folder_id=folder_id,
            is_active=is_active,
            search_engine=search_engine,
            search_iterations=search_iterations,
            questions_per_iteration=questions_per_iteration,
        )

    except ValueError as e:
        return JSONResponse(
            {"error": safe_error_message(e, "creating subscription")},
            status_code=400,
        )
    except NewsAPIException:
        # api.create_subscription() raises one of two NewsAPIException
        # shapes on failure: a SubscriptionCreationException (e.g. the N14
        # egress-policy rejection, or a wrapped DB error), or a plain
        # NewsAPIException with error_code INVALID_REFRESH_INTERVAL and
        # status 400 when the interval it resolved is outside the write cap.
        # Through this route only the saved news.subscription.refresh_minutes
        # default can cause the latter, because an explicit refresh_minutes
        # was already range-checked by _validate_subscription_payload above.
        # Let the NewsAPIException handler in fastapi_app.py render
        # exc.to_dict() (error_code, details) instead of downgrading it to
        # a generic {"error": ...} below.
        raise
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "creating subscription")},
            status_code=500,
        )


@router.post("/vote")
async def vote_on_news(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """
    Submit vote on a news item.

    JSON body:
        card_id: ID of the news card
        vote: "up" or "down"
    """
    # Parsed before the broad ``try`` below, which would turn the
    # request-size limit's exception into a 500 (see ``read_json_dict``).
    data, _body_err = await read_json_dict(
        request, "simple", "No JSON data provided"
    )
    if _body_err is not None:
        return _body_err

    try:
        # Get current user
        user_id = username

        card_id = data.get("card_id")
        vote = data.get("vote")

        # Validate
        if not all([card_id, vote]):
            return JSONResponse(
                {"error": "card_id and vote are required"}, status_code=400
            )

        # Sync DB work (SQLCipher session + commit) — threadpool, not loop.
        return await run_db_sync(
            api.submit_feedback, card_id=card_id, user_id=user_id, vote=vote
        )

    except ValueError as e:
        error_msg = str(e)
        if "not found" in error_msg.lower():
            return JSONResponse(
                {"error": "Resource not found"}, status_code=404
            )
        return JSONResponse(
            {"error": safe_error_message(e, "submitting vote")},
            status_code=400,
        )
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "submitting vote")},
            status_code=500,
        )


@router.post("/feedback/batch")
async def get_batch_feedback(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """
    Get feedback (votes) for multiple news cards.
    JSON body:
        card_ids: List of card IDs
    """
    # Parsed before the broad ``try`` below, which would turn the
    # request-size limit's exception into a 500 (see ``read_json_dict``).
    data, _body_err = await read_json_dict(
        request, "simple", "No JSON data provided"
    )
    if _body_err is not None:
        return _body_err

    try:
        card_ids = data.get("card_ids", [])
        if not isinstance(card_ids, list) or any(
            not isinstance(card_id, str) or not card_id.strip()
            for card_id in card_ids
        ):
            return JSONResponse(
                {"error": "card_ids must be a list of non-empty strings"},
                status_code=400,
            )
        if len(card_ids) > NEWS_BATCH_FEEDBACK_MAX_CARD_IDS:
            return JSONResponse(
                {
                    "error": (
                        "card_ids exceeds maximum "
                        f"({NEWS_BATCH_FEEDBACK_MAX_CARD_IDS})"
                    )
                },
                status_code=400,
            )
        if not card_ids:
            return {"votes": {}}

        # Get current user
        user_id = username

        # Sync DB work (SQLCipher session) — threadpool, not loop.
        return await run_db_sync(
            api.get_votes_for_cards, card_ids=card_ids, user_id=user_id
        )

    except ValueError as e:
        error_msg = str(e)
        if "not found" in error_msg.lower():
            return JSONResponse(
                {"error": "Resource not found"}, status_code=404
            )
        return JSONResponse(
            {"error": safe_error_message(e, "getting votes")},
            status_code=400,
        )
    except Exception as e:
        logger.exception("Error getting batch feedback")
        return JSONResponse(
            {"error": safe_error_message(e, "getting votes")},
            status_code=500,
        )


@router.post("/feedback/{card_id}")
@_news_feedback_limit
async def submit_feedback(
    card_id: str,
    request: Request,
    username: Annotated[str, Depends(require_auth)],
) -> Any:
    """
    Submit feedback (vote) for a news card.

    JSON body:
        vote: "up" or "down"
    """
    # Parsed before the broad ``try`` below, which would turn the
    # request-size limit's exception into a 500 (see ``read_json_dict``).
    data, _body_err = await read_json_dict(
        request, "simple", "No JSON data provided"
    )
    if _body_err is not None:
        return _body_err

    try:
        # Get current user
        user_id = username
        vote = data.get("vote")

        # Validate
        if not vote:
            return JSONResponse({"error": "vote is required"}, status_code=400)

        # Sync DB work (SQLCipher session + commit) — threadpool, not loop.
        return await run_db_sync(
            api.submit_feedback, card_id=card_id, user_id=user_id, vote=vote
        )

    except ValueError as e:
        error_msg = str(e)
        if "not found" in error_msg.lower():
            return JSONResponse(
                {"error": "Resource not found"}, status_code=404
            )
        if "must be" in error_msg.lower():
            return JSONResponse(
                {"error": "Invalid input value"}, status_code=400
            )
        return JSONResponse(
            {"error": safe_error_message(e, "submitting feedback")},
            status_code=400,
        )
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "submitting feedback")},
            status_code=500,
        )


@router.post("/research/{card_id}")
@_news_research_limit
async def research_news_item(
    card_id: str,
    request: Request,
    username: Annotated[str, Depends(require_auth)],
) -> Any:
    """
    Perform deeper research on a news item.

    JSON body:
        depth: "quick", "detailed", or "report" (default: "quick")
    """
    try:
        # Body is OPTIONAL here (`or {}`), so an absent/empty body is valid
        # and must stay valid. Only a body that will not PARSE is a 400 --
        # previously it escaped to the generic handler as a 500.
        try:
            data = await request.json() or {}
        except Exception:
            return json_body_error("simple", "Request body must be valid JSON")
        if not isinstance(data, dict):
            return json_body_error("simple", "Request body must be valid JSON")
        depth = data.get("depth", "quick")

        # Sync news-api work — threadpool, not loop.
        return await run_db_sync(api.research_news_item, card_id, depth)

    except NewsAPIException:
        # api.research_news_item() currently always raises
        # NotImplementedException (status_code=501) — let it carry that
        # status through the registered handler instead of downgrading it
        # to a generic 500 below.
        raise
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "researching news item")},
            status_code=500,
        )


@router.get("/subscriptions/current")
def get_current_user_subscriptions(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """Get all subscriptions for current user."""
    try:
        # Get current user
        user_id = username

        # Ensure we have a database session for the user
        # This will trigger register_activity
        logger.debug(f"Getting news feed for user {user_id}")

        # Use the API function
        result = api.get_subscriptions(user_id)
        if "error" in result:
            logger.error(
                f"Error getting subscriptions for user {user_id}: {result['error']}"
            )
            return JSONResponse(
                {"error": "Failed to retrieve subscriptions"}, status_code=500
            )
        return result

    except NewsAPIException:
        # api.get_subscriptions() raises DatabaseAccessException on failure
        # — let the NewsAPIException handler in fastapi_app.py render
        # exc.to_dict() (error_code, details) instead of downgrading it to
        # a generic {"error": ...} below.
        raise
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting subscriptions")},
            status_code=500,
        )


@router.get("/subscriptions/{subscription_id}")
def get_subscription(
    subscription_id: str,
    request: Request,
    username: Annotated[str, Depends(require_auth)],
) -> Any:
    """Get a single subscription by ID."""
    try:
        # Handle null or invalid subscription IDs
        if (
            subscription_id == "null"
            or subscription_id == "undefined"
            or not subscription_id
        ):
            return JSONResponse(
                {"error": "Invalid subscription ID"}, status_code=400
            )

        # api.get_subscription() always returns a subscription dict or
        # raises SubscriptionNotFoundException — never a falsy value — so
        # there is no "not found" case to handle here beyond the
        # except NewsAPIException re-raise below.
        return api.get_subscription(subscription_id, username=username)

    except NewsAPIException:
        # Carries its own status (e.g. 404 for not-found) — let the
        # NewsAPIException handler in fastapi_app.py render it instead of
        # downgrading it to a generic 500 below.
        raise
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting subscription")},
            status_code=500,
        )


@router.put(
    "/subscriptions/{subscription_id}",
    operation_id="news_update_subscription",
)
async def update_subscription(
    subscription_id: str,
    request: Request,
    username: Annotated[str, Depends(require_auth)],
) -> Any:
    """Update a subscription."""
    # No broad ``except`` around the parse: ``read_json_dict`` answers a
    # malformed body itself, and anything else it raises (the request-size
    # limit's 413, a client disconnect) must reach the middleware intact.
    data, _body_err = await read_json_dict(
        request, "simple", "No JSON data provided"
    )
    if _body_err is not None:
        return _body_err

    try:
        validation_error = _validate_subscription_payload(data)
        if validation_error is not None:
            return validation_error

        bad_endpoint = await _reject_custom_endpoint_async(
            data.get("custom_endpoint")
        )
        if bad_endpoint is not None:
            return bad_endpoint

        # Prepare update data
        update_data = {}

        # Map fields from request to storage format
        field_mapping = {
            "query": "query_or_topic",
            "name": "name",
            "refresh_minutes": "refresh_interval_minutes",
            "is_active": "is_active",
            "folder_id": "folder_id",
            "model_provider": "model_provider",
            "model": "model",
            "search_strategy": "search_strategy",
            "custom_endpoint": "custom_endpoint",
            "search_engine": "search_engine",
            "search_iterations": "search_iterations",
            "questions_per_iteration": "questions_per_iteration",
        }

        for request_field, storage_field in field_mapping.items():
            if request_field in data:
                update_data[storage_field] = data[request_field]

        # Update subscription — sync DB work goes to the threadpool.
        # api.update_subscription() always returns a {"status": "success",
        # "subscription": {...}} dict or raises a NewsAPIException — never a
        # dict with an "error" key — so there is no such case to handle
        # here beyond the except NewsAPIException re-raise below.
        return await run_db_sync(
            api.update_subscription,
            subscription_id,
            update_data,
            username=username,
        )

    except NewsAPIException:
        # Carries its own status (e.g. 404 for not-found) — let the
        # NewsAPIException handler in fastapi_app.py render it instead of
        # downgrading it to a generic 500 below.
        raise
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "updating subscription")},
            status_code=500,
        )


@router.delete("/subscriptions/{subscription_id}")
def delete_subscription(
    subscription_id: str,
    request: Request,
    username: Annotated[str, Depends(require_auth)],
) -> Any:
    """Delete a subscription."""
    try:
        # api.delete_subscription() always returns a truthy status dict or
        # raises SubscriptionNotFoundException — never a falsy value — so
        # there is no "not found" case to handle here beyond the
        # except NewsAPIException re-raise below.
        api.delete_subscription(subscription_id, username=username)
        return {
            "status": "success",
            "message": f"Subscription {subscription_id} deleted",
        }

    except NewsAPIException:
        # Carries its own status (e.g. 404 for not-found) — let the
        # NewsAPIException handler in fastapi_app.py render it instead of
        # downgrading it to a generic 500 below.
        raise
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "deleting subscription")},
            status_code=500,
        )


@router.post("/subscriptions/{subscription_id}/run")
def run_subscription_now(
    subscription_id: str,
    request: Request,
    username: Annotated[str, Depends(require_auth)],
) -> Any:
    """Manually trigger a subscription to run now."""
    try:
        from ...news.core.utils import get_local_date_string
        from ...news.subscription_runner import (
            advance_refresh_schedule,
            build_subscription_request_data,
        )
        from ...database.session_context import get_user_db_session
        from ...database.models.news import NewsSubscription
        from ...settings.manager import SettingsManager
        from datetime import datetime, timezone

        # Load the subscription from the user's database. Reading the ORM row
        # directly (rather than the trimmed api.get_subscriptions() dict, which
        # drops model_provider/model/search_strategy/search_engine) ensures the
        # manual run honors the subscription's saved model config, matching the
        # overdue sweep and the scheduler.
        # Read the subscription + build the payload, then release the read
        # transaction before the blocking POST below. The per-user encrypted DB
        # uses deferred isolation, so a SELECT holds a SHARED lock on the
        # SQLCipher file until the transaction ends — and the session is
        # request-cached, so it is NOT closed on `with` exit. We therefore
        # rollback() explicitly to drop the read lock before the (up to 30s)
        # HTTP call, then reopen a short session to advance.
        with get_user_db_session(username) as db:
            sub = (
                db.query(NewsSubscription)
                .filter(NewsSubscription.id == subscription_id)
                .first()
            )
            if not sub:
                return JSONResponse(
                    {"error": "Subscription not found"}, status_code=404
                )

            if not is_runnable_refresh_interval(sub.refresh_interval_minutes):
                return _subscription_validation_error(
                    SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE
                )

            subscription_pk = sub.id
            # Snapshot next_refresh for the post-POST compare-and-set (below):
            # a fast-failing run's failure handler may reset next_refresh on the
            # worker thread, and the advance must not clobber that.
            prev_next_refresh = sub.next_refresh
            settings_manager = SettingsManager(db)
            current_date = get_local_date_string(settings_manager)

            request_data = build_subscription_request_data(
                query_template=sub.query_or_topic,
                current_date=current_date,
                triggered_by="manual",
                subscription_id=sub.id,
                model_provider=sub.model_provider,
                model=sub.model,
                search_strategy=sub.search_strategy,
                search_engine=sub.search_engine,
                custom_endpoint=sub.custom_endpoint,
                title=sub.name,
            )
            # End the read transaction so the SHARED lock is released before the
            # blocking POST (exiting the `with` does not — the session is
            # request-cached and not closed on exit).
            db.rollback()

        # Start the research IN-PROCESS (no loopback HTTP POST). The old
        # loopback built its target URL from request.base_url — i.e. the
        # client-controlled Host header — and sent the caller's own session
        # cookie + CSRF token to it via safe_post(allow_localhost=True,
        # allow_private_ips=True). A spoofed Host turned this into an SSRF /
        # internal-network request oracle. Calling the handler directly
        # removes the network hop (and the SSRF) entirely and matches main's
        # #4834, which replaced the same loopback with an in-process call.
        data = _start_research_in_process(request, request_data, username)

        if data.get("status") in ("success", "queued"):
            # Advance the schedule so a subscription that was also overdue
            # is not immediately re-run by the scheduler while this run is
            # in flight. If the run later fails, the research failure
            # handler resets next_refresh so the scheduler retries it.
            # Compare-and-set: only advance if next_refresh is unchanged
            # since we read it pre-spawn. A fast-failing run can reset it
            # (worker thread) before we get here; in that case leave the
            # reset in place rather than clobbering it and re-hiding the
            # failed subscription for a full interval.
            with get_user_db_session(username) as db:
                sub = (
                    db.query(NewsSubscription)
                    .filter(NewsSubscription.id == subscription_pk)
                    .first()
                )
                if sub and sub.next_refresh == prev_next_refresh:
                    advance_refresh_schedule(sub, datetime.now(timezone.utc))
                    db.commit()
            return {
                "status": "success",
                "message": "Research started",
                "research_id": data.get("research_id"),
                "url": f"/progress/{data.get('research_id')}",
            }
        # start_research reports failures under "message"; keep "error"
        # as a fallback for any other shape. Propagate the real status code
        # (400/409/429) the in-process call surfaced, not a flat 500.
        return JSONResponse(
            {
                "error": data.get(
                    "message",
                    data.get("error", "Failed to start research"),
                )
            },
            status_code=data.get("_http_status", 500),
        )

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "running subscription")},
            status_code=500,
        )


@router.get("/subscriptions/{subscription_id}/history")
def get_subscription_history(
    subscription_id: str,
    request: Request,
    username: Annotated[str, Depends(require_auth)],
) -> Any:
    """Get research history for a subscription."""
    try:
        if not _is_valid_uuid(subscription_id):
            return JSONResponse(
                {"success": False, "error": "Invalid subscription_id"},
                status_code=400,
            )
        with get_user_db_session(username) as _db:
            settings_manager = get_settings_manager(_db, username)
            default_limit = settings_manager.get_setting(
                "news.feed.default_limit"
            )
        try:
            limit = int(request.query_params.get("limit", default_limit))
        except (TypeError, ValueError):
            limit = int(default_limit)
        limit = max(1, min(limit, NEWS_FEED_MAX_LIMIT))
        # api.get_subscription_history() always returns a dict with
        # subscription/history/total_runs, or raises SubscriptionNotFoundException
        # — never a dict with an "error" key — so there is no such case to
        # handle here beyond the except NewsAPIException re-raise below.
        return api.get_subscription_history(
            subscription_id, limit, username=username
        )
    except NewsAPIException:
        # Carries its own status (e.g. 404 for not-found) — let the
        # NewsAPIException handler in fastapi_app.py render it instead of
        # downgrading it to a generic 500 below.
        raise
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting subscription history")},
            status_code=500,
        )


@router.post("/preferences")
@_news_preferences_limit
async def save_preferences(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """Save user preferences for news."""
    # Parsed before the broad ``try`` below, which would turn the
    # request-size limit's exception into a 500 (see ``read_json_dict``).
    data, _body_err = await read_json_dict(
        request, "simple", "No JSON data provided"
    )
    if _body_err is not None:
        return _body_err

    try:
        # Get current user
        user_id = username
        preferences = data.get("preferences", {})

        # Sync DB work (SQLCipher session + commit) — threadpool, not loop.
        return await run_db_sync(
            api.save_news_preferences, user_id, preferences
        )

    except NewsAPIException:
        # api.save_news_preferences() currently always raises
        # NotImplementedException (status_code=501) — let it carry that
        # status through the registered handler instead of downgrading it
        # to a generic 500 below.
        raise
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "saving preferences")},
            status_code=500,
        )


@router.get("/categories")
def get_categories(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """Get news category distribution."""
    from ...news.exceptions import NotImplementedException

    try:
        return api.get_news_categories()
    except NotImplementedException:
        # Surface "not yet implemented" as 501 instead of generic 500. The
        # message is a static string — not ``str(e)`` — so this endpoint
        # doesn't echo exception-derived text to the client (CWE-209,
        # CodeQL "Information exposure through an exception"); every other
        # handler in this file already goes through safe_error_message()
        # for the same reason. The exception is still logged server-side.
        logger.exception("get_news_categories: feature not implemented")
        return JSONResponse(
            {
                "error": "This feature is not yet implemented.",
                "error_code": "NOT_IMPLEMENTED",
            },
            status_code=501,
        )
    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting categories")},
            status_code=500,
        )


@router.get("/scheduler/status")
def get_scheduler_status(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """Get activity-based scheduler status."""
    try:
        logger.info("Scheduler status endpoint called")
        from ...scheduler.background import get_background_job_scheduler

        # Get scheduler instance
        scheduler = get_background_job_scheduler()
        show_all = get_env_setting("news.scheduler.allow_api_control", False)
        logger.info(
            f"Scheduler instance obtained: is_running={scheduler.is_running}"
        )

        # Build status manually to avoid potential deadlock
        if show_all:
            active_users = (
                len(scheduler.user_sessions)
                if hasattr(scheduler, "user_sessions")
                else 0
            )
        else:
            active_users = (
                1
                if hasattr(scheduler, "user_sessions")
                and username in scheduler.user_sessions
                else 0
            )

        status = {
            "scheduler_available": True,  # APScheduler is installed and working
            "is_running": scheduler.is_running,
            "config": scheduler.config.copy()
            if hasattr(scheduler, "config")
            else {},
            "active_users": active_users,
            "total_scheduled_jobs": 0,
        }

        # Count scheduled jobs
        if hasattr(scheduler, "user_sessions"):
            if show_all:
                total_jobs = sum(
                    len(sess.get("scheduled_jobs", set()))
                    for sess in scheduler.user_sessions.values()
                )
            else:
                user_session = scheduler.user_sessions.get(username, {})
                total_jobs = len(user_session.get("scheduled_jobs", set()))
            status["total_scheduled_jobs"] = total_jobs

        # Also count actual APScheduler jobs
        if hasattr(scheduler, "scheduler") and scheduler.scheduler:
            try:
                apscheduler_jobs = scheduler.scheduler.get_jobs()
                if not show_all:
                    apscheduler_jobs = [
                        j
                        for j in apscheduler_jobs
                        if _is_job_owned_by_user(j, username, scheduler)
                    ]
                status["apscheduler_job_count"] = len(apscheduler_jobs)
                status["apscheduler_jobs"] = [
                    {
                        "id": job.id,
                        "name": job.name,
                        "next_run": job.next_run_time.isoformat()
                        if job.next_run_time
                        else None,
                    }
                    for job in apscheduler_jobs[
                        :10
                    ]  # Limit to first 10 for display
                ]
            except Exception:
                logger.exception("Error getting APScheduler jobs")
                status["apscheduler_job_count"] = 0

        logger.info(f"Status built: {list(status.keys())}")

        # Add scheduled_jobs field that JS expects
        status["scheduled_jobs"] = status.get("total_scheduled_jobs", 0)

        logger.info(
            f"Returning status: is_running={status.get('is_running')}, active_users={status.get('active_users')}"
        )
        return status

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting scheduler status")},
            status_code=500,
        )


@router.post("/scheduler/start")
def start_scheduler(
    request: Request,
    username: Annotated[str, Depends(require_auth)],
    _gate: Annotated[None, Depends(require_scheduler_control)],
) -> Any:
    """Start the subscription scheduler."""
    try:
        from ...scheduler.background import get_background_job_scheduler

        # Get scheduler instance
        scheduler = get_background_job_scheduler()

        if scheduler.is_running:
            return {"message": "Scheduler is already running"}

        # Start the scheduler
        scheduler.start()

        # Update app reference
        # Scheduler is a singleton — no need to store on app

        logger.info("News scheduler started via API")
        return {
            "status": "success",
            "message": "Scheduler started",
            "active_users": len(scheduler.user_sessions),
        }

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "starting scheduler")},
            status_code=500,
        )


@router.post("/scheduler/stop")
def stop_scheduler(
    request: Request,
    username: Annotated[str, Depends(require_auth)],
    _gate: Annotated[None, Depends(require_scheduler_control)],
) -> Any:
    """Stop the subscription scheduler."""
    try:
        from ...scheduler.background import get_background_job_scheduler

        if get_background_job_scheduler():
            scheduler = get_background_job_scheduler()
            if scheduler.is_running:
                scheduler.stop()
                logger.info("News scheduler stopped via API")
                return {"status": "success", "message": "Scheduler stopped"}

            return {"message": "Scheduler is not running"}
        return JSONResponse(
            {"message": "No scheduler instance found"}, status_code=404
        )

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "stopping scheduler")},
            status_code=500,
        )


@router.post("/scheduler/check-now")
def check_subscriptions_now(
    request: Request,
    username: Annotated[str, Depends(require_auth)],
    _gate: Annotated[None, Depends(require_scheduler_control)],
) -> Any:
    """Manually trigger subscription checking."""
    try:
        from ...scheduler.background import get_background_job_scheduler

        if not get_background_job_scheduler():
            return JSONResponse(
                {"error": "Scheduler not initialized"}, status_code=503
            )

        scheduler = get_background_job_scheduler()
        if not scheduler.is_running:
            return JSONResponse(
                {"error": "Scheduler is not running"}, status_code=503
            )

        # Run the check subscriptions task immediately
        scheduler_instance = get_background_job_scheduler()

        # Get count of due subscriptions
        from ...database.models import NewsSubscription as BaseSubscription
        from datetime import datetime, timedelta, timezone

        with get_user_db_session(username) as session:
            now = datetime.now(timezone.utc)
            count = (
                session.query(BaseSubscription)
                .filter(BaseSubscription.due_filter(now))
                .count()
            )

        # Trigger the check asynchronously via APScheduler, matching main:
        # the per-user job id + replace_existing dedups double-clicks, and
        # _wrap_job supplies the worker-side context handling.
        scheduler_instance.scheduler.add_job(
            func=scheduler_instance._wrap_job(
                scheduler_instance._check_user_overdue_subscriptions,
                username=username,
            ),
            args=[username],
            trigger="date",
            run_date=datetime.now(timezone.utc) + timedelta(seconds=1),
            id=f"manual_check_{username}",
            replace_existing=True,
        )

        return {
            "status": "success",
            "message": f"Checking {count} due subscriptions",
            "count": count,
        }

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "checking subscriptions")},
            status_code=500,
        )


@router.post("/scheduler/cleanup-now")
def trigger_cleanup(
    request: Request,
    username: Annotated[str, Depends(require_auth)],
    _gate: Annotated[None, Depends(require_scheduler_control)],
) -> Any:
    """Manually trigger cleanup job."""
    try:
        from ...scheduler.background import get_background_job_scheduler
        from datetime import datetime, UTC, timedelta

        scheduler = get_background_job_scheduler()

        if not scheduler.is_running:
            return JSONResponse(
                {"error": "Scheduler is not running"}, status_code=400
            )

        # Schedule cleanup to run in 1 second
        # replace_existing=True and _wrap_job both matter here, and both were
        # lost in the port. The job id is fixed and the run_date is 1s out, so
        # a second POST inside that window raises ConflictingIdError, which the
        # blanket except below turns into a 500 -- a double-click on the admin
        # button is enough. main replaced the pending job and returned 200.
        # _wrap_job is inert for a system job (username=None -> nullcontext)
        # but this was the only add_job in the tree without it; the sibling
        # check_subscriptions_now above keeps both and says why.
        scheduler.scheduler.add_job(
            scheduler._wrap_job(scheduler._run_cleanup_with_tracking),
            "date",
            run_date=datetime.now(UTC) + timedelta(seconds=1),
            id="manual_cleanup_trigger",
            replace_existing=True,
        )

        return {
            "status": "triggered",
            "message": "Cleanup job will run within seconds",
        }

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "triggering cleanup")},
            status_code=500,
        )


@router.get("/scheduler/users")
def get_active_users(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """Get summary of active user sessions."""
    try:
        from ...scheduler.background import get_background_job_scheduler

        scheduler = get_background_job_scheduler()
        users_summary = scheduler.get_user_sessions_summary()

        show_all = get_env_setting("news.scheduler.allow_api_control", False)
        if not show_all:
            users_summary = [
                u for u in users_summary if u.get("user_id") == username
            ]

        return {"active_users": len(users_summary), "users": users_summary}

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting active users")},
            status_code=500,
        )


@router.get("/scheduler/stats")
def scheduler_stats(
    request: Request, username: Annotated[str, Depends(require_auth)]
) -> Any:
    """Get scheduler statistics and state."""
    try:
        from ...scheduler.background import get_background_job_scheduler

        scheduler = get_background_job_scheduler()

        # Debug info
        debug_info = {
            "current_user": username,
            "scheduler_running": scheduler.is_running,
            "user_sessions": {},
            "apscheduler_jobs": [],
        }

        show_all = get_env_setting("news.scheduler.allow_api_control", False)

        # Get user session info
        if hasattr(scheduler, "user_sessions"):
            for user, session_info in scheduler.user_sessions.items():
                if not show_all and user != username:
                    continue
                debug_info["user_sessions"][user] = {
                    "has_password": bool(
                        scheduler._credential_store.retrieve(user)
                    ),
                    "last_activity": session_info.get(
                        "last_activity"
                    ).isoformat()
                    if session_info.get("last_activity")
                    else None,
                    "scheduled_jobs_count": len(
                        session_info.get("scheduled_jobs", set())
                    ),
                }

        # Get APScheduler jobs
        if hasattr(scheduler, "scheduler") and scheduler.scheduler:
            jobs = scheduler.scheduler.get_jobs()
            if not show_all:
                jobs = [
                    j
                    for j in jobs
                    if _is_job_owned_by_user(j, username, scheduler)
                ]
            debug_info["apscheduler_jobs"] = [
                {
                    "id": job.id,
                    "name": job.name,
                    "next_run": job.next_run_time.isoformat()
                    if job.next_run_time
                    else None,
                    "trigger": str(job.trigger),
                }
                for job in jobs
            ]

        return debug_info

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting scheduler stats")},
            status_code=500,
        )


@router.post("/check-overdue")
def check_overdue_subscriptions(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Check and run all overdue subscriptions for the current user."""
    try:
        from ...news.subscription_runner import (
            advance_refresh_schedule,
            build_subscription_request_data,
        )
        from ...database.session_context import get_user_db_session
        from ...database.models.news import NewsSubscription
        from datetime import datetime, UTC

        # Get overdue subscriptions
        overdue_count = 0
        results = []
        with get_user_db_session(username) as db:
            now = datetime.now(UTC)
            overdue_subs = (
                db.query(NewsSubscription)
                .filter(NewsSubscription.due_filter(now))
                .all()
            )

            logger.info(
                f"Found {len(overdue_subs)} overdue subscriptions for {username}"
            )

            # Get timezone-aware current date using settings
            from ...news.core.utils import get_local_date_string
            from ...settings.manager import SettingsManager

            settings_manager = SettingsManager(db)
            current_date = get_local_date_string(settings_manager)

            for sub in overdue_subs:
                # Capture identity up front as plain strings. This loop shares
                # one DB session across every start_research call, and
                # start_research's error path does not roll back — so a failed
                # run can leave the session in a PendingRollbackError state.
                # Reading sub.* again in an error branch would then raise (the
                # row was expired by an earlier commit), collapsing the whole
                # sweep. Snapshotting here keeps the error branches session-free.
                sub_id = str(sub.id)
                sub_label = sub.name or sub.query_or_topic[:50]
                if not is_runnable_refresh_interval(
                    sub.refresh_interval_minutes
                ):
                    results.append(
                        {
                            "id": sub_id,
                            "name": sub_label,
                            "error": SUBSCRIPTION_INTERVAL_REPAIR_MESSAGE,
                        }
                    )
                    continue
                try:
                    # Run the subscription using the same pattern as run_subscription_now
                    logger.info(
                        "Running overdue subscription: {}",
                        # (#6938: redacted before it is cut; ``sub_label``
                        # is already a cut of the query.)
                        redact_and_bound_for_log(
                            str(sub.name or sub.query_or_topic), 30
                        ),
                    )

                    # Snapshot for the post-run compare-and-set (see below).
                    prev_next_refresh = sub.next_refresh

                    request_data = build_subscription_request_data(
                        query_template=sub.query_or_topic,
                        current_date=current_date,
                        triggered_by="overdue_check",
                        subscription_id=sub.id,
                        model_provider=sub.model_provider,
                        model=sub.model,
                        search_strategy=sub.search_strategy,
                        search_engine=sub.search_engine,
                        custom_endpoint=sub.custom_endpoint,
                        title=sub.name,
                    )

                    # Start the research IN-PROCESS — see the run-now handler
                    # above. The old loopback built its URL from the
                    # client-controlled Host header and forwarded the caller's
                    # cookie/CSRF via safe_post(allow_localhost/private=True):
                    # an SSRF whose raw response body was reflected back to the
                    # client in the `error` field below (an internal-response
                    # oracle). The in-process call removes both.
                    result = _start_research_in_process(
                        request, request_data, username
                    )

                    if result.get("status") in ("success", "queued"):
                        overdue_count += 1

                        # Update subscription's last/next refresh times.
                        # Compare-and-set: re-read and skip the advance if a
                        # fast-failing run already reset next_refresh (worker
                        # thread), so we don't clobber the reset and re-hide it.
                        db.refresh(sub)
                        if sub.next_refresh == prev_next_refresh:
                            advance_refresh_schedule(sub, datetime.now(UTC))
                            db.commit()

                        results.append(
                            {
                                "id": sub_id,
                                "name": sub_label,
                                "research_id": result.get("research_id"),
                            }
                        )
                    else:
                        # start_research failed and may have left the shared
                        # session dirty; reset it so the next subscription runs.
                        db.rollback()
                        results.append(
                            {
                                "id": sub_id,
                                "name": sub_label,
                                # start_research reports failures under
                                # "message"; keep "error" as a fallback for
                                # any other shape.
                                "error": result.get(
                                    "message",
                                    result.get(
                                        "error", "Failed to start research"
                                    ),
                                ),
                            }
                        )
                except Exception as e:
                    # Recover the shared session (a failed start_research commit
                    # can leave it in a PendingRollbackError state) so the
                    # remaining overdue subscriptions in the sweep still run.
                    db.rollback()
                    logger.exception(f"Error running subscription {sub_id}")
                    results.append(
                        {
                            "id": sub_id,
                            "name": sub_label,
                            "error": safe_error_message(
                                e, "running subscription"
                            ),
                        }
                    )

        return {
            "status": "success",
            "overdue_found": len(overdue_subs),
            "started": overdue_count,
            "results": results,
        }

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "checking overdue subscriptions")},
            status_code=500,
        )


# Folder and subscription management routes
@router.get("/subscription/folders")
def get_folders(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Get all folders for the current user"""
    try:
        user_id = username

        with get_user_db_session(username) as session:
            manager = FolderManager(session)
            folders = manager.get_user_folders(user_id)

            return [folder.to_dict() for folder in folders]

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting folders")},
            status_code=500,
        )


@router.post("/subscription/folders")
async def create_folder(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Create a new folder"""
    data, _body_err = await read_json_dict(
        request, "simple", "Request body must be valid JSON"
    )
    if _body_err is not None:
        return _body_err

    def _impl():
        try:
            if not data.get("name"):
                return JSONResponse(
                    {"error": "Folder name is required"}, status_code=400
                )

            with get_user_db_session(username) as session:
                manager = FolderManager(session)

                # Check if folder already exists
                existing = (
                    session.query(SubscriptionFolder)
                    .filter_by(name=data["name"])
                    .first()
                )
                if existing:
                    return JSONResponse(
                        {"error": "Folder already exists"}, status_code=409
                    )

                folder = manager.create_folder(
                    name=data["name"],
                    description=data.get("description"),
                )

                return JSONResponse(folder.to_dict(), status_code=201)

        except Exception as e:
            return JSONResponse(
                {"error": safe_error_message(e, "creating folder")},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.put("/subscription/folders/{folder_id}")
async def update_folder(
    request: Request, folder_id, username: Annotated[str, Depends(require_auth)]
):
    """Update a folder.

    Restricted to an explicit allowlist (``name``, ``description``,
    ``color``, ``icon``, ``is_default``, ``sort_order``) via
    :func:`folder_update_error` -- ``FolderManager.update_folder`` used
    to run a blind ``hasattr``-gated ``setattr`` loop, which is true for
    SQLAlchemy internals (``_sa_instance_state``) and the bound ``to_dict``
    method too, not just real columns. Validate before dispatching to the
    DB thread so an invalid body never reaches the session.
    """
    data, _body_err = await read_json_dict(
        request, "simple", "Request body must be valid JSON"
    )
    if _body_err is not None:
        return _body_err
    error = folder_update_error(data)
    if error is not None:
        return _subscription_validation_error(error)

    def _impl():
        # Defense in depth: the route already validated above (before
        # dispatch to this DB thread), but FolderManager.update_folder
        # validates again, so run the same check here BEFORE opening the DB
        # session so it returns the same 400 (named field). Using the
        # message-returning helper rather than catching ValueError means an
        # unrelated DB/ORM exception can never be echoed as a validation
        # error.
        error = folder_update_error(data)
        if error is not None:
            return _subscription_validation_error(error)

        try:
            with get_user_db_session(username) as session:
                manager = FolderManager(session)
                folder = manager.update_folder(folder_id, **data)

                if not folder:
                    return JSONResponse(
                        {"error": "Folder not found"}, status_code=404
                    )

                return folder.to_dict()

        except Exception as e:
            return JSONResponse(
                {"error": safe_error_message(e, "updating folder")},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.delete("/subscription/folders/{folder_id}")
def delete_folder(
    request: Request, folder_id, username: Annotated[str, Depends(require_auth)]
):
    """Delete a folder"""
    try:
        move_to = request.query_params.get("move_to")

        with get_user_db_session(username) as session:
            manager = FolderManager(session)
            success = manager.delete_folder(folder_id, move_to)

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

            return {"status": "deleted"}

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "deleting folder")},
            status_code=500,
        )


@router.get("/subscription/subscriptions/organized")
def get_subscriptions_organized(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Get subscriptions organized by folder"""
    try:
        user_id = username

        with get_user_db_session(username) as session:
            manager = FolderManager(session)
            organized = manager.get_subscriptions_by_folder(user_id)

            # get_subscriptions_by_folder already returns JSON-friendly dicts
            # ({"folders": [{"folder": {...}, "subscriptions": [...]}, ...],
            # "uncategorized": [...]}). The previous code called .to_dict() on
            # those plain dicts (AttributeError -> HTTP 500). Flatten into the
            # {folder_name: [subscription, ...]} map the subscriptions UI
            # consumes (Object.values(...) for the "all" view, keyed lookup per
            # folder), with ungrouped subscriptions under "uncategorized".
            result = {}
            for entry in organized.get("folders", []):
                folder_name = entry["folder"].get("name") or entry[
                    "folder"
                ].get("id")
                result[folder_name] = entry["subscriptions"]
            # Merge (don't overwrite) so a user folder literally named
            # "uncategorized" doesn't have its subscriptions dropped by the
            # ungrouped bucket. In the normal case this just sets the key.
            result.setdefault("uncategorized", []).extend(
                organized.get("uncategorized", [])
            )

            return result

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting organized subscriptions")},
            status_code=500,
        )


@router.put("/subscription/subscriptions/{subscription_id}")
async def update_subscription_folder(
    request: Request,
    subscription_id,
    username: Annotated[str, Depends(require_auth)],
):
    """Update a subscription (mainly for folder assignment).

    Restricted to an explicit allowlist (``folder``, ``folder_id``,
    ``name``, ``notes``, ``refresh_interval_minutes``, ``is_active``,
    ``status``) via :func:`compact_subscription_update_error` -- the
    blind ``setattr`` loop this route used to run wrote any matching
    column, so a request body could reach protected or privileged columns
    (``custom_endpoint`` included, a third write path alongside
    create_subscription / update_subscription that #5603 could only
    SSRF-check rather than close). Reject unsupported fields at the
    boundary, before the sync helper's unredacted log line, so this route
    cannot persist anything its allowlisted siblings reject.
    """
    data, _body_err = await read_json_dict(
        request, "simple", "Request body must be valid JSON"
    )
    if _body_err is not None:
        return _body_err
    error = compact_subscription_update_error(data)
    if error is not None:
        return _subscription_validation_error(error)
    return await run_db_sync(
        _update_subscription_folder_sync, data, subscription_id, username
    )


def _update_subscription_folder_sync(data, subscription_id, username):
    try:
        error = compact_subscription_update_error(data)
        if error is not None:
            return _subscription_validation_error(error)
        # The payload is unchanged after validation above. The normalizer
        # repeats that check to preserve its contract for non-route callers;
        # unexpected failures fall through to the scrubbed 500 below.
        update_data = normalize_compact_subscription_update(data)

        logger.info(
            "Updating subscription {} with data: {}",
            _log_value_preview(subscription_id),
            _log_value_preview(update_data),
        )

        with get_user_db_session(username) as session:
            # Manually handle the update to ensure next_refresh is recalculated
            from ...database.models.news import (
                NewsSubscription as BaseSubscription,
            )
            from datetime import datetime, timedelta, timezone

            sub = (
                session.query(BaseSubscription)
                .filter_by(id=subscription_id)
                .first()
            )
            if not sub:
                return JSONResponse(
                    {"error": "Subscription not found"}, status_code=404
                )

            # `normalize_compact_subscription_update` already translated a
            # legacy `is_active` toggle into canonical `status` (with an
            # explicit `status` taking precedence) and restricted
            # `update_data` to the allowlisted fields -- no blind setattr
            # over the raw request body, so protected columns (id, user_id,
            # created_at, ...) and unrelated ones (custom_endpoint included)
            # can never be reached from this route.
            for key, value in update_data.items():
                setattr(sub, key, value)

            # Recalculate next_refresh if refresh_interval_minutes changed
            if "refresh_interval_minutes" in update_data:
                new_minutes = update_data["refresh_interval_minutes"]
                if sub.last_refresh:
                    sub.next_refresh = sub.last_refresh + timedelta(
                        minutes=new_minutes
                    )
                else:
                    sub.next_refresh = datetime.now(timezone.utc) + timedelta(
                        minutes=new_minutes
                    )
                logger.info(f"Recalculated next_refresh: {sub.next_refresh}")

            sub.updated_at = datetime.now(timezone.utc)
            session.commit()

            # NewsSubscription has no to_dict(); serialize the fields the UI
            # needs explicitly. is_active is derived from status (the source of
            # truth) so the response stays consistent with the toggle above.
            result = {
                "id": sub.id,
                "name": sub.name,
                "status": sub.status,
                "is_active": sub.status == "active",
                "folder_id": sub.folder_id,
                "refresh_interval_minutes": sub.refresh_interval_minutes,
                "next_refresh": sub.next_refresh.isoformat()
                if sub.next_refresh
                else None,
                "last_refresh": sub.last_refresh.isoformat()
                if sub.last_refresh
                else None,
            }
            logger.info(
                f"Updated subscription result: refresh_interval_minutes={result.get('refresh_interval_minutes')}, next_refresh={result.get('next_refresh')}"
            )

        # Reschedule like the full update path (api.update_subscription)
        # does; without this, a row skipped for an invalid interval stays
        # unscheduled after the interval is repaired here, until the next
        # login. It runs after the commit and after the reply is built, and
        # the helper swallows its own failures, so it cannot change the reply.
        api._notify_scheduler_about_subscription_change("updated", username)
        return result

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "updating subscription")},
            status_code=500,
        )


@router.get("/subscription/stats")
def get_subscription_stats(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Get subscription statistics"""
    try:
        user_id = username

        with get_user_db_session(username) as session:
            manager = FolderManager(session)
            return manager.get_subscription_stats(user_id)

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting stats")},
            status_code=500,
        )


@router.get("/search-history")
def get_search_history(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Get search history for current user."""
    try:
        # Get search history from user's encrypted database
        from ...database.session_context import get_user_db_session
        from ...database.models import UserNewsSearchHistory

        password = None

        with get_user_db_session(username, password) as db_session:
            history = (
                db_session.query(UserNewsSearchHistory)
                .order_by(UserNewsSearchHistory.created_at.desc())
                .limit(20)
                .all()
            )

            return {"search_history": [item.to_dict() for item in history]}

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "getting search history")},
            status_code=500,
        )


@router.post("/search-history")
async def add_search_history(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Add a search to the history."""
    data = await request.json()

    def _impl():
        try:
            # `data` may be any JSON value here (a bare list, string, or
            # number is valid JSON but not a dict) — check the type before
            # calling dict-only methods on it, else a truthy non-dict body
            # raises AttributeError and lands in the `except Exception`
            # below as a 500 instead of the 400 a malformed request should
            # get.
            logger.info(
                "add_search_history received data keys: {}",
                _log_value_preview(list(data.keys()))
                if isinstance(data, dict)
                else "None",
            )
            if not isinstance(data, dict) or not data.get("query"):
                logger.warning("Invalid search history data: missing query")
                return JSONResponse(
                    {"error": "query is required"}, status_code=400
                )

            # Add to user's encrypted database
            from ...database.session_context import get_user_db_session
            from ...database.models import UserNewsSearchHistory

            password = None

            with get_user_db_session(username, password) as db_session:
                search_history = UserNewsSearchHistory(
                    query=data["query"],
                    search_type=data.get("type", "filter"),
                    result_count=data.get("resultCount", 0),
                )
                db_session.add(search_history)
                db_session.commit()

                return {"status": "success", "id": search_history.id}

        except Exception as e:
            logger.exception("Error adding search history")
            return JSONResponse(
                {"error": safe_error_message(e, "adding search history")},
                status_code=500,
            )

    return await run_db_sync(_impl)


@router.delete("/search-history")
def clear_search_history(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Clear all search history for current user."""
    try:
        # Clear from user's encrypted database
        from ...database.session_context import get_user_db_session
        from ...database.models import UserNewsSearchHistory

        password = None

        with get_user_db_session(username, password) as db_session:
            db_session.query(UserNewsSearchHistory).delete()
            db_session.commit()

            return {"status": "success"}

    except Exception as e:
        return JSONResponse(
            {"error": safe_error_message(e, "clearing search history")},
            status_code=500,
        )