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.

15.0 KB
Raw Blame
"""Subagents spawned by ``research_subtopic`` must receive the same
specialized-engine set as the lead agent, filtered by the same egress
policy / ``agent_enabled`` gate via the shared ``_load_specialized_engine_tools``
helper.

These tests pin three invariants that the original code (which only gave
subagents ``web_search`` + ``fetch_content``) did not satisfy:

* the subagent tool list includes the specialized engines, so e.g. a
  subagent researching a medical topic can call ``search_pubmed`` directly;
* the specialized-tools list is built ONCE per ``research_subtopic`` call
  rather than per subtopic, so ``list_eligible_engine_configs`` (which hits
  the per-user DB) runs once even when the lead agent fans out to
  ``MAX_SUBTOPICS``;
* the egress policy filter and ``agent_enabled`` gate apply to subagents
  too — not just the lead agent — so a STRICT-scoped run never exposes
  forbidden tool names to a subagent's LLM.
"""

from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest

import local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy as strat  # noqa: E501


def _stub_subagent_internals(
    *, specialized_engine_names=None, engine_configs=None
):
    """Stub the leaf pieces of subagent setup and return (call_log, tool).

    ``call_log["list_eligible_engine_configs"]`` counts how many times the
    engine discovery ran during one ``research_subtopic`` invocation.
    ``specialized_engine_names`` controls what the (stubbed) discovery
    returns so individual tests can pin the egress/agent_enabled filtering;
    pass ``engine_configs`` instead to control the full per-engine config
    dicts (e.g. to set ``agent_enabled``).
    """
    if engine_configs is None:
        engine_configs = {
            name: {"description": f"{name} desc", "strengths": []}
            for name in specialized_engine_names
        }
    call_log = {
        "list_eligible_engine_configs": 0,
        "create_agent_tool_lists": [],
        "system_prompts": [],
    }

    def fake_list_eligible_engine_configs(*_a, **_k):
        call_log["list_eligible_engine_configs"] += 1
        return engine_configs

    def fake_make_specialized_tool(name, *_a, **_k):
        return SimpleNamespace(name=f"search_{name}")

    def fake_make_web_search_tool(*_a, **_k):
        return SimpleNamespace(name="web_search")

    def fake_create_agent(*, model, tools, system_prompt, **kwargs):  # noqa: ARG001
        call_log["create_agent_tool_lists"].append(
            [getattr(t, "name", "?") for t in tools]
        )
        call_log["system_prompts"].append(system_prompt)
        agent = MagicMock()
        agent.invoke.return_value = {
            "messages": [SimpleNamespace(content="ok")]
        }
        return agent

    ctx = (
        patch.object(strat, "_make_web_search_tool", fake_make_web_search_tool),
        patch.object(
            strat, "_make_specialized_search_tool", fake_make_specialized_tool
        ),
        patch.object(strat, "build_fetch_tool", lambda *_a, **_k: None),
        patch(
            "local_deep_research.web_search_engines.search_engines_config.list_eligible_engine_configs",
            fake_list_eligible_engine_configs,
        ),
        patch("langchain.agents.create_agent", fake_create_agent),
    )
    for c in ctx:
        c.start()

    try:
        tool = strat._make_research_subtopic_tool(
            search_engine_name="duckduckgo",
            model=MagicMock(),
            settings_snapshot={"search.tool": "duckduckgo"},
            collector=MagicMock(),
            max_sub_iterations=1,
        )
    except BaseException:
        _stop(ctx)
        raise
    return call_log, tool, ctx


def _stop(ctx):
    for c in ctx:
        c.stop()


