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.

14.5 KB
Raw Blame
from contextlib import ExitStack
from unittest.mock import MagicMock, patch
import pytest

from local_deep_research.advanced_search_system.strategies.source_based_strategy import (
    SourceBasedSearchStrategy,
)
from local_deep_research.api.research_functions import _init_search_system
from local_deep_research.search_system import AdvancedSearchSystem
from local_deep_research.search_system_factory import create_strategy
from tests.web.services.helpers import (
    MODULE,
    _base_run_patches,
    _get_raw_run_research_process,
)


@pytest.fixture(autouse=True)
def _clear_egress_context():
    from local_deep_research.security.egress.audit_hook import (
        clear_active_context,
    )

    clear_active_context()
    yield
    clear_active_context()


class TestResearchServiceRuntimeOverrides:
    def test_run_research_process_overlays_runtime_overrides_onto_snapshot(
        self,
    ):
        """Worker thread must overlay runtime kwargs onto settings_snapshot."""
        captured_snapshot = {}
        captured_factory_search_tool = None

        raw_run = _get_raw_run_research_process()
        patches = _base_run_patches()

        initial_snapshot = {
            "search.tool": {"value": "initial_engine", "ui_element": "select"},
            "search.iterations": {"value": 1, "ui_element": "number"},
            "search.questions_per_iteration": {
                "value": 1,
                "ui_element": "number",
            },
            "search.search_strategy": {
                "value": "initial_strategy",
                "ui_element": "select",
            },
            "llm.provider": {"value": "openai", "ui_element": "select"},
            "llm.model": {"value": "gpt-3.5", "ui_element": "text"},
        }

        def mock_factory_get_search(search_tool, *args, **kwargs):
            nonlocal captured_factory_search_tool
            captured_factory_search_tool = search_tool
            return MagicMock()

        def mock_system_cls(*args, **kwargs):
            nonlocal captured_snapshot
            captured_snapshot = kwargs.get("settings_snapshot", {})
            system = MagicMock()
            system.analyze_topic.return_value = {
                "findings": "test findings",
                "formatted_findings": "formatted findings",
            }
            return system

        patches[f"{MODULE}.apply_environment_overrides_to_snapshot"] = (
            lambda s: dict(s)
        )
        patches[f"{MODULE}.get_llm"] = MagicMock(return_value=MagicMock())
        patches[f"{MODULE}.AdvancedSearchSystem"] = mock_system_cls

        with ExitStack() as stack:
            stack.enter_context(
                patch(
                    "local_deep_research.config.search_config.factory_get_search",
                    mock_factory_get_search,
                )
            )
            stack.enter_context(
                patch(
                    "local_deep_research.security.egress.policy.context_from_snapshot",
                    return_value=MagicMock(),
                )
            )
            stack.enter_context(
                patch(
                    "local_deep_research.security.egress.run_classification.audit_run_from_snapshot",
                    return_value=MagicMock(allowed=True),
                )
            )
            for target, mock_obj in patches.items():
                stack.enter_context(patch(target, mock_obj))

            raw_run(
                research_id="test_run_123",
                query="test query",
                mode="quick",
                search_engine="searxng",
                model_provider="ollama",
                model="llama3",
                custom_endpoint="http://localhost:11434/v1",
                iterations=5,
                questions_per_iteration=4,
                strategy="source-based",
                settings_snapshot=initial_snapshot,
                username="testuser",
            )

        # Verify search engine factory received overridden search tool
        assert captured_factory_search_tool == "searxng"

        assert captured_snapshot["search.tool"]["value"] == "searxng"
        assert captured_snapshot["search.tool"]["ui_element"] == "select"
        assert captured_snapshot["search.iterations"]["value"] == 5
        assert captured_snapshot["search.questions_per_iteration"]["value"] == 4
        assert (
            captured_snapshot["search.search_strategy"]["value"]
            == "source-based"
        )
        assert captured_snapshot["llm.provider"]["value"] == "ollama"
        assert captured_snapshot["llm.model"]["value"] == "llama3"
        assert (
            captured_snapshot["llm.openai_endpoint.url"]
            == "http://localhost:11434/v1"
        )

    def test_run_research_process_does_not_widen_search_tool_under_strict(
        self, monkeypatch
    ):
        """Worker thread must NOT overwrite the saved primary when
        policy.egress_scope == strict — that would let the factory PEP
        silently widen the run to the requested engine even though the
        user locked scope to strict.
        """
        monkeypatch.delenv("LDR_POLICY_EGRESS_SCOPE", raising=False)

        raw_run = _get_raw_run_research_process()
        patches = _base_run_patches()

        initial_snapshot = {
            "policy.egress_scope": {
                "value": "strict",
                "ui_element": "select",
            },
            "search.tool": {"value": "library", "ui_element": "select"},
            "search.iterations": {"value": 1, "ui_element": "number"},
            "llm.provider": {"value": "openai", "ui_element": "select"},
            "llm.model": {"value": "gpt-3.5", "ui_element": "text"},
        }

        captured_snapshot = {}

        def mock_system_cls(*args, **kwargs):
            nonlocal captured_snapshot
            captured_snapshot = kwargs.get("settings_snapshot", {})
            system = MagicMock()
            system.analyze_topic.return_value = {
                "findings": "test findings",
                "formatted_findings": "formatted findings",
            }
            return system

        patches[f"{MODULE}.apply_environment_overrides_to_snapshot"] = (
            lambda s: dict(s)
        )
        patches[f"{MODULE}.get_llm"] = MagicMock(return_value=MagicMock())
        patches[f"{MODULE}.AdvancedSearchSystem"] = mock_system_cls

        with ExitStack() as stack:
            stack.enter_context(
                patch(
                    "local_deep_research.config.search_config.factory_get_search",
                    return_value=MagicMock(),
                )
            )
            stack.enter_context(
                patch(
                    "local_deep_research.security.egress.policy.context_from_snapshot",
                    return_value=MagicMock(),
                )
            )
            stack.enter_context(
                patch(
                    "local_deep_research.security.egress.run_classification.audit_run_from_snapshot",
                    return_value=MagicMock(allowed=True),
                )
            )
            for target, mock_obj in patches.items():
                stack.enter_context(patch(target, mock_obj))

            raw_run(
                research_id="strict_run",
                query="test query",
                mode="quick",
                search_engine="searxng",
                model_provider="ollama",
                model="llama3",
                iterations=5,
                settings_snapshot=initial_snapshot,
                username="testuser",
            )

        # STRICT: search.tool stays on the saved primary (library).
        assert captured_snapshot["search.tool"]["value"] == "library"
        # Other non-engine kwargs still overlay as documented.
        assert captured_snapshot["search.iterations"]["value"] == 5
        assert captured_snapshot["llm.provider"]["value"] == "ollama"


