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.7 KB
Raw Blame
"""Provider refusals must survive real agent execution and retain controls."""

import json
from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import openai
import pytest
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI

from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
    LangGraphAgentStrategy,
)
from local_deep_research.config.llm_config import ProcessingLLMWrapper
from local_deep_research.error_handling.error_messages import (
    TYPED_RESEARCH_ERROR_MESSAGES,
)
from local_deep_research.error_handling.openai_compat_errors import (
    ProviderUsageLimitError,
    _TRANSPORT_MODULES,
    is_openai_payment_required,
)
from local_deep_research.exceptions import ResearchTerminatedException
from local_deep_research.security.egress.policy import (
    Decision,
    PolicyDeniedError,
)


DETAIL = "synthetic-provider-response-detail"
ANSWER = "A synthetic research answer."


def _response(content=ANSWER, tool_calls=None):
    message = {"role": "assistant", "content": content}
    if tool_calls:
        message["tool_calls"] = tool_calls
    return {
        "id": "synthetic-completion",
        "object": "chat.completion",
        "created": 0,
        "model": "synthetic-model",
        "choices": [
            {
                "index": 0,
                "message": message,
                "finish_reason": "tool_calls" if tool_calls else "stop",
            }
        ],
        "usage": {
            "prompt_tokens": 1,
            "completion_tokens": 1,
            "total_tokens": 2,
        },
    }


@contextmanager
def _sdk_model(responses):
    """Keep the SDK, wrapper and agent real; replace only outbound HTTP."""
    transport = next(
        module
        for module in _TRANSPORT_MODULES
        if issubclass(openai.DefaultHttpxClient, module.Client)
    )
    calls = []
    remaining = iter(responses)

    def respond(request):
        calls.append(json.loads(request.content))
        status, body = next(remaining)
        return transport.Response(status, json=body)

    with transport.Client(
        transport=transport.MockTransport(respond)
    ) as http_client:
        model = ChatOpenAI(
            api_key="synthetic-test-key",
            base_url="https://provider.invalid/v1",
            model="synthetic-model",
            max_retries=0,
            http_client=http_client,
        )
        yield ProcessingLLMWrapper(model, owns_llm=False), calls


def _strategy(model):
    return LangGraphAgentStrategy(
        model=model,
        search=MagicMock(),
        all_links_of_system=[],
        settings_snapshot={"search.tool": {"value": "mock"}},
    )


@tool
def synthetic_search(query: str) -> str:
    """Return an offline synthetic observation."""
    return "A synthetic observation."


@pytest.mark.parametrize("status", [200, 400, 402])
def test_real_sdk_agent_preserves_healthy_and_unrelated_results(status):
    body = _response() if status == 200 else {"error": {"message": DETAIL}}
    with _sdk_model([(status, body)]) as (model, calls):
        strategy = _strategy(model)
        with patch.object(
            strategy, "_build_tools", return_value=[synthetic_search]
        ):
            if status == 402:
                with pytest.raises(ProviderUsageLimitError) as raised:
                    strategy.analyze_topic("synthetic query")
                error = raised.value
                assert (
                    str(error)
                    == TYPED_RESEARCH_ERROR_MESSAGES["openai_payment_required"]
                )
                assert error.__cause__ is None
                assert error.__context__ is None
                assert DETAIL not in str(error)
            else:
                result = strategy.analyze_topic("synthetic query")
                if status == 200:
                    assert result.get("error") is None
                    assert ANSWER in result["formatted_findings"]
                else:
                    assert result["error"]
                    assert "billing or usage limits" not in result["error"]
        assert len(calls) == 1
        assert calls[0]["tools"][0]["function"]["name"] == "synthetic_search"