class TestSpecializedEngineFailureIsolation:
    @pytest.mark.parametrize(
        ("broken_config", "factory_fails"),
        [
            (None, False),
            ({"description": "broken", "strengths": []}, True),
        ],
        ids=["malformed_config", "tool_construction_failure"],
    )
    def test_later_engines_remain_available_when_prior_engine_fails(
        self, broken_config, factory_fails
    ):
        # Given: discovery returns a broken engine before two valid engines.
        engines = {
            "broken": broken_config,
            "valid_first": {"description": "first", "strengths": []},
            "valid_second": {"description": "second", "strengths": []},
        }

        def make_specialized_tool(name, *_args, **_kwargs):
            if factory_fails and name == "broken":
                raise RuntimeError("test tool construction failure")
            return SimpleNamespace(name=f"search_{name}")

        # When: the shared specialized-engine helper loads the ordered engines.
        with (
            patch(
                "local_deep_research.web_search_engines.search_engines_config"
                ".list_eligible_engine_configs",
                return_value=engines,
            ),
            patch.object(
                strat, "_make_specialized_search_tool", make_specialized_tool
            ),
        ):
            tools = strat._load_specialized_engine_tools(
                skip_engine=None,
                model=MagicMock(),
                settings_snapshot={},
                collector=MagicMock(),
            )

        # Then: the bad engine is omitted and later engines preserve order.
        assert [tool.name for tool in tools] == [
            "search_valid_first",
            "search_valid_second",
        ]

    def test_discovery_failure_returns_no_tools_without_construction(self):
        # Given: engine discovery itself fails before yielding any configuration.
        factory = MagicMock()

        # When: the shared specialized-engine helper attempts discovery.
        with (
            patch(
                "local_deep_research.web_search_engines.search_engines_config"
                ".list_eligible_engine_configs",
                side_effect=RuntimeError("test discovery failure"),
            ),
            patch.object(strat, "_make_specialized_search_tool", factory),
        ):
            tools = strat._load_specialized_engine_tools(
                skip_engine=None,
                model=MagicMock(),
                settings_snapshot={},
                collector=MagicMock(),
            )

        # Then: no partial tools exist and no factory was invoked.
        assert tools == []
        factory.assert_not_called()


class TestSubagentSpecializedEngines:
    def test_subagent_receives_specialized_engine_tools(self):
        call_log, tool, ctx = _stub_subagent_internals(
            specialized_engine_names=["arxiv", "pubmed"]
        )
        try:
            tool.invoke({"subtopics": ["t1", "t2"]})
        finally:
            _stop(ctx)

        # One create_agent call per subtopic, each with the full tool list.
        assert len(call_log["create_agent_tool_lists"]) == 2
        tool_names = call_log["create_agent_tool_lists"][0]
        assert "web_search" in tool_names
        assert "search_arxiv" in tool_names
        assert "search_pubmed" in tool_names
        # research_subtopic is never in the subagent's own tool list.
        assert "research_subtopic" not in tool_names

    def test_specialized_tools_built_once_per_call(self):
        # Five subtopics but list_eligible_engine_configs runs only ONCE.
        call_log, tool, ctx = _stub_subagent_internals(
            specialized_engine_names=["arxiv", "pubmed"]
        )
        try:
            tool.invoke({"subtopics": ["t1", "t2", "t3", "t4", "t5"]})
        finally:
            _stop(ctx)

        assert call_log["list_eligible_engine_configs"] == 1, (
            "specialized-engine discovery should run once per "
            "research_subtopic call, not once per subtopic"
        )

    def test_search_enabled_false_omits_web_search(self):
        call_log, tool, ctx = _stub_subagent_internals(
            specialized_engine_names=["arxiv"]
        )
        try:
            with patch.object(strat, "_make_web_search_tool") as m:
                tool = strat._make_research_subtopic_tool(
                    search_engine_name="duckduckgo",
                    model=MagicMock(),
                    settings_snapshot={"search.tool": "duckduckgo"},
                    collector=MagicMock(),
                    max_sub_iterations=1,
                    search_enabled=False,
                )
                tool.invoke({"subtopics": ["t1"]})
            # web_search factory must not be called when search_enabled=False
            m.assert_not_called()
        finally:
            _stop(ctx)

        tool_names = call_log["create_agent_tool_lists"][0]
        assert "web_search" not in tool_names
        # Specialized engines still get through.
        assert "search_arxiv" in tool_names

    def test_agent_enabled_false_collection_excluded_for_subagent(self):
        # A collection the user marked "not for the research agent" must be
        # excluded from the subagent tool list by the same gate as the lead's.
        call_log, tool, ctx = _stub_subagent_internals(
            engine_configs={
                "arxiv": {"description": "arxiv desc", "strengths": []},
                "collection_private": {
                    "description": "private collection",
                    "strengths": [],
                    "agent_enabled": False,
                },
            }
        )
        try:
            tool.invoke({"subtopics": ["t1"]})
        finally:
            _stop(ctx)

        tool_names = call_log["create_agent_tool_lists"][0]
        assert "search_arxiv" in tool_names
        assert "search_collection_private" not in tool_names

    def test_empty_toolbox_refuses_to_run_subagents(self):
        # No web_search (search_enabled=False), no fetch tool, no specialized
        # engines: research_subtopic must refuse instead of running tool-less
        # subagents whose un-grounded output would read as research findings.
        call_log, _tool, ctx = _stub_subagent_internals(
            specialized_engine_names=[]
        )
        progress_cb = MagicMock()
        try:
            tool = strat._make_research_subtopic_tool(
                search_engine_name="duckduckgo",
                model=MagicMock(),
                settings_snapshot={
                    "search.tool": "duckduckgo",
                    "search.engine.web.wayback.agent_enabled": False,
                },
                collector=MagicMock(),
                max_sub_iterations=1,
                search_enabled=False,
                progress_callback=progress_cb,
            )
            result = tool.invoke({"subtopics": ["t1"]})
        finally:
            _stop(ctx)

        assert "unavailable" in result
        # No subagent was ever spawned.
        assert call_log["create_agent_tool_lists"] == []
        # The refusal fires BEFORE the sub_research milestone, so the UI
        # never announces subtopic research that won't run.
        progress_cb.assert_not_called()

    def test_prompt_mentions_specialized_tools_only_when_present(self):
        # With specialized engines available, the subagent prompt steers the
        # model toward the live schema. Static names would keep recommending
        # an engine after the shared tracker puts it on cooldown.
        call_log, tool, ctx = _stub_subagent_internals(
            specialized_engine_names=["arxiv"]
        )
        try:
            tool.invoke({"subtopics": ["t1"]})
        finally:
            _stop(ctx)
        prompt = call_log["system_prompts"][0]
        assert "domain-specific search tools" in prompt
        assert "currently supplied" in prompt
        assert "search_arxiv" not in prompt
        assert "search_pubmed" not in prompt

        # ...but with none available the hint is omitted, so the prompt never
        # advertises tools the model doesn't have.
        call_log, tool, ctx = _stub_subagent_internals(
            specialized_engine_names=[]
        )
        try:
            tool.invoke({"subtopics": ["t1"]})
        finally:
            _stop(ctx)
        assert (
            "domain-specific search tools" not in call_log["system_prompts"][0]
        )


