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.

8.9 KB
Raw Blame
"""Archive tool wiring for the real lead-agent and subagent factories."""

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

import pytest
import requests

from local_deep_research.advanced_search_system.strategies import (
    langgraph_agent_strategy as strategy_module,
)
from local_deep_research.security import clear_active_context
from local_deep_research.web_search_engines.engine_availability import (
    SearchFailure,
    available_tool_names,
    engine_for_tool,
)


@pytest.fixture(autouse=True)
def _offline(monkeypatch):
    clear_active_context()
    monkeypatch.setattr(
        requests.sessions.Session,
        "request",
        Mock(side_effect=AssertionError("Unexpected outbound request")),
    )
    yield
    clear_active_context()


@pytest.fixture
def factories(monkeypatch):
    """Mock search discovery and agent execution, retaining both fetch builders."""
    configs = {
        "searxng": {"description": "Web search", "strengths": []},
        "wayback": {"description": "Historical pages", "strengths": []},
        "arxiv": {"description": "Research papers", "strengths": []},
    }
    discovery = Mock(return_value=configs)
    monkeypatch.setattr(
        "local_deep_research.web_search_engines.search_engines_config"
        ".list_eligible_engine_configs",
        discovery,
    )
    monkeypatch.setattr(
        "local_deep_research.web_search_engines.search_engines_config"
        ".search_config",
        Mock(return_value=configs),
    )
    web_search = Mock(return_value=SimpleNamespace(name="web_search"))
    specialized_search = Mock(
        side_effect=lambda name, *args, **kwargs: SimpleNamespace(
            name=f"search_{name}"
        )
    )
    monkeypatch.setattr(strategy_module, "_make_web_search_tool", web_search)
    monkeypatch.setattr(
        strategy_module, "_make_specialized_search_tool", specialized_search
    )
    created = []

    def create_agent(*, model, tools, system_prompt, **kwargs):
        created.append(SimpleNamespace(tools=tools, prompt=system_prompt))
        agent = MagicMock()
        agent.invoke.return_value = {
            "messages": [SimpleNamespace(content="Findings from the subagent")]
        }
        return agent

    monkeypatch.setattr("langchain.agents.create_agent", create_agent)
    return SimpleNamespace(
        configs=configs,
        discovery=discovery,
        web_search=web_search,
        specialized_search=specialized_search,
        created=created,
    )


def _strategy(
    *, primary="searxng", mode="full", scope="public_only", extra=None
):
    snapshot = {
        "search.tool": primary,
        "search.fetch.mode": mode,
        "search.fetch.max_per_topic": 2,
        "policy.egress_scope": scope,
    }
    snapshot.update(extra or {})
    return strategy_module.LangGraphAgentStrategy(
        model=MagicMock(),
        search=MagicMock(),
        citation_handler=object(),
        max_sub_iterations=1,
        include_sub_research=True,
        settings_snapshot=snapshot,
        programmatic_mode=True,
    )


def _lead_and_subagent_tools(strategy, factories):
    lead = strategy._build_tools("How did this page change over time?")
    research = next(tool for tool in lead if tool.name == "research_subtopic")
    research.invoke({"subtopics": ["Historical policy changes"]})
    assert len(factories.created) == 1
    return lead, factories.created[0].tools


@pytest.mark.parametrize("primary", ["searxng", "wayback"])
@pytest.mark.parametrize(
    "mode", ["full", "summary_focus", "summary_focus_query"]
)
def test_lead_and_subagent_get_archive_fetch_instead_of_wayback_search(
    primary, mode, factories
):
    strategy = _strategy(primary=primary, mode=mode)

    lead, subagent = _lead_and_subagent_tools(strategy, factories)

    for tools in (lead, subagent):
        names = [tool.name for tool in tools]
        assert names.count("fetch_archive") == 1
        assert "fetch_content" in names
        assert "search_arxiv" in names
        assert "search_wayback" not in names
        if primary == "wayback":
            assert "web_search" not in names
        else:
            assert "web_search" in names
        archive_tool = next(
            tool for tool in tools if tool.name == "fetch_archive"
        )
        assert set(archive_tool.args) == (
            {"url", "date"} if mode == "full" else {"url", "date", "focus"}
        )
    assert "search_wayback" not in factories.created[0].prompt
    assert "wayback" not in {
        call.args[0] for call in factories.specialized_search.call_args_list
    }
    if primary == "wayback":
        factories.web_search.assert_not_called()
    else:
        assert factories.web_search.call_count == 2


