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.

23.8 KB
Raw Blame
"""Actual provider failures must remove tools before the next model request."""

from contextlib import ExitStack, contextmanager
from types import SimpleNamespace
from unittest.mock import Mock, patch

import pytest
import requests
from langchain.agents import create_agent
from langchain_core.messages import AIMessage
from langchain_core.tools import tool

from tests.strategies.test_search_availability import ScriptedModel
from local_deep_research.advanced_search_system.strategies import (
    langgraph_agent_strategy as strategy,
)
from local_deep_research.advanced_search_system.tools.search_availability import (
    SearchAvailabilityMiddleware,
    run_search,
)
from local_deep_research.web_search_engines import search_engine_factory
from local_deep_research.web_search_engines.engine_availability import (
    SearchEngineAvailability,
    SearchEngineInitializationError,
    SearchFailure,
    failure_from_exception,
)


FACTORY = "local_deep_research.web_search_engines.search_engine_factory.create_search_engine"
ENGINES = "local_deep_research.web_search_engines.engines.search_engine_"
PROVIDERS = {
    "brave": "BraveSearch.run",
    "tavily": "safe_post",
    "exa": "safe_post",
    "scaleserp": "safe_get",
    "serply": "safe_get",
    "sofya": "safe_post",
    "serpapi": "SerpAPIWrapper.results",
    "mojeek": "safe_get",
    "tinyfish": "safe_get",
    "nasa_ads": "safe_get",
    "github": "safe_get",
    "zenodo": "safe_get",
    "openlibrary": "safe_get",
    "gutenberg": "safe_get",
    "stackexchange": "safe_get",
    "pubchem": "safe_get",
    "guardian": "safe_get",
    "google_pse": "safe_get",
    "paperless": "safe_get",
    "elasticsearch": "Elasticsearch.search",
}


@pytest.fixture(autouse=True)
def offline_providers():
    tracker = Mock(enabled=False)
    tracker.apply_rate_limit.return_value = 0.0
    tracker.get_wait_time.return_value = 0.0
    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_base.get_tracker",
            return_value=tracker,
        ),
        patch(
            "requests.sessions.Session.send",
            side_effect=AssertionError("Unexpected external HTTP request"),
        ),
    ):
        yield


def snapshot(engine):
    settings = {
        "policy.egress_scope": (
            "private_only"
            if engine in {"paperless", "elasticsearch"}
            else "public_only"
        ),
        "search.tool": engine,
        "search.engine.library.enabled": False,
        "search.snippets_only": True,
        f"search.engine.web.{engine}.enabled": True,
        f"search.engine.web.{engine}.requires_api_key": True,
        f"search.engine.web.{engine}.api_key": "offline-contract-credential",
        f"search.engine.web.{engine}.default_params.enable_llm_relevance_filter": False,
    }
    if engine == "google_pse":
        settings.update(
            {
                "search.engine.web.google_pse.default_params.search_engine_id": "offline-search-id",
                "search.engine.web.google_pse.default_params.max_retries": 1,
                "search.engine.web.google_pse.default_params.retry_delay": 0,
            }
        )
    if engine == "paperless":
        settings["search.engine.web.paperless.default_params.api_url"] = (
            "http://127.0.0.1:8000"
        )
    if engine == "elasticsearch":
        settings["search.engine.web.elasticsearch.default_params.hosts"] = [
            "http://127.0.0.1:9200"
        ]
    return settings


def response(status, payload=b'{"results": []}'):
    result = requests.Response()
    result.status_code = status
    result._content = payload
    result.headers["Retry-After"] = "180"
    return result


@contextmanager
def provider_reply(engine, outcome):
    leaf = PROVIDERS[engine]
    reply = None
    error = None
    if outcome == "timeout":
        error = requests.Timeout("Simulated provider timeout")
    elif "." in leaf:
        if outcome >= 400:
            error = requests.HTTPError(
                f"HTTP {outcome}", response=response(outcome)
            )
        else:
            reply = [] if engine == "brave" else {"organic_results": []}
    else:
        reply = response(outcome)
    with ExitStack() as stack:
        if engine == "google_pse":
            # Validate the constructor successfully, then fail the real query.
            # Retry timing itself is covered by the limiter's own tests.
            stack.enter_context(
                patch(f"{ENGINES}{engine}.respect_rate_limit", return_value=0)
            )
            error = [
                response(200, b"{}"),
                error if error is not None else reply,
            ]
            reply = None
        if engine == "elasticsearch":
            stack.enter_context(
                patch(f"{ENGINES}{engine}.Elasticsearch.info", return_value={})
            )
        request = stack.enter_context(
            patch(
                f"{ENGINES}{engine}.{leaf}",
                return_value=reply,
                side_effect=error,
            )
        )
        yield request