class TestSourceBasedStrategyIterationPlumbing:
    def test_source_based_strategy_respects_max_iterations_and_questions(self):
        """SourceBasedSearchStrategy must use explicit max_iterations and questions_per_iteration."""
        mock_model = MagicMock()
        mock_search = MagicMock()
        mock_search.run.return_value = []
        mock_search.include_full_content = True

        strategy = SourceBasedSearchStrategy(
            model=mock_model,
            search=mock_search,
            max_iterations=4,
            questions_per_iteration=6,
            settings_snapshot={
                "search.iterations": {"value": 1},
                "search.questions_per_iteration": {"value": 2},
            },
        )

        assert strategy.max_iterations == 4
        assert strategy.questions_per_iteration == 6

        # When analyzing topic, it should execute 4 iterations with questions_per_iteration=6
        with patch.object(
            strategy.question_generator,
            "generate_questions",
            return_value=["q1", "q2"],
        ) as mock_gen:
            strategy.analyze_topic("test topic")
            assert mock_gen.call_count == 4
            for call in mock_gen.call_args_list:
                assert call.kwargs["questions_per_iteration"] == 6

    def test_source_based_strategy_falls_back_to_settings(self):
        """When max_iterations is None, SourceBasedSearchStrategy falls back to settings."""
        mock_model = MagicMock()
        mock_search = MagicMock()
        mock_search.run.return_value = []
        mock_search.include_full_content = True

        strategy = SourceBasedSearchStrategy(
            model=mock_model,
            search=mock_search,
            settings_snapshot={
                "search.iterations": {"value": 2},
                "search.questions_per_iteration": {"value": 3},
            },
        )

        assert strategy.max_iterations is None
        assert strategy.questions_per_iteration is None

        with patch.object(
            strategy.question_generator,
            "generate_questions",
            return_value=["q1", "q2"],
        ) as mock_gen:
            strategy.analyze_topic("test topic")
            assert mock_gen.call_count == 2
            for call in mock_gen.call_args_list:
                assert call.kwargs["questions_per_iteration"] == 3

    def test_create_strategy_passes_iterations_to_source_based(self):
        """Factory must forward max_iterations and questions_per_iteration."""
        mock_model = MagicMock()
        mock_search = MagicMock()

        strategy = create_strategy(
            strategy_name="source-based",
            model=mock_model,
            search=mock_search,
            max_iterations=7,
            questions_per_iteration=8,
        )

        assert isinstance(strategy, SourceBasedSearchStrategy)
        assert strategy.max_iterations == 7
        assert strategy.questions_per_iteration == 8


