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.

31.8 KB
Raw Blame
"""Summary input budgets stay separate from text returned to the agent."""

from importlib import import_module
from unittest.mock import MagicMock, patch

import pytest
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 (
    _read_summary_input_max_length_setting,
    build_fetch_tool,
)
from local_deep_research.content_fetcher.url_classifier import URLType


SUMMARY_KEY = "search.fetch.summary_input_max_length"
FULL_KEY = "search.fetch.content_max_length"
ENV_VAR = "LDR_SEARCH_FETCH_SUMMARY_INPUT_MAX_LENGTH"
LOCAL_WINDOW_KEY = "llm.local_context_window_size"


@pytest.mark.parametrize("mode", ["summary_focus", "summary_focus_query"])
@pytest.mark.parametrize("source", ["http", "library", "citation"])
@pytest.mark.parametrize("configured_limit", [None, 1, 1001, 600001])
def test_summary_input_budget_reaches_extractor_only(
    mode, source, configured_limit
):
    """Exercise real HTTP truncation and local/cited documents in both modes.

    The model is built for a cloud provider with unrestricted context, so
    the configured budget applies unchanged. The default reads past the
    old 50k ceiling; a configured value can exceed even ContentFetcher's
    500k default or be below the old 1k floor.
    """
    limit = 500000 if configured_limit is None else configured_limit
    # Non-ASCII text proves that the budget counts characters, not bytes.
    page = "é" * (limit - 1) + "Z" + "TRUNCATED_TAIL"
    settings = {FULL_KEY: {"value": 1000, "ui_element": "number"}}
    if configured_limit is not None:
        settings[SUMMARY_KEY] = {
            "value": configured_limit,
            "ui_element": "number",
        }
    url = (
        "https://example.com/page"
        if source == "http"
        else "/library/document/large-doc"
    )
    collector = SearchResultsCollector([])
    if source == "citation":
        collector.add_results(
            [{"title": "Page", "link": url, "snippet": "search snippet"}],
            engine_name="library",
        )
    model, base = _built_model("openai", LOCAL_DEFAULTS)
    resolver = MagicMock(
        return_value={"content": page, "title": "Page", "url": url}
    )
    downloader = MagicMock()
    downloader.download_with_result.return_value.is_success = True
    downloader.download_with_result.return_value.content = page.encode()
    downloader.get_metadata.return_value = {"title": "Page"}
    tool = build_fetch_tool(
        mode,
        collector,
        model=model,
        overall_query="Research question",
        settings_snapshot=settings,
        library_resolver=None if source == "http" else resolver,
    )

    with (
        patch(
            "local_deep_research.content_fetcher.fetcher.policy_aware_validate_url",
            return_value=True,
        ),
        patch(
            "local_deep_research.content_fetcher.fetcher.URLClassifier"
        ) as classifier,
        patch(
            "local_deep_research.content_fetcher.fetcher.ContentFetcher._get_downloader",
            return_value=downloader,
        ),
    ):
        classifier.classify.return_value = URLType.HTML
        classifier.get_source_name.return_value = "Web Page"
        output = tool.invoke(
            {"url": "[1]" if source == "citation" else url, "focus": "fact"}
        )

    base.invoke.assert_called_once()
    prompt = base.invoke.call_args.args[0]
    extracted_input = prompt.split("Page content:\n", 1)[1].split(
        "\n\nOutput rules:", 1
    )[0]
    expected = page[:limit]
    if source == "http":
        expected += "\n\n[... content truncated ...]"
        downloader.download_with_result.assert_called_once()
    else:
        downloader.download_with_result.assert_not_called()
        resolver.assert_called_once_with(url)
    assert extracted_input == expected
    assert ("Overall research question:" in prompt) == (
        mode == "summary_focus_query"
    )
    assert "Extracted fact" in output
    assert "é" not in output
    assert "TRUNCATED_TAIL" not in output
    assert collector.results[-1]["snippet"] == "Extracted fact"