@tool
def search_control(query: str) -> str:
    """Search an independently available source."""
    return "Control result"


@pytest.mark.parametrize("engine", PROVIDERS)
@pytest.mark.parametrize("outcome", [401, 503, "timeout"])
def test_failed_provider_is_absent_from_model_and_stale_calls_are_skipped(
    engine,
    outcome,
):
    clock = SimpleNamespace(now=0.0)
    events = Mock()
    availability = SearchEngineAvailability(events, clock=lambda: clock.now)
    model = ScriptedModel(
        responses=[
            AIMessage(
                content="",
                tool_calls=[
                    {
                        "name": "web_search",
                        "args": {"query": "one"},
                        "id": "first",
                    }
                ],
            ),
            AIMessage(
                content="",
                tool_calls=[
                    {
                        "name": "search_control",
                        "args": {"query": "two"},
                        "id": "second",
                    }
                ],
            ),
            AIMessage(content="Findings"),
        ]
    )
    collector = strategy.SearchResultsCollector([])
    settings = snapshot(engine)
    primary = strategy._make_web_search_tool(
        engine,
        model,
        settings,
        collector,
        programmatic_mode=True,
        availability=availability,
    )
    alias = strategy._make_specialized_search_tool(
        engine,
        "Search provider",
        model,
        settings,
        collector,
        programmatic_mode=True,
        availability=availability,
    )
    real_factory = search_engine_factory.create_search_engine

    def instantiate(**kwargs):
        # Query optimization is optional; the scripted model drives only the
        # agent. The registry, constructors and provider request paths are real.
        kwargs["llm"] = None
        return real_factory(**kwargs)

    with (
        patch(FACTORY, side_effect=instantiate) as factory,
        provider_reply(engine, outcome) as request,
    ):
        agent = create_agent(
            model=model,
            tools=[primary, alias, search_control],
            middleware=[
                SearchAvailabilityMiddleware(
                    availability,
                    engine,
                    wait=lambda _: pytest.fail(
                        "A healthy search source is available"
                    ),
                )
            ],
        )
        result = agent.invoke(
            {"messages": [{"role": "user", "content": "Research"}]}
        )
        sent = request.call_count
        stale = alias.invoke({"query": "obsolete tool call"})
        assert request.call_count == sent
    assert (
        sent > 0
    )  # Exercise the actual provider path, not a config-only failure.
    factory.assert_called_once()
    assert result["messages"][-1].content == "Findings"
    assert model.seen_tools == [
        ["web_search", f"search_{engine}", "search_control"],
        ["search_control"],
        ["search_control"],
    ]
    state = availability.state(engine)
    assert state is not None
    assert state.failure.cooldown_seconds == (
        None if outcome == 401 else 180 if outcome == 503 else 60
    )
    assert "disabled since" in stale
    assert "Try rephrasing" not in stale
    events.assert_called_once()
    assert events.call_args.args[2]["status"] == "disabled"


@pytest.mark.parametrize("engine", ["brave", "tavily", "exa"])
def test_successful_empty_provider_search_is_not_disabled(engine):
    availability = SearchEngineAvailability()
    with provider_reply(engine, 200) as request:
        assert (
            run_search(
                engine,
                "query",
                lambda: search_engine_factory.create_search_engine(
                    engine,
                    settings_snapshot=snapshot(engine),
                    programmatic_mode=True,
                    use_full_search=False,
                ),
                availability,
                programmatic_mode=True,
            )
            == []
        )
    request.assert_called_once()
    assert availability.state(engine) is None


