~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
"""
Configuration file for search engines.
Loads search engine definitions from the user's configuration.
"""
import copy
import threading
import time
from typing import Any, Dict, Optional
from cachetools import LRUCache
from sqlalchemy.orm import Session
from ..security.secure_logging import logger
from ..config.thread_settings import get_setting_from_snapshot
from ..security.log_sanitizer import scrub_error
from ..settings.exceptions import SettingsValueError
from ..utilities.db_utils import get_settings_manager
from .search_engine_base import _is_api_key_placeholder
_COLLECTION_CACHE_LOCK = threading.Lock()
_COLLECTION_CACHE_MAXSIZE = 50
_COLLECTION_CACHE_TTL = 30.0 # seconds
_COLLECTION_CACHE_MAX_STALE = 300.0 # seconds (5 minutes stale age cap)
# username -> (timestamp, {engine_id: engine_dict})
_COLLECTION_ENGINES_CACHE: LRUCache[str, tuple[float, Dict[str, Any]]] = (
LRUCache(maxsize=_COLLECTION_CACHE_MAXSIZE)
)
# Write generation of _COLLECTION_ENGINES_CACHE: ONE process-wide counter,
# advanced (under _COLLECTION_CACHE_LOCK) by every invalidation, per-user
# or global, and by every eviction. A cache-miss reader captures it under
# the same lock as its lookup and stores its DB snapshot only if it is
# unchanged at store time. Any invalidation or eviction after the capture
# aborts the store. Otherwise the reader would cache a pre-change snapshot
# OVER the invalidation and resurrect stale engines for a full TTL (#6609).
#
# Never reused: the counter is only ever incremented, never reset or
# removed, so a value a reader captured cannot recur. That is why there
# is no per-user generation map. Its entries would have to be dropped
# (to bound memory and leave no logout residue), and a dropped entry
# reads back as a default an in-flight reader may already hold, so its
# stale store would match again (ABA). The cost is that one user's
# invalidation also aborts another user's in-flight store. That is a
# skipped store, never stale data: that user's next read re-queries.
_COLLECTION_CACHE_GENERATION = 0
def _advance_collection_cache_generation() -> None:
"""Abort every in-flight cache-miss store. Caller holds the lock."""
global _COLLECTION_CACHE_GENERATION
_COLLECTION_CACHE_GENERATION += 1
def invalidate_collection_engines_cache(
username: Optional[str] = None,
) -> None:
"""Invalidate cached collection search engine configurations.
Args:
username: If provided, invalidate cache for this username only.
If None, clear the entire collection engines cache.
"""
with _COLLECTION_CACHE_LOCK:
if username:
_COLLECTION_ENGINES_CACHE.pop(username, None)
else:
_COLLECTION_ENGINES_CACHE.clear()
# Advanced on both branches, so no miss in flight at this point
# can store, including one for a user with no cache entry (its
# first-ever read).
_advance_collection_cache_generation()
def evict_collection_engines_cache(username: str) -> None:
"""Drop ``username``'s cached collection engines at session teardown.
Called by every teardown path (logout, password change, idle-connection
sweeper): the entry holds the user's collection names/descriptions for
up to the 30 s TTL, or 5 minutes under stale-serve after a DB error.
The mechanics are those of a per-user invalidation. The entry is
removed (there is no other per-user state here) and the generation
advances, so a miss already in flight at teardown cannot store its
snapshot afterwards. Unlike the invalidation, a falsy ``username`` is a
no-op, never a global clear.
Best-effort: logout deliberately leaves a running research alive, and
that research can refill the entry through :func:`search_config`. Such
an entry then expires like any other (TTL, LRU).
"""
if not username:
return
with _COLLECTION_CACHE_LOCK:
_COLLECTION_ENGINES_CACHE.pop(username, None)
_advance_collection_cache_generation()
def _get_setting(
key: str,
default_value: Any = None,
db_session: Optional[Session] = None,
settings_snapshot: Optional[Dict[str, Any]] = None,
username: Optional[str] = None,
) -> Any:
"""
Get a setting from either a database session or settings snapshot.
Args:
key: The setting key
default_value: Default value if setting not found
db_session: Database session for direct access
settings_snapshot: Settings snapshot for thread context
username: Username for backward compatibility
Returns:
The setting value or default_value if not found
"""
# Try settings snapshot first (thread context)
if settings_snapshot:
try:
return get_setting_from_snapshot(
key, default_value, settings_snapshot=settings_snapshot
)
except SettingsValueError:
raise
except Exception as e:
logger.debug(
f"Could not get setting {key} from snapshot: {scrub_error(e)}"
)
# Try database session if available
if db_session:
try:
settings_manager = get_settings_manager(db_session, username)
return settings_manager.get_setting(key, default_value)
except SettingsValueError:
raise
except Exception as e:
logger.debug(
f"Could not get setting {key} from db_session: {scrub_error(e)}"
)
# Return default if all methods fail
logger.warning(
f"Could not retrieve setting '{key}', returning default: {default_value}"
)
return default_value
def _extract_per_engine_config(
raw_config: Dict[str, Any],
) -> Dict[str, Dict[str, Any]]:
"""
Converts the "flat" configuration loaded from the settings database into
individual settings dictionaries for each engine.
Args:
raw_config: The raw "flat" configuration.
Returns:
Configuration dictionaries indexed by engine name.
"""
nested_config: dict[str, Any] = {}
for key, value in raw_config.items():
if "." in key:
# This is a higher-level key.
top_level_key = key.split(".")[0]
lower_keys = ".".join(key.split(".")[1:])
nested_config.setdefault(top_level_key, {})[lower_keys] = value
else:
# This is a low-level key.
nested_config[key] = value
# Expand all the lower-level keys.
for key, value in nested_config.items():
if isinstance(value, dict):
# Expand the child keys.
nested_config[key] = _extract_per_engine_config(value)
return nested_config
def search_config(
username: Optional[str] = None,
db_session: Optional[Session] = None,
settings_snapshot: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""
Returns the search engine configuration loaded from the database or settings snapshot.
Args:
username: Username used to scope the per-user retriever listing
(own registrations plus shared ones) so one user's retrievers
never leak into another user's engine list. Falls back to the
``_username`` carried in ``settings_snapshot`` when omitted.
db_session: Database session for direct access (preferred for web routes)
settings_snapshot: Settings snapshot for thread context (preferred for background threads)
Returns:
The search engine configuration loaded from the database or snapshot.
"""
# Extract search engine definitions
config_data = _get_setting(
"search.engine.web",
{},
db_session=db_session,
settings_snapshot=settings_snapshot,
username=username,
)
search_engines = _extract_per_engine_config(config_data)
# Inject module/class from the hardcoded engine registry.
# This is the single source of truth for which Python module implements
# each engine — these values are never read from the settings DB.
from .engine_registry import ENGINE_REGISTRY
for name, entry in ENGINE_REGISTRY.items():
if name in search_engines:
search_engines[name]["module_path"] = entry.module_path
search_engines[name]["class_name"] = entry.class_name
if not entry.supports_query_search:
search_engines[name]["supports_query_search"] = False
if entry.full_search_module:
search_engines[name]["full_search_module"] = (
entry.full_search_module
)
search_engines[name]["full_search_class"] = (
entry.full_search_class
)
# Add registered retrievers as available search engines. Scope the
# listing to the requesting user (their own registrations plus shared
# ones) so one user's retriever names/metadata never leak into another
# user's engine list. Prefer an explicit username, else the ``_username``
# carried in the snapshot; None lists shared retrievers only.
from ..search_system import username_from_snapshot
effective_username = username or username_from_snapshot(settings_snapshot)
from .retriever_registry import retriever_registry
for name in retriever_registry.list_registered(username=effective_username):
search_engines[name] = {
"module_path": ".engines.search_engine_retriever",
"class_name": "RetrieverSearchEngine",
"requires_api_key": False,
"requires_llm": False,
"description": f"LangChain retriever: {name}",
"strengths": [
"Domain-specific knowledge",
"No rate limits",
"Fast retrieval",
],
"weaknesses": ["Limited to indexed content"],
"supports_full_search": True,
"is_retriever": True, # Mark as retriever for identification
}
logger.info(
f"Loaded {len(search_engines)} search engines from configuration file"
)
logger.info(f"\n {', '.join(sorted(search_engines.keys()))} \n")
# Register Library RAG as a search engine
library_enabled = _get_setting(
"search.engine.library.enabled",
True,
db_session=db_session,
settings_snapshot=settings_snapshot,
username=username,
)
if library_enabled:
search_engines["library"] = {
"module_path": ".engines.search_engine_library",
"class_name": "LibraryRAGSearchEngine",
"requires_llm": True,
"display_name": "Search All Collections",
"default_params": {},
"description": "Search across all your document collections using semantic search",
"strengths": [
"Searches all your curated collections of research papers and documents",
"Uses semantic search for better relevance",
"Returns documents you've already saved and reviewed",
],
"weaknesses": [
"Limited to documents already in your collections",
"Requires documents to be indexed first",
],
"reliability": "High - searches all your collections",
}
logger.info("Registered Library RAG as search engine")
# Register document collections as individual search engines
if library_enabled:
try:
from ..database.models.library import Collection
from ..database.session_context import get_user_db_session
from ..search_system import username_from_snapshot
# Get username from settings_snapshot if available
collection_username = (
username_from_snapshot(settings_snapshot)
if settings_snapshot
else username
)
if collection_username:
now = time.monotonic()
cached_engines = None
with _COLLECTION_CACHE_LOCK:
cache_entry = _COLLECTION_ENGINES_CACHE.get(
collection_username
)
if cache_entry and (
now - cache_entry[0] < _COLLECTION_CACHE_TTL
):
cached_engines = cache_entry[1]
# Captured under the same lock as the lookup so a
# concurrent invalidation cannot slip between "miss"
# and "capture": any later invalidation or eviction
# advances it and aborts our store below.
generation = _COLLECTION_CACHE_GENERATION
if cached_engines is not None:
search_engines.update(copy.deepcopy(cached_engines))
logger.debug(
f"Reused {len(cached_engines)} cached document collection engines for {collection_username}"
)
else:
try:
with get_user_db_session(
collection_username
) as session:
collections = session.query(Collection).all()
user_collection_engines = {}
for collection in collections:
engine_id = f"collection_{collection.id}"
# Add suffix to distinguish from the all-collections search
display_name = f"{collection.name} (Collection)"
# Egress classification follows the per-collection
# public/private flag (default private). A "public"
# collection counts as a public engine (allowed under
# PUBLIC_ONLY); a private one is local-only. NULL
# (pre-migration rows) reads as private — the safe
# default.
collection_is_public = bool(
getattr(collection, "is_public", False)
)
# Usability flag (NOT egress): whether the LangGraph
# research agent offers this collection as a tool. NULL
# (pre-migration rows) reads as available (True). Uses
# the same `is not False` idiom as the rag_routes
# serializers so all call sites share one NULL→available
# default and can't drift.
collection_agent_enabled = (
getattr(collection, "agent_enabled", True)
is not False
)
user_collection_engines[engine_id] = {
"module_path": ".engines.search_engine_collection",
"class_name": "CollectionSearchEngine",
"requires_llm": True,
"is_local": True,
"is_public": collection_is_public,
"agent_enabled": collection_agent_enabled,
"display_name": display_name,
"default_params": {
"collection_id": collection.id,
"collection_name": collection.name,
},
"description": (
collection.description
if collection.description
else f"Search documents in {collection.name} collection only"
),
"strengths": [
f"Searches only documents in {collection.name}",
"Focused semantic search within specific topic area",
"Returns documents from a curated collection",
],
"weaknesses": [
"Limited to documents in this collection",
"Smaller result pool than full library search",
],
"reliability": "High - searches a specific collection",
}
with _COLLECTION_CACHE_LOCK:
# Abort the store if an invalidation or
# eviction landed since our lookup: our
# snapshot may predate it, so caching it
# could resurrect a stale engine list for a
# full TTL (#6609).
if _COLLECTION_CACHE_GENERATION == generation:
_COLLECTION_ENGINES_CACHE[
collection_username
] = (
now,
copy.deepcopy(user_collection_engines),
)
else:
logger.debug(
f"Skipped caching collection engines for "
f"{collection_username}: the cache was "
f"invalidated during refresh"
)
search_engines.update(
copy.deepcopy(user_collection_engines)
)
logger.info(
f"Registered {len(user_collection_engines)} document collections as search engines"
)
except Exception as exc:
safe_msg = scrub_error(exc)
logger.warning(
f"Could not register document collections for {collection_username}: {safe_msg}"
)
logger.debug(
f"Traceback for collection registration failure for {collection_username}",
exc_info=True,
)
with _COLLECTION_CACHE_LOCK:
stale_entry = _COLLECTION_ENGINES_CACHE.get(
collection_username
)
if stale_entry and (
now - stale_entry[0] <= _COLLECTION_CACHE_MAX_STALE
):
search_engines.update(copy.deepcopy(stale_entry[1]))
logger.warning(
f"Using {len(stale_entry[1])} stale cached collection engines for {collection_username} after DB error"
)
elif stale_entry:
logger.warning(
f"Stale cached collection engines for {collection_username} expired "
f"(age {now - stale_entry[0]:.1f}s > {_COLLECTION_CACHE_MAX_STALE}s), skipping"
)
else:
logger.debug(
"No username available for collection registration"
)
except Exception as exc:
safe_msg = scrub_error(exc)
logger.warning(
f"Could not register document collections: {safe_msg}"
)
logger.debug(
"Traceback for document collections registration failure",
exc_info=True,
)
return search_engines
def list_eligible_engine_configs(
settings_snapshot: Optional[Dict[str, Any]] = None,
*,
use_api_key_services: bool = True,
egress_context: Optional[Any] = None,
check_agent_enabled: bool = False,
check_auto_search: bool = False,
skip_availability_probes: Optional[set[str]] = None,
) -> Dict[str, Any]:
"""Return every engine from ``search_config`` that can be instantiated.
This is the candidate pool for surfaces that decide visibility
through their own flag (``agent_enabled`` for the langgraph research
agent) — NOT for surfaces governed by ``use_in_auto_search``. Engines
that need a key are dropped when no key is set (or when
``use_api_key_services`` is False), so callers don't have to repeat the
credential check; everything else (retrievers, the built-in ``library``
engine, dynamic ``collection_*`` entries) flows through.
The name keys in the returned dict are the canonical engine names from
``search_config`` (e.g. ``searxng``, ``elasticsearch``) — matching the
registry in ``engine_registry.py`` 1:1, so callers can compare against
``search.tool`` directly.
Engines whose ``is_available(settings_snapshot)`` returns ``False`` are
excluded — this catches the case where the engine is configured but its
backing service is unreachable (e.g. Elasticsearch with no running
cluster on ``localhost:9200``). Without this filter the agent still
SEES the broken engine as a tool in its heartbeat and the factory logs
``Failed to create search engine '<name>' (ConnectionError)`` per step.
Subclasses of ``BaseSearchEngine`` override ``is_available`` to opt in;
the default is True so existing engines are unaffected.
Filters for disabled state (``agent_enabled`` / ``use_in_auto_search``) and
egress policy are evaluated BEFORE calling ``is_available`` so unreachable or
disabled engines do not trigger unnecessary network probes.
Args:
settings_snapshot: Thread-safe settings snapshot.
use_api_key_services: When False, engines that require an API key
are excluded even when the key is present.
egress_context: Optional EgressContext to evaluate policy before probing.
check_agent_enabled: When True, excludes engines disabled for agent.
check_auto_search: When True, excludes engines disabled for auto-search.
skip_availability_probes: Engines whose current state is already known
to a caller's availability tracker. Keep their configured entries
for cooldown recovery, without probing an unavailable service.
The caller must apply that state before model and tool calls.
Returns:
Dict of engine_name → config for engines that passed all checks.
"""
if not settings_snapshot:
logger.warning(
"list_eligible_engine_configs called without settings_snapshot, "
"returning empty dict"
)
return {}
all_engines = search_config(settings_snapshot=settings_snapshot)
if egress_context is not None:
from ..security.egress.policy import (
evaluate_engine,
evaluate_retriever,
)
eligible: Dict[str, Any] = {}
for name, config in all_engines.items():
requires_key = config.get("requires_api_key", False)
if requires_key and not use_api_key_services:
continue
if requires_key:
api_key = _resolve_api_key(name, config, settings_snapshot)
if not api_key:
logger.debug(
f"Skipping {name} — requires API key but none configured"
)
continue
# Optional auto-search flag check prior to probe
if check_auto_search:
use_in_auto = config.get("use_in_auto_search")
if use_in_auto is None:
auto_search_key = f"search.engine.web.{name}.use_in_auto_search"
use_in_auto = get_setting_from_snapshot(
auto_search_key, False, settings_snapshot=settings_snapshot
)
if not use_in_auto:
continue
# Optional agent_enabled flag check prior to probe
if check_agent_enabled:
agent_enabled = config.get("agent_enabled")
if agent_enabled is None:
agent_enabled = get_setting_from_snapshot(
f"search.engine.web.{name}.agent_enabled",
True,
settings_snapshot=settings_snapshot,
)
if not agent_enabled:
logger.debug(f"Skipping {name} — disabled for research agent")
continue
# Egress policy check prior to network probe
if egress_context is not None:
if config.get("is_retriever"):
try:
from .retriever_registry import retriever_registry
meta = retriever_registry.get_metadata(
name,
username=getattr(egress_context, "username", None),
)
except Exception:
meta = None
decision = evaluate_retriever(
name, egress_context, metadata=meta
)
else:
decision = evaluate_engine(
name,
egress_context,
settings_snapshot=settings_snapshot,
metadata=config,
)
if not decision.allowed:
logger.debug(
f"Skipping {name} — disallowed by egress policy ({decision.reason})"
)
continue
# Runtime availability probe. Cheap (cached, single TCP connect at
# worst) and excludes engines whose backing service isn't reachable
# so the agent doesn't waste steps advertising broken tools.
# ``is_retriever`` entries go through the retriever registry's own
# ``is_available`` path — skip them here so we don't double-probe
# or fall back to the base class default for custom retriever
# classes that never subclass ``BaseSearchEngine``.
if not config.get("is_retriever", False) and name not in (
skip_availability_probes or ()
):
if not _engine_class_is_available(name, config, settings_snapshot):
logger.debug(
f"Skipping {name} — runtime availability probe failed"
)
continue
eligible[name] = config
return eligible
def _engine_class_is_available(
name: str,
config: Dict[str, Any],
settings_snapshot: Dict[str, Any],
) -> bool:
"""Load the engine class for ``config`` and call its ``is_available``.
Returns True (fail-open) on any error — the class load can legitimately
fail (bad module_path, missing dependency, sandbox import restrictions)
and we don't want ``list_eligible_engine_configs`` to mask real config
problems with an unrelated "engine unavailable" log line. The factory
will surface the real ``Failed to create search engine`` error when
(and only when) the caller actually tries to instantiate it.
"""
from .search_engine_base import BaseSearchEngine
module_path = config.get("module_path")
class_name = config.get("class_name")
if not module_path or not class_name:
return True
try:
from ..security.module_whitelist import get_safe_module_class
engine_class = get_safe_module_class(module_path, class_name)
except Exception as exc:
logger.debug(
"Skipping availability probe for {} — could not load engine "
"class ({}: {})",
name,
type(exc).__name__,
exc,
)
return True
# Only subclasses of BaseSearchEngine get the ``is_available`` contract.
# Library / Collection / Retriever engines either subclass it (in which
# case the default ``True`` applies) or don't (also default True).
if not isinstance(engine_class, type) or not issubclass(
engine_class, BaseSearchEngine
):
return True
try:
return bool(
engine_class.is_available(settings_snapshot=settings_snapshot)
)
except SettingsValueError:
raise
except Exception as exc:
logger.debug(
"is_available raised for {} — treating as available ({})",
name,
type(exc).__name__,
)
return True
def get_available_engines(
settings_snapshot: Optional[Dict[str, Any]] = None,
use_api_key_services: bool = True,
exclude_engines: Optional[set] = None,
) -> Dict[str, Any]:
"""
Return query search engines that are actually usable: enabled for
auto-search and with valid API keys when required. URL-only archive
adapters remain in ``list_eligible_engine_configs`` for archive fetching.
This is the single shared filter used by ``auto``-mode search so the
LangGraph agent agrees with the rest of the system on which engines
show up in non-agent searches. For the agent's specialised tool list,
use :func:`list_eligible_engine_configs` instead — the agent's surface
is governed by per-engine ``agent_enabled``, not by this auto-search
filter (see #5015).
Args:
settings_snapshot: Thread-safe settings snapshot.
use_api_key_services: If False, engines that require an API key are
excluded even when the key is present.
exclude_engines: Additional engine names to skip (e.g. the caller's
own name).
Returns:
Dict of engine_name → config for engines that passed all checks.
"""
eligible = list_eligible_engine_configs(
settings_snapshot=settings_snapshot,
use_api_key_services=use_api_key_services,
check_auto_search=True,
)
excluded = set(exclude_engines) if exclude_engines else set()
return {
name: config
for name, config in eligible.items()
if name not in excluded and config.get("supports_query_search", True)
}
def _resolve_api_key(
engine_name: str,
engine_config: Dict[str, Any],
settings_snapshot: Dict[str, Any],
) -> Optional[str]:
"""
Try to find a valid API key for *engine_name*.
Resolution order (mirrors ``create_search_engine``):
1. ``search.engine.web.<name>.api_key`` in the snapshot
2. ``api_key`` inside the engine config dict
Returns the key string or None.
"""
api_key = None
api_key_path = f"search.engine.web.{engine_name}.api_key"
api_key_setting = settings_snapshot.get(api_key_path)
if api_key_setting:
api_key = (
api_key_setting.get("value")
if isinstance(api_key_setting, dict)
else api_key_setting
)
if not api_key:
api_key = engine_config.get("api_key")
if not api_key:
return None
# Reject common placeholder values
api_key_str = str(api_key).strip()
if _is_api_key_placeholder(api_key_str):
return None
return api_key_str
def default_search_engine(
username: Optional[str] = None,
db_session: Optional[Session] = None,
settings_snapshot: Optional[Dict[str, Any]] = None,
) -> str:
"""
Returns the configured default search engine.
Args:
username: Username for backward compatibility (deprecated)
db_session: Database session for direct access (preferred for web routes)
settings_snapshot: Settings snapshot for thread context (preferred for background threads)
Returns:
The configured default search engine.
"""
return str(
_get_setting(
"search.engine.DEFAULT_SEARCH_ENGINE",
"wikipedia",
db_session=db_session,
settings_snapshot=settings_snapshot,
username=username,
)
)