class TestLeadEmptyToolboxOmitsSubResearch:
    """When the lead toolbox is otherwise empty, research_subtopic must be
    dropped too: its subagents are gated on the same search/fetch/egress
    state, so a research_subtopic-only agent could only fan out tool-less
    subagents whose un-grounded output would read as findings."""

    def _build_lead_tools(self, *, specialized_engine_names):
        call_log, _tool, ctx = _stub_subagent_internals(
            specialized_engine_names=specialized_engine_names
        )
        try:
            strategy = strat.LangGraphAgentStrategy(
                model=MagicMock(),
                search=None,
                citation_handler=object(),
                include_sub_research=True,
                settings_snapshot={
                    "search.tool": "duckduckgo",
                    "search.fetch.mode": "disabled",
                },
            )
            return strategy._build_tools("overall query")
        finally:
            _stop(ctx)

    def test_research_subtopic_dropped_when_toolbox_otherwise_empty(self):
        tools = self._build_lead_tools(specialized_engine_names=[])
        assert tools == []

    def test_research_subtopic_present_when_other_tools_exist(self):
        names = [
            t.name
            for t in self._build_lead_tools(specialized_engine_names=["arxiv"])
        ]
        assert "search_arxiv" in names
        assert "research_subtopic" in names


class TestSubagentEgressPolicyFiltering:
    """The egress policy filter applied to the lead agent's specialized
    engines must ALSO apply to subagent specialized engines — otherwise a
    STRICT-scoped run would leak forbidden tool names to the subagent LLM.
    """

    def test_strict_scope_registers_no_specialized_engines_for_subagent(self):
        # Build a real EgressContext in STRICT scope.
        from local_deep_research.security.egress.policy import EgressScope

        strict_ctx = SimpleNamespace(scope=EgressScope.STRICT)
        # Under STRICT the helper short-circuits every engine; subagent's
        # tool list should be only web_search (search_enabled=True).
        call_log, tool, ctx = _stub_subagent_internals(
            specialized_engine_names=["arxiv", "pubmed", "wikipedia"]
        )
        try:
            tool = strat._make_research_subtopic_tool(
                search_engine_name="duckduckgo",
                model=MagicMock(),
                settings_snapshot={"search.tool": "duckduckgo"},
                collector=MagicMock(),
                max_sub_iterations=1,
                egress_context=strict_ctx,
            )
            tool.invoke({"subtopics": ["t1"]})
        finally:
            _stop(ctx)

        tool_names = call_log["create_agent_tool_lists"][0]
        assert "web_search" in tool_names
        # STRICT must drop ALL specialized engines.
        assert "search_arxiv" not in tool_names
        assert "search_pubmed" not in tool_names
        assert "search_wikipedia" not in tool_names