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.

11.5 KB
Raw Blame
"""Summary context budgets match actual compatible-endpoint requests."""

import json
from pathlib import Path
import socket

import pytest


PR_TREE = Path(__file__).resolve().parents[3]


@pytest.fixture(autouse=True)
def no_network(monkeypatch):
    def refused(*args, **kwargs):
        raise AssertionError("This regression must not open a connection")

    monkeypatch.setattr(socket.socket, "connect", refused)
    monkeypatch.setattr(socket.socket, "connect_ex", refused)
    monkeypatch.setattr(socket, "create_connection", refused)


@pytest.mark.parametrize(
    "provider,unrestricted,reply_tokens,rate_limited,scenario",
    [
        (provider, unrestricted, reply, limited, "bounded")
        for provider in ("openai_endpoint", "anthropic_endpoint")
        for unrestricted in (True, False)
        for reply in (30000, 1000)
        for limited in (False, True)
    ]
    + [
        (provider, True, 30000, limited, scenario)
        for provider in ("openai_endpoint", "anthropic_endpoint")
        for limited in (False, True)
        for scenario in ("full", "explicit")
    ]
    + [
        (provider, True, 30000, limited, "streaming")
        for provider in ("openai_endpoint", "anthropic_endpoint")
        for limited in (False, True)
    ],
)
def test_built_endpoint_payload_and_summary_prompt_share_one_budget(
    monkeypatch, provider, unrestricted, reply_tokens, rate_limited, scenario
):
    from langchain_core.messages import AIMessage, AIMessageChunk
    from langchain_core.outputs import (
        ChatGeneration,
        ChatGenerationChunk,
        ChatResult,
    )
    from local_deep_research.config.llm_config import get_llm
    from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
        SearchResultsCollector,
    )
    from local_deep_research.advanced_search_system.tools.fetch import (
        build_fetch_tool,
    )

    metadata = json.loads(
        (
            PR_TREE / "src/local_deep_research/defaults/default_settings.json"
        ).read_text()
    )
    snapshot = {key: row["value"] for key, row in metadata.items()}
    snapshot.update(
        {
            # An explicit per-run provider below overrides this saved value.
            "llm.provider": "ollama",
            "llm.context_window_unrestricted": unrestricted,
            "llm.local_context_window_size": 20480,
            "llm.context_window_size": 32768,
            "llm.max_tokens": reply_tokens,
            "llm.supports_max_tokens": True,
            "llm.streaming": scenario == "streaming",
            "llm.openai_endpoint.stream_usage": scenario == "streaming",
            "llm.openai_endpoint.url": "http://127.0.0.1:9/v1",
            "llm.anthropic_endpoint.url": "http://127.0.0.1:9",
            "llm.openai_endpoint.api_key": "local-review-placeholder",
            "llm.anthropic_endpoint.api_key": "local-review-placeholder",
            "rate_limiting.llm_enabled": rate_limited,
            "search.tool": "searxng",
            "policy.egress_scope": "public_only",
            "llm.require_local_endpoint": False,
        }
    )
    model = get_llm(
        model_name="review-model",
        provider=provider,
        settings_snapshot=snapshot,
    )
    try:
        assert model._provider == provider
        # Independently derive the documented prompt budget, so the same
        # probe works before and after refactoring the private helper.
        window = 20480 if unrestricted else 32768
        limit = 4 * (window - min(reply_tokens, int(window * 0.8)))
        raw_model = model.base_llm.base_llm if rate_limited else model.base_llm
        original_payload = raw_model._get_request_payload("control")
        captured = []
        rate_limiter_calls = []

        if rate_limited:

            def capture_rate_limit(call):
                rate_limiter_calls.append(True)
                return call()

            monkeypatch.setattr(
                model.base_llm, "_call_rate_limited", capture_rate_limit
            )

        def capture(self, messages, stop=None, run_manager=None, **kwargs):
            # Keep real LangChain dispatch and its binding kwargs, replacing
            # only the method that would send the network request.
            payload = self._get_request_payload(messages, stop=stop, **kwargs)
            captured.append((messages, payload))
            return ChatResult(
                generations=[
                    ChatGeneration(message=AIMessage(content="Extracted fact"))
                ]
            )

        if scenario == "streaming":
            from local_deep_research.llm.cancellation import (
                bind_llm_to_research,
            )

            # Exercise the real scoped Processing -> RateLimited -> SDK
            # stream path; no provider or wrapper binding is mocked.
            bind_llm_to_research(
                model, "summary-budget-stream", stop_at=raw_model
            )

            def capture_stream(
                self, messages, stop=None, run_manager=None, **kwargs
            ):
                kwargs["stream"] = True
                payload = self._get_request_payload(
                    messages, stop=stop, **kwargs
                )
                captured.append((messages, payload))
                yield ChatGenerationChunk(
                    message=AIMessageChunk(content="Extracted fact")
                )

            monkeypatch.setattr(type(raw_model), "_stream", capture_stream)
        else:
            monkeypatch.setattr(type(raw_model), "_generate", capture)
        tool = build_fetch_tool(
            "full" if scenario == "full" else "summary_focus_query",
            SearchResultsCollector([]),
            model=model,
            overall_query="Research question",
            settings_snapshot=snapshot,
            content_max_length=50000 if scenario == "explicit" else None,
            library_resolver=lambda url: {
                "content": "x" * 600000,
                "title": "Local document",
                "url": url,
            },
        )
        tool_input = {"url": "/library/document/review"}
        if scenario != "full":
            tool_input["focus"] = "Specific fact"
        output = tool.invoke(tool_input)
        # A fix must affect this one summary call, never mutate the shared
        # research model's defaults or the caller-owned settings snapshot.
        assert raw_model._get_request_payload("control") == original_payload
        assert snapshot["llm.max_tokens"] == reply_tokens
        if scenario == "full":
            assert output.split("\n\n", 1)[1] == "x" * 10000
            assert not captured
            assert not rate_limiter_calls
            return
        assert "Extracted fact" in output, output
        assert len(captured) == 1
        assert len(rate_limiter_calls) == int(rate_limited)
        messages, payload = captured[0]
        if scenario == "streaming":
            assert payload["stream"] is True
        actual_prompt = messages[0].content
        actual_reply = payload.get(
            "max_completion_tokens", payload.get("max_tokens")
        )
        assert isinstance(actual_reply, int) and actual_reply > 0
        if scenario == "explicit":
            original_reply = original_payload.get(
                "max_completion_tokens", original_payload.get("max_tokens")
            )
            page_text = actual_prompt.split("Page content:\n", 1)[1].split(
                "\n\nOutput rules:", 1
            )[0]
            assert page_text == "x" * 50000
            assert len(actual_prompt) > limit
            assert actual_reply == original_reply
            return
        assert len(actual_prompt) == limit
        estimated_prompt_tokens = len(actual_prompt) / 4
        assert estimated_prompt_tokens + actual_reply <= window, (
            f"{provider=} {unrestricted=} {limit=} {actual_reply=} "
            f"{window=}; the summary limit must reserve the reply allowance "
            "actually sent by this already-built provider"
        )
    finally:
        model.close()