@pytest.mark.parametrize("primary", ["searxng", "wayback"])
@pytest.mark.parametrize(
    "extra",
    [
        {"search.engine.web.wayback.agent_enabled": False},
        {
            "search.engine.web.wayback.agent_enabled": {
                "value": False,
                "ui_element": "checkbox",
            }
        },
        {"search.fetch.mode": "disabled"},
    ],
    ids=["disabled_archive", "wrapped_disabled_archive", "disabled_fetch_mode"],
)
def test_archive_flag_and_fetch_mode_hide_tool_from_both_agent_roles(
    primary, extra, factories
):
    strategy = _strategy(primary=primary, extra=extra)

    lead, subagent = _lead_and_subagent_tools(strategy, factories)

    for tools in (lead, subagent):
        names = {tool.name for tool in tools}
        assert "fetch_archive" not in names
        assert "search_wayback" not in names
        assert "search_arxiv" in names
        if primary == "wayback":
            assert "web_search" not in names
        if extra.get("search.fetch.mode") == "disabled":
            assert "fetch_content" not in names
        else:
            assert "fetch_content" in names
    assert "wayback" not in {
        call.args[0] for call in factories.specialized_search.call_args_list
    }


@pytest.mark.parametrize("primary", ["searxng", "wayback"])
@pytest.mark.parametrize("flag", [0, "off", "no", " false "])
@pytest.mark.parametrize("wrapped", [False, True], ids=["raw", "wrapped"])
def test_disabled_boolean_encodings_hide_archive_from_both_agent_roles(
    primary, flag, wrapped, factories
):
    setting = {"value": flag, "ui_element": "checkbox"} if wrapped else flag
    strategy = _strategy(
        primary=primary,
        extra={"search.engine.web.wayback.agent_enabled": setting},
    )

    lead, subagent = _lead_and_subagent_tools(strategy, factories)

    for tools in (lead, subagent):
        names = {tool.name for tool in tools}
        assert "fetch_archive" not in names
        assert "search_wayback" not in names
        assert "fetch_content" in names
        assert "search_arxiv" in names
        assert ("web_search" in names) == (primary != "wayback")


@pytest.mark.parametrize("scope", ["private_only", "strict"])
def test_egress_policy_hides_archive_tool_from_lead_and_subagents(
    scope, factories
):
    strategy = _strategy(scope=scope)

    lead, subagent = _lead_and_subagent_tools(strategy, factories)

    for tools in (lead, subagent):
        names = {tool.name for tool in tools}
        assert "fetch_archive" not in names
        assert "search_wayback" not in names
        assert "web_search" in names


def test_archive_availability_is_bound_to_wayback_not_primary(factories):
    strategy = _strategy()
    names = [tool.name for tool in strategy._build_tools("Historical changes")]
    assert "fetch_archive" in names
    assert engine_for_tool("fetch_archive", "searxng") == "wayback"

    strategy.engine_availability.disable(
        "wayback", SearchFailure("Archive temporarily unavailable", None)
    )
    available = available_tool_names(
        names, "searxng", strategy.engine_availability.disabled()
    )

    assert "fetch_archive" not in available
    assert "fetch_content" in available
    assert "web_search" in available
    assert "search_arxiv" in available


@pytest.mark.parametrize("enabled", [False, True])
def test_strict_saved_wayback_prompt_only_names_available_archive_tool(
    enabled, factories, monkeypatch
):
    strategy = _strategy(
        primary="wayback",
        scope="strict",
        extra={"search.engine.web.wayback.agent_enabled": enabled},
    )
    captured = []

    def capture_prompt(**kwargs):
        captured.append(kwargs)
        raise RuntimeError("Stop after capturing the prompt")

    monkeypatch.setattr("langchain.agents.create_agent", capture_prompt)
    monkeypatch.setattr(strategy, "_update_progress", Mock())
    monkeypatch.setattr(strategy, "check_termination", Mock())
    strategy.analyze_topic("Read https://example.com/history as it was in 2024")

    assert len(captured) == 1
    names = {tool.name for tool in captured[0]["tools"]}
    prompt = captured[0]["system_prompt"]
    assert ("fetch_archive" in names) is enabled
    assert ("fetch_archive" in prompt) is enabled
    if not enabled:
        assert "Archive fetching is unavailable" in prompt