@pytest.mark.parametrize("full_limit", [None, 3000])
def test_full_mode_keeps_its_own_budget(full_limit):
    settings = {SUMMARY_KEY: 900000}
    if full_limit is not None:
        settings[FULL_KEY] = full_limit
    tool = build_fetch_tool(
        "full",
        SearchResultsCollector([]),
        settings_snapshot=settings,
        library_resolver=lambda url: {"content": "A" * 50000, "url": url},
    )
    output = tool.invoke({"url": "/library/document/large-doc"})
    assert output.split("\n\n", 1)[1] == "A" * (
        10000 if full_limit is None else full_limit
    )


@pytest.mark.parametrize("mode", ["summary_focus", "summary_focus_query"])
def test_explicit_summary_budget_still_overrides_settings(mode):
    model = MagicMock()
    model.invoke.return_value.content = "summary"
    tool = build_fetch_tool(
        mode,
        SearchResultsCollector([]),
        model=model,
        overall_query="question",
        settings_snapshot={SUMMARY_KEY: 900000, FULL_KEY: 1000},
        library_resolver=lambda url: {"content": "A" * 50000, "url": url},
        content_max_length=1500,
    )
    output = tool.invoke(
        {"url": "/library/document/large-doc", "focus": "fact"}
    )
    assert "summary" in output
    prompt = model.invoke.call_args.args[0]
    assert "A" * 1500 in prompt
    assert "A" * 1501 not in prompt


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        (1, 1),
        (" 1 ", 1),
        (1001, 1001),
        (700000, 700000),
        (2**53 + 1, 2**53 + 1),
        (10**30, 10**30),
        (1200.0, 1200),
        ("1200.0", 1200),
        (1000000.0, 1000000),
    ],
)
@pytest.mark.parametrize("wrapped", [False, True])
def test_summary_budget_accepts_positive_integers_without_clamping(
    value, expected, wrapped
):
    setting = {"value": value, "ui_element": "number"} if wrapped else value
    assert (
        _read_summary_input_max_length_setting({SUMMARY_KEY: setting})
        == expected
    )


@pytest.mark.parametrize("snapshot", [None, {}, {FULL_KEY: 1000}])
def test_missing_summary_budget_uses_high_default(snapshot):
    assert _read_summary_input_max_length_setting(snapshot) == 500000


@pytest.mark.parametrize(
    "value",
    [
        0,
        -1,
        True,
        None,
        1.5,
        "1.0000000000000001",
        "1e10000000",
        "invalid",
        float("inf"),
    ],
)
@pytest.mark.parametrize("wrapped", [False, True])
def test_invalid_summary_budget_prevents_fetch_and_model_call(value, wrapped):
    model = MagicMock()
    resolver = MagicMock()
    tool = build_fetch_tool(
        "summary_focus",
        SearchResultsCollector([]),
        model=model,
        settings_snapshot={
            SUMMARY_KEY: {"value": value, "ui_element": "number"}
            if wrapped
            else value
        },
        library_resolver=resolver,
    )
    with patch("local_deep_research.content_fetcher.ContentFetcher") as fetcher:
        output = tool.invoke(
            {"url": "https://example.com/page", "focus": "fact"}
        )
    assert "must be a positive integer" in output
    fetcher.assert_not_called()
    resolver.assert_not_called()
    model.invoke.assert_not_called()


@pytest.mark.parametrize("value", ["invalid", "1.0000000000000001", "0"])
def test_invalid_summary_environment_budget_cannot_reuse_stored_value(
    monkeypatch, value
):
    from local_deep_research.settings.manager import (
        apply_environment_overrides_to_snapshot,
    )

    monkeypatch.setenv(ENV_VAR, value)
    queued_snapshot = {SUMMARY_KEY: {"value": 500000, "ui_element": "number"}}
    # A queued snapshot can predate the operator's environment change.
    # Dispatch keeps the invalid override rather than the stored budget,
    # and a fetch-tool read of either snapshot rejects it by name.
    refreshed = apply_environment_overrides_to_snapshot(queued_snapshot)
    assert refreshed[SUMMARY_KEY]["value"] == value
    for snapshot in (refreshed, queued_snapshot):
        with pytest.raises(ValueError, match=ENV_VAR):
            _read_summary_input_max_length_setting(snapshot)
    assert queued_snapshot[SUMMARY_KEY]["value"] == 500000


