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.

5.5 KB
Raw Blame
"""An explicit primary remains usable without enabling automatic discovery."""

from copy import deepcopy
from unittest.mock import MagicMock

import pytest

from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
    LangGraphAgentStrategy,
)
from local_deep_research.security import clear_active_context
from local_deep_research.security import module_whitelist
from local_deep_research.web_search_engines import (
    search_engine_factory,
    search_engines_config,
)
from local_deep_research.web_search_engines.search_engine_base import (
    BaseSearchEngine,
    Exposure,
    Sensitivity,
)


@pytest.mark.parametrize(
    "primary", ["openlibrary", "gutenberg", "github", "collection_hidden"]
)
def test_hidden_primary_executes_without_enabling_other_hidden_engines(
    monkeypatch, primary
):
    clear_active_context()
    snapshot = {
        "search.tool": primary,
        "policy.egress_scope": "public_only",
        "search.fetch.mode": "disabled",
        "search.engine.web.wayback.agent_enabled": False,
        f"search.engine.web.{primary}.agent_enabled": False,
        f"search.engine.web.{primary}.use_in_auto_search": False,
        "search.engine.web.github.api_key": "configured-test-credential",
    }
    configs = {}
    for name, enabled in ((primary, False), ("pubmed", False), ("arxiv", True)):
        configs[name] = {
            "module_path": ".engines.search_engine_openlibrary",
            "class_name": "OpenLibrarySearchEngine",
            "is_public": True,
            "is_local": False,
            "is_retriever": False,
            "agent_enabled": enabled,
            "use_in_auto_search": False,
            "requires_api_key": name == "github",
            "description": f"{name} source",
            "strengths": [],
        }
    original_snapshot, original_configs = deepcopy(snapshot), deepcopy(configs)
    queries = []

    class RecordingEngine(BaseSearchEngine):
        """Replace only the external engine; retain the real factory and tools."""

        is_public = True
        egress_sensitivity = Sensitivity.NON_SENSITIVE
        egress_exposure = Exposure.EXPOSING

        def __init__(
            self,
            settings_snapshot=None,
            api_key=None,
            programmatic_mode=False,
            **kwargs,
        ):
            super().__init__(
                settings_snapshot=settings_snapshot,
                programmatic_mode=programmatic_mode,
                **kwargs,
            )
            self.api_key = api_key

        def run(self, query):
            queries.append((self._engine_name, query))
            return [
                {
                    "title": "Primary result",
                    "link": "https://example.org/primary-result",
                    "snippet": "Evidence from the explicitly selected source.",
                }
            ]

        def close(self):
            pass

        def _get_previews(self, query):
            raise AssertionError("The recording engine handles run directly")

    monkeypatch.setattr(
        search_engines_config, "search_config", lambda **kwargs: configs
    )
    monkeypatch.setattr(
        search_engine_factory, "search_config", lambda **kwargs: configs
    )
    monkeypatch.setattr(
        search_engine_factory,
        "get_safe_module_class",
        lambda *args: RecordingEngine,
    )
    monkeypatch.setattr(
        module_whitelist, "get_safe_module_class", lambda *args: RecordingEngine
    )

    try:
        search = search_engine_factory.create_search_engine(
            primary, settings_snapshot=snapshot, programmatic_mode=True
        )
        assert search is not None
        if primary == "github":
            assert search.api_key == "configured-test-credential"
        strategy = LangGraphAgentStrategy(
            model=MagicMock(),
            search=search,
            citation_handler=object(),
            include_sub_research=False,
            settings_snapshot=snapshot,
            programmatic_mode=True,
        )
        tools = {
            tool.name: tool for tool in strategy._build_tools("A question")
        }
        assert set(tools) == {"web_search", "search_arxiv"}

        result = tools["web_search"].invoke({"query": "A question"})

        assert queries == [(primary, "A question")]
        assert "Primary result" in result
        assert len(strategy.collector.results) == 1
        assert snapshot == original_snapshot
        assert configs == original_configs

        # A later run with another primary must not inherit this selection
        # as an additional tool or enable either excluded engine.
        next_snapshot = {**snapshot, "search.tool": "arxiv"}
        next_strategy = LangGraphAgentStrategy(
            model=MagicMock(),
            search=search_engine_factory.create_search_engine(
                "arxiv", settings_snapshot=next_snapshot, programmatic_mode=True
            ),
            citation_handler=object(),
            include_sub_research=False,
            settings_snapshot=next_snapshot,
            programmatic_mode=True,
        )
        next_tools = {
            tool.name: tool
            for tool in next_strategy._build_tools("Another question")
        }
        assert set(next_tools) == {"web_search"}
        next_tools["web_search"].invoke({"query": "Another question"})
        assert queries == [
            (primary, "A question"),
            ("arxiv", "Another question"),
        ]
        assert snapshot == original_snapshot
        assert configs == original_configs
    finally:
        clear_active_context()