@pytest.mark.parametrize("status,delay", [(401, None), (503, 180)])
def test_factory_preserves_constructor_failure_for_agent(status, delay):
    error = requests.HTTPError(
        "Simulated constructor error", response=response(status)
    )
    with patch(
        f"{ENGINES}tavily.TavilySearchEngine.__init__", side_effect=error
    ):
        with pytest.raises(RuntimeError) as caught:
            search_engine_factory.create_search_engine(
                "tavily",
                settings_snapshot=snapshot("tavily"),
                programmatic_mode=True,
                raise_on_error=True,
            )
        assert (
            search_engine_factory.create_search_engine(
                "tavily",
                settings_snapshot=snapshot("tavily"),
                programmatic_mode=True,
            )
            is None
        )
    failure = failure_from_exception(caught.value)
    assert failure is not None
    assert failure.cooldown_seconds == delay
    assert "Simulated constructor error" not in failure.reason


def test_cooling_engine_is_not_probed_or_offered_and_returns_after_expiry():
    clock = SimpleNamespace(now=0.0)
    availability = SearchEngineAvailability(clock=lambda: clock.now)
    availability.disable(
        "tavily", SearchFailure("Search request timed out", 60)
    )
    model = ScriptedModel(
        responses=[AIMessage(content="First"), AIMessage(content="Retry")]
    )
    configs = {
        "tavily": {"agent_enabled": True},
        "brave": {"agent_enabled": True},
    }
    with (
        patch(
            "local_deep_research.web_search_engines.search_engines_config.search_config",
            return_value=configs,
        ),
        patch(
            "local_deep_research.web_search_engines.search_engines_config._engine_class_is_available",
            return_value=True,
        ) as probe,
    ):
        tools = strategy._load_specialized_engine_tools(
            None,
            model,
            snapshot("tavily"),
            strategy.SearchResultsCollector([]),
            programmatic_mode=True,
            availability=availability,
        )
    assert [call.args[0] for call in probe.call_args_list] == ["brave"]
    # Retaining the configured candidate permits recovery without rebuilding
    # the agent, while the middleware supplies only the currently usable tool.
    assert {t.name for t in tools} == {"search_tavily", "search_brave"}
    agent = create_agent(
        model=model,
        tools=tools,
        middleware=[SearchAvailabilityMiddleware(availability, "brave")],
    )
    agent.invoke({"messages": [{"role": "user", "content": "Research"}]})
    clock.now = 60
    agent.invoke({"messages": [{"role": "user", "content": "Continue"}]})
    assert model.seen_tools == [
        ["search_brave"],
        ["search_tavily", "search_brave"],
    ]


def test_subagent_does_not_receive_engine_that_timed_out_in_lead_tool():
    availability = SearchEngineAvailability()
    model = ScriptedModel(responses=[AIMessage(content="Subtopic findings")])
    collector = strategy.SearchResultsCollector([])
    settings = snapshot("tavily")
    primary = strategy._make_web_search_tool(
        "tavily",
        model,
        settings,
        collector,
        programmatic_mode=True,
        availability=availability,
    )
    alias = strategy._make_specialized_search_tool(
        "tavily",
        "Search provider",
        model,
        settings,
        collector,
        programmatic_mode=True,
        availability=availability,
    )
    with (
        provider_reply("tavily", "timeout") as request,
        patch(
            FACTORY, wraps=search_engine_factory.create_search_engine
        ) as factory,
        patch.object(strategy, "build_fetch_tool", return_value=None),
        patch.object(strategy, "build_archive_fetch_tool", return_value=None),
        patch.object(
            strategy,
            "_load_specialized_engine_tools",
            return_value=[alias, search_control],
        ),
        patch(
            "langchain.agents.create_agent", wraps=create_agent
        ) as build_agent,
    ):
        assert "timed out" in primary.invoke({"query": "one"})
        delegation = strategy._make_research_subtopic_tool(
            "tavily",
            model,
            settings,
            collector,
            3,
            availability=availability,
            programmatic_mode=True,
            max_subagent_workers=1,
            fetch_mode="disabled",
        )
        assert "Subtopic findings" in delegation.invoke({"subtopics": ["one"]})
    request.assert_called_once()
    factory.assert_called_once()
    assert model.seen_tools == [["search_control"]]
    assert "search_tavily" not in build_agent.call_args.kwargs["system_prompt"]