@pytest.mark.parametrize("value", [1, 600001, 2**53 + 1])
def test_summary_environment_budget_stays_exact_across_read_paths(
    monkeypatch, value
):
    from local_deep_research.settings.manager import (
        SettingsManager,
        apply_environment_overrides_to_snapshot,
    )

    monkeypatch.setenv("LDR_SEARCH_FETCH_SUMMARY_INPUT_MAX_LENGTH", str(value))
    manager = SettingsManager(db_session=None)
    assert manager.get_setting(SUMMARY_KEY) == value
    snapshot = manager.get_all_settings()
    assert snapshot[SUMMARY_KEY]["value"] == value
    assert _read_summary_input_max_length_setting(snapshot) == value
    queued_snapshot = {SUMMARY_KEY: {"value": 500000, "ui_element": "number"}}
    refreshed = apply_environment_overrides_to_snapshot(queued_snapshot)
    assert refreshed[SUMMARY_KEY]["value"] == value
    assert queued_snapshot[SUMMARY_KEY]["value"] == 500000


def test_summary_environment_validation_does_not_change_other_number_settings(
    monkeypatch,
):
    from local_deep_research.settings.manager import (
        SettingsManager,
        get_typed_setting_value,
    )
    from local_deep_research.web.routers.settings import (
        coerce_setting_for_write,
    )

    monkeypatch.setenv("LDR_SEARCH_FETCH_SUMMARY_INPUT_MAX_LENGTH", "invalid")
    monkeypatch.setenv("LDR_LLM_TEMPERATURE", "0.75")
    manager = SettingsManager(db_session=None)
    assert manager.get_setting("llm.temperature") == 0.75
    # Writes validate their submitted value, independent of read overrides.
    assert coerce_setting_for_write(SUMMARY_KEY, "600001", "number") == 600001
    assert (
        get_typed_setting_value(
            SUMMARY_KEY, "600001", "number", check_env=False
        )
        == 600001
    )


@pytest.mark.parametrize(
    "read_path",
    ["typed", "environment", "wrapped", "raw", "queued"],
)
def test_summary_budget_errors_do_not_retain_parser_context(
    monkeypatch, read_path
):
    from local_deep_research.settings.manager import (
        apply_environment_overrides_to_snapshot,
        get_typed_setting_value,
    )

    invalid_value = "invalid-private-parser-input"
    snapshot = {SUMMARY_KEY: {"value": invalid_value, "ui_element": "number"}}
    if read_path in ("environment", "queued"):
        monkeypatch.setenv(
            "LDR_SEARCH_FETCH_SUMMARY_INPUT_MAX_LENGTH", invalid_value
        )
        snapshot[SUMMARY_KEY]["value"] = 500000
    elif read_path == "raw":
        snapshot[SUMMARY_KEY] = invalid_value

    # Settings reads keep an unusable value instead of raising, so each
    # path ends at the fetch-time read, which is where the value fails.
    if read_path == "typed":
        kept = get_typed_setting_value(
            SUMMARY_KEY, invalid_value, "number", check_env=False
        )
        assert kept == invalid_value
        snapshot = {SUMMARY_KEY: kept}
    elif read_path == "queued":
        snapshot = apply_environment_overrides_to_snapshot(snapshot)

    with pytest.raises(ValueError, match="positive integer") as caught:
        _read_summary_input_max_length_setting(snapshot)

    assert invalid_value not in str(caught.value)
    assert caught.value.__context__ is None
    assert caught.value.__cause__ is None


@pytest.mark.parametrize(
    "value",
    ["invalid-private-parser-input", "1e10000000", float("inf"), float("nan")],
)
def test_positive_int_parser_errors_leave_no_context(value):
    """A caller that does not re-wrap must not inherit the raw value."""
    from local_deep_research.utilities.type_utils import to_positive_int

    with pytest.raises(ValueError, match="positive integer") as caught:
        to_positive_int(value)
    assert caught.value.__context__ is None
    assert caught.value.__cause__ is None