def test_next_invocation_recovers_after_a_usage_refusal():
    responses = [
        (402, {"error": {"message": DETAIL}}),
        (200, _response()),
    ]
    with _sdk_model(responses) as (model, calls):
        strategy = _strategy(model)
        with patch.object(
            strategy, "_build_tools", return_value=[synthetic_search]
        ):
            with pytest.raises(ProviderUsageLimitError):
                strategy.analyze_topic("synthetic query")
            result = strategy.analyze_topic("synthetic recovery query")
    assert len(calls) == 2
    assert result.get("error") is None
    assert ANSWER in result["formatted_findings"]


def test_real_tool_results_survive_a_later_usage_refusal():
    # The real agent calls the tool, then encounters 402. Both fallback
    # synthesis and citation processing still use the actual model wrapper.
    first = _response(
        content=None,
        tool_calls=[
            {
                "id": "synthetic-call",
                "type": "function",
                "function": {
                    "name": "synthetic_search",
                    "arguments": json.dumps({"query": "synthetic query"}),
                },
            }
        ],
    )
    responses = [
        (200, first),
        (402, {"error": {"message": DETAIL}}),
        (200, _response("A synthetic retained answer [1].")),
        (200, _response("A synthetic retained answer [1].")),
    ]
    with _sdk_model(responses) as (model, calls):
        strategy = _strategy(model)
        tool_calls = []

        @tool("synthetic_search")
        def collect(query: str) -> str:
            """Record an offline synthetic source."""
            tool_calls.append(query)
            strategy.collector.add_results(
                [
                    {
                        "title": "Synthetic source",
                        "link": "https://source.invalid/document",
                        "snippet": "Synthetic source evidence.",
                    }
                ]
            )
            return "[1] Synthetic source evidence."

        with patch.object(strategy, "_build_tools", return_value=[collect]):
            result = strategy.analyze_topic("synthetic query")
    assert tool_calls == ["synthetic query"]
    assert len(calls) == 4
    assert result.get("error") is None
    assert "synthetic retained answer" in result["formatted_findings"]
    assert "https://source.invalid/document" in result["formatted_findings"]
    assert DETAIL not in result["formatted_findings"]


def test_agent_creation_refusal_uses_a_payload_free_signal():
    with _sdk_model([(402, {"error": {"message": DETAIL}})]) as (model, calls):
        with pytest.raises(openai.APIStatusError) as sdk_error:
            model.invoke("synthetic query")
        strategy = _strategy(model)
        with (
            patch.object(
                strategy, "_build_tools", return_value=[synthetic_search]
            ),
            patch("langchain.agents.create_agent", side_effect=sdk_error.value),
            pytest.raises(ProviderUsageLimitError) as raised,
        ):
            strategy.analyze_topic("synthetic query")
    assert len(calls) == 1
    assert raised.value.__cause__ is None
    assert raised.value.__context__ is None
    assert DETAIL not in str(raised.value)


@pytest.mark.parametrize("boundary", ["creation", "stream"])
@pytest.mark.parametrize("control", ["policy", "cancellation"])
def test_agent_failures_never_convert_policy_or_cancellation(boundary, control):
    with _sdk_model([(402, {"error": {"message": DETAIL}})]) as (model, calls):
        with pytest.raises(openai.APIStatusError) as sdk_error:
            model.invoke("synthetic query")
        error = (
            PolicyDeniedError(
                Decision(allowed=False, reason="synthetic denial")
            )
            if control == "policy"
            else ResearchTerminatedException("synthetic cancellation")
        )
        error.__cause__ = sdk_error.value
        assert not is_openai_payment_required(error)
        agent = MagicMock()
        agent.stream.side_effect = error
        strategy = _strategy(model)
        with (
            patch.object(
                strategy, "_build_tools", return_value=[synthetic_search]
            ),
            patch(
                "langchain.agents.create_agent",
                side_effect=error if boundary == "creation" else None,
                return_value=agent,
            ),
            pytest.raises(type(error)) as raised,
        ):
            strategy.analyze_topic("synthetic query")
        assert raised.value is error
    assert len(calls) == 1