def test_shared_logs_warn_once_and_keep_skipped_calls_at_debug():
    availability = SearchEngineAvailability()
    factory = Mock(side_effect=requests.Timeout("Private provider diagnostics"))
    with (
        patch(
            "local_deep_research.advanced_search_system.tools.search_availability.logger"
        ) as adapter_log,
        patch(
            "local_deep_research.web_search_engines.engine_availability.logger"
        ) as state_log,
    ):
        first = run_search(
            "tavily", "one", factory, availability, programmatic_mode=True
        )
        skipped = run_search(
            "tavily", "two", factory, availability, programmatic_mode=True
        )
    factory.assert_called_once()
    assert "timed out" in first and "timed out" in skipped
    state_log.warning.assert_called_once()
    adapter_log.warning.assert_not_called()
    assert any(
        "Skipping" in str(call) for call in adapter_log.debug.call_args_list
    )
    assert "Private provider diagnostics" not in str(
        state_log.mock_calls + adapter_log.mock_calls
    )


@pytest.mark.parametrize(
    "engine",
    ["brave", "tavily", "exa", "mojeek", "serpapi", "openlibrary", "pubchem"],
)
def test_bad_query_does_not_disable_engine(engine):
    availability = SearchEngineAvailability()
    with provider_reply(engine, 400):
        for query in ("one", "two"):
            assert (
                run_search(
                    engine,
                    query,
                    lambda: search_engine_factory.create_search_engine(
                        engine,
                        settings_snapshot=snapshot(engine),
                        programmatic_mode=True,
                        use_full_search=False,
                    ),
                    availability,
                    programmatic_mode=True,
                )
                == []
            )
    assert availability.state(engine) is None


def test_missing_api_key_is_a_run_long_initialization_failure():
    settings = snapshot("tavily")
    settings.pop("search.engine.web.tavily.api_key")
    availability = SearchEngineAvailability()
    result = run_search(
        "tavily",
        "query",
        lambda: search_engine_factory.create_search_engine(
            "tavily",
            settings_snapshot=settings,
            programmatic_mode=True,
            raise_on_error=True,
        ),
        availability,
        programmatic_mode=True,
    )
    assert "API key" in result
    assert availability.state("tavily").retry_at is None


def test_retriever_override_reports_failure_and_clears_it_on_empty_success():
    from local_deep_research.web_search_engines.engines.search_engine_retriever import (
        RetrieverSearchEngine,
    )

    retriever = Mock()
    retriever.invoke.side_effect = [requests.Timeout("Simulated timeout"), []]
    engine = RetrieverSearchEngine(
        retriever=retriever, settings_snapshot={}, programmatic_mode=True
    )
    try:
        assert engine.run("one") == []
        assert engine.last_search_failure.cooldown_seconds == 60
        assert engine.run("two") == []
        assert engine.last_search_failure is None
    finally:
        engine.close()


@pytest.mark.parametrize("status,delay", [(401, None), (503, 180)])
def test_sanitized_constructor_failure_keeps_classification_without_response(
    status, delay
):
    with (
        patch(f"{ENGINES}google_pse.safe_get", return_value=response(status)),
        patch(f"{ENGINES}google_pse.respect_rate_limit", return_value=0),
        pytest.raises(SearchEngineInitializationError) as caught,
    ):
        search_engine_factory.create_search_engine(
            "google_pse",
            settings_snapshot=snapshot("google_pse"),
            programmatic_mode=True,
            raise_on_error=True,
        )
    assert failure_from_exception(caught.value).cooldown_seconds == delay
    sanitized = caught.value.__cause__
    assert isinstance(sanitized, requests.RequestException)
    assert sanitized.response is None


