Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

101 KB
Raw Blame
import json
import re
import time
from abc import ABC, abstractmethod
from contextlib import contextmanager
from contextvars import ContextVar
from dataclasses import dataclass
from typing import (
    TYPE_CHECKING,
    Any,
    Callable,
    Dict,
    List,
    Optional,
    Set,
    TypeVar,
    Union,
)
from urllib.parse import urlparse

import requests
from langchain_core.language_models import BaseLLM
from ..security.secure_logging import logger
from tenacity import (
    retry,
    retry_if_exception_type,
    stop_after_attempt,
)
from tenacity.wait import wait_base

from ..advanced_search_system.filters.base_filter import BaseFilter
from ..utilities.type_utils import _is_strict_scope, unwrap_setting
from ..utilities.url_utils import library_display_url
from ..config.constants import DEFAULT_MAX_FILTERED_RESULTS
from ..config.thread_settings import get_setting_from_snapshot
from ..security.log_sanitizer import sanitize_error_message, scrub_error
from ..security.egress.classification import Exposure, Sensitivity
from ..utilities.thread_context import clear_search_context, set_search_context
from ..utilities.research_content_cache import (
    LIBRARY_PROVENANCE as _LIBRARY_PROVENANCE,
    is_library_cache_key as _is_library_cache_key,
    is_library_provenance_entry as _is_library_provenance_entry,
)

from .rate_limiting import RateLimitError, get_tracker
from .engine_availability import (
    SearchEngineError,
    SearchFailure,
    failure_from_exception,
    http_status,
    is_query_error,
    retry_after_from_error,
)

if TYPE_CHECKING:
    from ..advanced_search_system.filters.journal_reputation_filter import (
        JournalReputationFilter,
    )


# Common placeholder values that should never be treated as a real API key.
# Three call sites previously had inconsistent subsets of this list, which
# let invalid placeholders silently through the production path in
# search_engines_config.py. Centralizing here so all three stay in sync.
#
# All entries are lowercased — comparison is case-insensitive (see
# ``_is_api_key_placeholder``). Substring patterns like ``endswith("_API_KEY")``
# are intentionally NOT used: they silently rejected legitimate keys that
# happened to contain those substrings (e.g. ``sk-real-secret_API_KEY``).
# Users who paste an env-var name like ``BRAVE_API_KEY`` instead of its value
# will get a clear API-call-time auth failure instead.
API_KEY_PLACEHOLDERS = frozenset(
    {
        "",
        "none",
        "null",
        "placeholder",
        # Underscore forms
        "your_api_key_here",
        "your_api_key",
        "api_key",
        "insert_api_key_here",
        "insert_api_key",
        # Hyphenated forms shipped in ``src/local_deep_research/defaults/.env.template``
        # or ``docs/env_configuration.md`` as the literal value users are told to
        # copy-paste. Each must be rejected verbatim. Drift is enforced by
        # ``TestEnvTemplatePlaceholdersCovered`` and ``TestEnvDocsPlaceholdersCovered``.
        "your-api-key-here",
        "your-anthropic-key-here",
        "your-openai-key-here",
        "your-openrouter-key-here",
        "your-google-api-key-here",
        "your-brave-key-here",
        "your-serpapi-key-here",
        "your-tavily-key-here",
        # Defensive extras — plausible variations users might type but not in
        # any template/docs. Listed explicitly so they don't silently accumulate.
        "your-api-key",  # without -here suffix
        "your-key-here",  # without api- prefix
        "your-azure-key",  # Azure provider variant
    }
)

# Canonical short codes for time-restricted search (the global
# ``search.time_period`` setting minus "all", which means "no filter" and is
# expressed by omitting the engine's recency param entirely). Engines that
# forward a recency filter to their API (brave, serpapi, tavily) validate
# against this shared set so the accepted codes can't drift apart.
VALID_TIME_PERIODS = frozenset({"d", "w", "m", "y"})


def _is_api_key_placeholder(api_key: Optional[str]) -> bool:
    """Return True if ``api_key`` looks like a placeholder, not a real key.

    Catches:
    - Exact-match placeholders, case-insensitively (see ``API_KEY_PLACEHOLDERS``)
    - Angle-bracket templates: ``<...>`` (e.g. ``<your_api_key>``)
    - Shell-variable templates: ``${...}`` (e.g. ``${BRAVE_API_KEY}``)
    """
    if not api_key:
        return True
    api_key = api_key.strip().lower()
    if api_key in API_KEY_PLACEHOLDERS:
        return True
    if api_key.startswith("<") and api_key.endswith(">"):
        return True
    if api_key.startswith("${") and api_key.endswith("}"):
        return True
    return False


@dataclass
class _SearchRunState:
    """Outcomes belonging to one search invocation, including its retries."""

    search_failed: bool = False
    rate_limited_after_previews: bool = False
    failure: SearchFailure | None = None
    error_type: str | None = None
    error_message: str | None = None


class AdaptiveWait(wait_base):
    """Custom wait strategy that uses adaptive rate limiting."""

    def __init__(self, get_wait_func):
        self.get_wait_func = get_wait_func

    def __call__(self, retry_state):
        return self.get_wait_func()


def metrics_engine_name(engine: Any) -> str:
    """The engine name ``SearchTracker`` records for ``engine``'s searches."""
    return type(engine).__name__.replace("SearchEngine", "").lower()


_SearchResult = TypeVar("_SearchResult")


class SearchAttempts:
    """Keep fallback outcomes local until the provider search is finished.

    A successful empty attempt supersedes an earlier failure. Useful results
    survive a later optional failure. If the final attempt failed and there
    are no usable results, propagate its exception to the shared boundary.
    """

    def __init__(self):
        self.last_error: Exception | None = None

    def run(self, operation: Callable, *args, fallback=None, **kwargs):
        try:
            result = operation(*args, **kwargs)
        except Exception as error:
            BaseSearchEngine._propagate_control_error(error)
            self.last_error = error
            return [] if fallback is None else fallback
        self.last_error = None
        return result

    def finish(self, results: _SearchResult) -> _SearchResult:
        if not results and self.last_error is not None:
            raise self.last_error
        return results