@pytest.mark.parametrize("value", ["invalid-private-budget", "12.5", "-7"])
def test_invalid_summary_environment_budget_fails_only_summary_fetches(
    monkeypatch, loguru_caplog, value
):
    """One bad override must not stop whole-settings loads or dispatch.

    The settings page, research start, benchmarks and MCP all load every
    setting. The override is kept rather than replaced by the stored
    budget, logged by name without its value, and rejected per fetch.
    """
    from local_deep_research.settings.manager import (
        SettingsManager,
        apply_environment_overrides_to_snapshot,
    )

    monkeypatch.setenv(ENV_VAR, value)
    monkeypatch.setenv("LDR_LLM_TEMPERATURE", "0.75")
    manager = SettingsManager(db_session=None)
    queued = {
        SUMMARY_KEY: {"value": 500000, "ui_element": "number"},
        "llm.temperature": {"value": 0.7, "ui_element": "range"},
    }
    with loguru_caplog.at_level("WARNING"):
        all_settings = manager.get_all_settings()
        snapshot = manager.get_settings_snapshot()
        single = manager.get_setting(SUMMARY_KEY)
        refreshed = apply_environment_overrides_to_snapshot(queued)

    assert all_settings[SUMMARY_KEY]["value"] == value
    assert all_settings[SUMMARY_KEY]["editable"] is False
    assert snapshot[SUMMARY_KEY] == value
    assert single == value
    assert refreshed[SUMMARY_KEY] == {
        "value": value,
        "ui_element": "number",
        "editable": False,
    }
    assert queued[SUMMARY_KEY]["value"] == 500000
    # The other overrides in the same loads still apply.
    assert snapshot["llm.temperature"] == 0.75
    assert refreshed["llm.temperature"]["value"] == 0.75
    warnings = [
        record.getMessage()
        for record in loguru_caplog.records
        if ENV_VAR in record.getMessage()
    ]
    assert warnings
    assert not any(value in message for message in warnings)

    for fetch_snapshot in (snapshot, refreshed):
        model = MagicMock()
        resolver = MagicMock()
        tool = build_fetch_tool(
            "summary_focus_query",
            SearchResultsCollector([]),
            model=model,
            overall_query="question",
            settings_snapshot=fetch_snapshot,
            library_resolver=resolver,
        )
        with patch(
            "local_deep_research.content_fetcher.ContentFetcher"
        ) as fetcher:
            output = tool.invoke(
                {"url": "https://example.com/page", "focus": "fact"}
            )
        assert ENV_VAR in output
        assert "must be a positive integer" in output
        assert value not in output
        fetcher.assert_not_called()
        resolver.assert_not_called()
        model.invoke.assert_not_called()

    # Full-text fetches do not read the summary budget.
    full = build_fetch_tool(
        "full",
        SearchResultsCollector([]),
        settings_snapshot=snapshot,
        library_resolver=lambda url: {"content": "A" * 20000, "url": url},
    )
    output = full.invoke({"url": "/library/document/large-doc"})
    assert output.split("\n\n", 1)[1] == "A" * 10000


def test_invalid_stored_summary_budget_is_kept_and_named(loguru_caplog):
    from local_deep_research.settings.manager import get_typed_setting_value

    with loguru_caplog.at_level("WARNING"):
        kept = get_typed_setting_value(
            SUMMARY_KEY,
            "invalid-private-budget",
            "number",
            default=500000,
            check_env=False,
        )
    # Never the default: that would silently replace the user's value.
    assert kept == "invalid-private-budget"
    assert f"Setting {SUMMARY_KEY} must be a positive integer" in (
        loguru_caplog.text
    )
    assert "invalid-private-budget" not in loguru_caplog.text


PAGE = "x" * 600000 + "TAIL"
# Every window setting is explicit, so no read falls through to a thread
# settings context.
LOCAL_DEFAULTS = {
    LOCAL_WINDOW_KEY: 20480,
    "llm.max_tokens": 30000,
    "llm.supports_max_tokens": True,
    "llm.context_window_unrestricted": True,
    "llm.context_window_size": 128000,
}
# compute_max_tokens keeps min(30000, 80% of 20480) = 16384 tokens of a
# 20480-token window for the reply. That leaves 4096 tokens, or 16384
# characters at four per token, for the whole prompt.
LOCAL_PROMPT_LIMIT = 16384
# A 32768-token window keeps 26214 tokens for the reply: 6554 tokens left.
RESTRICTED_32K = {
    "llm.context_window_unrestricted": False,
    "llm.context_window_size": 32768,
}
RESTRICTED_32K_PROMPT_LIMIT = 26216
# The 8192-token fallback window keeps 6553 tokens: 1639 tokens left.
FALLBACK_PROMPT_LIMIT = 6556