class TestSearchSystemPlumbing:
    def test_search_system_followup_passes_iterations_to_delegate(self):
        """EnhancedContextualFollowUpStrategy delegate must receive max_iterations."""
        mock_model = MagicMock()
        mock_search = MagicMock()

        system = AdvancedSearchSystem(
            llm=mock_model,
            search=mock_search,
            strategy_name="enhanced-contextual-followup",
            max_iterations=3,
            questions_per_iteration=5,
            research_context={"delegate_strategy": "source-based"},
        )

        delegate = system.strategy.delegate_strategy
        assert isinstance(delegate, SourceBasedSearchStrategy)
        assert delegate.max_iterations == 3
        assert delegate.questions_per_iteration == 5

    def test_init_search_system_programmatic_plumbing(self):
        """_init_search_system overlays snapshot and constructs AdvancedSearchSystem with iterations."""
        mock_llm = MagicMock()
        mock_search = MagicMock()
        snapshot = {}

        with (
            patch(
                "local_deep_research.api.research_functions.get_llm",
                return_value=mock_llm,
            ),
            patch(
                "local_deep_research.api.research_functions.get_search",
                return_value=mock_search,
            ),
            patch(
                "local_deep_research.api.research_functions.AdvancedSearchSystem"
            ) as mock_sys_cls,
        ):
            _init_search_system(
                search_tool="searxng",
                iterations=4,
                questions_per_iteration=6,
                search_strategy="source-based",
                settings_snapshot=snapshot,
            )

            mock_sys_cls.assert_called_once()
            _, kwargs = mock_sys_cls.call_args
            overlaid = kwargs["settings_snapshot"]
            assert overlaid["search.tool"] == "searxng"
            assert overlaid["search.iterations"] == 4
            assert overlaid["search.questions_per_iteration"] == 6
            assert overlaid["search.search_strategy"] == "source-based"
            assert snapshot == {}
            assert kwargs["max_iterations"] == 4
            assert kwargs["questions_per_iteration"] == 6
            assert kwargs["strategy_name"] == "source-based"

    def test_init_search_system_does_not_clobber_snapshot_defaults(self):
        snapshot = {
            "search.tool": {"value": "wikipedia"},
            "search.iterations": {"value": 5},
            "search.questions_per_iteration": {"value": 4},
            "search.search_strategy": {"value": "langgraph-agent"},
        }

        with (
            patch(
                "local_deep_research.api.research_functions.get_llm",
                return_value=MagicMock(),
            ),
            patch(
                "local_deep_research.api.research_functions.get_search",
                return_value=MagicMock(),
            ),
            patch(
                "local_deep_research.api.research_functions.AdvancedSearchSystem"
            ) as mock_sys_cls,
        ):
            _init_search_system(settings_snapshot=snapshot)

            _, kwargs = mock_sys_cls.call_args
            overlaid = kwargs["settings_snapshot"]
            assert overlaid["search.tool"]["value"] == "wikipedia"
            assert overlaid["search.iterations"]["value"] == 5
            assert overlaid["search.questions_per_iteration"]["value"] == 4
            assert overlaid["search.search_strategy"]["value"] == (
                "langgraph-agent"
            )
            assert kwargs["strategy_name"] == "langgraph-agent"
            assert kwargs["max_iterations"] is None
            assert kwargs["questions_per_iteration"] is None
            assert snapshot["search.search_strategy"]["value"] == (
                "langgraph-agent"
            )