class BaseSearchEngine(ABC):
    """
    Abstract base class for search engines with two-phase retrieval capability.
    Handles common parameters and implements the two-phase search approach.

    Subclass contract for ``__init__``
    ---------------------------------
    Concrete engines should forward ``settings_snapshot`` and
    ``programmatic_mode`` to ``super().__init__`` so the base class can wire
    them up correctly. The cleanest way is to declare ``**kwargs`` and pass
    it along::

        def __init__(self, max_results=10, *, my_param=None, **kwargs):
            super().__init__(max_results=max_results, **kwargs)
            self.my_param = my_param

    Many existing engines accept ``**kwargs`` but don't forward — that
    silently drops ``programmatic_mode`` (and used to bind the wrong rate
    tracker). The factory has a post-construction safety net that calls
    ``_configure_programmatic_mode`` when the engine's mode doesn't match
    what the caller asked for, but new engines should not rely on it: the
    safety net only covers ``programmatic_mode``, not ``settings_snapshot``
    or other base kwargs.
    """

    # Class attribute to indicate if this engine searches public internet sources
    # Should be overridden by subclasses - defaults to False for safety
    is_public = False

    _initial_search_failure: SearchFailure | None = None
    # True when the provider answers an invalid ``self.api_key`` with a plain
    # HTTP 403 that carries no authentication hint. A 403 on a request sent
    # with a configured key then disables the engine for the run instead of
    # cooling it down again and again.
    bare_403_means_bad_api_key = False

    # Class attribute to indicate if this is a generic search engine (vs specialized)
    # Generic engines are general web search (Google, Bing, etc) vs specialized (arXiv, PubMed).
    # Note: generic does NOT imply good native ranking — see is_lexical.
    is_generic = False

    # Class attribute to indicate if this is a scientific/academic search engine
    # Scientific engines include arXiv, PubMed, Semantic Scholar, etc.
    is_scientific = False

    # Class attribute to indicate if this is a local RAG/document search engine
    # Local engines search private document collections stored locally
    is_local = False

    # Cache key provenance: only engines that read the user's own library
    # database (the library and collection RAG engines) may write shared
    # research cache entries under library keys (``/library/document/...``).
    # Every other engine's result links come from the engine's own data, so
    # a library-shaped link there must not populate the library namespace.
    writes_library_cache_keys = False

    # Egress data classification (ADR-0007). Two orthogonal axes, declared
    # explicitly per engine alongside is_public/is_local. The defaults fail
    # closed: an engine that forgets to classify itself is treated as the most
    # restrictive quadrant (sensitive data + exposing sink → usable only by
    # itself). Engines whose label depends on configuration (a collection's
    # is_public flag, a self-hosted store's URL) are refined by the classifier
    # at run time; these constants are the static fallback.
    egress_sensitivity = Sensitivity.SENSITIVE
    egress_exposure = Exposure.EXPOSING

    # Whether full-content fetches of this engine's RESULT URLs may reach
    # private / loopback hosts. The search-engine factory reads it when it
    # builds the FullSearchResults wrapper (the only full-content path) and
    # hands it over as the wrapper's ``allow_private_ips``. Default False:
    # an engine that holds such a grant resolves it itself in ``__init__``
    # (SearXNG: instance approval AND the env-only
    # ``search.allow_private_result_fetch`` opt-in); every other engine keeps
    # private result URLs blocked (issue #2477). The wrapper applies the
    # grant only under the run's ``unprotected`` egress scope.
    allow_private_result_fetch = False

    # Class attribute to indicate if this is a news search engine
    # News engines specialize in news articles and current events
    is_news = False

    # Class attribute to indicate if this is a code search engine
    # Code engines specialize in searching code repositories
    is_code = False

    # Class attribute to indicate if this is a book/literature search engine
    # Book engines search libraries and literary archives
    is_books = False

    # Classification: does this engine use lexical/keyword-based search?
    # Lexical engines (arXiv, PubMed, Wikipedia, Mojeek, etc.) match results by
    # keywords without ML-based ranking. This is an informational flag that can
    # drive multiple behaviors (query optimization, result deduplication, UI hints).
    # For LLM relevance filtering specifically, see needs_llm_relevance_filter.
    is_lexical = False

    # Behavioral: should the factory auto-enable LLM relevance filtering?
    # When True, the factory sets enable_llm_relevance_filter=True on the engine
    # instance, causing _filter_for_relevance() to run after previews are fetched.
    # Typically set alongside is_lexical=True, but can be set independently —
    # e.g. a non-lexical engine with noisy results could opt in.
    needs_llm_relevance_filter = False

    # Tuning for the LLM relevance filter (only applies when the filter
    # is active for this engine).
    #
    # relevance_filter_batch_size: split previews into chunks of this many
    # before sending to the LLM. Smaller batches are faster per call and
    # more reliable on weaker models which struggle with many indices in
    # one context. None or 0 = single-call mode (no batching).
    #
    # relevance_filter_max_parallel_batches: number of batches to dispatch
    # concurrently against the LLM. 1 = sequential. Most providers handle
    # parallel requests fine (Ollama with OLLAMA_NUM_PARALLEL>1, OpenAI,
    # Anthropic).
    relevance_filter_batch_size: Optional[int] = 5
    relevance_filter_max_parallel_batches: int = 10

    # Class attribute for rate limit detection patterns
    # Subclasses can override to add engine-specific patterns
    rate_limit_patterns: Set[str] = {
        "rate limit",
        "rate_limit",
        "ratelimit",
        "too many requests",
        "throttl",
        "quota exceeded",
        "quota_exceeded",
        "limit exceeded",
        "request limit",
        "api limit",
        "usage limit",
    }

    # Instance-attribute names holding credential values to redact from error
    # messages/logs via _scrub_error(). Subclasses override when they store
    # secrets under different names (e.g. Elasticsearch: _api_key/_password).
    # Centralizing the list keeps every _scrub_error() call site uniform and
    # prevents the per-site drift that previously dropped a secret.
    # ``_rejected_api_key`` is in the default because the OpenAlex DOI
    # enrichment pass can store a refused key there on *any* scientific
    # engine (arxiv, semantic_scholar, …), not just OpenAlexSearchEngine.
    # ``_scrub_error`` reads it through ``getattr(..., None)``, so engines
    # that never see a rejection are unaffected.
    _secret_attrs: tuple[str, ...] = ("api_key", "_rejected_api_key")

    # Class-level defaults so every engine has them without touching
    # __init__ (and so a ``__new__``-built instance degrades rather than
    # raising). ``_openalex_key_rejected`` latches "OpenAlex refused this
    # credential" for the instance's lifetime: dropping the key from the
    # engine's own attributes is not enough, because
    # ``_resolve_openalex_enrichment_key`` would otherwise re-read the
    # very same key out of the settings snapshot it came from and send it
    # again on the enrichment request of the same ``run()``.
    _openalex_key_rejected: bool = False
    # The refused literal, kept only so it stays in the redaction set
    # (see ``_secret_attrs``) after the live attribute is cleared.
    _rejected_api_key: Optional[str] = None

    def _new_search_run_state(self) -> _SearchRunState:
        """Allow adapters to keep additional retry state in the same scope."""
        return _SearchRunState()

    def _search_run_state_var(self) -> ContextVar[Optional[_SearchRunState]]:
        # Normally initialized in __init__; retain support for bare instances
        # used to call individual adapter helpers without constructing an engine.
        state_var: ContextVar[Optional[_SearchRunState]] | None = getattr(
            self, "_search_run_state", None
        )
        if state_var is None:
            state_var = ContextVar("search_run_state", default=None)
            self._search_run_state = state_var
        return state_var

    def _get_search_run_state(self) -> _SearchRunState:
        state_var = self._search_run_state_var()
        state = state_var.get()
        if state is None:
            # Direct calls to private adapter helpers can share retry state in
            # this context. Public run() always starts a fresh invocation.
            state = self._new_search_run_state()
            state.failure = self.last_search_failure
            state_var.set(state)
        return state

    def _completed_search_failure_var(
        self,
    ) -> ContextVar[tuple[bool, SearchFailure | None]]:
        state_var: ContextVar[tuple[bool, SearchFailure | None]] | None = (
            getattr(self, "_completed_search_failure", None)
        )
        if state_var is None:
            state_var = ContextVar(
                "completed_search_failure", default=(False, None)
            )
            self._completed_search_failure = state_var
        return state_var

    @property
    def last_search_failure(self) -> SearchFailure | None:
        """Health from this context's invocation, or the initial probe."""
        state = self._search_run_state_var().get()
        if state is not None:
            return state.failure
        completed, failure = self._completed_search_failure_var().get()
        return failure if completed else self._initial_search_failure

    @last_search_failure.setter
    def last_search_failure(self, failure: SearchFailure | None) -> None:
        state = self._search_run_state_var().get()
        if state is None:
            # Constructor probes are visible to every context that receives
            # this engine, before any provider search has run there.
            self._initial_search_failure = failure
        else:
            state.failure = failure

    @contextmanager
    def _search_context(self):
        """Keep outcomes separate across threads, tasks and nested searches."""
        state_var = self._search_run_state_var()
        state = self._new_search_run_state()
        token = state_var.set(state)
        try:
            yield
        finally:
            self._completed_search_failure_var().set((True, state.failure))
            state_var.reset(token)

    @property
    def _search_failed(self) -> bool:
        """An absorbed preview failure in the current search attempt."""
        return self._get_search_run_state().search_failed

    @_search_failed.setter
    def _search_failed(self, value: bool) -> None:
        self._get_search_run_state().search_failed = value

    @property
    def _rate_limited_after_previews(self) -> bool:
        """An absorbed full-content HTTP 429 in the current search attempt."""
        return self._get_search_run_state().rate_limited_after_previews

    @_rate_limited_after_previews.setter
    def _rate_limited_after_previews(self, value: bool) -> None:
        self._get_search_run_state().rate_limited_after_previews = value

    def _note_openalex_key_rejected(
        self, api_key: Optional[str] = None
    ) -> None:
        """Latch that OpenAlex refused *api_key*, for this engine's life.

        Called by every OpenAlex path that can learn a key is bad: the
        search engine's own ``_drop_rejected_api_key`` and the DOI
        enrichment pass's ``on_key_rejected`` callback. Once set,
        ``_resolve_openalex_enrichment_key`` returns ``None``, so no
        path on this object sends the refused credential a second time.

        ``or self._rejected_api_key`` keeps the stored literal: a second
        call (two threads driving one engine) must not overwrite it with
        ``None`` and silently remove it from the redaction set.
        """
        self._openalex_key_rejected = True
        self._rejected_api_key = api_key or self._rejected_api_key

    @staticmethod
    def _ensure_list(value, *, default=None):
        """Normalize a value that should be a list.

        Handles JSON-encoded strings, comma-separated strings, and
        already-parsed lists.  Returns *default* (empty list when not
        supplied) for ``None`` or empty/unparseable input.
        """
        if default is None:
            default = []
        if value is None:
            return default
        if isinstance(value, list):
            return value
        if isinstance(value, str):
            stripped = value.strip()
            if not stripped:
                return default
            if stripped.startswith("["):
                try:
                    parsed = json.loads(stripped)
                    if isinstance(parsed, list):
                        return [str(item) for item in parsed]
                except (json.JSONDecodeError, ValueError, RecursionError):
                    pass
            return [
                item.strip() for item in stripped.split(",") if item.strip()
            ]
        return default

    @classmethod
    def _load_engine_class(cls, name: str, config: Dict[str, Any]):
        """
        Helper method to load an engine class dynamically.

        Args:
            name: Engine name
            config: Engine configuration dict with module_path and class_name

        Returns:
            Tuple of (success: bool, engine_class or None, error_msg or None)
        """
        from ..security.module_whitelist import (
            ModuleNotAllowedError,
            get_safe_module_class,
        )

        try:
            module_path = config.get("module_path")
            class_name = config.get("class_name")

            if not module_path or not class_name:
                return (
                    False,
                    None,
                    f"Missing module_path or class_name for {name}",
                )

            # Use whitelist-validated safe import
            engine_class = get_safe_module_class(module_path, class_name)

            return True, engine_class, None

        except ModuleNotAllowedError as e:
            return (
                False,
                None,
                f"Security error loading engine class for {name}: "
                f"{scrub_error(e)}",
            )
        except Exception as e:
            return (
                False,
                None,
                f"Could not load engine class for {name}: {scrub_error(e)}",
            )

    @classmethod
    def is_available(
        cls, settings_snapshot: Optional[Dict[str, Any]] = None
    ) -> bool:
        """Runtime availability probe used by ``list_eligible_engine_configs``.

        Subclasses whose ``__init__`` performs a network probe that can
        hard-fail (e.g. Elasticsearch's ``client.info()`` raises
        ``ConnectionError`` when the cluster is unreachable) should override
        this to fail closed: returning ``False`` causes the engine to be
        omitted from specialized agent tool surfaces and the auto-search
        candidate pool, so those paths do not advertise or instantiate a
        broken engine. The primary generic ``web_search`` tool and direct
        factory callers can still attempt construction and surface the
        underlying configuration or connection error.

        Default is ``True`` — engines that don't probe at init stay
        eligible. This is the opposite default of ``BaseLLMProvider`` because
        the vast majority of search engines here are pure REST clients with
        lazy connection errors; changing the default would force every
        subclass to opt in to ``True`` for no benefit.
        """
        return True

    def __init__(
        self,
        llm: Optional[BaseLLM] = None,
        max_filtered_results: Optional[int] = None,
        max_results: Optional[int] = 10,  # Default value if not provided
        preview_filters: List[BaseFilter] | None = None,
        content_filters: List[BaseFilter] | None = None,
        search_snippets_only: bool = True,  # New parameter with default
        include_full_content: bool = False,
        settings_snapshot: Optional[Dict[str, Any]] = None,
        programmatic_mode: bool = False,
        content_cache: Any = None,
        progress_callback=None,
        **kwargs,
    ):
        """
        Initialize the search engine with common parameters.

        Args:
            llm: Optional language model for relevance filtering
            max_filtered_results: Maximum number of results to keep after filtering
            max_results: Maximum number of search results to return
            preview_filters: Filters that will be applied to all previews
                produced by the search engine, before relevancy checks.
            content_filters: Filters that will be applied to the full content
                produced by the search engine, after relevancy checks.
            search_snippets_only: Whether to return only snippets or full content
            include_full_content: Whether to use FullSearchResults for full webpage content
            settings_snapshot: Settings snapshot for configuration
            programmatic_mode: If True, disables database operations and uses memory-only tracking
            content_cache: Optional shared ``ResearchContentCache`` for the
                whole research run. When None, the cache bound to the
                current research (if any) is used, so repeat URLs across
                the quick-report phase, detailed subsections, lead agent
                and subagents are served without re-fetching on the spot.
            progress_callback: Optional ``(done, total)`` bulk-fetch liveness
                hook forwarded to ``FullSearchResults`` via
                ``_init_full_search`` / ``set_fetch_progress_callback``
                (pipeline-strategy fetch progress, #7193).
            **kwargs: Additional engine-specific parameters
        """
        self._search_run_state = ContextVar("search_run_state", default=None)
        self._completed_search_failure = ContextVar[
            tuple[bool, SearchFailure | None]
        ]("completed_search_failure", default=(False, None))
        if max_filtered_results is None:
            max_filtered_results = DEFAULT_MAX_FILTERED_RESULTS
        if max_results is None:
            max_results = 10
        self._preview_filters: List[BaseFilter] = (
            preview_filters if preview_filters is not None else []
        )
        self._content_filters: List[BaseFilter] = (
            content_filters if content_filters is not None else []
        )

        self.llm = llm  # LLM for relevance filtering
        self._max_filtered_results = int(
            max_filtered_results
        )  # Ensure it's an integer
        self._max_results = max(
            1, int(max_results)
        )  # Ensure it's a positive integer
        self.search_snippets_only = search_snippets_only  # Store the setting
        self.include_full_content = include_full_content
        self.settings_snapshot = (
            settings_snapshot or {}
        )  # Store settings snapshot
        # Single shared cache for the whole research run. Also honoured
        # from **kwargs for engines whose __init__ forwards extras.
        if content_cache is None:
            content_cache = kwargs.get("content_cache")
        self.content_cache = content_cache
        # Bulk-fetch liveness hook (``(done, total)``) for full-search
        # progress. Set at construction via the factory or later via
        # ``set_fetch_progress_callback`` (e.g. when
        # ``AdvancedSearchSystem.set_progress_callback`` arrives after
        # engine creation). Forwarded to ``FullSearchResults``.
        self._fetch_progress_callback = progress_callback

        self.engine_type = self.__class__.__name__
        self._engine_name: str = ""  # set by the factory after construction
        # The snapshot dict the runtime egress backstop last verified
        # against (identity comparison; holding the reference keeps the
        # memo valid — a verification re-runs when the caller assigns a
        # NEW snapshot) plus the policy-relevant VALUES it carried at
        # verification time, so an in-place mutation of the scope or
        # primary key on the same dict is also caught.
        self._egress_verified_snapshot: Optional[Dict[str, Any]] = None
        self._egress_verified_policy_key: Optional[tuple] = None
        self._egress_skip_warned = False
        self._configure_programmatic_mode(programmatic_mode)
        self._last_wait_time = (
            0.0  # Default to 0 for successful searches without rate limiting
        )
        self._last_results_count = 0

    def _create_journal_filter(
        self,
        engine_name: str,
        llm: Optional[BaseLLM],
        settings_snapshot: Optional[Dict[str, Any]],
    ) -> Optional["JournalReputationFilter"]:
        """Build the default :class:`JournalReputationFilter` for this engine.

        Wraps the identical 8-line ``JournalReputationFilter.create_default``
        boilerplate that previously lived in every academic subclass. The
        ``engine_name`` is passed explicitly because the auto-derived class
        name does not match the settings key for ``nasa_ads`` (would be
        ``"nasaads"``) or ``semantic_scholar`` (would be
        ``"semanticscholar"``).

        ``llm`` and ``settings_snapshot`` are passed in rather than read from
        ``self`` because subclasses build their preview filters *before*
        calling ``super().__init__()`` (the filter is handed to the parent
        constructor), so ``self.llm`` / ``self.settings_snapshot`` do not
        exist yet at call time.

        Args:
            engine_name: Settings key identifying the engine (e.g.
                ``"arxiv"``, ``"nasa_ads"``).
            llm: Language model used for the filter's relevance pass.
            settings_snapshot: Settings snapshot for configuration lookups.

        Returns:
            A configured :class:`JournalReputationFilter`, or ``None`` if
            filtering is disabled in settings for this engine.
        """
        from ..advanced_search_system.filters.journal_reputation_filter import (
            JournalReputationFilter,
        )

        return JournalReputationFilter.create_default(
            model=llm,  # type: ignore[arg-type]
            engine_name=engine_name,
            settings_snapshot=settings_snapshot,
        )

    def _configure_programmatic_mode(self, programmatic_mode: bool) -> None:
        """Set ``programmatic_mode`` and (re)bind the matching rate tracker.

        Called from ``__init__`` and from the factory as a fallback when an
        engine subclass swallows the ``programmatic_mode`` kwarg without
        forwarding it to ``super().__init__``. Safe to call after init —
        rebinding ``rate_tracker`` discards the previous tracker (no
        resources to release) and the new one starts with empty in-memory
        caches.
        """
        self.programmatic_mode = programmatic_mode
        if programmatic_mode:
            from .rate_limiting.tracker import AdaptiveRateLimitTracker

            self.rate_tracker = AdaptiveRateLimitTracker(
                settings_snapshot=self.settings_snapshot,
                programmatic_mode=programmatic_mode,
            )
        else:
            self.rate_tracker = get_tracker()

    def get_content_cache(self) -> Any:
        """Return the effective shared content cache for this engine.

        Prefers the explicitly configured ``self.content_cache`` (the
        single cache for the whole research run, threaded through the
        factory from the strategy), else the cache bound to the current
        research via the thread-local search context. Returns ``None``
        when no research cache is active (legacy direct-fetch behaviour).
        """
        if getattr(self, "content_cache", None) is not None:
            return self.content_cache
        try:
            from ..utilities.research_content_cache import get_current_cache
        except Exception:  # pragma: no cover - defensive
            return None
        try:
            return get_current_cache()
        except Exception:  # pragma: no cover - defensive
            return None

    @staticmethod
    def _collection_scope_for_item(item: Dict[str, Any]) -> Optional[str]:
        """Return the normalised collection scope for a library chunk *item*.

        The same ``(document, chunk_index)`` can hold different text in
        two collections (different chunk settings), so chunk cache entries
        are namespaced by collection. Prefers ``collection_id`` (UUID or
        ``collection_<uuid>``) from metadata/top-level, falling back to a
        ``collection_*``-prefixed ``collection_name``. Returns ``None``
        when no collection can be established — callers bypass the shared
        URL entry rather than risk serving another collection's text.
        """
        try:
            from ..utilities.research_content_cache import (
                normalize_collection_scope as _norm,
            )
        except Exception:  # pragma: no cover - defensive
            return None
        metadata = item.get("metadata")
        if not isinstance(metadata, dict):
            metadata = {}
        for source in (metadata, item):
            if not isinstance(source, dict):
                continue
            cid = source.get("collection_id")
            if isinstance(cid, str) and cid.strip():
                scoped = _norm(cid)
                if scoped is not None:
                    return scoped
        for source in (metadata, item):
            if not isinstance(source, dict):
                continue
            cname = source.get("collection_name")
            if isinstance(cname, str) and cname.startswith("collection_"):
                return cname
        return None

    @staticmethod
    def _is_library_chunk_link(link: Any) -> bool:
        """Return True when *link* is a library chunk URL (``#chunk-N``)."""
        if not isinstance(link, str):
            return False
        try:
            from ..utilities.url_utils import library_display_url
        except Exception:  # pragma: no cover - defensive
            return False
        try:
            display = library_display_url(link.strip())
        except Exception:  # pragma: no cover - defensive
            return None  # type: ignore[return-value]
        return display is not None and "#chunk-" in display

    def _get_cache_egress_context(self) -> Any | None:
        """Egress context for the cache scope gate, memoized per snapshot.

        Mirrors ``_verify_egress_scope`` memo discipline (identity + policy
        key) so ADAPTIVE resolution — which can do a bounded DNS lookup —
        runs once per snapshot, not once per cached URL. Returns None when
        no snapshot is configured (callers fall back to SSRF-only, matching
        the network path without a context) or when the policy cannot be
        evaluated — callers must then refuse the cache hit, matching the
        network path which disables fetching
        (``_build_full_search_egress_context`` sets ``include_full_content``
        to False) instead of fetching SSRF-only.
        """
        try:
            snapshot = getattr(self, "settings_snapshot", None)
            if not isinstance(snapshot, dict):
                return None
            strict = _is_strict_scope(snapshot or {})
            if not snapshot and not strict:
                return None
            primary_default = (
                None if strict else getattr(self, "_engine_name", None)
            )
            policy_key = (self._egress_policy_key(), primary_default)
            cached_ctx = getattr(self, "_cache_egress_ctx", None)
            cached_key = getattr(self, "_cache_egress_key", None)
            cached_snap = getattr(self, "_cache_egress_snapshot", None)
            if (
                cached_ctx is not None
                and cached_snap is snapshot
                and cached_key == policy_key
            ):
                return cached_ctx
            from ..security.egress.policy import (
                context_from_snapshot,
                resolve_run_primary_engine,
            )
            from ..search_system import username_from_snapshot

            ctx = context_from_snapshot(
                snapshot,
                resolve_run_primary_engine(snapshot, default=primary_default),
                username=username_from_snapshot(snapshot),
            )
            self._cache_egress_ctx = ctx
            self._cache_egress_key = policy_key
            self._cache_egress_snapshot = snapshot
            return ctx
        except Exception:  # pragma: no cover - defensive
            logger.debug("cache scope gate: egress context unavailable")
            return None

    def _is_cache_url_allowed(self, link: Any) -> bool:
        """Whether a cache hit for *link* may be served to this engine.

        Defense-in-depth for the shared research cache: content cached by a
        granted engine (``allow_private_result_fetch=True``) must not flow
        into a non-granted engine sharing the same run cache. Path-only
        library routes (``/library/...``, ``/lib/...``) are local DB reads,
        not network fetches, so they are always allowed. The absolute
        ``https://library.document/<id>`` alias is NOT exempted — it is a
        network URL from the engine fill/store path and must pass the same
        SSRF gate as any web URL. Web URLs must pass the same SSRF gate
        the fetch path enforces (grant + link-local carve-out).

        Scope-aware: the run's ``evaluate_url`` scope check must also allow
        the URL (matching the network path's SSRF + scope pair in
        ``FullSearchResults.run`` / ``_get_full_content``). Unlike
        ``FullSearchResults._downloader_allow_private_ips`` — which withholds
        the private grant from the download pipeline because it cannot
        enforce scope on redirect hops / browser subresources — the cache
        has no hops, so the boolean grant is kept and scope is enforced
        per-URL via ``evaluate_url``. No current producer births a
        scoped-grant private entry the network path would refuse, so this
        is latent hardening.

        Fail-closed twice: when the SSRF validator cannot be imported the
        hit is refused, and when a settings snapshot is configured but its
        policy cannot be evaluated the hit is refused too — matching the
        network path, which disables fetching instead of falling back to
        SSRF-only. Path-only library routes stay servable even then: they
        are local DB reads (the fetch tool's ``_try_resolve_url`` fast path
        likewise skips the egress gate), so refusing them would hide
        local-only content under a corrupted scope setting. Successful SSRF
        verdicts are memoized per-run in the shared cache so repeat hits
        skip DNS (and stay servable when DNS drops mid-run after a
        success); refusals are recomputed so a transient DNS blip never
        pins the URL. The scope verdict reuses the run's own DNS memo in
        ``EgressContext`` and never burns the run-wide denial quota (cache
        probes use the non-recording evaluation; only real fetches count).
        That also means cache probes emit no ``policy denied`` audit line
        while fetch-path denials of the same URL still do — intended
        (probes must not burn quota), just a triage asymmetry to know
        about. An exhausted quota still refuses cache probes
        (``denial_quota_exceeded``), same as the fetch path.
        """
        if not isinstance(link, str) or not link.strip():
            return False
        # Local-only fast path first: path-only library routes are DB
        # reads, not network fetches, so they stay servable even when the
        # run policy is unevaluable. This deliberately mirrors the fetch
        # tool's strict grammar (32/64-hex or UUID via
        # ``library_resolver.parse_library_url``), NOT the wider citation
        # grammar (``is_safe_document_id`` / ``library_display_url`` accepts
        # any ``[0-9A-Za-z_-]+`` slug): a slug-shaped route can never
        # resolve via the fetch tool, so exempting it here would let the
        # two surfaces drift.
        try:
            from ..advanced_search_system.tools.fetch.library_resolver import (
                parse_library_url,
            )

            stripped = link.strip()
            if (
                stripped.startswith("/")
                and parse_library_url(stripped) is not None
            ):
                return True
        except Exception:  # pragma: no cover - defensive
            logger.debug("cache grant check: library route parse failed")
        # Unevaluable policy fails closed for web URLs: a snapshot is
        # configured but its policy cannot be evaluated (e.g. unknown
        # egress scope), and the network path disables fetching outright
        # instead of falling back to SSRF-only.
        egress_ctx = self._get_cache_egress_context()
        snapshot = getattr(self, "settings_snapshot", None)
        if egress_ctx is None and (
            snapshot or _is_strict_scope(snapshot or {})
        ):
            logger.debug(
                "cache grant check: unevaluable egress policy, refusing hit"
            )
            return False
        try:
            from ..security.ssrf_validator import validate_url
        except Exception:  # pragma: no cover - defensive
            logger.debug(
                "cache grant check: validator unavailable, refusing hit"
            )
            return False
        try:
            allow_private = bool(
                getattr(self, "allow_private_result_fetch", False)
            )

            def _compute() -> bool:
                return bool(
                    validate_url(
                        link,
                        allow_private_ips=allow_private,
                        block_link_local=True,
                    )
                )

            cache = self.get_content_cache()
            _memo = getattr(cache, "validated", None)
            if callable(_memo):
                try:
                    ssrf_ok = bool(_memo(link, allow_private, True, _compute))
                except Exception:  # pragma: no cover - defensive
                    return False
            else:
                ssrf_ok = _compute()
            if not ssrf_ok:
                return False
            # Scope axis: the network path (FullSearchResults.run /
            # _get_full_content) runs evaluate_url after SSRF. The cache
            # must not serve what that path would deny — but the probe must
            # not burn the run-wide denial quota either, so it evaluates
            # without recording; only real fetches count toward
            # MAX_DENIED_FETCHES_PER_RUN.
            if egress_ctx is not None:
                try:
                    from ..security.egress.policy import evaluate_url

                    decision = evaluate_url(
                        link, egress_ctx, record_denial=False
                    )
                    if not decision.allowed:
                        return False
                except Exception:  # pragma: no cover - defensive
                    return False
            return True
        except Exception:  # pragma: no cover - defensive
            return False

    def _fill_full_content_from_cache(
        self, items: list[Dict[str, Any]]
    ) -> tuple[list[Dict[str, Any]], list[Dict[str, Any]]]:
        """Split *items* into (already_cached, needs_fetch).

        Cached items get ``full_content`` filled in place from the shared
        research cache. Items without a link, or with no cache entry, go
        to *needs_fetch*. Returns ``([], items)`` when no cache is active.

        Library chunk URLs are namespaced by collection: the same
        document/chunk URL in two collections holds different text, so a
        chunk whose collection cannot be established bypasses the shared
        entry (fresh DB lookup) rather than risking another collection's
        text. Document-level library URLs (``Document.text_content`` is
        collection-independent) and web URLs use the URL-only key.
        """
        cache = self.get_content_cache()
        if cache is None or not items:
            return [], list(items)
        cached: list[Dict[str, Any]] = []
        missing: list[Dict[str, Any]] = []
        for item in items:
            if not isinstance(item, dict):
                missing.append(item)
                continue
            link = item.get("link") or item.get("url")
            if not link:
                missing.append(item)
                continue
            if not self._is_cache_url_allowed(link):
                missing.append(item)
                continue
            try:
                if self._is_library_chunk_link(link):
                    scope = self._collection_scope_for_item(item)
                    if scope is None:
                        missing.append(item)
                        continue
                    hit = cache.get(link, collection_id=scope)
                else:
                    hit = cache.get(link)
            except Exception:  # pragma: no cover - defensive
                hit = None
            if hit is not None and _is_library_cache_key(link):
                # Library keys are served only from entries written by a
                # library DB read path; anything else is a miss here.
                if not _is_library_provenance_entry(hit):
                    hit = None
            if hit is not None and isinstance(hit.get("content"), str):
                # A preview can already carry an abstract in full_content.
                # The shared cache contains complete text and takes priority.
                if (
                    not item.get("full_content")
                    or item.get("full_content_is_complete") is not True
                ):
                    item["full_content"] = hit["content"]
                item["full_content_is_complete"] = True
                cached.append(item)
            else:
                missing.append(item)
        return cached, missing

    def _store_full_content_in_cache(self, items: list[Dict[str, Any]]) -> None:
        """Store only explicitly complete text in the shared research cache.

        Engines must set ``full_content_is_complete=True`` at the point
        they retrieve source text. The legacy ``full_content`` field also
        holds abstracts, snippets, errors and retrieved fragments; those
        must not satisfy a later full-document fetch or prevent a retry.
        Unknown producers remain usable as search results without caching.

        Library chunk entries are namespaced by collection (see
        :meth:`_fill_full_content_from_cache`); chunks without an
        establishable collection are not stored under the shared URL key.
        Entries this engine's grant would not allow to read are not
        written either, keeping the shared cache grant-consistent.

        Library keys (``/library/document/...``) are written only by
        engines with ``writes_library_cache_keys`` (the library and
        collection engines), tagged with library provenance. Other engines'
        library-shaped links are not stored.
        """
        cache = self.get_content_cache()
        if cache is None or not items:
            return
        writes_library = bool(getattr(self, "writes_library_cache_keys", False))
        for item in items:
            if not isinstance(item, dict):
                continue
            if item.get("full_content_is_complete") is not True:
                continue
            link = item.get("link") or item.get("url")
            content = item.get("full_content")
            if not link or not isinstance(content, str) or not content:
                continue
            if not self._is_cache_url_allowed(link):
                continue
            is_library_key = _is_library_cache_key(link)
            if is_library_key and not writes_library:
                continue
            provenance = _LIBRARY_PROVENANCE if is_library_key else None
            try:
                # Web result titles describe search listings, not fetched
                # page metadata. Leave that cache field empty until a page
                # reader supplies it; library titles come from local data.
                cache_title = (
                    item.get("title")
                    if library_display_url(link) is not None
                    else ""
                )
                if self._is_library_chunk_link(link):
                    scope = self._collection_scope_for_item(item)
                    if scope is None:
                        continue
                    cache.put(
                        link,
                        content,
                        title=cache_title,
                        url_out=link,
                        collection_id=scope,
                        provenance=provenance,
                    )
                else:
                    cache.put(
                        link,
                        content,
                        title=cache_title,
                        url_out=link,
                        provenance=provenance,
                    )
            except Exception:  # pragma: no cover - defensive
                continue

    def _get_full_content_cached(
        self, relevant_items: list[Dict[str, Any]]
    ) -> list[Dict[str, Any]]:
        """Cache-aware wrapper around ``_get_full_content``.

        Serves repeat URLs (quick phase vs subagents, lead vs subagents,
        repeated ``web_search`` calls) from the single research-scoped
        cache and only fetches uncached URLs on the spot. Newly fetched
        content is stored for later consumers in the same research run.
        """
        if not relevant_items:
            return []
        _, needs_fetch = self._fill_full_content_from_cache(relevant_items)
        if not needs_fetch:
            return relevant_items
        # Fetch only the uncached subset; cached items were filled in place.
        fetched = self._get_full_content(needs_fetch)
        # _get_full_content may return a new list; merge back by identity:
        # cached items are already correct in relevant_items, fetched ones
        # carry fresh full_content to store.
        try:
            self._store_full_content_in_cache(
                fetched if isinstance(fetched, list) else needs_fetch
            )
        except Exception:  # allow: silent-exception
            pass
        if isinstance(fetched, list) and fetched is not needs_fetch:
            # Engine returned a new list for the subset: splice it back in
            # order. Fall back to the in-place list on any shape mismatch.
            # The contract is length-preserving and order-preserving —
            # ``zip`` truncation would silently drop results if an engine
            # returned fewer/reordered items, so pin it with a warning.
            if len(fetched) != len(needs_fetch):
                logger.warning(
                    "_get_full_content returned {} items for {} "
                    "requested; falling back to in-place list",
                    len(fetched),
                    len(needs_fetch),
                )
                return relevant_items
            try:
                by_id = {id(o): n for o, n in zip(needs_fetch, fetched)}
                merged: list[Dict[str, Any]] = []
                for item in relevant_items:
                    merged.append(by_id.get(id(item), item))
                return merged
            except Exception:  # pragma: no cover - defensive
                return relevant_items
        return relevant_items

    @property
    def max_filtered_results(self) -> int:
        """Get the maximum number of filtered results."""
        return self._max_filtered_results

    @max_filtered_results.setter
    def max_filtered_results(self, value: int) -> None:
        """Set the maximum number of filtered results."""
        if value is None:
            value = DEFAULT_MAX_FILTERED_RESULTS
            logger.warning(
                f"Setting max_filtered_results to {DEFAULT_MAX_FILTERED_RESULTS}"
            )
        self._max_filtered_results = int(value)

    @property
    def max_results(self) -> int:
        """Get the maximum number of search results."""
        return self._max_results

    @max_results.setter
    def max_results(self, value: int) -> None:
        """Set the maximum number of search results."""
        if value is None:
            value = 10
        self._max_results = max(1, int(value))

    def _get_adaptive_wait(self) -> float:
        """Get adaptive wait time from tracker."""
        wait_time = self.rate_tracker.get_wait_time(self.engine_type)
        self._last_wait_time = wait_time
        logger.debug(
            f"{self.engine_type} waiting {wait_time:.2f}s before retry"
        )
        return wait_time

    def _record_retry_outcome(self, retry_state) -> None:
        """Record outcome after retry completes."""
        success = (
            not retry_state.outcome.failed if retry_state.outcome else False
        )
        self.rate_tracker.record_outcome(
            self.engine_type,
            self._last_wait_time or 0,
            success,
            retry_state.attempt_number,
            error_type="RateLimitError" if not success else None,
            search_result_count=self._last_results_count if success else 0,
        )

    def _verify_egress_scope(self) -> None:
        """Runtime backstop: verify this engine is allowed under the egress
        scope in ``self.settings_snapshot`` before executing a search.

        No-op when ``settings_snapshot`` is missing or empty (programmatic
        API callers) unless the operator enforces STRICT, or when
        ``_engine_name`` has not been set. Raises
        ``PolicyDeniedError`` when the policy denies the engine; any other
        internal error in the policy evaluation is logged and ignored so a
        broken backstop never takes down searches the factory PEP already
        approved.

        This is a defense-in-depth check behind the factory PEP, the
        strategy-level filters, and the audit hook.  It covers engines that
        bypassed the factory (direct instantiation with a snapshot).
        Changes to the captured scope or primary are caught both when the
        caller refreshes ``settings_snapshot`` and when it edits those
        keys in place.

        The verification is memoized per snapshot IDENTITY plus the
        policy-relevant VALUES (scope, primary): the full policy inputs
        are stable for a given snapshot object, so re-evaluating on every
        ``run()`` would only repeat the same decision — and under
        ADAPTIVE scope with a URL-configurable primary the evaluation can
        include a DNS lookup (bounded at 2s), which must not become a
        per-search stall. Assigning a refreshed snapshot OR mutating the
        scope/primary keys in place invalidates the memo and re-verifies.
        Denials are never memoized (they raise).
        """
        if not self.settings_snapshot and not _is_strict_scope(None):
            return
        if not self._engine_name:
            # A snapshot WITHOUT a stamped engine name means a direct
            # instantiation the factory never saw and no subclass
            # self-stamps — the backstop cannot evaluate it. Surface the
            # gap in the audit log (once per instance) instead of
            # skipping silently.
            if not self._egress_skip_warned:
                self._egress_skip_warned = True
                logger.bind(policy_audit=True).warning(
                    "egress backstop skipped: engine has a settings "
                    "snapshot but no _engine_name stamped",
                    engine_type=self.__class__.__name__,
                )
            return
        if (
            self._egress_verified_snapshot is not None
            and self.settings_snapshot is self._egress_verified_snapshot
            and self._egress_policy_key() == self._egress_verified_policy_key
        ):
            return
        from ..security.egress.policy import PolicyDeniedError

        try:
            self._check_egress_policy()
        except PolicyDeniedError:
            logger.bind(policy_audit=True).warning(
                "runtime egress backstop denied engine",
                engine=self._engine_name,
            )
            raise
        except Exception:
            logger.bind(policy_audit=True).debug(
                "egress runtime verify errored; "
                "factory PEP still enforces at instantiation",
                engine=self._engine_name,
            )
        else:
            self._egress_verified_snapshot = self.settings_snapshot
            self._egress_verified_policy_key = self._egress_policy_key()

    def _egress_policy_key(self) -> tuple:
        """The policy-relevant snapshot values the backstop memo guards on.

        Cheap dict reads only — no context construction, no DNS. Not an
        exhaustive input set (URL settings or DB-side collection flags can
        also influence the decision), but it covers the keys an in-place
        mutation would realistically target; the factory PEP remains the
        primary enforcement for everything else.

        MAINTENANCE: if a new snapshot key ever becomes policy-relevant in
        ``context_from_snapshot``/``evaluate_engine``, add it here too —
        otherwise an in-place mutation of that key returns a stale memo.

        """
        scope = unwrap_setting(
            self.settings_snapshot.get("policy.egress_scope")
        )
        primary = unwrap_setting(self.settings_snapshot.get("search.tool"))
        return (scope, primary)

    def _check_egress_policy(self) -> None:
        """Inner helper so the raise is not inside the except handler
        (ruff TRY301).  Raises PolicyDeniedError on scope mismatch."""
        from ..security.egress.policy import (
            Decision,
            PolicyDeniedError,
            context_from_snapshot,
            evaluate_engine,
            resolve_run_primary_engine,
        )
        from ..search_system import username_from_snapshot

        try:
            # Match the factory: STRICT requires the saved primary and must
            # never let this engine approve itself as a fallback.
            primary_default = (
                None
                if _is_strict_scope(self.settings_snapshot)
                else self._engine_name
            )
            ctx = context_from_snapshot(
                self.settings_snapshot,
                resolve_run_primary_engine(
                    self.settings_snapshot, default=primary_default
                ),
                username=username_from_snapshot(self.settings_snapshot),
            )
        except ValueError as exc:
            # The backstop propagates policy denials but ignores other
            # errors; invalid configuration must stop a cached engine too.
            raise PolicyDeniedError(
                Decision(False, "invalid_policy_config"),
                target=self._engine_name,
            ) from exc
        decision = evaluate_engine(
            self._engine_name,
            ctx,
            settings_snapshot=self.settings_snapshot,
        )
        if not decision.allowed:
            raise PolicyDeniedError(decision, target=self._engine_name)

    def run(
        self, query: str, research_context: Dict[str, Any] | None = None
    ) -> List[Dict[str, Any]]:
        """
        Run the search engine with a given query, retrieving and filtering results.
        This implements a two-phase retrieval approach:
        1. Get preview information for many results
        2. Filter the previews for relevance
        3. Get full content for only the relevant results

        Args:
            query: The search query
            research_context: Context from previous research to use.

        Returns:
            List of search results with full content (if available)
        """
        with self._search_context():
            return self._run_search(query, research_context)

    def _run_search(
        self, query: str, research_context: Dict[str, Any] | None = None
    ) -> List[Dict[str, Any]]:
        """Execute and record one search inside its invocation state."""
        logger.info(f"---Execute a search using {self.__class__.__name__}---")

        # Runtime egress scope backstop: verify this engine is allowed
        # before making any HTTP requests.
        self._verify_egress_scope()

        # Track search call for metrics (if available and not in programmatic mode)
        should_record_metrics = False
        context_was_set = False
        if not self.programmatic_mode:
            from ..metrics.search_tracker import SearchTracker

            should_record_metrics = True

            # For thread-safe context propagation: if we have research_context parameter, use it
            # Otherwise, try to inherit from current thread context (normal case)
            # This allows strategies running in threads to explicitly pass context when needed
            if research_context:
                # Explicit context provided - use it and set it for this thread
                set_search_context(research_context)
                context_was_set = True

        engine_name = metrics_engine_name(self)
        start_time = time.time()

        success = True
        error_message = None
        error_type = None
        results_count = 0

        # Define the core search function with retry logic. Each branch
        # defines its own function and both are selected through
        # ``run_search``: conditionally redefining a single name is a
        # mypy error since tenacity 9.2, whose @retry returns a typed
        # wrapper (_RetryDecorated) instead of a plain Callable.
        run_search: Callable[[], Any]
        if self.rate_tracker.enabled:
            # Rate limiting enabled - use retry with adaptive wait
            @retry(
                stop=stop_after_attempt(3),
                wait=AdaptiveWait(lambda: self._get_adaptive_wait()),
                retry=retry_if_exception_type((RateLimitError,)),
                after=self._record_retry_outcome,
                reraise=True,
            )
            def _run_with_retry():
                nonlocal success, error_message, results_count
                return _execute_search()

            run_search = _run_with_retry
        else:
            # Rate limiting disabled - run without retry
            def _run_without_retry():
                nonlocal success, error_message, results_count
                return _execute_search()

            run_search = _run_without_retry

        def _execute_search():
            nonlocal success, error_message, error_type, results_count

            previews_received = False
            try:
                # Step 1: Get preview information for items
                self._search_failed = False
                self._rate_limited_after_previews = False
                self.last_search_failure = None
                previews = self._search_previews(query)
                if self._search_failed:
                    success = False
                    state = self._get_search_run_state()
                    error_message = (
                        state.error_message or "Preview retrieval failed"
                    )
                    error_type = state.error_type
                if not previews:
                    if self.last_search_failure is not None:
                        success = False
                        error_message = (
                            error_message or self.last_search_failure.reason
                        )
                        error_type = error_type or "SearchFailure"
                    logger.info(
                        f"Search engine {self.__class__.__name__} returned no preview results"
                    )
                    results_count = 0
                    return []
                # The search request worked. A partial failure while
                # gathering previews, or the relevance filter dropping every
                # result below, does not make the engine unavailable.
                previews_received = True
                self.last_search_failure = None

                # Pre-filter enrichment: resolve DOIs to OpenAlex source
                # IDs BEFORE the preview filters run. The
                # JournalReputationFilter is registered as a preview
                # filter and uses ``result["openalex_source_id"]`` for
                # Tier 2 lookups; running enrichment afterwards (as the
                # old pipeline did) left the field empty at filter time,
                # silently forcing a fragile-name-match fallback. Only
                # for scientific engines whose results carry DOIs.
                if getattr(self, "is_scientific", False):
                    try:
                        from ..utilities.openalex_enrichment import (
                            enrich_results_with_source_ids,
                        )

                        email = getattr(self, "email", None)
                        previews = enrich_results_with_source_ids(
                            previews,
                            email=email,
                            api_key=self._resolve_openalex_enrichment_key(),
                            # Enrichment's own drop is call-local; this
                            # latches it on the engine so later runs
                            # resolve None instead of the same refused
                            # key, and keeps the literal redactable.
                            on_key_rejected=self._note_openalex_key_rejected,
                        )
                    except Exception:
                        logger.debug(
                            "DOI enrichment skipped (import or call failed)"
                        )

                for preview_filter in self._preview_filters:
                    previews = preview_filter.filter_results(previews, query)

                # Step 2: Filter previews for relevance with LLM
                enable_llm_filter = getattr(
                    self, "enable_llm_relevance_filter", False
                )

                if enable_llm_filter and self.llm:
                    filtered_items = self._filter_for_relevance(previews, query)
                else:
                    filtered_items = previews
                    logger.debug(
                        f"[{type(self).__name__}] Relevance filter skipped "
                        f"(enabled={enable_llm_filter}, "
                        f"llm={'yes' if self.llm else 'no'})"
                    )

                # Step 3: Get full content for filtered items
                if self.search_snippets_only:
                    logger.info("Returning snippet-only results as per config")
                    results = filtered_items
                else:
                    # Cache-aware: repeat URLs across the quick phase,
                    # subsections and subagents are served from the single
                    # research-scoped cache instead of re-fetched on the spot.
                    results = self._get_full_content_cached(filtered_items)

                for content_filter in self._content_filters:
                    results = content_filter.filter_results(results, query)

                results_count = len(results)
                self._last_results_count = results_count

                # Record success if we get here and rate limiting is enabled
                if (
                    self.rate_tracker.enabled
                    and self._rate_limited_after_previews
                ):
                    # The results are usable, but the provider answered a
                    # later request with HTTP 429: tell the limiter, as
                    # ``_record_retry_outcome`` does for a raised 429.
                    logger.info(
                        f"Recording rate-limited search for {self.engine_type}: wait_time={self._last_wait_time}s, results={results_count}"
                    )
                    self.rate_tracker.record_outcome(
                        self.engine_type,
                        self._last_wait_time or 0,
                        success=False,
                        retry_count=1,
                        error_type="RateLimitError",
                        search_result_count=results_count,
                    )
                elif self.rate_tracker.enabled:
                    logger.info(
                        f"Recording successful search for {self.engine_type}: wait_time={self._last_wait_time}s, results={results_count}"
                    )
                    self.rate_tracker.record_outcome(
                        self.engine_type,
                        self._last_wait_time,
                        success=True,
                        retry_count=1,  # First attempt succeeded
                        search_result_count=results_count,
                    )
                else:
                    logger.info(
                        f"Rate limiting disabled, not recording search for {self.engine_type}"
                    )

                return results

            except RateLimitError as exc:
                # Retry only primary provider work. An optional filter or
                # content request must not repeat a healthy search or disable
                # its provider because another service was rate limited.
                if not previews_received and self.rate_tracker.enabled:
                    raise
                if not previews_received:
                    self._record_search_failure(exc)
                # If rate limiting is disabled, treat as regular error
                success = False
                error_message = (
                    "Optional search processing was rate limited"
                    if previews_received
                    else "Rate limit hit but rate limiting disabled"
                )
                error_type = type(exc).__name__
                logger.warning("{}: {}", type(self).__name__, error_message)
                results_count = 0
                return []
            except Exception as e:
                self._propagate_control_error(e)
                if not previews_received:
                    # Past the previews, the provider already answered: a
                    # filter or content error is not an engine outage.
                    self._record_search_failure(e)
                # Other errors - don't retry
                success = False
                # Sanitize before it flows to SearchTracker.record_search
                # (and the database) and to the log. Use logger.warning with
                # the scrubbed text instead of logger.exception: the
                # cause chain frequently carries the request URL or auth
                # header from upstream HTTP clients (see #4131).
                error_message = safe_msg = self._scrub_error(e)
                error_type = type(e).__name__
                logger.warning(
                    f"Search engine {self.__class__.__name__} failed: {safe_msg}"
                )
                results_count = 0
                return []

        try:
            return run_search()  # type: ignore[no-any-return]
        except Exception as e:
            # Includes the last RateLimitError once retries are exhausted:
            # the decorator re-raises it (reraise=True), never RetryError.
            # Mark the outcome first: ``_record_search_failure`` re-raises
            # control errors (egress policy denial, unreadable setting) and
            # the metrics row written below must still record a failure.
            success = False
            error_message = safe_msg = self._scrub_error(e)
            error_type = type(e).__name__
            results_count = 0
            self._record_search_failure(e)
            logger.warning(
                f"Search engine {self.__class__.__name__} error: {safe_msg}"
            )
            return []
        finally:
            try:
                # Record search metrics BEFORE clearing context (record_search needs it)
                if should_record_metrics:
                    response_time_ms = int((time.time() - start_time) * 1000)
                    SearchTracker.record_search(
                        engine_name=engine_name,
                        query=query,
                        results_count=results_count,
                        response_time_ms=response_time_ms,
                        success=success,
                        error_message=error_message,
                        error_type=error_type,
                    )
            finally:
                # Clean up temporary search result storage. One dict pop:
                # a check-then-delete raises AttributeError when another
                # search on this instance removes the attribute in between.
                # Temp attributes are plain instance attributes (no slots
                # or properties).
                for attr in self._temp_attributes():
                    self.__dict__.pop(attr, None)
                # ALWAYS clean up search context, even if recording fails
                if context_was_set:
                    clear_search_context()

    def invoke(self, query: str) -> List[Dict[str, Any]]:
        """Compatibility method for LangChain tools"""
        return self.run(query)

    def _filter_for_relevance(
        self, previews: List[Dict[str, Any]], query: str
    ) -> List[Dict[str, Any]]:
        """
        Filter search results by relevance using the LLM.

        Delegates to the ``relevance_filter`` module, which prompts the
        LLM for a plain-text list of relevant indices and parses them
        with a regex (no structured output).

        Args:
            previews: List of preview dictionaries
            query: The original search query

        Returns:
            Filtered list of preview dictionaries
        """
        engine_name = self._engine_name or type(self).__name__

        if not self.llm or len(previews) <= 1:
            logger.debug(
                f"[{engine_name}] Skipping relevance filter "
                f"(llm={'yes' if self.llm else 'no'}, "
                f"previews={len(previews)})"
            )
            return previews

        from .relevance_filter import filter_previews_for_relevance

        return filter_previews_for_relevance(
            llm=self.llm,
            previews=previews,
            query=query,
            max_filtered_results=self.max_filtered_results,
            engine_name=engine_name,
            batch_size=self.relevance_filter_batch_size,
            max_parallel_batches=self.relevance_filter_max_parallel_batches,
        )

    # =========================================================================
    # Shared Helper Methods for Subclasses
    # =========================================================================

    def _resolve_openalex_enrichment_key(self) -> Optional[str]:
        """Resolve the OpenAlex API key used by the DOI enrichment pass.

        The enrichment request always goes to ``api.openalex.org``, no
        matter which engine produced the previews, so every
        ``is_scientific`` engine (arxiv, semantic_scholar, zenodo,
        nasa_ads, pubchem, pubmed, …) needs the *OpenAlex* credential
        here — not its own. Only ``OpenAlexSearchEngine`` sets the
        ``openalex_api_key`` attribute, so the settings snapshot is the
        source for everyone else.

        Deliberately never reads ``self.api_key``: sending, say, a NASA
        ADS key to OpenAlex would hand one vendor's credential to another
        host. The isolation is pinned by
        ``test_openalex_enrichment_key_isolated_from_other_engine_keys``.
        """
        from ..utilities.openalex_enrichment import (
            normalize_openalex_api_key,
        )

        if self._openalex_key_rejected:
            # OpenAlex already refused this installation's key on this
            # engine. Clearing ``self.openalex_api_key`` alone would not
            # help: the snapshot fallback below is where the key came
            # from in the first place, so without this latch the
            # enrichment request of the very same ``run()`` re-sends it.
            return None

        key = normalize_openalex_api_key(
            getattr(self, "openalex_api_key", None)
        )
        if key:
            return key

        settings_snapshot = getattr(self, "settings_snapshot", None)
        if not settings_snapshot:
            return None

        # An explicit default is required: with no ``default`` argument
        # at all, a non-empty snapshot that lacks this key and no
        # thread-local settings context makes ``get_setting_from_snapshot``
        # raise ``NoSettingsContextError``. That would propagate out of
        # this resolver in the *argument* position of the enrichment call
        # and be swallowed by the caller's blanket handler, silently
        # skipping DOI enrichment for every scientific engine. The empty
        # string (rather than None) is the safer spelling because it is
        # falsy under every version of that function's "was a default
        # supplied?" test, and ``normalize_openalex_api_key`` maps it to
        # None, i.e. "enrich keylessly".
        return normalize_openalex_api_key(
            get_setting_from_snapshot(
                "search.engine.web.openalex.api_key",
                default="",
                settings_snapshot=settings_snapshot,
            )
        )

    @staticmethod
    def _is_valid_api_key(api_key: Optional[str]) -> bool:
        """
        Check if an API key is valid (not a placeholder value).

        Args:
            api_key: The API key to validate

        Returns:
            True if the key appears to be a real API key, False if it's a placeholder

        Example:
            >>> BaseSearchEngine._is_valid_api_key("sk-abc123")
            True
            >>> BaseSearchEngine._is_valid_api_key("YOUR_API_KEY_HERE")
            False
        """
        return not _is_api_key_placeholder(api_key)

    @staticmethod
    def _extract_display_link(url: str, fallback: str = "") -> str:
        """Extract the netloc (domain) from a URL for display purposes.

        Args:
            url: The URL to extract from
            fallback: Value to return on parse failure (default: empty string)

        Returns:
            The netloc portion of the URL, or *fallback* if parsing fails.
        """
        if not url:
            return fallback
        try:
            parsed = urlparse(url)
            return parsed.netloc or fallback
        except Exception:
            return fallback

    @staticmethod
    def _clean_result_url(value: Any) -> str:
        """Normalize a raw search-result URL for the validity gate.

        Coerces ``None`` (and any other falsy value) to ``""`` and any
        truthy value to ``str`` before stripping surrounding whitespace.

        The strip is **load-bearing**, not merely cosmetic: several engines
        gate the URL on a ``url.lower().startswith(("http://", "https://"))``
        prefix check (see ``_is_valid_search_result``) *before* it reaches
        the SSRF validator's own internal strip, so leading/trailing
        whitespace — common in HTML-scraped ``href`` attributes — would
        silently drop an otherwise-valid result. Cleaning once at extraction
        also keeps the URL tidy for logs and downstream consumers (preview
        ``id``/``link`` fields, etc.).

        Args:
            value: The raw URL value from a search result, e.g.
                ``result.get("url")`` or a parsed HTML ``href`` attribute.

        Returns:
            The whitespace-stripped URL string, or ``""`` if *value* is
            falsy (``None``, empty, etc.).
        """
        if not value:
            return ""
        return str(value).strip()

    def _resolve_api_key(
        self,
        api_key: Optional[str],
        setting_key: str,
        engine_name: str = "search engine",
        settings_snapshot: Optional[Dict[str, Any]] = None,
    ) -> str:
        """
        Resolve an API key from multiple sources with priority order.

        Environment variables are handled automatically by SettingsManager
        when building the settings snapshot, so they don't need to be
        checked separately here.

        Priority order:
        1. Direct parameter (api_key argument)
        2. Settings snapshot (via setting_key)

        Args:
            api_key: API key passed directly as parameter
            setting_key: Key to look up in settings snapshot (e.g., "search.brave_api_key")
            engine_name: Human-readable engine name for error messages
            settings_snapshot: Optional settings snapshot dict (uses self.settings_snapshot if not provided)

        Returns:
            The resolved API key string

        Raises:
            ValueError: If no valid API key is found from any source

        Example:
            >>> engine._resolve_api_key(
            ...     api_key=None,
            ...     setting_key="search.brave_api_key",
            ...     engine_name="Brave Search"
            ... )
            "sk-abc123..."
        """
        # Use instance settings snapshot if not provided
        if settings_snapshot is None:
            settings_snapshot = self.settings_snapshot

        # Priority 1: Direct parameter
        if self._is_valid_api_key(api_key) and api_key is not None:
            return api_key.strip()

        # Priority 2: Settings snapshot (includes env var overrides via SettingsManager)
        if settings_snapshot:
            settings_value = get_setting_from_snapshot(
                setting_key,
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if self._is_valid_api_key(settings_value):
                return settings_value.strip() if settings_value else ""

        # No valid API key found
        masked_key = self._mask_api_key(str(api_key)) if api_key else "None"
        raise ValueError(
            f"No valid API key found for {engine_name}. "
            f"Checked: direct parameter ({masked_key}), "
            f"settings key '{setting_key}'. "
            f"Please provide a valid API key."
        )

    @staticmethod
    def _propagate_control_error(error: BaseException) -> None:
        """Configuration and policy failures must not become provider outages."""
        from ..security.egress.policy import PolicyDeniedError
        from ..settings.exceptions import SettingsValueError

        if isinstance(error, (PolicyDeniedError, SettingsValueError)):
            raise error

    def _handle_search_error(
        self, error: Exception, *, retry_rate_limit: bool = True
    ) -> List[Dict[str, Any]]:
        """Normalize a primary provider failure at the shared search boundary.

        Adapters propagate failures, including SDK exceptions, instead of
        reporting availability themselves. Rate limits retain their response
        metadata and escape to the caller's retry loop. Custom execution paths
        without such a loop can request the usual empty public result instead.
        """
        self._propagate_control_error(error)
        if (
            not isinstance(error, SearchEngineError)
            and not is_query_error(error)
            and self._is_rate_limit_error(error)
        ):
            error = RateLimitError(
                "Rate limit reached",
                retry_after=retry_after_from_error(error),
            )
        self._record_search_failure(error)
        state = self._get_search_run_state()
        state.error_type = type(error).__name__
        state.error_message = self._scrub_error(error)
        if isinstance(error, RateLimitError) and retry_rate_limit:
            raise error from None
        self._search_failed = True
        logger.warning(
            "Search engine {} preview request failed ({}): {}",
            type(self).__name__,
            type(error).__name__,
            self.last_search_failure.reason
            if self.last_search_failure is not None
            else "Search query was rejected",
        )
        return []

    def _search_previews(self, query: str) -> List[Dict[str, Any]]:
        """Run an adapter's primary work through the common failure boundary.

        Only provider discovery belongs here. Relevance filtering and optional
        full-content enrichment run afterwards, so their failures cannot mark
        the search provider unavailable.
        """
        # Direct preview entry points also need an invocation-local outcome.
        state = self._get_search_run_state()
        state.failure = None
        state.search_failed = False
        state.error_type = None
        state.error_message = None
        try:
            previews = self._get_previews(query)
        except Exception as error:
            return self._handle_search_error(error)
        if not isinstance(previews, list):
            return self._handle_search_error(
                TypeError("Provider previews must be a list")
            )
        return previews

    def _search_attempts(self) -> SearchAttempts:
        """Create independent state for one provider's fallback sequence."""
        return SearchAttempts()

    def _record_search_failure(self, error: BaseException) -> None:
        """Preserve request failures even when an engine returns an empty list.

        The shared preview boundary calls this for propagated exceptions.
        Constructor probes also use it before a search can run. Primary
        adapters should propagate exceptions to the shared boundary. An empty
        list alone is a successful empty search. Never report optional
        enrichment/fetch errors.
        A later successful attempt or usable results supersede this signal.
        An error specific to this query (HTTP 400) is not recorded: the
        engine itself still works.
        """
        self._propagate_control_error(error)
        if is_query_error(error):
            return
        bare_403_is_bad_key = self.bare_403_means_bad_api_key and bool(
            getattr(self, "api_key", None)
        )
        self.last_search_failure = (
            failure_from_exception(
                error, bare_403_is_bad_key=bare_403_is_bad_key
            )
            or self.last_search_failure
            or SearchFailure(
                "Search request failed; a later attempt may succeed"
            )
        )

    def _is_rate_limit_error(
        self,
        error: Union[Exception, str, int, requests.Response],
        additional_patterns: Optional[Set[str]] = None,
    ) -> bool:
        """
        Detect if an error is a rate limit error.

        Checks multiple sources for rate limit indicators:
        - HTTP status code 429
        - HTTPError response objects
        - Error messages containing rate limit phrases

        Args:
            error: Exception, string, HTTP response, or status code to check.
            additional_patterns: Optional set of additional patterns to match

        Returns:
            True if the error appears to be a rate limit error

        Example:
            >>> engine._is_rate_limit_error(429)
            True
            >>> engine._is_rate_limit_error("Rate limit exceeded")
            True
            >>> engine._is_rate_limit_error(ValueError("Invalid input"))
            False
        """
        # Combine default and additional patterns
        patterns = self.rate_limit_patterns.copy()
        if additional_patterns:
            patterns.update(additional_patterns)

        # Check integer status code directly
        if isinstance(error, int):
            return error == 429

        if isinstance(error, RateLimitError):
            return True
        status_code = http_status(error)
        if status_code is not None:
            # Typed protocol information wins over incidental words in a
            # request URL or diagnostic message. Provider-specific quota
            # responses must be translated explicitly by their adapter.
            return status_code == 429
        if isinstance(
            error,
            (
                requests.ConnectionError,
                requests.Timeout,
                ConnectionError,
                TimeoutError,
            ),
        ):
            return False

        # Convert to string for pattern matching
        error_str = ""

        if isinstance(error, str):
            error_str = error
        elif isinstance(error, Exception):
            error_str = str(error)

        # Case-insensitive pattern matching
        # Some SDKs discard the response and only expose an error message.
        # Request URLs can include the user's query, so never infer provider
        # throttling from their contents. Typed statuses above take priority.
        error_lower = re.sub(
            r"https?://[^\s<>\"']+", "", error_str, flags=re.IGNORECASE
        ).lower()
        if isinstance(error, Exception):
            # Messages that merely discuss these terms (for example a query
            # diagnostic) are not a rate-limit signal. Match an error phrase
            # at the beginning when the SDK supplies no protocol metadata.
            error_lower = re.sub(
                r"^(?:error(?: \d{3})?|429)[: ]*", "", error_lower.strip()
            )
            return any(
                re.match(
                    r"(?:api |request |you hit the )?" + re.escape(pattern),
                    error_lower,
                )
                for pattern in patterns
            )
        for pattern in patterns:
            if pattern.lower() in error_lower:
                return True

        return False

    def _raise_if_rate_limit(
        self,
        error: Union[Exception, str, int, requests.Response],
        additional_patterns: Optional[Set[str]] = None,
    ) -> None:
        """
        Raise RateLimitError if the given error is a rate limit error.

        Convenience method that combines _is_rate_limit_error check with
        raising RateLimitError.

        Args:
            error: Error or HTTP response to check. Pass the response rather
                than its status code to retain Retry-After when translating it.
            additional_patterns: Optional set of additional patterns to match

        Raises:
            RateLimitError: If the error is detected as a rate limit error

        Example:
            >>> try:
            ...     response = make_api_call()
            ... except Exception as e:
            ...     engine._raise_if_rate_limit(e)
        """
        if self._is_rate_limit_error(error, additional_patterns):
            error_msg = str(error) if not isinstance(error, str) else error
            raise RateLimitError(
                f"Rate limit detected: {self._sanitize_error_message(error_msg)}",
                retry_after=retry_after_from_error(error),
            )

    def _extract_full_result(self, item: Dict[str, Any]) -> Dict[str, Any]:
        """
        Extract the full result from an item that may contain a _full_result key.

        This is a helper for the default _get_full_content implementation.
        It extracts data from the _full_result key if present, otherwise uses
        the item directly, and removes the internal _full_result key.

        Args:
            item: A search result item that may contain a _full_result key

        Returns:
            A dictionary with the full result data, without the _full_result key

        Example:
            >>> engine._extract_full_result({"title": "A", "_full_result": {"title": "A", "content": "Full"}})
            {"title": "A", "content": "Full"}
        """
        source = item.get("_full_result")
        if source is None:
            source = item
        return {k: v for k, v in source.items() if k != "_full_result"}

    def _get_full_content(
        self, relevant_items: List[Dict[str, Any]]
    ) -> List[Dict[str, Any]]:
        """
        Get full content for the relevant items.

        Default implementation extracts data from _full_result keys if present.
        Subclasses can override this method to fetch additional content from
        external sources (e.g., web scraping, API calls).

        Args:
            relevant_items: List of relevant preview dictionaries

        Returns:
            List of result dictionaries with full content

        Example:
            >>> engine._get_full_content([
            ...     {"title": "A", "_full_result": {"title": "A", "content": "Full A"}},
            ...     {"title": "B"}
            ... ])
            [{"title": "A", "content": "Full A"}, {"title": "B"}]
        """
        if not relevant_items:
            return []
        return [self._extract_full_result(item) for item in relevant_items]

    def _build_full_search_egress_context(self):
        """Build an EgressContext from self.settings_snapshot for the
        per-URL scope gate inside FullSearchResults. Returns None when a
        context can't be built and disables full-content fetching when
        policy evaluation fails on a supplied snapshot.

        The previous "fail closed by returning None" comment was wrong:
        full_search.py treats ``egress_context is None`` as
        ``evaluate_url_fn = None`` and skips the per-URL scope check
        entirely, leaving only SSRF — which allows public hosts under
        PRIVATE_ONLY. When the snapshot was supplied but the policy is
        unevaluable, we now turn full-content fetching off for this
        engine instead.
        """
        snapshot = getattr(self, "settings_snapshot", None)
        if not snapshot and not _is_strict_scope(None):
            return None
        if not isinstance(snapshot, dict):
            logger.bind(policy_audit=True).warning(
                "Disabling include_full_content: settings snapshot "
                "is missing or malformed"
            )
            self.include_full_content = False
            return None
        try:
            from ..security.egress.policy import (
                PolicyDeniedError,
                context_from_snapshot,
                resolve_run_primary_engine,
            )
        except ImportError:
            logger.debug(
                "egress_policy unavailable in search_engine_base; "
                "FullSearchResults fetches will be SSRF-only"
            )
            return None

        from ..search_system import username_from_snapshot

        try:
            # Thread username so a per-user private retriever primary
            # resolves PRIVATE_ONLY here too; otherwise the per-URL fetch
            # gate would fall back to BOTH and allow public hosts under a
            # run whose posture is private.
            primary_default = (
                None if _is_strict_scope(snapshot) else self._engine_name
            )
            return context_from_snapshot(
                snapshot,
                resolve_run_primary_engine(snapshot, default=primary_default),
                username=username_from_snapshot(snapshot),
            )
        except (PolicyDeniedError, ValueError) as exc:
            safe_msg = self._scrub_error(exc)
            logger.bind(policy_audit=True).warning(
                "Disabling include_full_content: egress policy could "
                "not be evaluated for this engine",
                reason=safe_msg,
            )
            self.include_full_content = False
            return None

    def set_fetch_progress_callback(self, hook) -> None:
        """Attach a ``(done, total)`` bulk-fetch liveness hook.

        Stores the hook for future ``_init_full_search`` calls and, when
        this engine already holds a ``FullSearchResults`` wrapper (legacy
        ``include_full_content`` seam), forwards it immediately. Used by
        the factory stamp and by ``AdvancedSearchSystem`` late-binding so
        pipeline-strategy full-search progress works regardless of whether
        the engine was created before the UI callback existed.
        """
        self._fetch_progress_callback = hook
        full_search = getattr(self, "full_search", None)
        if full_search is not None and hasattr(
            full_search, "progress_callback"
        ):
            full_search.progress_callback = hook

    def _init_full_search(
        self,
        web_search=None,
        language="en",
        max_results=10,
        region=None,
        time_period=None,
        safe_search=None,
        progress_callback=None,
    ):
        """Initialize FullSearchResults if include_full_content is True.

        Call this at the end of your __init__ after setting up your search wrapper.

        Args:
            web_search: The search wrapper/engine to pass to FullSearchResults
            language: Language for search results
            max_results: Maximum number of results
            region: Region/country code for results
            time_period: Time period filter
            safe_search: Safe search setting (string value for FullSearchResults)
            progress_callback: Optional ``(done, total)`` liveness hook for
                the bulk page fetch. Defaults to the hook stored on
                ``self`` (construction-time or ``set_fetch_progress_callback``).
        """
        if self.include_full_content and self.llm:
            try:
                from .engines.full_search import FullSearchResults

                # Egress context for per-URL scope gating. Built once
                # here (not per fetch) so the policy snapshot is the
                # same one used by the factory PEP that authorized this
                # engine.
                egress_context = self._build_full_search_egress_context()
                self.full_search = FullSearchResults(
                    llm=self.llm,
                    web_search=web_search,
                    language=language,
                    max_results=max_results,
                    region=region,
                    time=time_period,
                    safesearch=safe_search,
                    settings_snapshot=self.settings_snapshot,
                    egress_context=egress_context,
                    # Same hand-over as the factory's
                    # ``_create_full_search_wrapper``: the engine's own
                    # explicit boolean grant, nothing else, so an engine
                    # built through this seam does not lose it silently.
                    allow_private_ips=self.allow_private_result_fetch is True,
                    content_cache=self.get_content_cache(),
                    progress_callback=(
                        progress_callback
                        if progress_callback is not None
                        else self._fetch_progress_callback
                    ),
                )
            except ImportError:
                logger.warning(
                    "FullSearchResults not available. "
                    "Full content retrieval disabled."
                )
                self.include_full_content = False

    def _temp_attributes(self):
        """Return list of temporary attribute names to clean up after run().

        Override in subclasses that store additional temporary data.
        """
        return ["_search_results"]

    def _sanitize_error_message(self, message: str) -> str:
        """
        Remove/mask API keys, tokens, and secrets from error messages.

        Uses pattern matching for common credential formats.

        Args:
            message: The error message to sanitize

        Returns:
            Sanitized message with sensitive data redacted

        Example:
            >>> engine._sanitize_error_message(
            ...     "Error with key sk-abc123def456ghi789jkl012"
            ... )
            "Error with key [REDACTED_KEY]"
        """
        return sanitize_error_message(message)

    def _scrub_error(self, error: Union[BaseException, str]) -> str:
        """Return a log/DB-safe rendering of *error*.

        Delegates the "triple-scrub" to :func:`~..security.log_sanitizer.scrub_error`,
        resolving this engine's known literal secret values from
        ``_secret_attrs``. *error* may be an exception or a pre-built
        message string.

        Use this at every catch site that logs or persists an exception so
        the scrub passes can never drift apart per-engine.
        """
        return scrub_error(
            error, *(getattr(self, name, None) for name in self._secret_attrs)
        )

    def _mask_api_key(self, api_key: str, visible_chars: int = 4) -> str:
        """
        Mask an API key for safe logging, showing only first and last characters.

        Args:
            api_key: The API key to mask
            visible_chars: Number of characters to show at start and end

        Returns:
            Masked API key in format "sk-1...nop" or "***" for short keys

        Example:
            >>> engine._mask_api_key("sk-abcdefghijklmnop123456")
            "sk-a...3456"
            >>> engine._mask_api_key("short")
            "***"
        """
        if not api_key:
            return "***"

        api_key = str(api_key).strip()

        if len(api_key) <= visible_chars * 2:
            return "***"

        return f"{api_key[:visible_chars]}...{api_key[-visible_chars:]}"

    @abstractmethod
    def _get_previews(self, query: str) -> List[Dict[str, Any]]:
        """
        Get preview information (titles, summaries) for initial search results.

        Args:
            query: The search query

        Returns:
            List of preview dictionaries with at least 'id', 'title', and 'snippet' keys

        Raise provider/transport/format errors for the shared ``_search_previews``
        boundary. Return [] only for a successful search without matches. When
        trying optional fallbacks, preserve usable results; if no attempt
        succeeds, propagate the final error rather than returning [].
        """
        pass

    def close(self) -> None:
        """
        Close any resources held by this search engine.

        Subclasses with HTTP sessions or other resources should override this.
        The base implementation safely closes any 'session' attribute if present
        and closes both preview and content filters that hold resources.
        """
        from ..utilities.resource_utils import safe_close

        if hasattr(self, "session") and self.session is not None:
            safe_close(self.session, "HTTP session")
        # Close preview filters as well as content filters — the journal
        # reputation filter is registered as a preview filter on academic
        # engines (arxiv, pubmed, openalex, nasa_ads, semantic_scholar)
        # and holds a SearXNG engine + LLM client that need releasing.
        if hasattr(self, "_preview_filters"):
            for preview_filter in self._preview_filters:
                safe_close(preview_filter, "preview filter")
        if hasattr(self, "_content_filters"):
            for content_filter in self._content_filters:
                safe_close(content_filter, "content filter")

    def __enter__(self):
        """Support context manager usage."""
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Cleanup on context exit."""
        self.close()
        return False