def _snapshot(provider, extra=None):
    """One snapshot for the build and the tool, as a research run has."""
    return {"llm.provider": provider, **LOCAL_DEFAULTS, **(extra or {})}


def _built_model(provider, build_snapshot):
    """Wrap a stand-in chat model the way ``get_llm`` wraps its build.

    The fetch tool receives the wrapper. The stand-in records the prompt.
    """
    from local_deep_research.config.llm_config import (
        wrap_llm_without_think_tags,
    )

    base = MagicMock()
    base.invoke.return_value = AIMessage(content="Extracted fact")
    model = wrap_llm_without_think_tags(
        base,
        provider=provider,
        settings_snapshot={
            "rate_limiting.llm_enabled": False,
            **build_snapshot,
        },
    )
    return model, base


def _summary_prompt(
    mode,
    model,
    tool_snapshot,
    page,
    source="library",
    *,
    title="Page",
    url=None,
    **tool_kwargs,
):
    """Run one summary fetch and return its prompt and the page text in it.

    *model* is a ``_built_model`` wrapper, or a bare ``MagicMock`` that
    stands for a model ``get_llm`` did not build.
    """
    from local_deep_research.config.llm_config import ProcessingLLMWrapper

    if isinstance(model, ProcessingLLMWrapper):
        recorder = model.base_llm
    else:
        recorder = model
        model.invoke.return_value.content = "Extracted fact"
    if url is None:
        url = (
            "https://example.com/page"
            if source == "http"
            else "/library/document/large-doc"
        )
    downloader = MagicMock()
    downloader.download_with_result.return_value.is_success = True
    downloader.download_with_result.return_value.content = page.encode()
    downloader.get_metadata.return_value = {"title": title}
    tool = build_fetch_tool(
        mode,
        SearchResultsCollector([]),
        model=model,
        overall_query="Research question",
        settings_snapshot=tool_snapshot,
        library_resolver=None
        if source == "http"
        else (lambda u: {"content": page, "title": title, "url": u}),
        **tool_kwargs,
    )
    with (
        patch(
            "local_deep_research.content_fetcher.fetcher.policy_aware_validate_url",
            return_value=True,
        ),
        patch(
            "local_deep_research.content_fetcher.fetcher.URLClassifier"
        ) as classifier,
        patch(
            "local_deep_research.content_fetcher.fetcher.ContentFetcher._get_downloader",
            return_value=downloader,
        ),
    ):
        classifier.classify.return_value = URLType.HTML
        classifier.get_source_name.return_value = "Web Page"
        output = tool.invoke({"url": url, "focus": "fact"})
    assert "Extracted fact" in output
    recorder.invoke.assert_called_once()
    prompt = recorder.invoke.call_args.args[0]
    page_text = prompt.split("Page content:\n", 1)[1].split(
        "\n\nOutput rules:", 1
    )[0]
    return prompt, page_text


def _assert_prompt_fills_limit(prompt, page_text, prompt_limit):
    assert len(prompt) == prompt_limit
    assert page_text == PAGE[: len(page_text)]
    # The prompt's own text is under 1500 characters here.
    assert len(page_text) > prompt_limit - 1500