@pytest.mark.parametrize(
    "strategy_name", ["langgraph-agent", "enhanced-contextual-followup"]
)
def test_startup_failure_is_not_offered_before_first_model_call(strategy_name):
    from local_deep_research.config.search_config import get_search
    from local_deep_research.search_system import AdvancedSearchSystem

    settings = {
        **snapshot("google_pse"),
        "search.fetch.mode": "disabled",
        "langgraph_agent.include_sub_research": False,
        "search.engine.web.brave.enabled": True,
        "search.engine.web.brave.requires_api_key": True,
        "search.engine.web.brave.api_key": "offline-contract-credential",
    }
    # Follow-up construction can summarize context before it builds tools.
    model = ScriptedModel(
        responses=[AIMessage(content="{}") for _ in range(10)]
    )
    with (
        patch(
            f"{ENGINES}google_pse.safe_get", return_value=response(503)
        ) as request,
        patch(f"{ENGINES}google_pse.respect_rate_limit", return_value=0),
        patch(
            FACTORY, wraps=search_engine_factory.create_search_engine
        ) as factory,
        patch(
            "local_deep_research.metrics.search_tracker.SearchTracker.record_search"
        ),
    ):
        with pytest.raises(SearchEngineInitializationError) as caught:
            get_search(
                "google_pse",
                llm_instance=model,
                settings_snapshot=settings,
                programmatic_mode=True,
                raise_on_error=True,
            )
        system = AdvancedSearchSystem(
            llm=model,
            search=None,
            strategy_name=strategy_name,
            settings_snapshot=settings,
            programmatic_mode=True,
            initial_search_failure=caught.value.failure,
            research_context={"delegate_strategy": "langgraph-agent"},
        )
        try:
            assert all(not names for names in model.seen_tools)
            model.seen_tools.clear()
            model.responses[:] = [AIMessage(content="Schema inspected")]
            research = (
                system.strategy.delegate_strategy
                if strategy_name == "enhanced-contextual-followup"
                else system.strategy
            )
            tools = research._build_tools()
            agent = create_agent(
                model=model,
                tools=tools,
                middleware=[
                    SearchAvailabilityMiddleware(
                        research.engine_availability, "google_pse"
                    )
                ],
            )
            agent.invoke(
                {"messages": [{"role": "user", "content": "Research"}]}
            )
            stale = next(t for t in tools if t.name == "search_google_pse")
            assert "disabled since" in stale.invoke({"query": "stale"})
        finally:
            system.close()
    factory.assert_called_once()
    request.assert_called_once()
    assert model.seen_tools == [["search_brave"]]


def test_provider_failure_preserves_diagnostics_in_search_metrics():
    from local_deep_research.web_search_engines.engines.search_engine_tavily import (
        TavilySearchEngine,
    )

    engine = TavilySearchEngine(
        api_key="offline-contract-credential", settings_snapshot={}
    )
    try:
        with (
            provider_reply("tavily", "timeout"),
            patch(
                "local_deep_research.metrics.search_tracker.SearchTracker.record_search"
            ) as record,
        ):
            assert engine.run("query") == []
        record.assert_called_once()
        assert record.call_args.kwargs["success"] is False
        assert record.call_args.kwargs["error_type"] == "Timeout"
        assert (
            record.call_args.kwargs["error_message"]
            == "Simulated provider timeout"
        )
        assert engine.last_search_failure.reason == "Search request timed out"
    finally:
        engine.close()


def test_paperless_expansion_failure_preserves_successful_primary_results():
    from local_deep_research.web_search_engines.engines.search_engine_paperless import (
        PaperlessSearchEngine,
    )

    engine = PaperlessSearchEngine(
        api_url="http://127.0.0.1:8000",
        api_key="offline-contract-credential",
        llm=Mock(),
        settings_snapshot={},
        programmatic_mode=True,
        include_content=False,
        search_snippets_only=True,
    )
    try:
        with (
            patch(
                f"{ENGINES}paperless.invoke_llm_sync",
                return_value=Mock(content="expanded keywords"),
            ),
            patch(
                f"{ENGINES}paperless.safe_get",
                side_effect=[
                    response(
                        200,
                        b'{"results": [{"id": 1, "title": "Useful document", "content": "Evidence"}]}',
                    ),
                    requests.Timeout("Expansion timed out"),
                ],
            ) as request,
        ):
            results = engine.run("original")
        assert request.call_count == 2
        assert len(results) == 1
        assert results[0]["title"] == "Useful document"
        assert engine.last_search_failure is None
    finally:
        engine.close()


def test_guardian_filter_failure_does_not_mark_provider_unavailable():
    from local_deep_research.web_search_engines.engines.search_engine_guardian import (
        GuardianSearchEngine,
    )

    engine = GuardianSearchEngine(
        api_key="offline-contract-credential", llm=Mock(), settings_snapshot={}
    )
    try:
        with (
            patch.object(
                engine,
                "_get_previews",
                return_value=[{"id": "one", "title": "Useful article"}],
            ),
            patch.object(
                engine,
                "_filter_for_relevance",
                side_effect=requests.Timeout("Filter timed out"),
            ),
        ):
            assert engine.run("query") == []
        assert engine.last_search_failure is None
    finally:
        engine.close()