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.

12.1 KB
Raw Blame
"""LangChain adapter for the standalone search engine availability tracker."""

import asyncio
import time

from langchain.agents.middleware import AgentMiddleware
from langchain_core.messages import SystemMessage

from ...exceptions import ResearchTerminatedException
from ...security.secure_logging import logger
from ...utilities.resource_utils import safe_close
from ...web_search_engines.search_engine_base import (
    BaseSearchEngine,
    metrics_engine_name,
)
from ...web_search_engines.engine_availability import (
    SearchEngineAvailability,
    SearchFailure,
    available_tool_names,
    engine_failure,
    engine_for_tool,
    failure_from_exception,
    http_status,
    is_query_error,
    search_engines_for_tools,
)


class NoResearchToolsAvailable(RuntimeError):
    """Availability filtering removed every research tool from a request.

    Raised instead of calling the model with an empty tool list: several
    providers reject a request that carries tool-call history but defines
    no tools. This covers only an empty list: if a fetch tool survives
    while every search engine is disabled, the model keeps it, since it can
    still read URLs from the query or from earlier results. It can then also
    answer without searching; the prompt tells it to say when evidence is
    insufficient. The message lists only safe disable explanations.

    A cooldown is not a reason to stop: when every search engine is only
    cooling down, the middleware waits for the soonest one instead (see
    ``wait_for_cooldown``). Only disables that last the rest of the run, or
    a wait that hits its bound, end up here.
    """


def wait_for_cooldown(
    availability: SearchEngineAvailability,
    engines: set[str],
    *,
    sleep=None,
    check=None,
) -> None:
    """Block until one of ``engines`` leaves its cooldown, within the run budget.

    Returns at once when an engine is already available or when every
    remaining disable lasts the rest of the run. Once the soonest retry lies
    beyond the run's remaining cooldown-wait budget, it disables the cooling
    ``engines`` for the rest of the run (``end_cooldowns``) and returns
    instead of waiting. A concurrent search can start another cooldown
    during this wait; it must not leave the model with no search tool while
    the run can still wait for a retry. Parallel waiters spend the shared
    budget once. ``check`` (by default the tracker's ``check_cancelled``)
    runs before each sleep of at most one second, so cancellation stays
    responsive; a cancel already recorded on the tracker
    (``note_cancelled``) ends the wait as well.
    """
    sleep = sleep or time.sleep
    check = check or availability.check_cancelled
    with availability.waiting():
        while engines and engines <= availability.disabled().keys():
            if availability.cancelled():
                raise ResearchTerminatedException(
                    "Research was terminated by user during a cooldown wait"
                )
            if check is not None:
                try:
                    check()
                except ResearchTerminatedException:
                    # The run's termination flag is single-use; record the
                    # cancel so parallel waiters and subagents stop too.
                    availability.note_cancelled()
                    raise
            remaining = availability.seconds_until_retry(engines)
            if remaining is None:
                return
            if remaining > availability.wait_budget_remaining():
                availability.end_cooldowns(engines)
                return
            sleep(min(1.0, max(remaining, 0.01)))


def note_metrics_name(
    availability: SearchEngineAvailability, engine_name: str, engine
) -> None:
    """Tell ``availability`` which name metrics record for ``engine``."""
    # Full-content wrappers delegate the search, and its metrics, inward.
    inner = getattr(engine, "web_search", None)
    availability.note_metrics_name(
        engine_name,
        metrics_engine_name(
            inner if isinstance(inner, BaseSearchEngine) else engine
        ),
    )


def _record_skipped_search(
    engine_name: str,
    query: str,
    availability: SearchEngineAvailability,
    explanation: str,
) -> None:
    """Keep outages visible in search metrics although nothing was sent."""
    try:
        from ...metrics.search_tracker import (
            SEARCH_STATUS_SKIPPED,
            SearchTracker,
        )

        SearchTracker.record_search(
            engine_name=availability.metrics_name(engine_name),
            query=query,
            success=False,
            error_message=explanation,
            status=SEARCH_STATUS_SKIPPED,
        )
    except Exception as exc:
        logger.warning(
            f"Could not record skipped {engine_name} search "
            f"({type(exc).__name__})"
        )