@pytest.mark.parametrize("mode", ["summary_focus", "summary_focus_query"])
@pytest.mark.parametrize(
    ("provider", "extra", "prompt_limit"),
    [
        # Ollama is sent the local window as num_ctx.
        ("ollama", None, LOCAL_PROMPT_LIMIT),
        # LM Studio and llama.cpp cap max_tokens with the same window.
        ("lmstudio", None, LOCAL_PROMPT_LIMIT),
        ("llamacpp", None, LOCAL_PROMPT_LIMIT),
        # Compatible endpoints with unrestricted context may be local or
        # private, so they assume the local window, not the cloud budget.
        (
            "openai_endpoint",
            {"llm.openai_endpoint.url": "http://localhost:8000/v1"},
            LOCAL_PROMPT_LIMIT,
        ),
        (
            "anthropic_endpoint",
            {"llm.anthropic_endpoint.url": "http://10.0.0.5:8080"},
            LOCAL_PROMPT_LIMIT,
        ),
        # So does a provider LDR does not ship, such as a registered model.
        ("my_registered_llm", None, LOCAL_PROMPT_LIMIT),
        # A declared window is the one the build capped max_tokens with.
        ("openai", RESTRICTED_32K, RESTRICTED_32K_PROMPT_LIMIT),
        ("openai_endpoint", RESTRICTED_32K, RESTRICTED_32K_PROMPT_LIMIT),
        # Unrestricted cloud context keeps the configured budget.
        ("openai", None, None),
        ("anthropic", None, None),
    ],
)
def test_summary_prompt_fits_each_provider_class_window(
    mode, provider, extra, prompt_limit
):
    snapshot = _snapshot(provider, extra)
    model, _ = _built_model(provider, snapshot)
    prompt, page_text = _summary_prompt(mode, model, snapshot, PAGE)
    if prompt_limit is None:
        assert page_text == PAGE[:500000]
    else:
        _assert_prompt_fills_limit(prompt, page_text, prompt_limit)


@pytest.mark.parametrize("mode", ["summary_focus", "summary_focus_query"])
def test_http_summary_prompt_fits_default_local_context_window(mode):
    snapshot = _snapshot("ollama")
    model, _ = _built_model("ollama", snapshot)
    prompt, page_text = _summary_prompt(mode, model, snapshot, PAGE, "http")
    _assert_prompt_fills_limit(prompt, page_text, LOCAL_PROMPT_LIMIT)


@pytest.mark.parametrize(
    ("built_provider", "build_extra", "tool_snapshot", "prompt_limit"),
    [
        # A run that overrides a saved cloud provider with Ollama.
        ("ollama", None, _snapshot("openai"), LOCAL_PROMPT_LIMIT),
        # A run that overrides a saved local provider with a cloud one.
        (
            "openai",
            None,
            _snapshot("ollama", {LOCAL_WINDOW_KEY: 2048}),
            None,
        ),
        # The window is the build's, not the tool snapshot's.
        (
            "ollama",
            {LOCAL_WINDOW_KEY: 8192},
            _snapshot("ollama", {LOCAL_WINDOW_KEY: 131072}),
            FALLBACK_PROMPT_LIMIT,
        ),
    ],
)
def test_summary_prompt_limit_follows_the_model_actually_built(
    built_provider, build_extra, tool_snapshot, prompt_limit
):
    model, _ = _built_model(
        built_provider, _snapshot(built_provider, build_extra)
    )
    prompt, page_text = _summary_prompt(
        "summary_focus", model, tool_snapshot, PAGE
    )
    if prompt_limit is None:
        assert page_text == PAGE[:500000]
    else:
        _assert_prompt_fills_limit(prompt, page_text, prompt_limit)


@pytest.mark.parametrize(
    ("built_provider", "extra", "prompt_limit"),
    [
        # A model get_llm did not build has no known provider, even when
        # the tool snapshot names an unrestricted cloud one.
        (None, None, LOCAL_PROMPT_LIMIT),
        # Non-positive legacy windows fall back to 8192 tokens. Malformed
        # managed rows retain main's explicit SettingsValueError instead.
        ("ollama", {LOCAL_WINDOW_KEY: 0}, FALLBACK_PROMPT_LIMIT),
        ("openai_endpoint", {LOCAL_WINDOW_KEY: -1}, FALLBACK_PROMPT_LIMIT),
    ],
)
def test_unknown_or_unusable_windows_use_the_local_window(
    built_provider, extra, prompt_limit
):
    snapshot = _snapshot(built_provider or "openai", extra)
    if built_provider is None:
        model = MagicMock()
    else:
        model, _ = _built_model(built_provider, snapshot)
    prompt, page_text = _summary_prompt(
        "summary_focus_query", model, snapshot, PAGE
    )
    _assert_prompt_fills_limit(prompt, page_text, prompt_limit)