@pytest.mark.parametrize(
    "provider", ["openai", "openai_endpoint", "anthropic_endpoint"]
)
@pytest.mark.parametrize("registration_kind", ["instance", "factory"])
def test_registered_shadow_keeps_custom_model_invocation_contract(
    provider, registration_kind
):
    from uuid import uuid4

    from langchain_core.language_models import BaseChatModel
    from langchain_core.messages import AIMessage
    from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
        SearchResultsCollector,
    )
    from local_deep_research.advanced_search_system.tools.fetch import (
        build_fetch_tool,
    )
    from local_deep_research.config.llm_config import get_llm
    from local_deep_research.llm.llm_registry import (
        get_llm_from_registry,
        register_llm,
        unregister_llm,
    )

    captured = []

    class PromptOnlyModel(BaseChatModel):
        @property
        def _llm_type(self):
            return "summary-budget-custom"

        def _generate(self, messages, stop=None, run_manager=None, **kwargs):
            raise AssertionError("The custom invoke method must be used")

        def invoke(self, prompt):
            # A registered model's public contract need not accept the
            # built-in provider's max_tokens keyword.
            captured.append(prompt)
            return AIMessage(content="Custom extracted fact")

    custom_model = PromptOnlyModel()

    def factory(model_name, temperature, settings_snapshot):
        return custom_model

    shared_provider = get_llm_from_registry(provider)
    username = f"summary-budget-shadow-{uuid4().hex}"
    snapshot = {
        "llm.provider": "ollama",
        "llm.context_window_unrestricted": True,
        "llm.local_context_window_size": 20480,
        "llm.max_tokens": 30000,
        "llm.supports_max_tokens": True,
        "rate_limiting.llm_enabled": False,
        "search.tool": "searxng",
        "policy.egress_scope": "public_only",
        "llm.require_local_endpoint": False,
        "search.fetch.summary_input_max_length": 500000,
    }
    register_llm(
        provider,
        factory if registration_kind == "factory" else custom_model,
        username=username,
    )
    model = None
    try:
        model = get_llm(
            model_name="custom-summary-model",
            provider=provider,
            username=username,
            settings_snapshot=snapshot,
        )
        assert model.base_llm is custom_model
        assert model._owns_llm is False
        tool = build_fetch_tool(
            "summary_focus_query",
            SearchResultsCollector([]),
            model=model,
            overall_query="Research question",
            settings_snapshot=snapshot,
            library_resolver=lambda url: {
                "content": "x" * 600000,
                "title": "Local document",
                "url": url,
            },
        )
        output = tool.invoke(
            {"url": "/library/document/review", "focus": "Specific fact"}
        )
        assert "Custom extracted fact" in output, output
        assert len(captured) == 1
        # A user-owned registration is not proof of a known cloud model,
        # even when it shadows a built-in provider's name.
        assert len(captured[0]) == 4 * (20480 - int(20480 * 0.8))
        assert snapshot["llm.max_tokens"] == 30000
    finally:
        if model is not None:
            model.close()
        unregister_llm(provider, username=username)
    assert get_llm_from_registry(provider) is shared_provider
    assert get_llm_from_registry(provider, username=username) is shared_provider