def run_search(
    engine_name: str,
    query: str,
    factory,
    availability: SearchEngineAvailability,
    *,
    programmatic_mode: bool = False,
):
    """Return results or a disable explanation, with cleanup on every path.

    A search skipped because the engine is disabled is recorded in search
    metrics (unless ``programmatic_mode``, like the engine's own searches).
    """
    disabled = availability.state(engine_name)
    if disabled is not None:
        explanation = disabled.describe()
        logger.debug(
            "Skipping unavailable search engine {}: {}",
            engine_name,
            explanation,
        )
        if not programmatic_mode:
            _record_skipped_search(
                engine_name, query, availability, explanation
            )
        return explanation
    engine = None
    try:
        engine = factory()
        if engine is not None:
            note_metrics_name(availability, engine_name, engine)
        if engine is None:
            failure = SearchFailure(
                "Could not initialize search engine; check its configuration"
            )
        else:
            failure = engine_failure(engine)
            if failure is None:
                results = engine.run(query)
                # Usable results win over a failed optional enrichment,
                # fallback attempt or full-content request.
                if isinstance(results, list) and results:
                    availability.record_success(engine_name)
                    return results
                failure = engine_failure(engine)
                if failure is None:
                    availability.record_success(engine_name)
                    return []
        return availability.disable(engine_name, failure).describe()
    except Exception as exc:
        # Type name only: provider exceptions can embed request URLs
        # carrying API keys, and loguru would render them.
        logger.debug(f"{engine_name} search failed ({type(exc).__name__})")
        if is_query_error(exc):
            # This query was rejected; the engine still works for others.
            return (
                f"{engine_name} rejected this query "
                f"(HTTP {http_status(exc)}); rephrase it and try again"
            )
        failure = failure_from_exception(exc) or SearchFailure(
            "Search error; a later attempt may succeed"
        )
        return availability.disable(engine_name, failure).describe()
    finally:
        safe_close(engine, f"{engine_name} search engine", allow_none=True)
        try:
            from ...database.thread_local_session import (
                cleanup_current_thread,
                thread_session_manager,
            )

            if not thread_session_manager.in_scope():
                cleanup_current_thread()
        except Exception:
            logger.exception("Could not clean up search tool thread")


class SearchAvailabilityMiddleware(AgentMiddleware):
    """Remove unavailable tools from every model request, not just the prompt.

    Cooldowns are re-checked on every model call, so a tool comes back as
    soon as its cooldown ends. When every search engine behind the request
    is disabled and at least one of them only for a cooldown, the call
    first waits (bounded) for the soonest cooldown via ``wait``, which
    receives the set of engine names. The default is ``wait_for_cooldown``,
    which checks the tracker's ``check_cancelled`` every second and charges
    the run's cooldown-wait budget. Once the cancel is recorded on the
    tracker (``note_cancelled``), every further model call raises
    ``ResearchTerminatedException`` instead.
    """

    def __init__(self, availability, primary_engine, wait=None):
        self.availability = availability
        self.primary_engine = primary_engine
        self.wait = wait or (
            lambda engines: wait_for_cooldown(availability, engines)
        )

    def _filter_request(self, request):
        # The run's termination flag is gone once the run has cleaned up, so
        # a subagent back from a slow model call would otherwise keep going.
        if self.availability.cancelled():
            raise ResearchTerminatedException("Research was terminated by user")
        names = [t.name for t in request.tools]
        relevant = {
            engine_for_tool(name, self.primary_engine) for name in names
        } - {None}
        # Archive fetching tracks provider health but cannot replace URL
        # discovery. Preserve search recovery even when it remains available.
        disabled = self.availability.disabled()
        waiting_engines = search_engines_for_tools(names, self.primary_engine)
        if self.availability.seconds_until_retry(waiting_engines) is None:
            # Discovery may be permanently unavailable while a known URL's
            # archive can recover. Keep its bounded cooldown wait as well.
            waiting_engines = relevant
        if (
            waiting_engines
            and waiting_engines <= disabled.keys()
            and self.availability.seconds_until_retry(waiting_engines)
            is not None
        ):
            # Nothing can use these providers now, but something will again: a
            # transient failure must not end the run or leave the model
            # answering from memory. Wait instead of filtering.
            self.wait(waiting_engines)
            disabled = self.availability.disabled()
        available = available_tool_names(names, self.primary_engine, disabled)
        tools = [t for t in request.tools if t.name in available]
        explanations = [
            state.describe()
            for name, state in disabled.items()
            if name in relevant
        ]
        if not explanations:
            return request
        if not tools:
            raise NoResearchToolsAvailable(
                "No research tools remain available:\n"
                + "\n".join(explanations)
            )
        guidance = (
            "\nSearch engine availability for this step:\n"
            + "\n".join(explanations)
            + "\nUse only the tools currently supplied. If the primary search "
            "is disabled, use another available source. Do not call disabled "
            "tools or wait for cooldowns; continue with available tools and "
            "sources already gathered. If evidence is insufficient, say so."
        )
        if request.system_message is None:
            message = SystemMessage(content=guidance)
        else:
            # Preserve structured content and any provider-specific metadata.
            content = request.system_message.content
            content = (
                content + guidance
                if isinstance(content, str)
                else [*content, {"type": "text", "text": guidance}]
            )
            message = request.system_message.model_copy(
                update={"content": content}
            )
        return request.override(tools=tools, system_message=message)

    def wrap_model_call(self, request, handler):
        return handler(self._filter_request(request))

    async def awrap_model_call(self, request, handler):
        # Filtering may wait out a cooldown; keep that off the event loop.
        filtered = await asyncio.to_thread(self._filter_request, request)
        return await handler(filtered)