@pytest.mark.parametrize(
    ("provider", "key", "extra"),
    [
        (
            "openai",
            "llm.context_window_size",
            {"llm.context_window_unrestricted": False},
        ),
        ("ollama", "llm.local_context_window_size", {}),
        ("ollama", "llm.max_tokens", {}),
    ],
)
def test_invalid_managed_model_budget_does_not_fetch_with_a_fallback(
    provider, key, extra
):
    private_value = "private-invalid-config-value"
    snapshot = _snapshot(
        provider,
        {**extra, key: {"value": private_value, "ui_element": "number"}},
    )
    model, recorder = _built_model(provider, snapshot)
    resolver = MagicMock()
    collector = SearchResultsCollector([])
    fetch = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
        settings_snapshot=snapshot,
        library_resolver=resolver,
    )

    message = fetch.invoke(
        {"url": "/library/document/large-doc", "focus": "fact"}
    )

    assert "Invalid setting" in message
    assert key in message
    assert private_value not in message
    resolver.assert_not_called()
    recorder.invoke.assert_not_called()
    assert collector.fetch_count == 0
    assert collector.results == []


def test_summary_budget_keeps_unsupported_managed_type_strict():
    from local_deep_research.settings.exceptions import SettingsValueError
    from local_deep_research.settings.manager import get_typed_setting_value

    with pytest.raises(SettingsValueError, match="unsupported setting type"):
        get_typed_setting_value(
            SUMMARY_KEY, 500000, "unsupported", check_env=False
        )


@pytest.mark.parametrize(
    ("extra", "prompt_limit"),
    [
        # A smaller llm.max_tokens leaves 20480 - 1000 tokens.
        ({"llm.max_tokens": 1000}, 77920),
        # Without a positive max_tokens, the full 80% share is kept.
        ({"llm.supports_max_tokens": False}, LOCAL_PROMPT_LIMIT),
        ({"llm.max_tokens": 0}, LOCAL_PROMPT_LIMIT),
    ],
)
def test_reply_allowance_follows_compute_max_tokens(extra, prompt_limit):
    snapshot = _snapshot("ollama", extra)
    model, _ = _built_model("ollama", snapshot)
    prompt, page_text = _summary_prompt("summary_focus", model, snapshot, PAGE)
    _assert_prompt_fills_limit(prompt, page_text, prompt_limit)


def test_summary_prompt_limit_counts_the_title_and_url():
    snapshot = _snapshot("ollama")
    model, _ = _built_model("ollama", snapshot)
    _, short_page = _summary_prompt("summary_focus", model, snapshot, PAGE)
    title = "T" * 3000
    url = "/library/document/" + "d" * 3000
    model, _ = _built_model("ollama", snapshot)
    prompt, page_text = _summary_prompt(
        "summary_focus", model, snapshot, PAGE, title=title, url=url
    )
    assert len(prompt) == LOCAL_PROMPT_LIMIT
    assert len(short_page) - len(page_text) == (len(title) - len("Page")) + (
        len(url) - len("/library/document/large-doc")
    )


def test_window_too_small_for_the_prompt_keeps_one_page_character():
    # 512 tokens keep 409 for the reply. The 412 characters left are fewer
    # than the prompt's own text.
    snapshot = _snapshot("ollama", {LOCAL_WINDOW_KEY: 512})
    model, _ = _built_model("ollama", snapshot)
    _, page_text = _summary_prompt("summary_focus", model, snapshot, PAGE)
    assert page_text == PAGE[:1]


def test_explicit_summary_budget_ignores_context_window():
    page = "A" * 50000
    snapshot = _snapshot("ollama", {LOCAL_WINDOW_KEY: 512})
    model, _ = _built_model("ollama", snapshot)
    _, page_text = _summary_prompt(
        "summary_focus", model, snapshot, page, content_max_length=5000
    )
    assert page_text == page[:5000]


