~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
RAG Management API Routes
Provides endpoints for managing RAG indexing of library documents:
- Configure embedding models
- Index documents
- Get RAG statistics
- Bulk operations with progress tracking
"""
import asyncio
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import HTMLResponse, JSONResponse
from ..dependencies.auth import require_auth
from ..dependencies.rate_limit import (
limiter,
upload_rate_limit_ip,
upload_rate_limit_user,
)
from ..dependencies.threadpool import (
WorkerCleanupStreamingResponse,
run_db_sync,
)
from ..template_config import templates
import os
from loguru import logger
from sqlalchemy import case, func
import atexit
import json
import uuid
import time
import contextvars
import threading
import queue
from concurrent.futures import Future, ThreadPoolExecutor
from datetime import datetime, UTC
from pathlib import Path
from typing import Any, Dict, Optional, Set, Tuple, Annotated
from tenacity import (
retry,
retry_if_exception,
stop_after_attempt,
wait_exponential,
)
from sqlalchemy.orm import defer
from ...constants import (
DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP,
DEFAULT_LOCAL_SEARCH_CHUNK_SIZE,
DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC,
DEFAULT_LOCAL_SEARCH_INDEX_TYPE,
DEFAULT_LOCAL_SEARCH_MODEL,
DEFAULT_LOCAL_SEARCH_NORMALIZE_VECTORS,
DEFAULT_LOCAL_SEARCH_PROVIDER,
DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE,
DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS,
DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS_JSON,
FILE_PATH_SENTINELS,
FILE_PATH_TEXT_ONLY,
)
from ...security.log_sanitizer import (
sanitize_error_message,
)
from ...utilities.db_utils import get_settings_manager
from ...utilities.resource_utils import safe_close
from ...research_library.services.library_rag_service import LibraryRAGService
from ...settings.exceptions import SettingsValueError
from ...settings.manager import SettingsManager, check_env_setting
from ...research_library.utils import (
apply_user_subdir,
ensure_in_collection,
handle_api_error,
)
from ...database.models.library import (
Document,
Collection,
DocumentCollection,
RAGIndex,
SourceType,
EmbeddingProvider,
)
from ...database.models.queue import TaskMetadata
from ...database.thread_local_session import thread_cleanup
from ...config.paths import get_library_directory
from ..dependencies.json_body import json_body_error, read_json_dict
from .notes import _log_value_preview
#: Upper bound on ``page`` -- see context_overflow_api for the rationale.
_MAX_PAGE = 10_000
# Keep the configure endpoint's boundary aligned with the registered setting
# schema in defaults/settings_local_search.json. Text splitters require integer
# sizes; accepting strings, floats, or booleans here would silently coerce or
# truncate them only after the request reached the settings/index write path.
_RAG_CHUNK_SIZE_MIN = 100
_RAG_CHUNK_SIZE_MAX = 5_000
_RAG_CHUNK_OVERLAP_MIN = 0
_RAG_CHUNK_OVERLAP_MAX = 1_000
router = APIRouter(prefix="/library", tags=["rag"])
_EMBEDDING_TEST_SENTENCE = (
"This is a test sentence to verify the embedding model is working "
"correctly."
)
# Process-local registry tracking active SSE indexing streams for cancellation.
# Keyed by (username, collection_id) -> Set[threading.Event]
# Process-local cancellation and admission require a single worker process.
# Multiple workers need shared admission and cancellation coordination;
# sticky sessions alone do not exclude two devices indexing the same collection.
_active_sse_indexers: Dict[Tuple[str, str], Set[threading.Event]] = {}
_active_sse_indexers_lock = threading.Lock()
# A cancelled task can still have workers draining. Keep admission closed
# until its worker exits, independently of the task's persisted status.
_active_background_indexers: Dict[Tuple[str, str], str] = {}
# NOTE: Routes use username (not .get()) intentionally.
# Depends(require_auth) guarantees the key exists; direct access fails
# fast if the dependency is ever removed.
def _agent_enabled_default_on(collection) -> bool:
"""Read a collection's ``agent_enabled`` flag with NULL → available.
A missing attribute (pre-migration object) or a stored NULL both mean
"available to the research agent" (default-on); only an explicit ``False``
disables. Used by every serializer so the four read sites can't drift on
null-handling. ``is not False`` is safe here because the source is a
Boolean column (only ever None/True/False) — NOT raw JSON input, where a
falsy ``0`` must still mean disabled (handled separately at the input edge).
"""
return getattr(collection, "agent_enabled", True) is not False
def _is_protected_collection(collection) -> bool:
"""True when the collection's type is deletion-protected.
Single source of truth is PROTECTED_COLLECTION_TYPES in the deletion
service (imported lazily, matching update_collection's usage); the
serializers expose the result as ``is_protected`` so UI surfaces can
hide destructive affordances instead of offering an action the server
categorically refuses with a 409.
"""
from ...research_library.deletion.services.collection_deletion import (
PROTECTED_COLLECTION_TYPES,
)
return collection.collection_type in PROTECTED_COLLECTION_TYPES
# Global ThreadPoolExecutor for auto-indexing to prevent thread proliferation.
#
# ThreadPoolExecutor's internal _work_queue is unbounded. Without
# backpressure, a sustained burst of uploads (possible under the
# configurable upload rate cap from #3935) could queue thousands of indexing
# jobs in memory, exhausting RAM. We track pending submissions in a counter
# and reject new submissions once the queue is saturated.
_auto_index_executor: ThreadPoolExecutor | None = None
_auto_index_executor_lock = threading.Lock()
# Per-(user, collection) locks guarding admission for all collection indexers.
# Keyed by tuple so two users indexing different
# collections don't serialize against each other.
_start_bg_index_locks: dict[tuple[str, str], threading.Lock] = {}
# Maximum in-flight + queued indexing jobs. Each job is one upload batch
# (an unbounded list of document IDs), so per-job duration varies with batch
# size; 100 is a buffer on the number of *queued batches*, not documents.
# The OOM bound still holds because a queued job holds only a small list of
# ID strings, not document contents. Tunable if real-world workloads need
# more headroom.
_MAX_PENDING_AUTO_INDEX_JOBS = 100
_pending_auto_index_jobs = 0
_pending_auto_index_lock = threading.Lock()
def _get_auto_index_executor() -> ThreadPoolExecutor:
"""Get or create the global auto-indexing executor (thread-safe)."""
global _auto_index_executor
with _auto_index_executor_lock:
if _auto_index_executor is None:
_auto_index_executor = ThreadPoolExecutor(
max_workers=4,
thread_name_prefix="auto_index_",
)
return _auto_index_executor
def _try_reserve_auto_index_slot() -> bool:
"""Reserve a queue slot if capacity allows. Returns True on success.
Caller MUST call ``_release_auto_index_slot`` when the job completes
(success OR failure), otherwise the counter leaks and eventually
blocks all submissions.
"""
global _pending_auto_index_jobs
with _pending_auto_index_lock:
if _pending_auto_index_jobs >= _MAX_PENDING_AUTO_INDEX_JOBS:
return False
_pending_auto_index_jobs += 1
return True
def _release_auto_index_slot() -> None:
"""Release a previously reserved queue slot."""
global _pending_auto_index_jobs
with _pending_auto_index_lock:
_pending_auto_index_jobs = max(0, _pending_auto_index_jobs - 1)
def _shutdown_auto_index_executor() -> None:
"""Shutdown the auto-index executor gracefully.
Holds ``_auto_index_executor_lock`` for the read+nullify so two
concurrent callers (e.g. atexit + a test teardown) can't race into an
``AttributeError`` on the ``None`` reference; the blocking ``shutdown``
runs outside the lock. Resets the pending-jobs counter afterwards so a
re-created executor (tests, a dev-server reload) starts from a clean count
instead of inheriting a stale, possibly-saturated value.
"""
global _auto_index_executor, _pending_auto_index_jobs
with _auto_index_executor_lock:
executor = _auto_index_executor
_auto_index_executor = None
if executor is not None:
executor.shutdown(wait=True)
with _pending_auto_index_lock:
_pending_auto_index_jobs = 0
atexit.register(_shutdown_auto_index_executor)
def get_rag_service(
request: Request,
username: str,
collection_id: Optional[str] = None,
use_defaults: bool = False,
) -> LibraryRAGService:
"""
Get RAG service instance with appropriate settings.
Args:
request: The FastAPI request (for session lookup).
username: The authenticated username.
collection_id: Optional collection UUID to load stored settings from
use_defaults: When True, ignore stored collection settings and use
current defaults. Pass True on force-reindex so that the new
default embedding model is picked up.
"""
from ...research_library.services.rag_service_factory import (
get_rag_service as _get_rag_service,
)
from ...database.session_passwords import session_password_store
session_id = request.session.get("session_id")
db_password = None
if session_id:
db_password = session_password_store.get_session_password(
username, session_id
)
if not db_password:
db_password = session_password_store.get_any_session_password(username)
return _get_rag_service(
username,
collection_id,
use_defaults=use_defaults,
db_password=db_password,
)
def _get_text_separators(settings):
"""Return configured text separators, parsing string values if needed."""
# Load the stored setting. It may already be a list or a JSON string.
text_separators = settings.get_setting(
"local_search_text_separators",
DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS_JSON,
)
# If the setting is stored as a string, parse it into a list. A value that
# is not valid JSON (e.g. a not-yet-migrated corrupt row) falls back to the
# default separators — migration #4298 heals existing corrupt data.
if isinstance(text_separators, str):
try:
text_separators = json.loads(text_separators)
except json.JSONDecodeError:
logger.warning(
"Invalid JSON for local_search_text_separators setting: {!r}. Using default separators.",
text_separators,
)
text_separators = DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS
return text_separators
def _parse_configured_text_separators(value):
"""Coerce a text-separator setting to a list of strings, or None.
Called via ``asyncio.to_thread`` from ``configure_rag``: ``value``
comes straight from the request body, so a caller can hand it a
string up to that route's 16 MB body cap. ``json.loads`` runs at
~110 ms/MB, which is ~1.7 s at the cap -- fine on a worker thread
(Flask charged the same work to a request thread), a whole-instance
freeze on the event loop under single-worker uvicorn.
"""
separators = value
if isinstance(value, str):
try:
separators = json.loads(value)
except json.JSONDecodeError:
return None
if not isinstance(separators, list) or not all(
isinstance(separator, str) for separator in separators
):
return None
return separators
# Module prefix for exceptions DEFINED inside LDR itself. An exception whose
# class lives under ``local_deep_research`` is something only we can fix, so we
# steer the user to file a bug instead of second-guessing their model choice.
#
# We deliberately do NOT treat bare builtins (KeyError/TypeError/RuntimeError/
# ...) as internal: ``type(exc).__module__`` is where the class is *defined*,
# not where it was *raised*, so a builtin tells us nothing about whether LDR or
# a provider/langchain frame produced it. On this path an OpenAI-compatible
# server that returns a malformed 200 body can surface a bare TypeError from
# langchain's response parser; telling the user to "report it on GitHub" for
# that is exactly the misleading guidance #4208 set out to remove.
_INTERNAL_MODULE_PREFIX = "local_deep_research"
# Module prefixes whose exceptions are upstream-provider errors (network,
# auth, model not found, etc.). Matched against ``type(exc).__module__``.
_UPSTREAM_MODULE_PREFIXES = (
"openai",
"httpx",
"requests",
"urllib3",
"anthropic",
)
# Upstream exception classes that mean "the request never reached the
# provider, or never got an answer" as opposed to "the provider answered and
# refused". Told apart by class NAME across the MRO so no provider SDK has to
# be imported here, and matched only inside the upstream branch below — a
# builtins ``ConnectionError`` is still default-denied. The name is used to
# pick between two fixed messages; it is not part of either one.
_CONNECTION_FAILURE_CLASS_NAMES = frozenset(
{
"APIConnectionError", # openai (APITimeoutError subclasses it)
"ConnectError", # httpx
"ConnectionError", # requests.exceptions
"ConnectTimeout", # httpx
"ConnectTimeoutError", # urllib3
"NewConnectionError", # urllib3
"ProxyError", # httpx / requests / urllib3
}
)
def _module_matches(type_module: str, prefix: str) -> bool:
"""True if ``type_module`` is ``prefix`` itself or one of its submodules."""
return type_module == prefix or type_module.startswith(f"{prefix}.")
def _format_test_embedding_error(exc: Exception, model: str) -> str:
"""Build the user-facing error message for /api/rag/test-embedding.
Categorizes the exception by the module its *class* is defined in so the
UI distinguishes LDR-internal bugs (which the user can't fix and
shouldn't be steered into changing their model over) from real
upstream/provider errors. The previous implementation ran a keyword-match
heuristic over every exception text — see #4208, where
``NoSettingsContextError`` surfaced as a misleading "try a dedicated
embedding model" suggestion.
Exception text never crosses the HTTP boundary here. The exception's
module and class are used only to SELECT one of the fixed messages below;
neither is interpolated into the result, and the full detail stays in the
server-side log (``logger.exception`` at the call site).
Why not simply scrub the text and echo it, which is what
:func:`~...security.log_sanitizer.sanitize_error_for_client` is for
elsewhere in the tree: that function removes credential *shapes* plus
control characters, and caps the length. It does not remove server
filesystem paths, SQL text or dependency internals — and those are
exactly what the exceptions reachable from this ``try`` block carry. The
block opens the user's SQLCipher database and constructs an embedding
manager, so a ``sqlalchemy.exc.OperationalError`` (driver text plus
``[SQL: ...]`` and ``[parameters: ...]``), a ``builtins.OSError``
(absolute server path and OS account name) or a provider response body
are all in range. ``sanitize_error_for_client`` remains the right tool
for a message an author already knows to be safe; it is not a filter that
makes an arbitrary exception safe, which is why this function does not
use it.
"""
type_module = type(exc).__module__ or ""
if _module_matches(type_module, _INTERNAL_MODULE_PREFIX):
# Internal LDR exceptions can carry filesystem paths / SQL fragments
# that sanitize_error_message() does not pattern-match, so we do not
# echo their detail to the browser — the full trace is in the server
# logs (logger.exception at the call site) which the user is asked to
# attach.
return (
f"Embedding test failed for model '{model}' due to an "
"internal LDR error. This is a bug in LDR, "
"not your configuration. Please report it on GitHub with "
"the server logs."
)
if any(
_module_matches(type_module, prefix)
for prefix in _UPSTREAM_MODULE_PREFIXES
):
if _CONNECTION_FAILURE_CLASS_NAMES.intersection(
klass.__name__ for klass in type(exc).__mro__
):
# Nothing was rejected — the connection never completed. Saying
# "the provider rejected the request" here sends the user to
# check credentials for what is almost always a wrong host, a
# stopped Ollama, or a firewall.
return (
f"Embedding test failed for model '{model}'. Could not "
"connect to the provider; check the URL and that it is "
"reachable. The full error is in the server logs."
)
return (
f"Embedding test failed for model '{model}'. The request to the "
"provider failed. Check its URL, credentials, and model name; "
"the full error is in the server logs."
)
# Default-deny. Everything outside the upstream allowlist reaches here:
# sqlalchemy.exc.* (the try block opens the user's SQLCipher database, and
# a DBAPIError's str() renders the driver message plus [SQL: ...] and
# [parameters: ...]), builtins.OSError/PermissionError/FileNotFoundError
# (absolute server paths and the OS account name), chromadb,
# sentence_transformers, langchain_core, botocore... None of that is
# touched by the credential-shape scrubber, so no exception-derived detail
# is returned. The full text is in the server logs (logger.exception at
# the call site).
#
# Worded deliberately unlike the internal-LDR branch above: a bare
# TypeError from langchain's response parser is not an LDR bug, and
# steering the user to "report it on GitHub" for it is the misleading
# guidance #4208 removed.
return (
f"Embedding test failed for model '{model}' due to an unexpected "
"error. Check the provider URL and model name; the full error is "
"in the server logs."
)
# Config API Routes
@router.get("/api/config/supported-formats")
def get_supported_formats(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Return list of supported file formats for upload.
This endpoint provides the single source of truth for supported file
extensions, pulling from the document_loaders registry. The UI can
use this to dynamically update the file input accept attribute.
"""
from ...document_loaders import get_supported_extensions
extensions = get_supported_extensions()
# Sort extensions for consistent display
extensions = sorted(extensions)
return {
"extensions": extensions,
"accept_string": ",".join(extensions),
"count": len(extensions),
}
# Page Routes
@router.get("/embedding-settings")
def embedding_settings_page(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Render the Embedding Settings page."""
from ...embeddings.sentence_transformer_models import (
SENTENCE_TRANSFORMER_MODELS,
)
return templates.TemplateResponse(
request=request,
name="pages/embedding_settings.html",
context={
"request": request,
"active_page": "embedding-settings",
"sentence_transformer_models": SENTENCE_TRANSFORMER_MODELS,
"default_sentence_transformer_model": DEFAULT_LOCAL_SEARCH_MODEL,
},
)
@router.get("/document/{document_id}/chunks")
def view_document_chunks(
request: Request,
document_id,
username: Annotated[str, Depends(require_auth)],
):
"""View all chunks for a document across all collections."""
from ..utils.path_ids import validated_uuid_path_param
from ...database.session_context import get_user_db_session
from ...database.models.library import DocumentChunk
document_id = validated_uuid_path_param(document_id, "Document")
with get_user_db_session(username) as db_session:
# Get document info
document = db_session.query(Document).filter_by(id=document_id).first()
if not document:
# Browser navigation: return text/html, not JSON. These four page
# routes are reached from library.html as ordinary <a href> links, so a
# stale link showed the user a raw {"error": ...} body in the browser's
# JSON viewer. main returned `"Document not found", 404` (text/html)
# here; the sibling /api/ routes keep JSON.
return HTMLResponse("Document not found", status_code=404)
# Get all chunks for this document
chunks = (
db_session.query(DocumentChunk)
.filter(DocumentChunk.source_id == document_id)
.order_by(DocumentChunk.collection_name, DocumentChunk.chunk_index)
.all()
)
# Group chunks by collection
chunks_by_collection = {}
for chunk in chunks:
coll_name = chunk.collection_name
if coll_name not in chunks_by_collection:
# Get collection display name
collection_id = coll_name.replace("collection_", "")
collection = (
db_session.query(Collection)
.filter_by(id=collection_id)
.first()
)
chunks_by_collection[coll_name] = {
"name": collection.name if collection else coll_name,
"id": collection_id,
"chunks": [],
}
chunks_by_collection[coll_name]["chunks"].append(
{
"id": chunk.id,
"index": chunk.chunk_index,
"text": chunk.chunk_text,
"word_count": chunk.word_count,
"start_char": chunk.start_char,
"end_char": chunk.end_char,
"embedding_model": chunk.embedding_model,
"embedding_model_type": chunk.embedding_model_type.value
if chunk.embedding_model_type
else None,
"embedding_dimension": chunk.embedding_dimension,
"created_at": chunk.created_at,
}
)
return templates.TemplateResponse(
request=request,
name="pages/document_chunks.html",
context={
"request": request,
"document": document,
"chunks_by_collection": chunks_by_collection,
"total_chunks": len(chunks),
},
)
@router.get("/collections")
def collections_page(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Render the Collections page."""
return templates.TemplateResponse(
request=request,
name="pages/collections.html",
context={"request": request, "active_page": "collections"},
)
# NOTE: this static route MUST be registered before the parameterized
# /collections/{collection_id} below — FastAPI matches in registration
# order, so the param route would otherwise swallow /collections/create
# as collection_id="create" and render the details page instead of the
# create form (fenced by tests/web/routers/test_route_ordering.py).
@router.get("/collections/create")
def collection_create_page(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Render the Create Collection page."""
return templates.TemplateResponse(
request=request,
name="pages/collection_create.html",
context={"request": request, "active_page": "collections"},
)
def _validated_collection_id(collection_id: str) -> str:
"""Return ``collection_id``'s canonical UUID form if well-formed, else 404.
Collection ids are server-generated UUID4 strings (`Collection.id` is
String(36), assigned from `str(uuid.uuid4())`), so anything else is either
a typo or an injection attempt and cannot match a real row.
This was the first fix for a stored-reflection XSS: collection_upload.html
interpolated this value into an inline `onclick` handler, and
HTML-escaping is NOT sufficient there -- the browser entity-decodes
attribute content before treating it as JS, so an escaped quote reopens
the string. That template now passes the ID as a data attribute read by a
script listener instead. This check stays as the second layer: validating
the shape at the boundary means no template can receive a value capable of
breaking out, regardless of the context it is interpolated into, which
also covers collection_details.html and anything added later.
(`/collections/create` is a separate static route registered ahead of the
parameterised ones, so it never reaches this.)
The return value is the *canonical* form of the id (lowercase,
hyphenated, no wrapper) rather than the verbatim input, so alias
spellings of the same UUID — for example an ``urn:uuid:`` prefix,
``{...}`` braces, hyphen-free hex, or uppercase hex — come back in
the same shape stored on disk, which is what makes "well-formed
UUID string" above actually true of the return value. ``uuid.UUID``
tolerates a few more, such as ``_`` digit separators; all are
canonicalised here. Length is the binding reject: a padded input
that pushes the total past 32 characters fails the parser and 404s;
shorter padded inputs (e.g. leading/trailing whitespace) are
accepted by ``int(..., 16)`` and canonicalise with zero-padding,
and CPython's base-16 parser also accepts Unicode digit
characters (`"𝟏"` / Arabic-Indic digits), so this validator is
NOT a strict whitelist of ASCII hex — the canonical output is
pure ASCII hex, which is what makes it safe to interpolate in
any template context (#6602; the shared helper
``web/utils/path_ids.py::validated_uuid_path_param`` has the same
shape, this local twin stays in ``rag.py`` so the route-table
parity scanner keeps seeing its 404).
"""
try:
canonical = str(uuid.UUID(str(collection_id)))
except (ValueError, AttributeError, TypeError):
logger.warning(
f"Rejected malformed collection_id: {_log_value_preview(collection_id)}"
)
# `from None` mirrors `validated_uuid_path_param`: the only
# caller of this guard sees a clean 404 detail, and the
# `uuid.UUID` parse-failure traceback adds nothing the
# `_log_value_preview` line above doesn't already carry.
raise HTTPException(
status_code=404, detail="Collection not found"
) from None
return canonical
@router.get("/collections/{collection_id}")
def collection_details_page(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Render the Collection Details page."""
collection_id = _validated_collection_id(collection_id)
return templates.TemplateResponse(
request=request,
name="pages/collection_details.html",
context={
"active_page": "collections",
"collection_id": collection_id,
},
)
@router.get("/collections/{collection_id}/upload")
def collection_upload_page(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Render the Collection Upload page."""
collection_id = _validated_collection_id(collection_id)
from ...database.session_context import get_user_db_session
from ...utilities.db_utils import get_settings_manager
# Get the upload PDF storage setting
with get_user_db_session(username) as db_session:
settings = get_settings_manager(db_session, username)
upload_pdf_storage = settings.get_setting(
"research_library.upload_pdf_storage", "none"
)
# Only allow valid values for uploads (no filesystem)
if upload_pdf_storage not in ("database", "none"):
upload_pdf_storage = "none"
return templates.TemplateResponse(
request=request,
name="pages/collection_upload.html",
context={
"active_page": "collections",
"collection_id": collection_id,
"collection_name": None,
"upload_pdf_storage": upload_pdf_storage,
},
)
# API Routes
@router.get("/api/rag/settings")
def get_current_settings(
request: Request,
username: Annotated[str, Depends(require_auth)],
):
"""Get current RAG configuration from settings."""
from ...database.session_context import get_user_db_session
try:
with get_user_db_session(username) as db_session:
settings = get_settings_manager(db_session, username)
return {
"success": True,
"settings": {
"embedding_provider": settings.get_setting(
"local_search_embedding_provider",
DEFAULT_LOCAL_SEARCH_PROVIDER,
),
"embedding_model": settings.get_setting(
"local_search_embedding_model",
DEFAULT_LOCAL_SEARCH_MODEL,
),
"chunk_size": settings.get_setting(
"local_search_chunk_size",
DEFAULT_LOCAL_SEARCH_CHUNK_SIZE,
),
"chunk_overlap": settings.get_setting(
"local_search_chunk_overlap",
DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP,
),
"splitter_type": settings.get_setting(
"local_search_splitter_type",
DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE,
),
"text_separators": _get_text_separators(settings),
"distance_metric": settings.get_setting(
"local_search_distance_metric",
DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC,
),
"normalize_vectors": settings.get_setting(
"local_search_normalize_vectors",
DEFAULT_LOCAL_SEARCH_NORMALIZE_VECTORS,
),
"index_type": settings.get_setting(
"local_search_index_type",
DEFAULT_LOCAL_SEARCH_INDEX_TYPE,
),
},
}
except Exception as e:
return handle_api_error("getting RAG settings", e)
@router.post("/api/rag/test-embedding")
async def test_embedding(
request: Request,
username: Annotated[str, Depends(require_auth)],
):
"""Test an embedding configuration by generating a test embedding."""
from ...database.session_context import get_user_db_session
from ...utilities.db_utils import get_settings_manager
# Pre-bound: the except block below formats an error using these, so a
# malformed/empty body would otherwise raise UnboundLocalError from
# inside the handler's own error path.
provider = None
model = None
try:
data = await request.json()
if not isinstance(data, dict):
return json_body_error("success", "Request body must be valid JSON")
provider = data.get("provider")
model = data.get("model")
if not provider or not model:
return JSONResponse(
{"success": False, "error": "Provider and model are required"},
status_code=400,
)
from ...embeddings.embeddings_config import get_embeddings
logger.info(
"Testing embedding with provider={}, model={}",
_log_value_preview(provider),
_log_value_preview(model),
)
# Get user's settings so provider URLs (e.g. Ollama) are resolved correctly.
# Sync SQLAlchemy + a network-bound embedding call — both must run in a
# thread, never on the uvicorn event loop.
def _run_embedding_test() -> tuple[Any, int]:
with get_user_db_session(username) as db_session:
settings = get_settings_manager(db_session, username)
settings_snapshot = (
settings.get_all_settings()
if hasattr(settings, "get_all_settings")
else {}
)
start = time.time()
# get_embeddings (not get_embedding_function) so the owning
# instance is reachable here for cleanup: the wrapped callable
# from get_embedding_function discards it, leaking the
# per-instance httpx client(s) built for this one-shot test.
embeddings = get_embeddings(
provider=provider,
model=model,
settings_snapshot=settings_snapshot,
)
try:
emb = embeddings.embed_documents([_EMBEDDING_TEST_SENTENCE])[0]
finally:
from ...utilities.llm_utils import _close_base_llm
try:
_close_base_llm(embeddings)
except Exception:
logger.warning("Failed to close test embedding resources")
return emb, int((time.time() - start) * 1000)
# run_db_sync (not raw to_thread): the thunk opens a
# get_user_db_session block, and to_thread's reused workers would
# keep the user's thread-local session attached after the task.
embedding, response_time_ms = await run_db_sync(_run_embedding_test)
# Get embedding dimension
dimension = len(embedding) if hasattr(embedding, "__len__") else None
return {
"success": True,
"dimension": dimension,
"response_time_ms": response_time_ms,
"provider": provider,
"model": model,
}
except json.JSONDecodeError:
# Caught here (ahead of the broad `except Exception` below) so a
# malformed body gets the route's own "success"-shaped 400 instead
# of falling into `_format_test_embedding_error`, which would echo
# the raw decoder message (e.g. "Expecting value: line 1 column 1")
# back to the client as part of a hardcoded 500.
return json_body_error("success", "Request body must be valid JSON")
except Exception as e:
# Egress policy denial → clean 4xx (the embedding never fired).
from ...security.egress.policy import PolicyDeniedError
if isinstance(e, PolicyDeniedError):
reason = e.decision.reason
if reason == "embeddings_model_not_curated":
return JSONResponse(
{
"success": False,
"error": f"Sentence Transformers model '{model}' is not in LDR's vetted model list. Choose one of the listed models, or use Ollama for custom locally managed embedding models. Direct arbitrary Hugging Face downloads are disabled.",
"reason_code": reason,
},
status_code=400,
)
if reason == "embeddings_cache_revision_unknown":
return JSONResponse(
{
"success": False,
"error": f"The existing Sentence Transformers cache for '{model}' does not identify a single immutable model revision, so a new download is refused: it could change the model behind an existing index. To start over, delete that model's cache directory (the 'models--...' folder under the Hugging Face cache, by default ~/.cache/huggingface/hub) and run this test again under Public only — do this only if no collection was indexed with it. To keep an existing index, put a trusted copy of the model under the application's models directory and select it by that name instead, or select a different model.",
"reason_code": reason,
},
status_code=400,
)
if reason == "embeddings_model_not_cached":
return JSONResponse(
{
"success": False,
"error": f"Sentence Transformers model '{model}' has not been downloaded yet. Set Egress Scope to Public only, or use Adaptive with a public primary and no private collection selected. If 'Require local embeddings' is enabled, turn it off for this download, then run this test again. After the download completes, you can return to your preferred restrictive scope.",
"reason_code": reason,
},
status_code=400,
)
return JSONResponse(
{
"success": False,
"error": f"Embedding provider '{provider}' refused by egress policy ({reason}). Disable 'Require local embeddings' or pick a local provider.",
"reason_code": reason,
},
status_code=400,
)
logger.exception("Error during testing embedding")
user_message = _format_test_embedding_error(e, model)
# The formatter uses exception type metadata only to select a fixed
# message. Raw exception text stays in the server log above.
return JSONResponse(
{"success": False, "error": user_message}, status_code=500
)
@router.get("/api/rag/models")
def get_available_models(
request: Request,
username: Annotated[str, Depends(require_auth)],
):
"""Get list of available embedding providers and models."""
from ...database.session_context import get_user_db_session
from ...utilities.db_utils import get_settings_manager
try:
from ...embeddings.embeddings_config import _get_provider_classes
from ...security.egress.policy import (
Decision,
PolicyDeniedError,
)
# Get current settings for providers. STRICT snapshot is mandatory
# at this policy-sensitive seam: a non-strict read silently falls
# back to JSON defaults when the underlying settings query fails
# (SQLAlchemyError / stale enum row), and a defaults-only snapshot
# can lack the operator-selected scope/provider inputs that the
# local-only posture is supposed to enforce. We refuse before any
# provider discovery, credential read, or network probe instead of
# admitting a cloud embedder under a permissive fallback.
with get_user_db_session(username) as db_session:
settings = get_settings_manager(db_session, username)
try:
settings_snapshot = (
settings.get_all_settings(strict=True)
if hasattr(settings, "get_all_settings")
else {}
)
except PolicyDeniedError:
raise
except Exception as exc:
raise PolicyDeniedError(
Decision(False, "settings_unavailable"),
target="available_rag_models",
) from exc
# Get provider classes
provider_classes = _get_provider_classes()
# Egress policy: availability checks and model discovery can open
# connections to a provider. Under an effective
# local-only embeddings posture (PRIVATE_ONLY / adaptive-private) we
# must NOT probe remote-classified providers — mirror the PEP in
# embeddings_config.get_embeddings(). Fail closed before either
# provider call if the policy can't evaluate.
def _embeddings_model_fetch_allowed(provider_key: str) -> bool:
try:
from ...security.egress.policy import (
context_from_snapshot,
evaluate_embeddings,
resolve_run_primary_engine,
)
# Thread username so a per-user private retriever primary
# resolves PRIVATE_ONLY (forcing local embeddings) and this
# route can't probe a remote embedder for that user.
ctx = context_from_snapshot(
settings_snapshot,
resolve_run_primary_engine(settings_snapshot),
username=username,
)
if not ctx.require_local_embeddings:
return True
decision = evaluate_embeddings(
provider_key, ctx, settings_snapshot=settings_snapshot
)
if not decision.allowed:
logger.bind(policy_audit=True).info(
"skipping embeddings model-list probe "
"(local-only egress posture)",
provider=provider_key,
reason=decision.reason,
)
return decision.allowed
except Exception:
# Fail closed: a policy/snapshot error must not open a cloud
# probe under a local-only posture.
logger.bind(policy_audit=True).warning(
"embeddings model-list egress check failed; skipping probe",
provider=provider_key,
exc_info=True,
)
return False
# Provider display names
provider_labels = {
"sentence_transformers": "Sentence Transformers (Local)",
"ollama": "Ollama (Local)",
"openai": "OpenAI API",
}
# Get provider options and models by looping through providers
provider_options = []
providers = {}
for provider_key, provider_class in provider_classes.items():
policy_allowed = _embeddings_model_fetch_allowed(provider_key)
available = policy_allowed and provider_class.is_available(
settings_snapshot
)
# Always show the provider in the dropdown so users can
# configure its settings (e.g. fix a wrong Ollama URL).
provider_options.append(
{
"value": provider_key,
"label": provider_labels.get(provider_key, provider_key),
"available": available,
"policy_allowed": policy_allowed,
}
)
# Availability is checked only after the policy permits probing.
if available:
models = provider_class.get_available_models(settings_snapshot)
providers[provider_key] = [
{
"value": m["value"],
"label": m["label"],
"provider": provider_key,
**(
{"is_embedding": m["is_embedding"]}
if "is_embedding" in m
else {}
),
}
for m in models
]
else:
providers[provider_key] = []
return {
"success": True,
"provider_options": provider_options,
"providers": providers,
}
except Exception as e:
# Egress policy denial (incl. settings_unavailable) → clean 4xx/5xx
# so the caller distinguishes a refused probe from a server crash.
from ...security.egress.policy import PolicyDeniedError as _PDE
if isinstance(e, _PDE):
reason = e.decision.reason
status_code = 503 if reason == "settings_unavailable" else 400
logger.bind(policy_audit=True).info(
"available-models refused by egress policy",
reason=reason,
target=e.target,
)
return JSONResponse(
{
"success": False,
"error": (
"Settings are currently unavailable; cannot safely "
"enumerate embedding models."
)
if reason == "settings_unavailable"
else f"Available-models request refused ({reason}).",
},
status_code=status_code,
)
return handle_api_error("getting available models", e)
@router.get("/api/rag/info")
def get_index_info(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Get information about the current RAG index."""
from ...database.library_init import get_default_library_id
try:
# Get collection_id from request or use default Library collection
collection_id = request.query_params.get("collection_id")
if not collection_id:
collection_id = get_default_library_id(username)
logger.info(
f"Getting RAG index info for collection_id: {_log_value_preview(collection_id)}"
)
# `with`: LocalEmbeddingManager eagerly opens httpx client pairs for
# Ollama embeddings, so an unclosed service leaks file descriptors on
# every call (#4407). This is a hot endpoint — the collection page
# hits it on each load.
with get_rag_service(request, username, collection_id) as rag_service:
info = rag_service.get_current_index_info(collection_id)
if info is None:
logger.info(
f"No RAG index found for collection_id: {_log_value_preview(collection_id)}"
)
return {"success": True, "info": None, "message": "No index found"}
logger.info(
f"Found RAG index for collection_id: {_log_value_preview(collection_id)}"
)
return {"success": True, "info": info}
except Exception as e:
return handle_api_error("getting index info", e)
@router.get("/api/rag/stats")
def get_rag_stats(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Get RAG statistics for a collection."""
from ...database.library_init import get_default_library_id
try:
# Get collection_id from request or use default Library collection
collection_id = request.query_params.get("collection_id")
if not collection_id:
collection_id = get_default_library_id(username)
with get_rag_service(request, username, collection_id) as rag_service:
stats = rag_service.get_rag_stats(collection_id)
return {"success": True, "stats": stats}
except Exception as e:
return handle_api_error("getting RAG stats", e)
@router.post("/api/rag/index-document")
async def index_document(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Index a single document in a collection."""
from ...database.library_init import get_default_library_id
text_doc_id = None
try:
data = await request.json()
if not isinstance(data, dict):
return json_body_error("success", "Request body must be valid JSON")
text_doc_id = data.get("text_doc_id")
force_reindex = data.get("force_reindex", False)
collection_id = data.get("collection_id")
if not isinstance(force_reindex, bool):
return JSONResponse(
{
"success": False,
"error": "force_reindex must be a boolean",
},
status_code=400,
)
if not text_doc_id:
return JSONResponse(
{"success": False, "error": "text_doc_id is required"},
status_code=400,
)
if not collection_id:
# Opens the user's SQLCipher session (sync) — threadpool it.
collection_id = await run_db_sync(get_default_library_id, username)
def _index_sync():
with get_rag_service(
request, username, collection_id
) as rag_service:
return rag_service.index_document(
text_doc_id, collection_id, force_reindex
)
result = await run_db_sync(_index_sync)
if result["status"] == "error":
return JSONResponse(
{
"success": False,
"error": _client_indexing_error(result.get("error")),
},
status_code=400,
)
return {"success": True, "result": result}
except json.JSONDecodeError:
return json_body_error("success", "Request body must be valid JSON")
except Exception as e:
return handle_api_error(f"indexing document {text_doc_id}", e)
@router.post("/api/rag/remove-document")
async def remove_document(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Remove a document from RAG in a collection."""
from ...database.library_init import get_default_library_id
# Pre-bound for the same reason as test_embedding above: the except
# block interpolates text_doc_id into its error message.
text_doc_id = None
try:
data = await request.json()
if not isinstance(data, dict):
return json_body_error("success", "Request body must be valid JSON")
text_doc_id = data.get("text_doc_id")
collection_id = data.get("collection_id")
if not text_doc_id:
return JSONResponse(
{"success": False, "error": "text_doc_id is required"},
status_code=400,
)
if not collection_id:
# Opens the user's SQLCipher session (sync) — threadpool it.
collection_id = await run_db_sync(get_default_library_id, username)
def _remove_sync():
with get_rag_service(
request, username, collection_id
) as rag_service:
return rag_service.remove_document_from_rag(
text_doc_id, collection_id
)
result = await run_db_sync(_remove_sync)
if result["status"] == "error":
return JSONResponse(
{
"success": False,
"error": _client_indexing_error(result.get("error")),
},
status_code=400,
)
return {"success": True, "result": result}
except json.JSONDecodeError:
return json_body_error("success", "Request body must be valid JSON")
except Exception as e:
return handle_api_error(f"removing document {text_doc_id}", e)
@router.post("/api/rag/index-all")
def index_all(
request: Request,
username: Annotated[str, Depends(require_auth)],
):
"""Index all documents in a collection with Server-Sent Events progress.
POST, not GET, because it mutates: it resolves the caller's collection,
writes embedding metadata, and runs the embedding/index pipeline over
every document in it (``?force_reindex=true`` stages and replaces
vectors that already exist). CSRFMiddleware keys enforcement on the
method, so declaring it here is what puts it behind the token check —
a GET would be outside the middleware entirely and startable from
same-site markup riding the victim's session cookie. Progress still
streams as Server-Sent Events; the caller is a fetch client, which can
send both the X-CSRFToken header and a POST, so nothing about the
streaming response changes. Query parameters (``collection_id``,
``force_reindex``) are unchanged.
"""
from ...database.session_context import get_user_db_session
from ...database.library_init import get_default_library_id
from ...utilities.db_utils import get_settings_manager
# Parse as bool — a raw string "false" is truthy, which would silently
# force a full reindex for every caller that asked to skip already-indexed
# documents.
force_reindex = (
request.query_params.get("force_reindex", "false").lower() == "true"
)
# Get collection_id from request or use default Library collection
collection_id = request.query_params.get("collection_id")
if not collection_id:
collection_id = get_default_library_id(username)
if force_reindex:
# A replacement is one transaction for the complete collection. Use
# the heartbeat-capable stream instead of publishing batch by batch.
return index_collection(request, collection_id, username)
logger.info(
f"Starting index-all for collection_id: {_log_value_preview(collection_id)}, force_reindex: {force_reindex}"
)
# Create RAG service in request context before generator runs.
# use_defaults=force_reindex mirrors index_collection / the background
# worker so a force-reindex resolves the same embedding config.
rag_service = get_rag_service(
request, username, collection_id, use_defaults=force_reindex
)
# Import before building the SSE generator. Importing inside its try
# would leave the exception handler unbound if this import failed.
# Keep it below module scope because it pulls in the deletion package.
from ...research_library.deletion.utils.cascade_helper import (
RemoteVectorStoreDropError,
remote_drop_failure_message,
)
# Fetch batch size from settings in this thread before generator runs
with get_user_db_session(username) as db_session:
settings = get_settings_manager(db_session, username)
batch_size = int(settings.get_setting("rag.indexing_batch_size", 15))
try:
_max_workers = int(
settings.get_setting("rag.indexing_max_parallel_docs", 4)
)
except SettingsValueError:
raise
except Exception:
_max_workers = 4
_max_workers = max(1, min(_max_workers, 16))
def generate():
"""Generator function for SSE progress updates."""
_sse_cancel = threading.Event()
try:
if not _register_sse_indexer(
username, collection_id, None, _sse_cancel
):
yield f"data: {json.dumps({'type': 'error', 'error': 'Indexing is already in progress for this collection'})}\n\n"
return
# Send initial status
yield f"data: {json.dumps({'type': 'start', 'message': 'Starting bulk indexing...'})}\n\n"
# Get document IDs to index from DocumentCollection. All DB work
# happens inside this SHORT-LIVED session; NO ``yield`` runs while
# it is held (the "collection not found" error is yielded AFTER the
# block) — keeping a get_user_db_session scope open across a yield
# risks the Starlette/anyio thread-affinity corruption fixed in
# download_bulk (__enter__/__exit__ can land on different threads).
collection_found = True
doc_info = []
with get_user_db_session(username) as db_session:
# Persist embedding metadata + clean up on force-reindex via the
# shared helpers, so this bulk route behaves like the
# single-collection SSE route and the background worker
# (previously it stored no embedding_dimension and never reset
# stale chunks/indices on force-reindex).
collection = (
db_session.query(Collection)
.filter_by(id=collection_id)
.first()
)
if not collection:
collection_found = False
else:
# Ordinary indexing records its initial configuration.
# Forced rebuilds publish metadata with the new generation.
if collection.embedding_model is None and not force_reindex:
_store_collection_embedding_metadata(
collection, rag_service
)
db_session.commit()
doc_info = [
(doc.id, doc.title)
for _dc, doc in _query_documents_to_index(
db_session, collection_id, force_reindex
)
]
if not collection_found:
yield f"data: {json.dumps({'type': 'error', 'error': 'Collection not found'})}\n\n"
return
if not doc_info:
yield f"data: {json.dumps({'type': 'complete', 'results': {'successful': 0, 'skipped': 0, 'failed': 0, 'message': 'No documents to index'}})}\n\n"
return
results = {"successful": 0, "skipped": 0, "failed": 0, "errors": []}
total = len(doc_info)
# Process documents in batches to pace SSE progress events.
# The batch boundary is purely cosmetic now — documents within
# a batch run in parallel via the bounded worker pool below.
# batch_size and _max_workers are read from settings before the
# generator starts (closure capture) — no DB work in-stream.
processed = 0
for i in range(0, len(doc_info), batch_size):
batch = doc_info[i : i + batch_size]
# Run the per-batch docs through the bounded parallel
# helper. ``as_completed`` order means progress events
# fire in completion order, not submission order — the
# `processed` counter below keeps the SSE payload
# monotonically increasing so the UI percent stays sane.
# ``is_cancelled`` polls the SSE-disconnect event so an
# early-disconnect watcher (or this generator's own
# ``finally``) can short-circuit the batch before all
# queued futures run. The helper still drains in-flight
# workers via ``pool.shutdown(wait=True, ...)`` so no
# worker outlives the helper return — see
# ``index_documents_parallel`` for the rationale.
parallel_result = rag_service.index_documents_parallel(
batch,
collection_id,
force_reindex=force_reindex,
max_workers=_max_workers,
is_cancelled=_sse_cancel.is_set,
)
batch_results = parallel_result["results"]
for doc_id, title in batch:
processed += 1
result = batch_results[doc_id]
# Send progress update
yield f"data: {json.dumps({'type': 'progress', 'current': processed, 'total': total, 'title': title, 'percent': int((processed / total) * 100)})}\n\n"
if result["status"] == "success":
results["successful"] += 1
elif result["status"] in ("skipped", "cleared"):
# "cleared" (empty-text purge) is a handled non-failure.
results["skipped"] += 1
else:
results["failed"] += 1
results["errors"].append(
{
"doc_id": doc_id,
"title": title,
"error": _client_indexing_error(
result.get("error")
),
}
)
# Send completion status
yield f"data: {json.dumps({'type': 'complete', 'results': results})}\n\n"
# Log final status for debugging
logger.info(
f"Bulk indexing complete: {results['successful']} successful, {results['skipped']} skipped, {results['failed']} failed"
)
except RemoteVectorStoreDropError as exc:
# Caught ahead of the catch-all below: this is an actionable
# vector-store failure, not an internal error. Force-reindex rolls
# back, keeping the RAGIndex rows (the remote collections' only
# handle); the message says whether a retry can help and whether
# some collections were already dropped (#5765). Logged from the
# error's sanitized fields, with no traceback (a traceback would
# add frame state; the backend's error text can carry secrets).
drop_detail = exc.log_detail()
logger.warning(
"Force-reindex aborted: could not drop the vector-store "
f"collection before rebuilding it: {drop_detail}"
)
drop_error = {
"type": "error",
"error": remote_drop_failure_message(
exc, "indexing was aborted"
),
}
yield f"data: {json.dumps(drop_error)}\n\n"
except Exception:
logger.exception("Error in bulk indexing")
yield f"data: {json.dumps({'type': 'error', 'error': 'An internal error occurred during indexing'})}\n\n"
finally:
# Generator ``finally`` runs at stream completion (or client
# disconnect via ``GeneratorExit``) — the safe place to release
# the RAG service's embedding-manager httpx clients. Closing at
# the outer route scope would tear it down before the streamed
# generator runs. ``safe_close`` swallows close-time errors so
# a broken Ollama doesn't mask the original generator outcome.
#
# Signal cancellation BEFORE closing: the helper has already
# drained in-flight workers (its ``shutdown(wait=True, ...)``
# guarantees no worker outlives the helper return), so setting
# the event here is for any in-progress batch loop iteration
# and any future code path that bypasses the helper.
_sse_cancel.set()
_unregister_sse_indexer(username, collection_id, _sse_cancel)
safe_close(rag_service, "rag_service (index-all SSE)")
return WorkerCleanupStreamingResponse(
generate(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache, no-transform",
"X-Accel-Buffering": "no",
},
)
@router.post("/api/rag/configure")
async def configure_rag(
request: Request,
username: Annotated[str, Depends(require_auth)],
):
"""
Change RAG configuration (embedding model, chunk size, etc.).
This will create a new index with the new configuration.
"""
from ...database.session_context import get_user_db_session
from ...utilities.db_utils import get_settings_manager
try:
data = await request.json()
if not isinstance(data, dict):
return json_body_error("success", "Request body must be valid JSON")
embedding_model = data.get("embedding_model")
embedding_provider = data.get("embedding_provider")
chunk_size = data.get("chunk_size")
chunk_overlap = data.get("chunk_overlap")
collection_id = data.get("collection_id")
# Get new advanced settings (with defaults)
splitter_type = data.get(
"splitter_type", DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE
)
text_separators = data.get(
"text_separators", DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS
)
distance_metric = data.get(
"distance_metric", DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC
)
normalize_vectors = data.get(
"normalize_vectors", DEFAULT_LOCAL_SEARCH_NORMALIZE_VECTORS
)
index_type = data.get("index_type", DEFAULT_LOCAL_SEARCH_INDEX_TYPE)
if (
not embedding_model
or not embedding_provider
or chunk_size is None
or chunk_overlap is None
):
return JSONResponse(
{
"success": False,
"error": "All configuration parameters are required (embedding_model, embedding_provider, chunk_size, chunk_overlap",
},
status_code=400,
)
if type(chunk_size) is not int or not (
_RAG_CHUNK_SIZE_MIN <= chunk_size <= _RAG_CHUNK_SIZE_MAX
):
return JSONResponse(
{
"success": False,
"error": (
"chunk_size must be an integer between "
f"{_RAG_CHUNK_SIZE_MIN} and {_RAG_CHUNK_SIZE_MAX}"
),
},
status_code=400,
)
if type(chunk_overlap) is not int or not (
_RAG_CHUNK_OVERLAP_MIN <= chunk_overlap <= _RAG_CHUNK_OVERLAP_MAX
):
return JSONResponse(
{
"success": False,
"error": (
"chunk_overlap must be an integer between "
f"{_RAG_CHUNK_OVERLAP_MIN} and "
f"{_RAG_CHUNK_OVERLAP_MAX}"
),
},
status_code=400,
)
if chunk_overlap > chunk_size:
return JSONResponse(
{
"success": False,
"error": (
"chunk_overlap must be less than or equal to chunk_size"
),
},
status_code=400,
)
if not isinstance(normalize_vectors, bool):
return JSONResponse(
{
"success": False,
"error": "normalize_vectors must be a boolean",
},
status_code=400,
)
# The local_search_text_separators setting is registered with
# ui_element "json", so store the separators as a proper list. A
# string payload (e.g. a textarea value) is parsed into a list
# first; anything that isn't a JSON array of strings is rejected
# outright (400) rather than silently coerced to defaults, so a
# malformed payload can't get written into settings or fed into
# the chunker.
text_separators = await asyncio.to_thread(
_parse_configured_text_separators, text_separators
)
if text_separators is None:
return JSONResponse(
{
"success": False,
"error": "text_separators must be a JSON array of strings",
},
status_code=400,
)
# All settings writes + the collection reconfiguration (FAISS index
# creation) run sync I/O. Offload to a worker thread so the uvicorn
# event loop is free to serve other requests during the potentially
# slow index-create path.
def _persist_configuration():
"""Persist the settings and (optionally) create/promote the
collection's index in ONE database transaction.
Settings and the selected index must land atomically: a
mid-way failure (locked/environment-locked setting, DB error)
must leave BOTH unchanged, never commit the settings while the
old index stays "current" (or vice versa) — that mismatch is
exactly what would let a subsequent search silently run
against a stale/inconsistent configuration.
Returns a ``JSONResponse`` for an error that should short-
circuit the request, or the new/reused index hash (``None``
when no ``collection_id`` was supplied) on success.
"""
with get_user_db_session(username) as db_session:
settings = get_settings_manager(db_session, username)
requested_settings = (
("local_search_embedding_model", embedding_model),
("local_search_embedding_provider", embedding_provider),
("local_search_chunk_size", int(chunk_size)),
("local_search_chunk_overlap", int(chunk_overlap)),
("local_search_splitter_type", splitter_type),
("local_search_text_separators", text_separators),
("local_search_distance_metric", distance_metric),
(
"local_search_normalize_vectors",
normalize_vectors,
),
("local_search_index_type", index_type),
)
requested_setting_keys = [
key for key, _value in requested_settings
]
if settings.settings_locked:
return JSONResponse(
{
"success": False,
"error": "RAG configuration is locked by app.lock_settings",
},
status_code=403,
)
# Environment-locked settings (LDR_* env vars) must never be
# overwritten through a settings-mutation endpoint. Refuse
# the WHOLE request up front, before any write, rather than
# silently skipping just the locked keys — that would both
# mislead the caller with a 200 and partially apply an
# inconsistent configuration.
environment_locked_keys = [
key
for key, _value in requested_settings
if check_env_setting(key) is not None
]
if environment_locked_keys:
return JSONResponse(
{
"success": False,
"error": (
"Settings "
f"{', '.join(environment_locked_keys)} "
"are environment-locked"
),
},
status_code=403,
)
for key, value in requested_settings:
if not settings.set_setting(key, value, commit=False):
db_session.rollback()
return JSONResponse(
{
"success": False,
"error": "Unable to save RAG configuration. No changes were applied.",
},
status_code=500,
)
index_hash = None
if collection_id:
with LibraryRAGService(
username=username,
embedding_model=embedding_model,
embedding_provider=embedding_provider,
chunk_size=int(chunk_size),
chunk_overlap=int(chunk_overlap),
splitter_type=splitter_type,
text_separators=text_separators,
distance_metric=distance_metric,
normalize_vectors=normalize_vectors,
index_type=index_type,
) as new_rag_service:
rag_index = new_rag_service._get_or_create_rag_index(
collection_id, db_session=db_session, commit=False
)
index_hash = rag_index.index_hash
db_session.commit()
settings.emit_settings_changed_after_commit(
requested_setting_keys
)
return index_hash
# run_db_sync (not raw to_thread): get_user_db_session/LibraryRAGService
# open the user's DB session; reused to_thread workers must not retain it.
result = await run_db_sync(_persist_configuration)
if isinstance(result, JSONResponse):
return result
index_hash = result
if collection_id:
return {
"success": True,
"message": "Configuration updated for collection. You can now index documents with the new settings.",
"index_hash": index_hash,
}
# Just saving default settings without updating a specific collection
return {
"success": True,
"message": "Default embedding settings saved successfully. New collections will use these settings.",
}
except json.JSONDecodeError:
return json_body_error("success", "Request body must be valid JSON")
except Exception as e:
return handle_api_error("configuring RAG", e)
@router.get("/api/rag/documents")
def get_documents(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Get library documents with their RAG status for the default Library collection (paginated).
Query parameters:
page (int): 1-indexed page number, clamped to 1-10000 (default 1;
non-numeric input falls back to 1).
per_page (int): rows per page, clamped to 1-100 (default 50;
non-numeric input falls back to 50).
"""
from ...database.session_context import get_user_db_session
from ...database.library_init import get_default_library_id
try:
# Get pagination parameters
try:
page = int(request.query_params.get("page", "1"))
except (TypeError, ValueError):
page = 1
try:
per_page = int(request.query_params.get("per_page", "50"))
except (TypeError, ValueError):
per_page = 50
page = max(1, page)
filter_type = request.query_params.get(
"filter", "all"
) # all, indexed, unindexed
# Validate pagination parameters
# Upper-bound the page as well. A numerically valid but astronomical
# ?page=10**40 reaches .offset() and raises OverflowError from
# SQLite's 64-bit integer conversion, surfacing as a 500. The
# try/except above only rescues NON-numeric input. Mirrors
# metrics.py's _MAX_PAGE, which already handles this correctly.
page = max(1, min(page, _MAX_PAGE))
per_page = min(max(1, per_page), 100)
# Close current thread's session to force fresh connection
from ...database.thread_local_session import cleanup_current_thread
cleanup_current_thread()
# Get collection_id from request or use default Library collection
collection_id = request.query_params.get("collection_id")
if not collection_id:
collection_id = get_default_library_id(username)
logger.info(
f"Getting documents for collection_id: {_log_value_preview(collection_id)}, filter: {_log_value_preview(filter_type)}, page: {page}"
)
with get_user_db_session(username) as db_session:
# Expire all cached objects to ensure we get fresh data from DB
db_session.expire_all()
# Import RagDocumentStatus model
from ...database.models.library import RagDocumentStatus
# Build base query - join Document with DocumentCollection for the collection
# LEFT JOIN with rag_document_status to check indexed status
query = (
db_session.query(
Document, DocumentCollection, RagDocumentStatus
)
.join(
DocumentCollection,
(DocumentCollection.document_id == Document.id)
& (DocumentCollection.collection_id == collection_id),
)
.outerjoin(
RagDocumentStatus,
(RagDocumentStatus.document_id == Document.id)
& (RagDocumentStatus.collection_id == collection_id),
)
)
logger.opt(lazy=True).debug(
"Base query for collection {}: {}",
lambda: _log_value_preview(collection_id),
lambda: query,
)
# Apply filters based on rag_document_status existence
if filter_type == "indexed":
query = query.filter(RagDocumentStatus.document_id.isnot(None))
elif filter_type == "unindexed":
# Documents in collection but not indexed yet
query = query.filter(RagDocumentStatus.document_id.is_(None))
# Get total count before pagination
total_count = query.count()
logger.info(
f"Found {total_count} total documents for collection {_log_value_preview(collection_id)} with filter {_log_value_preview(filter_type)}"
)
# Apply pagination
results = (
query.order_by(Document.created_at.desc())
.limit(per_page)
.offset((page - 1) * per_page)
.all()
)
documents = [
{
"id": doc.id,
"title": doc.title,
"original_url": doc.original_url,
"rag_indexed": rag_status is not None,
"chunk_count": rag_status.chunk_count if rag_status else 0,
"created_at": doc.created_at.isoformat()
if doc.created_at
else None,
}
for doc, doc_collection, rag_status in results
]
# Debug logging to help diagnose indexing status issues
indexed_count = sum(1 for d in documents if d["rag_indexed"])
# Additional debug: check rag_document_status for this collection
all_indexed_statuses = (
db_session.query(RagDocumentStatus)
.filter_by(collection_id=collection_id)
.all()
)
logger.info(
f"rag_document_status table shows: {len(all_indexed_statuses)} documents indexed for collection {_log_value_preview(collection_id)}"
)
logger.info(
f"Returning {len(documents)} documents on page {page}: "
f"{indexed_count} indexed, {len(documents) - indexed_count} not indexed"
)
return {
"success": True,
"documents": documents,
"pagination": {
"page": page,
"per_page": per_page,
"total": total_count,
"pages": (total_count + per_page - 1) // per_page,
},
}
except Exception as e:
return handle_api_error("getting documents", e)
# Collection Management Routes
@router.get("/api/collections")
def get_collections(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Get all document collections for the current user."""
from ...database.session_context import get_user_db_session
try:
with get_user_db_session(username) as db_session:
# No need to filter by username - each user has their own database
collections = db_session.query(Collection).all()
# Canonical durable count. Reconciliation maintains these rows from
# actual FAISS membership; the legacy DocumentCollection.indexed flag
# is retained only for compatibility and must not drive user-visible
# truth.
collection_counts = {
collection_id: (
int(document_count or 0),
int(indexed_count or 0),
)
for collection_id, document_count, indexed_count in (
db_session.query(
DocumentCollection.collection_id,
func.count(DocumentCollection.document_id),
func.count(
case(
(
DocumentCollection.indexed.is_(True),
DocumentCollection.document_id,
),
else_=None,
)
),
)
.group_by(DocumentCollection.collection_id)
.all()
)
}
result = []
for coll in collections:
document_count, indexed_document_count = collection_counts.get(
coll.id,
(0, 0),
)
collection_data = {
"id": coll.id,
"name": coll.name,
"description": coll.description,
"created_at": coll.created_at.isoformat()
if coll.created_at
else None,
"collection_type": coll.collection_type,
"is_default": coll.is_default
if hasattr(coll, "is_default")
else False,
"is_public": bool(getattr(coll, "is_public", False)),
"agent_enabled": _agent_enabled_default_on(coll),
"document_count": document_count,
"indexed_document_count": indexed_document_count,
"folder_count": len(coll.linked_folders)
if hasattr(coll, "linked_folders")
else 0,
}
# Include embedding metadata if available
if coll.embedding_model:
collection_data["embedding"] = {
"model": coll.embedding_model,
"provider": coll.embedding_model_type.value
if coll.embedding_model_type
else None,
"dimension": coll.embedding_dimension,
"chunk_size": coll.chunk_size,
"chunk_overlap": coll.chunk_overlap,
}
else:
collection_data["embedding"] = None
result.append(collection_data)
return {"success": True, "collections": result}
except Exception as e:
return handle_api_error("getting collections", e)
@router.post("/api/collections")
async def create_collection(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Create a new document collection."""
data, body_error = await read_json_dict(
request, "success", "Request body must be valid JSON"
)
if body_error is not None:
return body_error
return await run_db_sync(_create_collection_sync, data, username)
def _create_collection_sync(data, username):
from ...database.session_context import get_user_db_session
try:
name = data.get("name", "")
if not isinstance(name, str):
# `.get(key, "")` only supplies the default when the key is
# ABSENT; a present-but-wrong-typed value (e.g. `"name": 123`
# or `"name": null`) passes through untouched and blows up on
# `.strip()` with AttributeError -> 500. Reject it explicitly.
return JSONResponse(
{"success": False, "error": "Name must be a string"},
status_code=400,
)
name = name.strip()
description = data.get("description", "")
if not isinstance(description, str):
return JSONResponse(
{"success": False, "error": "Description must be a string"},
status_code=400,
)
description = description.strip()
collection_type = data.get("type", "user_uploads")
if not isinstance(collection_type, str):
# Ported from main's Flask original, which returned
# ``jsonify({...}), 400``. Neither half survives here:
# ``jsonify`` is undefined (NameError -> 500), and a
# ``(body, status)`` tuple is a Flask convention FastAPI does
# not honour. Matches the sibling error returns below.
return JSONResponse(
{
"success": False,
"error": "Collection type must be a string",
},
status_code=400,
)
# Allowlist user-creatable types. System types (notes,
# default_library, research_history) are lazy-created by their
# owning subsystems; a user-crafted impostor (e.g. type="notes")
# would be undeletable under PROTECTED_COLLECTION_TYPES and could
# nondeterministically win _get_or_create_notes_collection's
# .first() lookup, splitting the notes corpus.
allowed_types = {"user_uploads", "user_collection"}
if collection_type not in allowed_types:
return JSONResponse(
{
"success": False,
"error": (
f"Invalid collection type '{collection_type}'. "
f"Allowed: {sorted(allowed_types)}"
),
},
status_code=400,
)
# Egress classification, default private (the safe choice). A public
# collection counts as a public engine and may use cloud inference.
is_public = data.get("is_public", False)
if not isinstance(is_public, bool):
return JSONResponse(
{
"success": False,
"error": "is_public must be a boolean",
},
status_code=400,
)
# Usability switch (NOT egress): offer this collection to the research
# agent? Default True (available) — behaviour-preserving. An explicit
# JSON null normalizes to True so it matches how NULL is read back
# everywhere else (NULL → available); only an explicit false disables.
agent_enabled_raw = data.get("agent_enabled", True)
if agent_enabled_raw is not None and not isinstance(
agent_enabled_raw, bool
):
return JSONResponse(
{
"success": False,
"error": "agent_enabled must be a boolean or null",
},
status_code=400,
)
agent_enabled = True if agent_enabled_raw is None else agent_enabled_raw
if not name:
return JSONResponse(
{"success": False, "error": "Name is required"}, status_code=400
)
with get_user_db_session(username) as db_session:
# Check if collection with this name already exists in this user's database
existing = db_session.query(Collection).filter_by(name=name).first()
if existing:
return JSONResponse(
{
"success": False,
"error": f"Collection '{name}' already exists",
},
status_code=400,
)
# Create new collection (no username needed - each user has their own DB)
# Note: created_at uses default=utcnow() in the model, so we don't need to set it manually
collection = Collection(
id=str(uuid.uuid4()), # Generate UUID for collection
name=name,
description=description,
collection_type=collection_type,
is_public=is_public,
agent_enabled=agent_enabled,
)
db_session.add(collection)
db_session.commit()
from ...web_search_engines.search_engines_config import (
invalidate_collection_engines_cache,
)
invalidate_collection_engines_cache(username)
return {
"success": True,
"collection": {
"id": collection.id,
"name": collection.name,
"description": collection.description,
"created_at": collection.created_at.isoformat()
if collection.created_at
else None,
"collection_type": collection.collection_type,
"is_public": bool(collection.is_public),
"agent_enabled": _agent_enabled_default_on(collection),
},
}
except Exception as e:
return handle_api_error("creating collection", e)
@router.put("/api/collections/{collection_id}")
async def update_collection(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Update a collection's details."""
data, body_error = await read_json_dict(
request, "success", "Request body must be valid JSON"
)
if body_error is not None:
return body_error
return await run_db_sync(
_update_collection_sync, data, collection_id, username
)
def _update_collection_sync(data, collection_id, username):
from ...database.session_context import get_user_db_session
try:
name = data.get("name", "")
if not isinstance(name, str):
# See _create_collection_sync: `.get(key, "")` only supplies
# the default when the key is absent, so a present-but-wrong-
# typed value reaches `.strip()` untouched -> AttributeError -> 500.
return JSONResponse(
{"success": False, "error": "Name must be a string"},
status_code=400,
)
name = name.strip()
description = data.get("description", "")
if not isinstance(description, str):
return JSONResponse(
{"success": False, "error": "Description must be a string"},
status_code=400,
)
description = description.strip()
is_public = data.get("is_public")
if "is_public" in data and not isinstance(is_public, bool):
return JSONResponse(
{
"success": False,
"error": "is_public must be a boolean",
},
status_code=400,
)
agent_enabled = data.get("agent_enabled")
if (
"agent_enabled" in data
and agent_enabled is not None
and not isinstance(agent_enabled, bool)
):
return JSONResponse(
{
"success": False,
"error": "agent_enabled must be a boolean or null",
},
status_code=400,
)
with get_user_db_session(username) as db_session:
# No need to filter by username - each user has their own database
collection = (
db_session.query(Collection).filter_by(id=collection_id).first()
)
if not collection:
return JSONResponse(
{"success": False, "error": "Collection not found"},
status_code=404,
)
# System collections (Notes, Library, Research History) own
# first-class user data whose lifecycle is managed by other
# subsystems. Renaming them via this generic endpoint
# bypasses their intended invariants and confuses every UI
# surface that lists collections. Mirrors PROTECTED_COLLECTION_TYPES
# in the deletion service. Only identity fields (name,
# description) are locked — the is_public / agent_enabled
# toggles below are deliberate per-collection settings that
# must keep working for system collections too.
from ...research_library.deletion.services.collection_deletion import (
PROTECTED_COLLECTION_TYPES,
)
if collection.collection_type in PROTECTED_COLLECTION_TYPES and (
name or "description" in data
):
return JSONResponse(
{
"success": False,
"error": (
f"Cannot rename or redescribe system "
f"collection '{collection.name}' "
f"(type={collection.collection_type})."
),
},
status_code=409,
)
if name:
# Check if new name conflicts with existing collection
existing = (
db_session.query(Collection)
.filter(
Collection.name == name,
Collection.id != collection_id,
)
.first()
)
if existing:
return JSONResponse(
{
"success": False,
"error": f"Collection '{name}' already exists",
},
status_code=400,
)
collection.name = name
# Only write when the caller sends the key (sending "" still
# clears) — otherwise toggle-only PUTs wipe the description.
if "description" in data:
collection.description = description
# Egress classification toggle (only when the caller sends it).
if "is_public" in data:
collection.is_public = is_public
# Research-agent availability toggle (only when the caller sends it).
# Explicit null normalizes to True (available) for parity with how
# NULL is read back elsewhere; only an explicit false disables.
if "agent_enabled" in data:
collection.agent_enabled = (
True if agent_enabled is None else agent_enabled
)
db_session.commit()
from ...web_search_engines.search_engines_config import (
invalidate_collection_engines_cache,
)
invalidate_collection_engines_cache(username)
return {
"success": True,
"collection": {
"id": collection.id,
"name": collection.name,
"description": collection.description,
"created_at": collection.created_at.isoformat()
if collection.created_at
else None,
"collection_type": collection.collection_type,
"is_public": bool(collection.is_public),
"agent_enabled": _agent_enabled_default_on(collection),
},
}
except Exception as e:
return handle_api_error("updating collection", e)
def _try_pdf_upgrade(
*,
db_session,
document,
file_content: bytes,
filename: str,
pdf_storage: str,
pdf_storage_manager,
) -> bool:
"""Check PDF upgrade eligibility and attempt upgrading a Document to include PDF bytes.
Returns True if the document was upgraded, False otherwise. Exceptions
raised during the upgrade attempt are logged and swallowed; the caller
sees False and the upload continues as a non-upgraded success.
"""
if pdf_storage != "database" or pdf_storage_manager is None:
return False
# NOTE: Only the PDF magic-byte check is needed here.
# Size limits are already enforced by the async upload wrapper, before
# these bytes are ever buffered.
# Filename sanitization already happens via sanitize_filename() in
# upload_to_collection() before this helper is invoked.
# See PR #3145 review for details.
if file_content[:4] != b"%PDF":
logger.debug(
"Skipping PDF upgrade for {}: not a PDF file",
filename,
)
return False
try:
return bool(
pdf_storage_manager.upgrade_to_pdf(
document=document,
pdf_content=file_content,
session=db_session,
)
)
except Exception:
logger.exception(f"Failed to upgrade PDF for {filename}")
return False
@router.post("/api/collections/{collection_id}/upload")
@upload_rate_limit_user
@upload_rate_limit_ip
async def upload_to_collection(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Upload files to a collection.
The async wrapper consumes the multipart form (the only legitimate
awaits — request.form() and per-file file.read()) and validates
sizes. The sync DB body — opening the SQLCipher session, hashing,
text extraction, and Document/Collection writes — is offloaded to
a threadpool so a multi-file upload doesn't stall the event loop
behind PBKDF2 key derivation and per-file extraction work.
"""
# Starlette's form parser yields STARLETTE UploadFile instances;
# fastapi.UploadFile is a SUBCLASS of it on fastapi>=0.113, so
# `isinstance(f, fastapi.UploadFile)` is False for every real
# upload and the filter below silently dropped all files
# (every upload 400'd with "No files provided").
from starlette.datastructures import UploadFile
# Per-file and total upload limits to prevent memory exhaustion.
#
# These MUST come from FileUploadValidator, not a route-local
# constant: GET /api/config/limits (research.py) advertises
# FileUploadValidator.MAX_FILE_SIZE / MAX_FILES_PER_REQUEST to the
# frontend as "the backend's authoritative limits", and
# BodySizeLimitMiddleware (fastapi_app.py) already enforces the same
# constants as the global per-request body cap for every route,
# including this one. A previous hardening pass gave this route its
# own tighter, undocumented cap (100MB / 50 files) instead of reusing
# FileUploadValidator like origin/main's Flask handler did — so a
# file the frontend was told is acceptable could be silently
# rejected here with a different limit. Only the size/count checks
# are reused (not validate_upload's MIME/PDF-structure checks): this
# route indexes arbitrary document types via document_loaders, not
# just PDFs.
from ...security import FileUploadValidator
MAX_FILE_SIZE = FileUploadValidator.MAX_FILE_SIZE
MAX_FILES_PER_UPLOAD = FileUploadValidator.MAX_FILES_PER_REQUEST
MAX_TOTAL_UPLOAD_SIZE = MAX_FILES_PER_UPLOAD * MAX_FILE_SIZE
# Early request size check based on Content-Length, before reading body.
content_length = request.headers.get("content-length")
if content_length:
try:
cl = int(content_length)
if cl > MAX_TOTAL_UPLOAD_SIZE:
return JSONResponse(
{
"success": False,
"error": f"Request too large. Max {MAX_TOTAL_UPLOAD_SIZE // (1024 * 1024)}MB",
},
status_code=413,
)
except ValueError:
pass
# Parse the multipart form once and reuse — request.form() can only be
# consumed once per request.
form = await request.form()
upload_files: list[UploadFile] = [
f for f in form.getlist("files") if isinstance(f, UploadFile)
]
if not upload_files:
return JSONResponse(
{"success": False, "error": "No files provided"},
status_code=400,
)
if len(upload_files) > MAX_FILES_PER_UPLOAD:
return JSONResponse(
{
"success": False,
"error": f"Too many files. Max {MAX_FILES_PER_UPLOAD} per upload.",
},
status_code=400,
)
# Buffer every file's bytes here in the async path — UploadFile.read()
# is async (the body may still be streaming from the client), and we
# need the bytes available before we can hand control to the sync
# threadpool worker. Large-file rejection happens up front so we
# don't load anything we'll just throw away.
pdf_storage_form_value = form.get("pdf_storage")
files_data: list[dict] = [] # [{filename, content, oversized}]
for uf in upload_files:
# Preserve empty browser file inputs in the submitted-part count, but
# do not read or process them. The canonical Flask route skipped these
# parts while still reporting them in summary.total.
if not uf.filename:
files_data.append(
{"filename": "", "content": None, "oversized": False}
)
continue
# Per-file size guard before reading into memory. On the pinned
# Starlette range (1.3.x), UploadFile.size is the byte count
# request.form() actually spooled for this part, not a
# client-declared value, so it is never None here.
if uf.size is not None and uf.size > MAX_FILE_SIZE:
files_data.append(
{
"filename": uf.filename,
"content": None,
"oversized": True,
}
)
continue
content = await uf.read()
files_data.append(
{
"filename": uf.filename,
"content": content,
"oversized": len(content) > MAX_FILE_SIZE,
}
)
session_id = request.session.get("session_id")
return await run_db_sync(
_upload_to_collection_sync,
files_data,
pdf_storage_form_value,
collection_id,
username,
session_id,
MAX_FILE_SIZE,
)
def _upload_to_collection_sync(
files_data,
pdf_storage_form_value,
collection_id,
username,
session_id,
MAX_FILE_SIZE,
):
from ...database.session_context import (
get_user_db_session,
)
from ...utilities.db_utils import get_settings_manager
from ...security import sanitize_filename, UnsafeFilenameError
import hashlib
import uuid
from ...research_library.services.pdf_storage_manager import (
PDFStorageManager,
resolve_pdf_storage_mode,
)
try:
with get_user_db_session(username) as db_session:
settings = get_settings_manager(db_session, username)
# Verify collection exists in this user's database
collection = (
db_session.query(Collection).filter_by(id=collection_id).first()
)
if not collection:
return JSONResponse(
{"success": False, "error": "Collection not found"},
status_code=404,
)
# Get PDF storage mode from form data, falling back to user's setting
default_pdf_storage = settings.get_setting(
"research_library.upload_pdf_storage", "none"
)
# `is None`, not `or`: Flask's form.get(k, default) returned the
# default only when the field was ABSENT. An explicit empty
# `pdf_storage=` fell through to the not-in-(database,none)
# check below. `or` also swallows the empty string, silently
# substituting the user's stored default -- which may be
# "database" -- for a caller who explicitly sent nothing.
pdf_storage = (
default_pdf_storage
if pdf_storage_form_value is None
else pdf_storage_form_value
)
if pdf_storage not in ("database", "none"):
# Security: user uploads can only use database (encrypted) or none (text-only)
# Filesystem storage is not allowed for user uploads
pdf_storage = "none"
# Initialize PDF storage manager if storing PDFs in database
pdf_storage_manager = None
if pdf_storage == "database":
library_root = settings.get_setting(
"research_library.storage_path",
str(get_library_directory()),
)
library_root = str(
Path(os.path.expandvars(library_root))
.expanduser()
.resolve()
)
shared_library = settings.get_setting(
"research_library.shared_library", False
)
# Per-user library root (issue #5521), mirroring the sibling
# PDFStorageManager construction sites (library.py's
# view_pdf_page, DownloadService.__init__, zotero
# sync_service._library_root()): narrow the shared base to
# this user's own subdirectory so two users' uploads can't
# collide under one shared root. Not exploitable today since
# this instance is only used in "database" mode (bytes go to
# DocumentBlob, never through library_root), but the isolation
# should hold regardless of which methods happen to read it.
#
# Route the mode through resolve_pdf_storage_mode() as well,
# mirroring sync_service's own database-only construction —
# `pdf_storage` is already guaranteed "database" by the branch
# above, so this is a no-op today, but it ties the
# construction to the actual resolved value (rather than a
# bare literal) so it can't silently start bypassing the
# unencrypted-filesystem gate if this code is ever refactored
# to pass `pdf_storage` straight through.
pdf_storage_manager = PDFStorageManager(
library_root=apply_user_subdir(
Path(library_root), username, shared_library
),
storage_mode=resolve_pdf_storage_mode(pdf_storage),
)
logger.info("PDF storage mode: database (encrypted)")
else:
logger.info("PDF storage mode: none (text-only)")
uploaded_files = []
errors = []
# Track hashes processed earlier in THIS request so we can
# distinguish intra-batch duplicates (two uploaded files with
# identical content) from duplicates against a pre-existing
# library doc. Without this, the second occurrence of an
# intra-batch duplicate was reported as "already_in_collection"
# under the FIRST occurrence's filename -- the warning looked
# like it was skipping books that were actually being added,
# and the skipped entry showed the wrong filename (#5495).
# Maps content hash -> Document so a later identical PDF in the
# same batch can still run the PDF-upgrade path against the
# document kept from the first occurrence.
seen_hashes: dict[str, Document] = {}
for fd in files_data:
# Match main: an empty browser file input counts as a submitted
# part in the summary but is not processed as an upload.
if not fd["filename"]:
continue
# Files were already buffered + size-checked in the async
# wrapper; oversized entries arrive with content=None and
# oversized=True so we can record an error here without
# having held the bytes in memory.
if fd["oversized"]:
# Sanitize BEFORE echoing. Flask sanitized first and only
# ever put the cleaned name into `errors`; the port
# inverted the order, so this branch reflected the raw
# multipart filename — attacker-chosen bytes — straight
# back in the JSON response. Masked today only because the
# single consumer escapes it client-side, which is not a
# property this handler should depend on. Every other
# errors.append below already uses the sanitized name.
try:
safe_name = sanitize_filename(fd["filename"])
except UnsafeFilenameError:
safe_name = "rejected"
errors.append(
{
"filename": safe_name,
"error": f"File too large (max {MAX_FILE_SIZE // (1024 * 1024)}MB)",
}
)
continue
try:
filename = sanitize_filename(fd["filename"])
except UnsafeFilenameError:
errors.append(
{
"filename": "rejected",
"error": "Invalid or unsafe filename",
}
)
continue
# Per-file SAVEPOINT: a failure rolls back only this file so
# earlier successes (and seen_hashes) stay consistent with
# the outer transaction. Commit the savepoint on every
# non-exception exit — including soft validation failures —
# so we never leave nested SAVEPOINTs stacked open across
# the batch (same pattern as research_sources_service).
sp = None
try:
sp = db_session.begin_nested()
# Size limits were already enforced in the async wrapper
# (oversized entries arrive with content=None and are
# rejected above), so main's Content-Length pre-flight and
# post-read re-check have no work left to do here.
file_content = fd["content"]
# Calculate file hash for deduplication
file_hash = hashlib.sha256(file_content).hexdigest()
# Intra-batch duplicate: an earlier file in THIS
# request had identical bytes. Report it as its own
# status so the UI doesn't show it under the first
# occurrence's filename and pretend it was a
# "skipped - already in collection" hit. The first
# occurrence already linked the matching Document to
# this collection (or to the library), so doing the
# Document hash lookup and link check here would be
# misleading at best. Still allow PDF upgrade when
# the kept twin was stored text-only and this copy
# is a PDF with database storage enabled.
if file_hash in seen_hashes:
logger.info(
"Intra-batch duplicate detected: {} (hash {})",
filename,
file_hash,
)
target_doc = seen_hashes[file_hash]
pdf_upgraded = _try_pdf_upgrade(
db_session=db_session,
document=target_doc,
file_content=file_content,
filename=filename,
pdf_storage=pdf_storage,
pdf_storage_manager=pdf_storage_manager,
)
sp.commit()
uploaded_files.append(
{
"filename": filename,
"status": "duplicate_in_batch",
"id": target_doc.id,
"pdf_upgraded": pdf_upgraded,
}
)
continue
# Check if document already exists
existing_doc = (
db_session.query(Document)
.filter_by(document_hash=file_hash)
.first()
)
if existing_doc:
# Document exists, check if we can upgrade to include PDF
pdf_upgraded = _try_pdf_upgrade(
db_session=db_session,
document=existing_doc,
file_content=file_content,
filename=filename,
pdf_storage=pdf_storage,
pdf_storage_manager=pdf_storage_manager,
)
# Check if already in collection
existing_link = (
db_session.query(DocumentCollection)
.filter_by(
document_id=existing_doc.id,
collection_id=collection_id,
)
.first()
)
if not existing_link:
ensure_in_collection(
db_session, existing_doc.id, collection_id
)
status = "added_to_collection"
if pdf_upgraded:
status = "added_to_collection_pdf_upgraded"
sp.commit()
uploaded_files.append(
{
# Report the file the user actually
# uploaded, not the existing doc's
# filename. Otherwise the user sees
# one filename repeated under both
# "added to collection" and
# "new uploads" (or here under
# "already in collection") and
# can't tell which file was which.
"filename": filename,
"status": status,
"id": existing_doc.id,
"pdf_upgraded": pdf_upgraded,
}
)
seen_hashes[file_hash] = existing_doc
else:
status = "already_in_collection"
if pdf_upgraded:
status = "pdf_upgraded"
sp.commit()
uploaded_files.append(
{
"filename": filename,
"status": status,
"id": existing_doc.id,
"pdf_upgraded": pdf_upgraded,
}
)
seen_hashes[file_hash] = existing_doc
else:
# Create new document
from ...document_loaders import (
extract_text_from_bytes,
is_extension_supported,
)
from ...security.isolated_loader import (
NLTK_DATA_MISSING_MESSAGE,
ParserBusyError,
ParserDataMissingError,
)
file_extension = Path(filename).suffix.lower()
# Validate extension is supported before extraction
if not is_extension_supported(file_extension):
sp.commit()
errors.append(
{
"filename": filename,
"error": f"Unsupported format: {file_extension}",
}
)
continue
# Use file_type without leading dot for storage
file_type = (
file_extension[1:]
if file_extension.startswith(".")
else file_extension
)
# Parsing can import large libraries or spend minutes
# extracting a document. Release this read snapshot and
# any earlier file's write lock before that work. A
# background writer may otherwise make the snapshot
# impossible to upgrade (SQLITE_BUSY_SNAPSHOT).
sp.commit()
sp = None
db_session.commit()
try:
extracted_text = extract_text_from_bytes(
file_content, file_extension, filename
)
except ParserBusyError:
# Every bounded parser slot was serving another
# upload. Unlike an unreadable file, this one can
# succeed when uploaded again, so say so. No
# SAVEPOINT is open here.
errors.append(
{
"filename": filename,
"error": "Document parser busy with other uploads; upload this file again",
}
)
continue
except ParserDataMissingError:
# The parser child never downloads NLTK data, and
# this server lacks the data the parse needed. The
# file may be fine; tell the operator the fix. No
# SAVEPOINT is open here.
errors.append(
{
"filename": filename,
"error": NLTK_DATA_MISSING_MESSAGE,
}
)
continue
# Keep write failures isolated to this file after the
# parser returns, using a fresh database snapshot.
sp = db_session.begin_nested()
# Clean the extracted text to remove surrogate characters
if extracted_text:
from ...text_processing import remove_surrogates
extracted_text = remove_surrogates(extracted_text)
if not extracted_text:
sp.commit()
errors.append(
{
"filename": filename,
"error": f"Could not extract text from {file_type} file",
}
)
logger.warning(
f"Skipping file {filename} - no text could be extracted"
)
continue
# Get or create the user_upload source type
logger.info(
f"Getting or creating user_upload source type for {filename}"
)
source_type = (
db_session.query(SourceType)
.filter_by(name="user_upload")
.first()
)
if not source_type:
logger.info("Creating new user_upload source type")
source_type = SourceType(
id=str(uuid.uuid4()),
name="user_upload",
display_name="User Upload",
description="Documents uploaded by users",
icon="fas fa-upload",
)
db_session.add(source_type)
db_session.flush()
logger.info(
f"Created source type with ID: {source_type.id}"
)
else:
logger.info(
f"Found existing source type with ID: {source_type.id}"
)
# Create document with extracted text (no username needed - in user's own database)
# Note: uploaded_at uses default=utcnow() in the model, so we don't need to set it manually
doc_id = str(uuid.uuid4())
logger.info(
f"Creating document {doc_id} for {filename}"
)
# Determine storage mode and file_path
store_pdf_in_db = (
pdf_storage == "database"
and file_type == "pdf"
and pdf_storage_manager is not None
)
new_doc = Document(
id=doc_id,
source_type_id=source_type.id,
filename=filename,
document_hash=file_hash,
file_size=len(file_content),
file_type=file_type,
text_content=extracted_text, # Always store extracted text
file_path=None
if store_pdf_in_db
else FILE_PATH_TEXT_ONLY,
storage_mode="database"
if store_pdf_in_db
else "none",
)
db_session.add(new_doc)
db_session.flush() # Get the ID
logger.info(
f"Document {new_doc.id} created successfully"
)
# Store PDF in encrypted database if requested
pdf_stored = False
if store_pdf_in_db:
try:
stored_path, _ = pdf_storage_manager.save_pdf(
pdf_content=file_content,
document=new_doc,
session=db_session,
filename=filename,
)
# A PDF the manager refuses (over its size
# limit) is reported by returning no path, not
# by raising, so only a returned path means
# the PDF was stored.
pdf_stored = bool(stored_path)
if pdf_stored:
logger.info(
f"PDF stored in encrypted database for {filename}"
)
else:
logger.warning(
f"PDF for {filename} was not stored "
"(over the storage size limit); "
"keeping the extracted text only"
)
except Exception:
logger.exception(
f"Failed to store PDF in database for {filename}"
)
# Continue without PDF storage - text is still saved
if not pdf_stored:
# The document was created labelled as holding
# its PDF in the database. Nothing was stored,
# so label it as the text-only upload it is,
# as for an upload with PDF storage off.
new_doc.storage_mode = "none"
new_doc.file_path = FILE_PATH_TEXT_ONLY
# Add to collection
ensure_in_collection(
db_session, new_doc.id, collection_id
)
# Release the per-file SAVEPOINT so later failures cannot
# undo this file's writes (and so soft/success paths do
# not leave nested SAVEPOINTs stacked open).
sp.commit()
uploaded_files.append(
{
"filename": filename,
"status": "uploaded",
"id": new_doc.id,
"text_length": len(extracted_text),
"pdf_stored": pdf_stored,
}
)
seen_hashes[file_hash] = new_doc
except Exception:
# A per-file savepoint rollback isolates the failure of this
# file so earlier successful uploads in the batch remain intact
# in the transaction state. Record the error and log the traceback
# FIRST so a rollback failure cannot suppress them or crash the batch.
errors.append(
{
"filename": filename,
"error": "Failed to upload file",
}
)
logger.exception(f"Error uploading file {filename}")
if sp is not None:
try:
sp.rollback()
except Exception:
logger.opt(exception=True).warning(
f"Failed to rollback savepoint for {filename}"
)
db_session.commit()
# Trigger auto-indexing for successfully uploaded documents
document_ids = [
f["id"]
for f in uploaded_files
if f.get("status") in ("uploaded", "added_to_collection")
]
if document_ids:
from ...database.session_passwords import session_password_store
# session_id was captured by the async wrapper before to_thread
db_password = session_password_store.get_session_password(
username, session_id
)
if db_password:
trigger_auto_index(
document_ids, collection_id, username, db_password
)
return {
"success": True,
"uploaded": uploaded_files,
"errors": errors,
"summary": {
"total": len(files_data),
"successful": len(uploaded_files),
"failed": len(errors),
},
}
except Exception as e:
return handle_api_error("uploading files", e)
# Research History Semantic Search Routes have been moved to
# web/routers/library_search.py
def _collection_rebuild_info(collection, index):
"""Report durable rebuild state, independently of expiring task rows."""
pending = collection.rebuild_pending_since if collection else None
updated = index.last_updated_at if index else None
return {
"rebuild_pending_since": pending.isoformat()
if isinstance(pending, datetime)
else None,
"has_published_index": index is not None,
"index_last_updated_at": updated.isoformat()
if isinstance(updated, datetime)
else None,
}
@router.get("/api/collections/{collection_id}/documents")
def get_collection_documents(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Get all documents in a collection."""
from ...database.session_context import get_user_db_session
try:
with get_user_db_session(username) as db_session:
# Verify collection exists in this user's database
collection = (
db_session.query(Collection).filter_by(id=collection_id).first()
)
if not collection:
return JSONResponse(
{"success": False, "error": "Collection not found"},
status_code=404,
)
# Look up note source type to filter notes into separate array
note_source_type = (
db_session.query(SourceType).filter_by(name="note").first()
)
note_source_type_id = (
note_source_type.id if note_source_type else None
)
# Get documents through junction table. Compute "has text in DB"
# at the SQL level and defer the text_content column, so listing a
# collection doesn't pull every document's full text body into
# memory just to test it for truthiness (#4560).
has_text_col = (
Document.text_content.isnot(None)
& (Document.text_content != "")
).label("has_text_db")
doc_links = (
db_session.query(DocumentCollection, Document, has_text_col)
.join(Document)
.options(defer(Document.text_content))
.filter(DocumentCollection.collection_id == collection_id)
.all()
)
documents = []
for link, doc, has_text_db in doc_links:
# Skip notes — they go in the separate notes array
if (
note_source_type_id
and doc.source_type_id == note_source_type_id
):
continue
# Check if PDF file is stored
has_pdf = bool(
doc.file_path and doc.file_path not in FILE_PATH_SENTINELS
)
has_text_db = bool(has_text_db)
# Use title if available, otherwise filename
display_title = doc.title or doc.filename or "Untitled"
# Get source type name
source_type_name = (
doc.source_type.name if doc.source_type else "unknown"
)
# Check if document is in other collections
other_collections_count = (
db_session.query(DocumentCollection)
.filter(
DocumentCollection.document_id == doc.id,
DocumentCollection.collection_id != collection_id,
)
.count()
)
documents.append(
{
"id": doc.id,
"filename": display_title,
"title": display_title,
"file_type": doc.file_type,
"file_size": doc.file_size,
"uploaded_at": doc.created_at.isoformat()
if doc.created_at
else None,
"indexed": link.indexed,
"chunk_count": link.chunk_count,
"last_indexed_at": link.last_indexed_at.isoformat()
if link.last_indexed_at
else None,
"has_pdf": has_pdf,
"has_text_db": has_text_db,
"source_type": source_type_name,
"in_other_collections": other_collections_count > 0,
"other_collections_count": other_collections_count,
}
)
# Get notes in this collection (note_source_type resolved above)
notes = []
if note_source_type:
note_links = (
db_session.query(DocumentCollection, Document)
.join(Document)
.filter(
DocumentCollection.collection_id == collection_id,
Document.source_type_id == note_source_type.id,
)
.all()
)
for link, note in note_links:
content = note.text_content or ""
notes.append(
{
"id": note.id,
"title": note.title or "Untitled",
"content_preview": content[:200] + "..."
if len(content) > 200
else content,
"tags": note.tags or [],
"pinned": note.favorite,
"created_at": note.created_at.isoformat()
if note.created_at
else None,
"updated_at": note.updated_at.isoformat()
if note.updated_at
else None,
"indexed": link.indexed,
"chunk_count": link.chunk_count,
"source_type": "note",
}
)
# Get index file size if available
index_file_size = None
index_file_size_bytes = None
collection_name = f"collection_{collection_id}"
rag_index = (
db_session.query(RAGIndex)
.filter_by(collection_name=collection_name, is_current=True)
.first()
)
if rag_index and rag_index.index_path:
from pathlib import Path
from ...vector_stores.segment_files import index_storage_size
# The helper tolerates files vanishing under a concurrent
# writer and returns None if the root itself is gone.
size_bytes = index_storage_size(Path(rag_index.index_path))
if size_bytes is not None:
index_file_size_bytes = size_bytes
# Format as human-readable
if size_bytes < 1024:
index_file_size = f"{size_bytes} B"
elif size_bytes < 1024 * 1024:
index_file_size = f"{size_bytes / 1024:.1f} KB"
else:
index_file_size = f"{size_bytes / (1024 * 1024):.1f} MB"
return {
"success": True,
"collection": {
**_collection_rebuild_info(collection, rag_index),
"id": collection.id,
"name": collection.name,
"description": collection.description,
"is_public": bool(getattr(collection, "is_public", False)),
"agent_enabled": _agent_enabled_default_on(collection),
"embedding_model": collection.embedding_model,
"embedding_model_type": collection.embedding_model_type.value
if collection.embedding_model_type
else None,
"embedding_dimension": collection.embedding_dimension,
"chunk_size": collection.chunk_size,
"chunk_overlap": collection.chunk_overlap,
# Advanced settings
"splitter_type": collection.splitter_type,
"distance_metric": collection.distance_metric,
"index_type": collection.index_type,
"normalize_vectors": collection.normalize_vectors,
# Index file info
"index_file_size": index_file_size,
"index_file_size_bytes": index_file_size_bytes,
"collection_type": collection.collection_type,
# Deletion policy comes from the server so the UI can't
# drift from PROTECTED_COLLECTION_TYPES: the details page
# hides its Delete button for system collections, whose
# deletion the service refuses with a 409 anyway.
"is_protected": _is_protected_collection(collection),
},
"documents": documents,
"notes": notes,
}
except Exception as e:
return handle_api_error("getting collection documents", e)
# =============================================================================
# Shared collection-indexing helpers
#
# Three routes index a collection: the single-collection SSE route
# (index_collection), the bulk SSE route (index_all), and the background worker
# (_background_index_worker). These helpers hold the logic that must stay
# identical across all three so it cannot drift again — the embedding metadata
# to persist, the force-reindex cleanup, and the query for documents to index.
# The callers differ only in how they report progress (SSE stream vs
# TaskMetadata) and so keep their own loops.
# =============================================================================
def _indexing_conflict(db_session, username, collection_id):
"""Check while holding the collection's admission lock."""
key = (username, str(collection_id))
conflict = {
"success": False,
"error": "Indexing is already in progress for this collection",
}
with _active_sse_indexers_lock:
if _active_sse_indexers.get(key):
return conflict
task_id = _active_background_indexers.get(key)
if task_id is not None:
return {**conflict, "task_id": task_id}
in_progress = (
db_session.query(TaskMetadata)
.filter(
TaskMetadata.task_type == "indexing",
TaskMetadata.status == "processing",
)
.all()
)
for task in in_progress:
if (task.metadata_json or {}).get("collection_id") == collection_id:
return {**conflict, "task_id": task.task_id}
return None
def _register_sse_indexer(username, collection_id, db_password, cancel_event):
"""Admit an SSE worker atomically with SSE and background requests."""
from ...database.session_context import get_user_db_session
key = (username, str(collection_id))
lock = _start_bg_index_locks.setdefault(key, threading.Lock())
with lock, get_user_db_session(username, db_password) as db_session:
if _indexing_conflict(db_session, username, collection_id):
return False
with _active_sse_indexers_lock:
_active_sse_indexers.setdefault(key, set()).add(cancel_event)
return True
def _unregister_sse_indexer(username, collection_id, cancel_event):
"""Release admission only once every worker belonging to this stream stopped."""
key = (username, str(collection_id))
with _active_sse_indexers_lock:
events = _active_sse_indexers.get(key)
if events is not None:
events.discard(cancel_event)
if not events:
_active_sse_indexers.pop(key, None)
def _unregister_background_indexer(username, collection_id, task_id):
key = (username, str(collection_id))
with _active_sse_indexers_lock:
if _active_background_indexers.get(key) == task_id:
_active_background_indexers.pop(key, None)
def _store_collection_embedding_metadata(collection, rag_service):
"""Persist the embedding/index configuration used to index a collection.
Run on first incremental index. Forced rebuilds publish their metadata
together with the replacement index. Includes the embedding dimension,
probed by embedding a test string (the same way LibraryRAGService derives
it for the RAGIndex row). Previously this read
``embedding_manager.provider.embedding_dimension``, an attribute the real
LocalEmbeddingManager does not have, so the probe always failed and
``embedding_dimension`` was persisted as NULL via both indexing paths. Does
not commit — the caller owns the transaction.
"""
embedding_dim = None
try:
# Derive the dimension by embedding a test string — mirrors
# LibraryRAGService.* which computes len(embed_query("test")) for the
# RAGIndex row. The embedding manager has no usable dimension attribute.
test_embedding = rag_service.embedding_manager.embeddings.embed_query(
"test"
)
embedding_dim = len(test_embedding)
except Exception:
logger.debug("Could not determine embedding dimension", exc_info=True)
collection.embedding_model = rag_service.embedding_model
collection.embedding_model_type = EmbeddingProvider(
rag_service.embedding_provider
)
collection.embedding_dimension = embedding_dim
collection.chunk_size = rag_service.chunk_size
collection.chunk_overlap = rag_service.chunk_overlap
# Advanced settings
collection.splitter_type = rag_service.splitter_type
collection.text_separators = rag_service.text_separators
collection.distance_metric = rag_service.distance_metric
# Ensure normalize_vectors is a proper boolean for the database
collection.normalize_vectors = bool(rag_service.normalize_vectors)
collection.index_type = rag_service.index_type
logger.info(
f"Stored embedding metadata for collection: provider={rag_service.embedding_provider}"
)
def _query_documents_to_index(db_session, collection_id, force_reindex):
"""Return the ``(DocumentCollection, Document)`` pairs to index.
When not force-reindexing, only un-indexed documents are returned.
"""
query = (
db_session.query(DocumentCollection, Document)
.join(Document)
# Don't load the large text_content body: the indexing loop only
# reads doc.id/filename/title (index_document re-fetches by id in a
# worker thread), so loading every document's full text here just to
# iterate is what exhausts memory on big collections (#4560).
.options(defer(Document.text_content))
.filter(DocumentCollection.collection_id == collection_id)
)
if not force_reindex:
# IMPORTANT: DO NOT CHANGE THIS LINE! DO NOT REMOVE THIS COMMENT!
# We MUST use `== False` instead of `not DocumentCollection.indexed`
# The Python `not` operator does NOT work correctly in SQLAlchemy filters.
# Using `not` will cause the query to return NO results (zero documents).
# SQLAlchemy requires explicit comparison: `== False` or `== True`
# This has been fixed multiple times - DO NOT change it back to `not`!
query = query.filter(DocumentCollection.indexed == False) # noqa: E712
return query.all()
@router.post("/api/collections/{collection_id}/index")
def index_collection(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Index all documents in a collection with Server-Sent Events progress.
POST, not GET, because it mutates — same reasoning as index-all:
a first index writes embedding metadata, and it runs the
embedding/index pipeline over every document in the collection
(``?force_reindex=true`` stages a complete replacement and publishes
it in one transaction, keeping the existing vectors until then), and
CSRFMiddleware keys enforcement on the method, so the verb is what
puts it behind the token check. A GET would sit outside the
middleware entirely and be startable from same-site markup riding
the victim's session cookie. Nothing in the shipped frontend called
it — the history page's Index button POSTs COLLECTION_INDEX_START.
Progress still streams as SSE; a fetch client can send both a POST
and the header. Query parameters are unchanged."""
from ...database.session_context import get_user_db_session
from ...database.session_passwords import session_password_store
# Parse as bool — a raw string "false" is truthy, which would silently
# force a full reindex for every caller that asked to skip already-indexed
# documents.
force_reindex = (
request.query_params.get("force_reindex", "false").lower() == "true"
)
session_id = request.session.get("session_id")
logger.info(f"Starting index_collection, force_reindex={force_reindex}")
# Get password for thread access to encrypted database
db_password = None
if session_id:
db_password = session_password_store.get_session_password(
username, session_id
)
# Create RAG service — on force reindex use current default model
rag_service = get_rag_service(
request, username, collection_id, use_defaults=force_reindex
)
logger.info(
f"RAG service created: provider={rag_service.embedding_provider}"
)
# Import before building the SSE generator. Importing inside its try
# would leave the exception handler unbound if this import failed.
# Keep it below module scope because it pulls in the deletion package.
from ...research_library.deletion.utils.cascade_helper import (
RemoteVectorStoreDropError,
remote_drop_failure_message,
)
# Resolve the parallel worker bound before the generator runs — the
# parallel helper must never touch SettingsManager from a worker thread.
with get_user_db_session(username, db_password) as _settings_session:
_settings = get_settings_manager(_settings_session, username)
try:
_max_workers = int(
_settings.get_setting("rag.indexing_max_parallel_docs", 4)
)
except SettingsValueError:
raise
except Exception:
_max_workers = 4
_max_workers = max(1, min(_max_workers, 16))
def generate():
"""Generator for SSE progress updates."""
logger.info("SSE generator started")
_sse_cancel = threading.Event()
worker_thread = None
worker_finished: Future[None] = Future()
try:
if not _register_sse_indexer(
username, collection_id, db_password, _sse_cancel
):
yield f"data: {json.dumps({'type': 'error', 'error': 'Indexing is already in progress for this collection'})}\n\n"
return
# All setup DB work happens in a SHORT-LIVED session; the per-doc
# ids/filenames are snapshotted into plain tuples so the long
# indexing loop below (which yields progress + heartbeats) holds NO
# session across a yield — see download_bulk for the Starlette/anyio
# thread-affinity rationale. index_document opens its own session
# per document in a worker thread and commits its own writes, so the
# old outer db_session.commit()/flush() was vestigial.
collection_found = True
collection_name = None
doc_info = []
with get_user_db_session(username, db_password) as db_session:
# Verify collection exists in this user's database
collection = (
db_session.query(Collection)
.filter_by(id=collection_id)
.first()
)
if not collection:
collection_found = False
else:
collection_name = collection.name
# Keep the published configuration during a rebuild.
# Its replacement is committed with the complete new index.
if collection.embedding_model is None and not force_reindex:
_store_collection_embedding_metadata(
collection, rag_service
)
db_session.commit()
# Snapshot (id, display name) for each document to index.
doc_info = [
(doc.id, doc.filename or doc.title or "Unknown")
for _link, doc in _query_documents_to_index(
db_session, collection_id, force_reindex
)
]
if not collection_found:
yield f"data: {json.dumps({'type': 'error', 'error': 'Collection not found'})}\n\n"
return
if not doc_info and not force_reindex:
logger.info("No documents to index in collection")
yield f"data: {json.dumps({'type': 'complete', 'results': {'successful': 0, 'skipped': 0, 'failed': 0, 'message': 'No documents to index'}})}\n\n"
return
total = len(doc_info)
logger.info(f"Found {total} documents to index")
results = {
"successful": 0,
"skipped": 0,
"failed": 0,
"errors": [],
}
yield f"data: {json.dumps({'type': 'start', 'message': f'Indexing {total} documents in collection: {collection_name}'})}\n\n"
# Fan out indexing across a bounded worker pool. We run the
# pool in a background thread so the SSE generator's main
# thread can still yield heartbeats during long embedding
# round-trips (matters for nginx / browser SSE timeout
# behaviour). The progress_callback fired by the parallel
# helper pushes per-document events into ``progress_queue``,
# which the generator drains as they arrive.
#
# ``_max_workers`` is resolved from settings before the
# generator starts (closure capture) so the parallel helper
# never touches SettingsManager from inside a worker.
progress_queue: queue.Queue = queue.Queue()
_SENTINEL_COMPLETE = object()
_SENTINEL_ERROR = object()
def _on_progress(
completed: int,
total: int,
title: str,
status: str,
) -> None:
progress_queue.put(
(
"progress",
completed,
total,
title,
status,
)
)
def _run_parallel():
try:
# ``is_cancelled`` polls the SSE-disconnect event
# so the helper can exit early between completions
# when the client has gone away; the helper's
# ``pool.shutdown(wait=True, ...)`` still drains
# in-flight workers before returning so no worker
# outlives the helper return — see ``index_documents_parallel`` for the
# rationale.
index_documents = (
rag_service.rebuild_collection
if force_reindex
else rag_service.index_documents_parallel
)
aggregate = index_documents(
doc_info,
collection_id,
max_workers=_max_workers,
progress_callback=_on_progress,
is_cancelled=_sse_cancel.is_set,
)
progress_queue.put((_SENTINEL_COMPLETE, aggregate))
except Exception as exc:
logger.exception(
"Parallel indexing in SSE generator crashed"
)
progress_queue.put((_SENTINEL_ERROR, exc))
finally:
# Best-effort thread-local DB session cleanup so a
# long-running worker batch doesn't accumulate
# scoped sessions (#3194).
try:
from ...database.thread_local_session import (
cleanup_current_thread,
)
cleanup_current_thread()
except Exception:
logger.debug(
"best-effort thread-local DB session cleanup",
exc_info=True,
)
finally:
# All indexing writes and thread-local cleanup have
# finished. A disconnected stream can release its
# admission and service here without another thread.
worker_finished.set_result(None)
# Copy the request's contextvars so username-dependent
# fallbacks (e.g. log attribution) inside the worker resolve
# to the authenticated user rather than None.
_par_ctx = contextvars.copy_context()
worker_thread = threading.Thread(
target=_par_ctx.run,
args=(_run_parallel,),
daemon=True,
name="index-collection-parallel",
)
worker_thread.start()
# Drain the queue: yield progress events as they arrive,
# yield heartbeat comments on idle to keep SSE proxies
# from timing out, exit on completion / error sentinels.
heartbeat_interval = 5 # seconds
_aggregate = None
while True:
try:
item = progress_queue.get(timeout=heartbeat_interval)
except queue.Empty:
# No event in 5s. Heartbeat unless the worker
# died without signalling (would hang forever).
if worker_thread.is_alive():
yield f": heartbeat {total}\n\n"
continue
logger.exception(
"Indexer worker exited without completion "
"signal; aborting SSE"
)
yield f"data: {json.dumps({'type': 'error', 'error': 'Indexer stopped unexpectedly'})}\n\n"
return
if item[0] is _SENTINEL_COMPLETE:
_aggregate = item[1]
break
if item[0] is _SENTINEL_ERROR:
yield f"data: {json.dumps({'type': 'error', 'error': 'An internal error occurred during indexing'})}\n\n"
return
_kind, completed, total, title, status = item
yield f"data: {json.dumps({'type': 'progress', 'current': completed, 'total': total, 'filename': title, 'percent': int((completed / total) * 100)})}\n\n"
if status == "error":
# Per-doc error surfaced so the browser can show
# failed documents inline. Scrub creds/keys here
# (the full trace stays in server logs above).
# Look the error up from the aggregate's errors
# list — at this point _aggregate is None
# (indexer still running), so we accept a brief
# imprecision on the exact error text and emit
# a generic doc_error; the final ``complete``
# event below will list precise errors.
yield f"data: {json.dumps({'type': 'doc_error', 'filename': title, 'error': 'Indexing failed'})}\n\n"
if _aggregate is not None:
results["successful"] = _aggregate["successful"]
results["skipped"] = _aggregate["skipped"]
results["failed"] = _aggregate["failed"]
# Keep document identity for the UI, but never put an
# extension's arbitrary error detail in the SSE event.
results["errors"] = [
{
"filename": err.get("title"),
"error": _client_indexing_error(err.get("error")),
}
for err in _aggregate.get("errors", [])
]
logger.info(
f"Indexing complete: "
f"{results['successful']} successful, "
f"{results['failed']} failed, "
f"{results['skipped']} skipped"
)
yield f"data: {json.dumps({'type': 'complete', 'results': results})}\n\n"
logger.info("SSE generator finished successfully")
except RemoteVectorStoreDropError as exc:
# Caught ahead of the catch-all below: this is an actionable
# vector-store failure, not an internal error. Force-reindex rolls
# back, keeping the RAGIndex rows (the remote collections' only
# handle); the message says whether a retry can help and whether
# some collections were already dropped (#5765). Logged from the
# error's sanitized fields, with no traceback (a traceback would
# add frame state; the backend's error text can carry secrets).
drop_detail = exc.log_detail()
logger.warning(
"Force-reindex aborted: could not drop the vector-store "
f"collection before rebuilding it: {drop_detail}"
)
drop_error = {
"type": "error",
"error": remote_drop_failure_message(
exc, "indexing was aborted"
),
}
yield f"data: {json.dumps(drop_error)}\n\n"
except Exception:
logger.exception("Error in collection indexing")
yield f"data: {json.dumps({'type': 'error', 'error': 'An internal error occurred during indexing'})}\n\n"
finally:
# Signal cancellation immediately, but retain admission until
# every in-flight writer has stopped, including deferred drains.
_sse_cancel.set()
# On client disconnect this finally can run on the event loop
# thread (generator close), so the drain must be bounded: give
# in-flight workers a short grace period, then hand the
# remaining cleanup to the existing worker's completion callback.
# _sse_cancel is already set, so the parallel helper admits no
# new work and terminates once in-flight docs finish.
if worker_thread is not None and worker_thread.is_alive():
worker_thread.join(timeout=5.0)
if worker_thread is not None and worker_thread.is_alive():
logger.info(
"Indexer still draining after SSE close; deferring "
"cleanup until the worker finishes"
)
def _close_after_worker(_finished):
_unregister_sse_indexer(
username, collection_id, _sse_cancel
)
safe_close(
rag_service,
"rag_service (index-collection SSE, deferred)",
)
# Future invokes the callback exactly once, even if the
# worker finished between is_alive() and registration.
# Starting a separate drain thread could fail and strand
# admission after the indexing worker has already exited.
worker_finished.add_done_callback(_close_after_worker)
else:
_unregister_sse_indexer(username, collection_id, _sse_cancel)
safe_close(rag_service, "rag_service (index-collection SSE)")
response = WorkerCleanupStreamingResponse(
generate(), media_type="text/event-stream"
)
# Prevent buffering for proper SSE streaming
response.headers["Cache-Control"] = "no-cache, no-transform"
response.headers["Connection"] = "keep-alive"
response.headers["X-Accel-Buffering"] = "no"
return response
# =============================================================================
# Background Indexing Endpoints
# =============================================================================
def _get_rag_service_for_thread(
collection_id: str,
username: str,
db_password: str,
use_defaults: bool = False,
) -> LibraryRAGService:
"""
Create RAG service for use in background threads (no Flask context).
Delegates settings resolution to the shared rag_service_factory, then
propagates db_password to the embedding manager for thread-safe DB access.
"""
from ...research_library.services.rag_service_factory import (
get_rag_service as _get_rag_service,
)
service = _get_rag_service(
username,
collection_id,
use_defaults=use_defaults,
db_password=db_password,
)
# The factory passes db_password to LibraryRAGService, but __init__ stores
# it in the backing field (_db_password) without propagating to sub-managers.
# Re-assign via the property setter to propagate to embedding_manager and
# integrity_manager, which need it for thread-safe session access.
service.db_password = db_password
return service
def trigger_auto_index(
document_ids: list[str],
collection_id: str,
username: str,
db_password: str,
) -> None:
"""
Trigger automatic RAG indexing for documents if auto-indexing is enabled.
This function checks the auto_index_enabled setting and spawns a background
thread to index the specified documents. It does not block the caller.
Args:
document_ids: List of document IDs to index
collection_id: The collection to index into
username: The username for database access
db_password: The user's database password for thread-safe access
"""
from ...database.session_context import get_user_db_session
if not document_ids:
logger.debug("No documents to auto-index")
return
# Check if auto-indexing is enabled
try:
with get_user_db_session(username, db_password) as db_session:
settings = SettingsManager(db_session)
auto_index_enabled = settings.get_bool_setting(
"research_library.auto_index_enabled", True
)
if not auto_index_enabled:
logger.debug("Auto-indexing is disabled, skipping")
return
try:
_auto_max_workers = int(
settings.get_setting("rag.indexing_max_parallel_docs", 4)
)
except SettingsValueError:
raise
except Exception:
_auto_max_workers = 4
_auto_max_workers = max(1, min(_auto_max_workers, 16))
except Exception:
logger.exception(
"Failed to check auto-index setting, skipping auto-index"
)
return
# Reserve a queue slot before submission. If the indexing queue is
# saturated (too many uploads in flight), drop this auto-index job
# rather than letting the executor's unbounded internal queue grow
# without bound. The user can reindex manually later via the UI.
if not _try_reserve_auto_index_slot():
logger.warning(
"Auto-index queue saturated ({}+ jobs pending); dropping "
"auto-index for {} document(s) in collection {}. "
"Documents are uploaded; trigger a manual reindex if needed.",
_MAX_PENDING_AUTO_INDEX_JOBS,
len(document_ids),
collection_id,
)
return
logger.info(
f"Auto-indexing {len(document_ids)} documents in collection {collection_id}"
)
# Release the reserved slot EXACTLY ONCE per submission. CPython's
# ThreadPoolExecutor.submit() enqueues the work item BEFORE it tries to
# spin up a worker thread; if thread-start raises (e.g.
# RuntimeError("can't start new thread")), the item is already queued, so
# a live worker can run _wrapped_worker (releasing in its finally) AND the
# except block below also fires. A per-call lock + flag makes the release
# idempotent so that double-fire only releases once — otherwise the
# counter under-counts in-flight jobs and erodes the OOM bound (the
# max(0, ...) floor would silently hide the drift).
_release_lock = threading.Lock()
_slot_released = {"done": False}
def _release_slot_once():
with _release_lock:
if _slot_released["done"]:
return
_slot_released["done"] = True
_release_auto_index_slot()
def _wrapped_worker(*args, **kwargs):
try:
_auto_index_documents_worker(*args, **kwargs)
finally:
_release_slot_once()
# Submit to thread pool (bounded concurrency, prevents thread proliferation).
# Build the executor inside the try too: if _get_auto_index_executor()
# itself fails (OS thread/mutex exhaustion), the slot must still be
# released — otherwise a reserved slot leaks permanently.
try:
executor = _get_auto_index_executor()
executor.submit(
_wrapped_worker,
document_ids,
collection_id,
username,
db_password,
_auto_max_workers,
)
except Exception:
# If submit itself fails (executor shutting down, OOM, etc.), the
# wrapped worker may never run, so release the slot here. The release
# is idempotent: if the work item was already enqueued and a worker
# ran (and released) before thread-start failed, this is a no-op. Do
# NOT re-raise: the upload has already been committed by the caller,
# so propagating would turn a successful upload into a 500 (and prompt
# the client to retry, creating duplicates). Auto-indexing is simply
# skipped; the user can trigger a manual reindex later via the UI.
_release_slot_once()
logger.exception(
"Failed to submit auto-index job for {} document(s) in "
"collection {}; upload succeeded, auto-indexing skipped. "
"Trigger a manual reindex if needed.",
len(document_ids),
collection_id,
)
@thread_cleanup
def _auto_index_documents_worker(
document_ids: list[str],
collection_id: str,
username: str,
db_password: str,
max_workers: int = 4,
) -> None:
"""
Background worker to index documents automatically.
This is a simpler worker than _background_index_worker - it doesn't track
progress via TaskMetadata since it's meant to be a lightweight auto-indexing
operation.
Documents inside one call are indexed concurrently via
:meth:`LibraryRAGService.index_documents_parallel`. The outer
``_auto_index_executor`` already parallelises separate jobs at the upload
layer (one job per upload batch) and we additionally fan out inside each
job, so a burst of uploads doesn't serialise within a single upload's
document set.
The ``max_workers`` parameter is resolved on the caller (which owns
the Flask request context and therefore a real
:func:`get_settings_manager` call). Background-thread workers must
NOT call ``get_settings_manager()`` without an explicit
``db_session=`` — see pre-commit hook
``check-settings-manager-thread-safety`` and issue #3453.
"""
try:
# Create RAG service (thread-safe, no Flask context needed)
with _get_rag_service_for_thread(
collection_id, username, db_password
) as rag_service:
# The auto-indexer doesn't surface per-doc titles to a user,
# so we stub them with empty strings — the parallel helper
# only reads titles for progress reporting.
doc_info = [(doc_id, "") for doc_id in document_ids]
aggregate = rag_service.index_documents_parallel(
doc_info,
collection_id,
force_reindex=False,
max_workers=max(1, min(int(max_workers), 16)),
)
logger.info(
f"Auto-indexing complete: {aggregate['successful']}"
f"/{len(document_ids)} documents indexed "
f"(skipped={aggregate['skipped']}, "
f"failed={aggregate['failed']})"
)
except Exception:
logger.exception("Auto-indexing worker failed")
# Fixed per-document guidance that LibraryRAGService returns as a result's
# "error". An exact match reaches the client; _client_indexing_error replaces
# any other text. TestIndexingErrorAllowlistMatchesService in
# tests/security/test_library_rag_security_fastapi.py checks this set, and
# _EMPTY_VECTOR_STORE_REASON below, against the text the service builds.
_SAFE_INDEXING_ERRORS = frozenset(
{
"Document not found",
"Document not found in collection",
"Document has no text content",
"Document deleted during indexing",
"Document changed; retry indexing",
"Collection configuration changed; retry indexing",
}
)
_EMPTY_VECTOR_STORE_REASON = (
"live vector store reported zero ids while DocumentChunk rows exist; "
"refusing to clear indexed flags"
)
_COLLECTION_NOT_FOUND = "Collection not found"
_REMOTE_DROP_OUTCOME = "indexing was aborted"
# Appended when a cancel arrived after a rebuild began publishing.
_LATE_CANCEL_NOTE = (
"The cancel request arrived after the rebuild began publishing, "
"so the new index is in use."
)
# Codes for ``metadata_json["failure"]``. The indexing worker records one next
# to an ``error_message`` it built only from fixed phrases and counts, and the
# status endpoint rebuilds that message from the record instead of serving the
# stored text.
_FAILURE_COLLECTION_NOT_FOUND = "collection_not_found"
_FAILURE_REMOTE_DROP = "remote_vector_store_drop"
def _client_indexing_error(error: Any) -> str:
"""Return only messages known to be safe for indexing clients.
Service extensions and exception handlers can supply arbitrary error text.
Credential-shape scrubbing does not remove paths, SQL, or provider URLs.
"""
if error is None or (isinstance(error, str) and not error):
return "Indexing failed"
if isinstance(error, str) and error in _SAFE_INDEXING_ERRORS:
return error
return "Indexing failed. Check the server logs for details."
def _client_reconciliation_reason(reason: Any) -> str:
"""Preserve the known durable-state warning, never arbitrary detail."""
if isinstance(reason, str) and reason == _EMPTY_VECTOR_STORE_REASON:
return _EMPTY_VECTOR_STORE_REASON
return (
"Could not verify the vector store. Check the server logs for details."
)
def _reconciliation_skipped_message(reason: str) -> str:
"""Task error for a skipped reconciliation, given a client-safe reason.
The worker stores it and the status endpoint rebuilds it, both through
this function, so the two texts cannot drift apart.
"""
return (
f"Reconciliation skipped: {reason.rstrip('.')}. Indexed flags "
"preserved; verify the FAISS index before retrying."
)
def _reconciled_failures_message(failed: int) -> str:
"""Task error when documents failed and reconciliation then completed."""
return (
f"{failed} document(s) failed to index. The collection was "
"reconciled to the durable vector store."
)
def _remote_drop_failure(exc: Any) -> dict:
"""The failure record for a force-reindex whose remote drop failed.
It keeps only the two fields ``remote_drop_failure_message`` reads: a
flag and a count. The error's own text is never recorded. A count that is
not a non-negative int is stored as None, so the status endpoint withholds
the message rather than rebuild it from a bad value.
"""
dropped = exc.dropped
return {
"code": _FAILURE_REMOTE_DROP,
"retryable": bool(exc.retryable),
"dropped": dropped if type(dropped) is int and dropped >= 0 else None,
}
def _client_task_failure_message(failure: Any) -> Optional[str]:
"""Rebuild the client text for a worker failure record.
Returns None for anything else: no record, an unknown code, or a field of
the wrong type. The caller then withholds the stored message.
"""
if not isinstance(failure, dict):
return None
code = failure.get("code")
if code == _FAILURE_COLLECTION_NOT_FOUND:
return _COLLECTION_NOT_FOUND
if code == _FAILURE_REMOTE_DROP:
retryable = failure.get("retryable")
dropped = failure.get("dropped")
if type(retryable) is bool and type(dropped) is int and dropped >= 0:
from ...research_library.deletion.utils.cascade_helper import (
RemoteVectorStoreDropError,
remote_drop_failure_message,
)
# The same template the worker used, fed only the recorded
# flag and count.
return remote_drop_failure_message(
RemoteVectorStoreDropError(
"recorded remote-drop failure",
retryable=retryable,
dropped=dropped,
),
_REMOTE_DROP_OUTCOME,
)
return None
def _sanitized_indexing_errors(results: dict, limit: int = 50) -> list:
"""Return a bounded list of client-safe per-document errors."""
errors = results.get("errors")
if not isinstance(errors, list):
return []
return [
{
"doc_id": item.get("doc_id"),
"title": item.get("title"),
"error": _client_indexing_error(item.get("error")),
}
for item in errors[: max(0, limit)]
if isinstance(item, dict)
]
def _client_indexing_summary(results: dict) -> dict:
"""Keep only the non-negative integer counts and the ``cancelled`` flag.
Every other key is dropped.
"""
summary = {}
for key in ("successful", "skipped", "failed", "total"):
value = results.get(key)
if type(value) is int and value >= 0:
summary[key] = value
if type(results.get("cancelled")) is bool:
summary["cancelled"] = results["cancelled"]
return summary
def _client_indexing_result(result: Any) -> Optional[dict]:
"""Filter old and new task rows at the HTTP boundary."""
if not isinstance(result, dict):
return None
safe = _client_indexing_summary(result)
for key in (
"durable_indexed_documents",
"durable_indexed_chunks",
"live_vectors",
"orphan_vectors",
"orphan_vectors_removed",
"partial_document_vectors",
):
value = result.get(key)
if type(value) is int and value >= 0:
safe[key] = value
if result.get("reconciliation_skipped") is True:
safe["reconciliation_skipped"] = True
safe["reconciliation_reason"] = _client_reconciliation_reason(
result.get("reconciliation_reason")
)
if "errors" in result:
safe["errors"] = _sanitized_indexing_errors(result)
return safe
def _client_indexing_task_error(
message: Any, result: Optional[dict], failure: Any = None
) -> Optional[str]:
"""Rebuild known task errors rather than trusting persisted text.
The client text comes from the worker's failure record (``failure``) or
from the result counts (``result``, already filtered by
``_client_indexing_result``). Any other stored message gets the generic
text. That includes rows written before these rules, whose text can hold
exception detail.
A stored message may be served unchanged only through an exact match on
text that this module writes to an indexing task row.
``TestIndexingTaskErrorPassThroughs`` in
tests/security/test_library_rag_security_fastapi.py fails on a match
that nothing writes, and on fixed text written without a failure record
that is neither matched nor listed there as withheld.
"""
if message is None:
return None
rebuilt = _client_task_failure_message(failure)
if rebuilt is not None:
return rebuilt
if message == "Failed to start indexing. Please try again.":
return message
if result is not None:
if result.get("reconciliation_skipped") is True:
return _reconciliation_skipped_message(
_client_reconciliation_reason(
result.get("reconciliation_reason")
)
)
failed = result.get("failed")
if type(failed) is int and failed > 0:
if "durable_indexed_documents" in result:
return _reconciled_failures_message(failed)
return f"{failed} document(s) failed to index. Check the server logs for details."
return "Indexing failed. Check the server logs for details."
@thread_cleanup
def _background_index_worker(
task_id: str,
collection_id: str,
username: str,
db_password: str,
force_reindex: bool,
max_workers: int = 4,
settings_snapshot: Optional[dict] = None,
):
"""
Background worker thread for indexing documents.
Updates TaskMetadata with progress and checks for cancellation.
The ``max_workers`` parameter bounds the per-job fan-out into
:meth:`LibraryRAGService.index_documents_parallel`. It must be
resolved on the route (which has Flask app context), not inside
this worker — background threads cannot call
:func:`get_settings_manager` safely (#3453, pre-commit hook
``check-settings-manager-thread-safety``).
``settings_snapshot`` remains accepted for caller compatibility. Staged
rebuilds currently require exclusively local-file vector-store providers;
remote providers need a separate atomic publication protocol.
"""
from ...database.session_context import get_user_db_session
from ...research_library.deletion.utils.cascade_helper import (
RemoteVectorStoreDropError,
remote_drop_failure_message,
)
try:
# Create RAG service (thread-safe, no Flask context needed)
with _get_rag_service_for_thread(
collection_id, username, db_password, use_defaults=force_reindex
) as rag_service:
with get_user_db_session(username, db_password) as db_session:
# Get collection
collection = (
db_session.query(Collection)
.filter_by(id=collection_id)
.first()
)
if not collection:
_update_task_status(
username,
db_password,
task_id,
status="failed",
error_message=_COLLECTION_NOT_FOUND,
failure={"code": _FAILURE_COLLECTION_NOT_FOUND},
)
return
# A rebuild retains the published configuration until its
# complete replacement can be committed atomically.
if collection.embedding_model is None and not force_reindex:
_store_collection_embedding_metadata(
collection, rag_service
)
db_session.commit()
# Keep only scalar values after the setup scope exits. The
# parallel coordinator owns its per-document write scopes.
doc_info = [
(doc.id, doc.filename or doc.title or "Unknown")
for _link, doc in _query_documents_to_index(
db_session, collection_id, force_reindex
)
]
if not doc_info and not force_reindex:
_update_task_status(
username,
db_password,
task_id,
status="completed",
progress_message="No documents to index",
)
return
total = len(doc_info)
results = {"successful": 0, "skipped": 0, "failed": 0}
# Update task with total count
_update_task_status(
username,
db_password,
task_id,
progress_total=total,
progress_message=f"Indexing {total} documents",
)
# The caller (start_background_index, which owns the
# Flask request context) resolved ``max_workers`` from
# ``rag.indexing_max_parallel_docs`` and forwarded it
# here. Workers must NOT call ``get_settings_manager()``
# themselves — no Flask app context, pre-commit
# ``check-settings-manager-thread-safety`` would reject,
# and #3453 documents the data-loss shape that produces.
_max_workers = max(1, min(int(max_workers), 16))
def _on_progress(
completed: int,
total: int,
title: str,
status: str,
) -> None:
_update_task_status(
username,
db_password,
task_id,
progress_current=completed,
progress_message=(f"Indexing {completed}/{total}: {title}"),
)
def _is_cancelled() -> bool:
return _is_task_cancelled(username, db_password, task_id)
index_documents = (
rag_service.rebuild_collection
if force_reindex
else rag_service.index_documents_parallel
)
aggregate = index_documents(
doc_info,
collection_id,
max_workers=_max_workers,
progress_callback=_on_progress,
is_cancelled=_is_cancelled,
)
results["successful"] = aggregate["successful"]
results["skipped"] = aggregate["skipped"]
results["failed"] = aggregate["failed"]
results["errors"] = aggregate["errors"]
if aggregate["cancelled"]:
completed = (
aggregate["successful"]
+ aggregate["skipped"]
+ aggregate["failed"]
)
_update_task_status(
username,
db_password,
task_id,
status="cancelled",
progress_message=(
f"Cancelled after {completed}/{total} documents"
),
)
logger.info(
f"Indexing task {task_id} was cancelled "
f"after {completed}/{total} documents"
)
return
# Wrap reconciliation so a transient store/DB error does not
# leave the task in "processing" forever (the previous code
# let exceptions escape, so _update_task_status never ran and
# the UI polled indefinitely — see PR #5235 review comment
# 5085604502). On failure we still record a terminal status
# so cleanup_old_tasks can reap the row.
try:
reconciliation = rag_service.reconcile_collection_index(
collection_id
)
except Exception as exc:
logger.exception(
f"Background indexing task {task_id}: reconciliation failed"
)
reconciliation = {
"indexed_documents": results["successful"],
"indexed_chunks": 0,
"live_vectors": 0,
"orphan_vectors": 0,
"reconciliation_skipped": True,
"reconciliation_reason": _client_reconciliation_reason(exc),
}
if not isinstance(reconciliation, dict):
reconciliation = {
"indexed_documents": results["successful"],
"indexed_chunks": 0,
"live_vectors": 0,
"orphan_vectors": 0,
}
# The reconciler returns ``reconciliation_skipped=True`` when
# live_ids is empty but chunk rows exist (refuse-to-shrink
# guard). Surface this as a task failure so the user sees the
# durable-state inconsistency instead of a misleading
# "0 indexed" success.
if isinstance(reconciliation, dict) and reconciliation.get(
"reconciliation_skipped"
):
reason = reconciliation.get("reconciliation_reason") or (
reconciliation.get("reason")
or "stored vectors could not be loaded"
)
sanitized_reason = _client_reconciliation_reason(reason)
logger.warning(
f"Background indexing task {task_id}: reconciliation "
f"skipped — {sanitize_error_message(str(reason))}"
)
_update_task_status(
username,
db_password,
task_id,
status="failed",
progress_current=total,
progress_message=(
f"Completed: {results['successful']} indexed, "
f"{results['failed']} failed, "
f"{results['skipped']} skipped; reconciliation "
f"skipped: {sanitized_reason}"
),
error_message=_reconciliation_skipped_message(
sanitized_reason
),
result_metadata={
**_client_indexing_summary(results),
# Omit durable_indexed_documents/chunks and
# live_vectors/orphan_vectors: true durable state is
# unverified on the skipped path, and the UI
# suppresses its "Durable vector store: ..." sentence
# when these keys are absent (``Number.isInteger``
# on undefined returns false). Reporting zeros here
# would imply data loss where state is merely
# unverified — see PR #5235 review comment
# 5085604502.
"reconciliation_skipped": True,
"reconciliation_reason": sanitized_reason,
"errors": _sanitized_indexing_errors(results),
},
published_rebuild=force_reindex,
)
return
durable_documents = reconciliation["indexed_documents"]
durable_chunks = reconciliation["indexed_chunks"]
task_result = {
**_client_indexing_summary(results),
"durable_indexed_documents": durable_documents,
"durable_indexed_chunks": durable_chunks,
"live_vectors": reconciliation["live_vectors"],
"orphan_vectors": reconciliation["orphan_vectors"],
"orphan_vectors_removed": reconciliation.get(
"orphan_vectors_removed", 0
),
"partial_document_vectors": reconciliation.get(
"partial_document_vectors", 0
),
"errors": _sanitized_indexing_errors(results),
}
has_failures = results["failed"] > 0
status = "failed" if has_failures else "completed"
message = (
f"Completed: {results['successful']} indexed, "
f"{results['failed']} failed, {results['skipped']} skipped; "
f"durable vector store: {durable_documents} documents, "
f"{durable_chunks} chunks"
)
_update_task_status(
username,
db_password,
task_id,
status=status,
progress_current=total,
progress_message=message,
error_message=(
_reconciled_failures_message(results["failed"])
if has_failures
else None
),
result_metadata=task_result,
# A forced rebuild only gets here after it has published.
published_rebuild=force_reindex,
)
logger.info(
f"Background indexing task {task_id} completed: {results}"
)
except RemoteVectorStoreDropError as exc:
# Caught ahead of the catch-all below: this is an actionable
# vector-store failure, not an internal error (see index_all's
# handler), and logged the same way.
drop_detail = exc.log_detail()
logger.warning(
f"Background indexing task {task_id}: force-reindex aborted, "
f"could not drop the vector-store collection before rebuilding "
f"it: {drop_detail}"
)
_update_task_status(
username,
db_password,
task_id,
status="failed",
error_message=remote_drop_failure_message(
exc, _REMOTE_DROP_OUTCOME
),
# Lets the status endpoint rebuild this fixed-phrase message.
failure=_remote_drop_failure(exc),
)
except Exception as e:
logger.exception(f"Background indexing task {task_id} failed")
_update_task_status(
username,
db_password,
task_id,
status="failed",
# Always the generic text for an exception, which is also what
# the status endpoint serves for a message it cannot rebuild. The
# full exception stays in the server log above.
error_message=_client_indexing_error(e),
)
def _is_database_locked_error(exc: BaseException) -> bool:
"""Return True if ``exc`` looks like a SQLite ``database is locked`` error.
Background indexing can hit transient ``database is locked`` errors when
SQLite is contended; those are recoverable via a short backoff retry.
Anything else (connection failure, integrity error, programming mistake)
should surface immediately so the outer exception handler marks the task
as failed and sets ``completed_at`` for reaping.
"""
return "database is locked" in str(exc).lower()
@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=0.05),
retry=retry_if_exception(_is_database_locked_error),
)
def _do_update_task_status(
username: str,
db_password: str,
task_id: str,
status: str = None,
progress_current: int = None,
progress_total: int = None,
progress_message: str = None,
error_message: str = None,
result_metadata: dict = None,
failure: Optional[dict] = None,
published_rebuild: bool = False,
):
"""Perform the task-status update under a tenacity-managed retry loop.
Only SQLite ``database is locked`` errors are retried (up to 5 attempts
with 0.05s exponential backoff); any other exception escapes so the
outer caller can log it without retrying. See PR #5235 review comment
3669857779.
``result_metadata`` is stored as ``metadata_json["result"]`` and
``failure`` as ``metadata_json["failure"]`` (see
``_client_task_failure_message``).
``published_rebuild`` marks the final update of a forced rebuild whose
replacement index has been committed. Only that update may replace
``cancelled``: a cancel that arrives after publication starts cannot
stop it, and ``cancelled`` would report the previous index as kept.
"""
from ...database.session_context import get_user_db_session
with get_user_db_session(username, db_password) as db_session:
task_query = db_session.query(TaskMetadata).filter_by(task_id=task_id)
# Serialize the guard and update across cancellation and worker
# connections. A SELECT alone does not start SQLite's deferred write
# transaction, and a nested cancellation scope may cache an old row.
task_query.update(
{TaskMetadata.task_id: TaskMetadata.task_id},
synchronize_session=False,
)
task = task_query.first()
if task:
db_session.refresh(task)
if (
published_rebuild
and task.status == "cancelled"
and status in ("completed", "failed")
):
progress_message = " ".join(
filter(None, (progress_message, _LATE_CANCEL_NOTE))
)
# Preserve terminal states: once completed, cancelled or failed, only
# updates confirming that same state are allowed.
elif task.status in ("completed", "cancelled", "failed") and (
status != task.status
):
db_session.commit()
return
elif status == "completed" and task.status != "processing":
db_session.commit()
return
if status is not None:
task.status = status
# Set completed_at for ALL terminal statuses so
# ``cleanup_old_tasks`` (which filters on
# ``status in ["completed", "failed", "cancelled"] AND
# completed_at < cutoff_date``) can reap them.
# Previously only "completed" set the timestamp,
# leaving failed/cancelled rows permanent.
if status in ("completed", "failed", "cancelled"):
task.completed_at = datetime.now(UTC)
if progress_current is not None:
task.progress_current = progress_current
if progress_total is not None:
task.progress_total = progress_total
if progress_message is not None:
task.progress_message = progress_message
if error_message is not None:
task.error_message = error_message
if result_metadata is not None or failure is not None:
metadata = dict(task.metadata_json or {})
if result_metadata is not None:
metadata["result"] = result_metadata
if failure is not None:
metadata["failure"] = failure
task.metadata_json = metadata
db_session.commit()
def _update_task_status(
username: str,
db_password: str,
task_id: str,
status: str = None,
progress_current: int = None,
progress_total: int = None,
progress_message: str = None,
error_message: str = None,
result_metadata: dict = None,
failure: Optional[dict] = None,
published_rebuild: bool = False,
):
"""Update task metadata in the database."""
try:
_do_update_task_status(
username,
db_password,
task_id,
status=status,
progress_current=progress_current,
progress_total=progress_total,
progress_message=progress_message,
error_message=error_message,
result_metadata=result_metadata,
failure=failure,
published_rebuild=published_rebuild,
)
except Exception:
logger.exception(f"Failed to update task status for {task_id}")
def _is_task_cancelled(username: str, db_password: str, task_id: str) -> bool:
"""Check if a task has been cancelled."""
from ...database.session_context import get_user_db_session
try:
with get_user_db_session(username, db_password) as db_session:
task = (
db_session.query(TaskMetadata)
.filter_by(task_id=task_id)
.first()
)
return task and task.status == "cancelled"
except Exception:
logger.warning(
"Could not check cancellation status for task {}", task_id
)
return False
@router.post("/api/collections/{collection_id}/index/start")
async def start_background_index(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Start background indexing for a collection."""
from ...database.session_passwords import session_password_store
session_id = request.session.get("session_id")
# Get password for thread access
db_password = None
if session_id:
db_password = session_password_store.get_session_password(
username, session_id
)
# The shared parser rejects both malformed and non-object bodies before
# they reach the indexing controls below.
data, body_error = await read_json_dict(
request, "success", "Request body must be valid JSON"
)
if body_error is not None:
return body_error
force_reindex = data.get("force_reindex", False)
if not isinstance(force_reindex, bool):
return JSONResponse(
{
"success": False,
"error": "force_reindex must be a boolean",
},
status_code=400,
)
# The lock acquisition and SQLCipher session open below are blocking —
# run the whole check-and-create + thread spawn in the threadpool so a
# contended lock or a cold-engine PBKDF2 derivation can't stall the
# event loop (contextvars propagate through to_thread, so the
# copy_context() for the worker still sees this request's user).
return await run_db_sync(
_start_background_index_sync,
collection_id,
username,
db_password,
force_reindex,
)
def _start_background_index_sync(
collection_id, username, db_password, force_reindex
):
from ...database.session_context import get_user_db_session
# Serialize check-and-create per (user, collection) to close the TOCTOU
# window between scanning for an in-progress task and inserting a new
# row. Two concurrent threads would otherwise both see no match and
# both spawn an indexer on the same collection, racing FAISS writes.
# Single-worker scope only (matches the rest of this module's locking
# model; multi-worker deployments already documented as unsupported
# for this kind of coordination).
lock_key = (username, str(collection_id))
lock = _start_bg_index_locks.setdefault(lock_key, threading.Lock())
task_id = None
try:
with lock, get_user_db_session(username, db_password) as db_session:
conflict = _indexing_conflict(db_session, username, collection_id)
if conflict:
return JSONResponse(conflict, status_code=409)
# Preserve the caller's provider snapshot for force-reindex.
# Staged rebuilds currently refuse remote providers, but the
# background-worker interface still accepts this context.
# Captured here, on the request-scoped session, because
# the worker thread must not build one -- and BEFORE the task row
# is committed, so a failure cannot leave a stuck 'processing'
# task behind. Read strictly and not swallowed: a defaults-only
# snapshot (or None) would silently resolve the global default
# provider instead of the configured backend (see
# read_vector_cleanup_settings_snapshot).
from ...research_library.deletion.utils.cascade_helper import (
read_vector_cleanup_settings_snapshot,
)
_bg_settings_snapshot = (
read_vector_cleanup_settings_snapshot(
get_settings_manager(db_session, username)
)
if force_reindex
else None
)
# Resolve per-job parallel fan-out before committing the task
# (request-scoped session) and forward into the worker, which
# runs on a background thread where the no-arg
# ``get_settings_manager()`` is unsafe (#3453).
try:
_bg_settings = get_settings_manager(db_session, username)
_bg_max_workers = int(
_bg_settings.get_setting(
"rag.indexing_max_parallel_docs", 4
)
)
except SettingsValueError:
raise
except Exception:
_bg_max_workers = 4
_bg_max_workers = max(1, min(_bg_max_workers, 16))
# Create new task
task_id = str(uuid.uuid4())
task = TaskMetadata(
task_id=task_id,
status="processing",
task_type="indexing",
created_at=datetime.now(UTC),
started_at=datetime.now(UTC),
progress_current=0,
progress_total=0,
progress_message="Starting collection rebuild..."
if force_reindex
else "Starting indexing...",
metadata_json={
"collection_id": collection_id,
"force_reindex": force_reindex,
},
)
db_session.add(task)
db_session.commit()
with _active_sse_indexers_lock:
_active_background_indexers[lock_key] = task_id
# Start background thread. Copy the request's contextvars so
# any service code inside the worker that calls
# `get_current_username()` sees the actual user instead of None.
_bg_ctx = contextvars.copy_context()
def _ctx_worker():
try:
_bg_ctx.run(
_background_index_worker,
task_id,
collection_id,
username,
db_password,
force_reindex,
_bg_max_workers,
settings_snapshot=_bg_settings_snapshot,
)
finally:
_unregister_background_indexer(username, collection_id, task_id)
thread = threading.Thread(target=_ctx_worker, daemon=True)
thread.start()
logger.info(
f"Started background indexing task {task_id} for collection {_log_value_preview(collection_id)}"
)
return {
"success": True,
"task_id": task_id,
"message": "Indexing started in background",
}
except Exception:
logger.exception("Failed to start background indexing")
if task_id is not None:
_update_task_status(
username,
db_password,
task_id,
status="failed",
error_message="Failed to start indexing. Please try again.",
)
_unregister_background_indexer(username, collection_id, task_id)
return JSONResponse(
{
"success": False,
"error": "Failed to start indexing. Please try again.",
},
status_code=500,
)
@router.get("/api/collections/{collection_id}/index/status")
@limiter.exempt
def get_index_status(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Get the current indexing status for a collection."""
from ...database.session_context import get_user_db_session
from ...database.session_passwords import session_password_store
session_id = request.session.get("session_id")
db_password = None
if session_id:
db_password = session_password_store.get_session_password(
username, session_id
)
# Observe admission before opening the read transaction. If the worker
# finishes between these reads, report active for one extra poll rather
# than combine an old cancelled task snapshot with a now-inactive worker.
with _active_sse_indexers_lock:
active_task_id = _active_background_indexers.get(
(username, str(collection_id))
)
try:
with get_user_db_session(username, db_password) as db_session:
# Find the most recent indexing task FOR THIS COLLECTION.
#
# collection_id is stored inside the metadata_json JSON column, so
# we can't portably filter on it in SQL (SQLite/SQLCipher JSON
# support varies). Instead, scan recent indexing tasks newest-first
# and return the first whose metadata.collection_id matches. This
# is scoped per collection: a newer indexing task for a DIFFERENT
# collection no longer makes this one falsely report "idle".
#
# This endpoint is polled every ~2s during a reindex and the
# indexing-task table is never pruned, so an unbounded .all() would
# materialize the entire history on every poll (and task_type /
# created_at are unindexed). Bound the scan to the newest N tasks.
# The task we're looking for was just created by the reindex that
# started this poll, so it sits near the top; N=200 is large enough
# that concurrent reindexes of other collections can't push it out
# of the window before it terminates. Trade-off: if more than ~200
# newer indexing tasks for OTHER collections appear while this one
# is still in-flight, the scoped lookup falls back to "idle".
recent_task_scan_limit = 200
recent_tasks = (
db_session.query(TaskMetadata)
.filter(TaskMetadata.task_type == "indexing")
.order_by(TaskMetadata.created_at.desc())
.limit(recent_task_scan_limit)
.all()
)
task = None
for candidate in recent_tasks:
metadata = candidate.metadata_json or {}
if metadata.get("collection_id") == collection_id:
task = candidate
break
collection = db_session.get(Collection, collection_id)
index = (
db_session.query(RAGIndex)
.filter_by(
collection_name=f"collection_{collection_id}",
is_current=True,
)
.first()
)
rebuild_info = _collection_rebuild_info(collection, index)
if not task:
return {
**rebuild_info,
"status": "idle",
"collection_id": collection_id,
"message": "No indexing task for this collection",
}
task_metadata = task.metadata_json or {}
safe_result = _client_indexing_result(task_metadata.get("result"))
worker_active = active_task_id == task.task_id
return {
**rebuild_info,
"task_id": task.task_id,
"force_reindex": task_metadata.get("force_reindex") is True,
# A cancellation request can precede drain or an indivisible
# publication. Clients must observe the worker's final result.
"worker_active": worker_active,
"collection_id": collection_id,
"status": task.status,
"progress_current": task.progress_current or 0,
"progress_total": task.progress_total or 0,
"progress_message": (
"Indexing failed"
if task.status == "failed"
else task.progress_message
),
"error_message": _client_indexing_task_error(
task.error_message,
safe_result,
task_metadata.get("failure"),
),
"result": safe_result,
"created_at": task.created_at.isoformat()
if task.created_at
else None,
"completed_at": task.completed_at.isoformat()
if task.completed_at
else None,
}
except Exception:
logger.exception("Failed to get index status")
return JSONResponse(
{
"status": "error",
"error": "Failed to get indexing status. Please try again.",
},
status_code=500,
)
@router.post("/api/collections/{collection_id}/index/cancel")
def cancel_indexing(
request: Request,
collection_id,
username: Annotated[str, Depends(require_auth)],
):
"""Cancel an active indexing task for a collection."""
from ...database.session_context import get_user_db_session
from ...database.session_passwords import session_password_store
session_id = request.session.get("session_id")
db_password = None
if session_id:
db_password = session_password_store.get_session_password(
username, session_id
)
try:
# Signal active process-local SSE generator(s) if present
sse_cancelled = False
with _active_sse_indexers_lock:
sse_events = _active_sse_indexers.get((username, collection_id))
if sse_events:
for sse_event in sse_events:
sse_event.set()
sse_cancelled = True
logger.info(
f"Signalled active SSE indexing event(s) for collection {collection_id}"
)
with get_user_db_session(username, db_password) as db_session:
# Find active indexing task for this collection
tasks = (
db_session.query(TaskMetadata)
.filter(
TaskMetadata.task_type == "indexing",
TaskMetadata.status == "processing",
)
.all()
)
# Another collection's earlier task must not hide this one.
# Match the same collection metadata used by index/start.
matched_task = next(
(
task
for task in tasks
if (task.metadata_json or {}).get("collection_id")
== collection_id
),
None,
)
if not matched_task and not sse_cancelled:
if tasks:
return JSONResponse(
{
"success": False,
"error": "No active indexing task for this collection",
},
status_code=404,
)
return JSONResponse(
{
"success": False,
"error": "No active indexing task found",
},
status_code=404,
)
if matched_task:
# This endpoint must surface a failed cancellation write.
# Background workers use the best-effort wrapper, but returning
# success here after it swallowed an error would mislead the
# caller while the task remains active.
task_id_to_cancel = matched_task.task_id
_do_update_task_status(
username,
db_password,
task_id_to_cancel,
status="cancelled",
progress_message="Cancellation requested...",
)
logger.info(
f"Requested cancellation of indexing task {task_id_to_cancel} for collection {collection_id}"
)
return {
"success": True,
"message": "Cancellation requested",
"task_id": matched_task.task_id if matched_task else None,
}
except Exception:
logger.exception("Failed to cancel indexing")
return JSONResponse(
{
"success": False,
"error": "Failed to cancel indexing. Please try again.",
},
status_code=500,
)
# Research History Semantic Search Routes have been moved to
# web/routers/library_search.py