def test_invalid_summary_budget_fails_before_context_window_lookup():
    model = MagicMock()
    resolver = MagicMock()
    tool = build_fetch_tool(
        "summary_focus_query",
        SearchResultsCollector([]),
        model=model,
        overall_query="question",
        settings_snapshot={
            "llm.provider": "ollama",
            LOCAL_WINDOW_KEY: 20480,
            SUMMARY_KEY: "invalid",
        },
        library_resolver=resolver,
    )
    with (
        patch(
            "local_deep_research.llm.providers._helpers.get_context_window_for_provider"
        ) as window,
        patch("local_deep_research.content_fetcher.ContentFetcher") as fetcher,
    ):
        output = tool.invoke(
            {"url": "https://example.com/page", "focus": "fact"}
        )
    assert ENV_VAR in output
    assert "must be a positive integer" in output
    window.assert_not_called()
    fetcher.assert_not_called()
    resolver.assert_not_called()
    model.invoke.assert_not_called()


def test_summary_budget_metadata_allows_values_outside_old_bounds():
    from local_deep_research.settings.manager import (
        SettingsManager,
        validate_imported_setting_value,
    )

    meta = SettingsManager(db_session=None).default_settings[SUMMARY_KEY]
    assert meta["value"] == 500000
    assert meta["min_value"] == 1
    assert meta["max_value"] is None
    assert meta["step"] == 1
    for value in (1, 1001, 600001):
        assert validate_imported_setting_value(SUMMARY_KEY, value, meta) is None
    for value in (0, -1, 1.5, None, "", True, "1e10000000"):
        assert (
            validate_imported_setting_value(SUMMARY_KEY, value, meta)
            is not None
        )


@pytest.mark.parametrize(
    "module_name",
    [
        "settings.manager",
        "web.routers.settings",
        "web.services.settings_service",
    ],
)
def test_summary_budget_validation_errors_hide_parser_details(
    monkeypatch, module_name
):
    from local_deep_research.database.models import Setting

    module = import_module(f"local_deep_research.{module_name}")

    def fail_conversion(value):
        raise ValueError("internal-parser-diagnostic")

    monkeypatch.setattr(module, "to_positive_int", fail_conversion)
    if module_name == "settings.manager":
        error = module.validate_imported_setting_value(SUMMARY_KEY, 600001, {})
    else:
        valid, error = module.validate_setting(
            Setting(key=SUMMARY_KEY, ui_element="number"), 600001
        )
        assert valid is False
    assert error == "Value must be a positive integer"


@pytest.mark.parametrize("value", [0, -1, 1.5, None, "", True, "1e10000000"])
def test_summary_budget_save_validators_reject_unusable_values(value):
    from local_deep_research.database.models import Setting
    from local_deep_research.web.routers.settings import (
        coerce_setting_for_write,
        validate_setting as validate_router_setting,
    )
    from local_deep_research.web.services.settings_service import (
        validate_setting,
    )

    setting = Setting(
        key=SUMMARY_KEY,
        ui_element="number",
        min_value=1,
        max_value=None,
        step=1,
    )
    coerced = coerce_setting_for_write(SUMMARY_KEY, value, "number")
    for validate in (validate_setting, validate_router_setting):
        valid, error = validate(setting, coerced)
        assert valid is False
        assert "positive integer" in error


@pytest.mark.parametrize("value", [1, 1001, 600001, "600001.0"])
def test_summary_budget_save_validators_accept_valid_budgets(value):
    from local_deep_research.database.models import Setting
    from local_deep_research.web.routers.settings import (
        coerce_setting_for_write,
        validate_setting as validate_router_setting,
    )
    from local_deep_research.web.services.settings_service import (
        validate_setting,
    )

    setting = Setting(
        key=SUMMARY_KEY,
        ui_element="number",
        min_value=1,
        max_value=None,
        step=1,
    )
    coerced = coerce_setting_for_write(SUMMARY_KEY, value, "number")
    for validate in (validate_setting, validate_router_setting):
        assert validate(setting, coerced) == (True, None)
    assert (
        _read_summary_input_max_length_setting(
            {SUMMARY_KEY: {"value": coerced, "ui_element": "number"}}
        )
        == coerced
    )