~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Fetch limits must preserve managed settings errors before doing work."""
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock
import pytest
from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
SearchResultsCollector,
)
from local_deep_research.advanced_search_system.tools.fetch import (
CONTENT_MAX_LENGTH,
_read_content_max_length_setting,
build_fetch_tool,
)
from local_deep_research.settings.exceptions import SettingsValueError
KEY = "search.fetch.content_max_length"
ENVIRONMENT_KEY = "LDR_SEARCH_FETCH_CONTENT_MAX_LENGTH"
SUMMARY_KEY = "search.fetch.summary_input_max_length"
SUMMARY_ENVIRONMENT_KEY = "LDR_SEARCH_FETCH_SUMMARY_INPUT_MAX_LENGTH"
INVALID_VALUE = "invalid-private-number"
def _invalid_snapshot(monkeypatch, source, key=KEY):
monkeypatch.delenv(ENVIRONMENT_KEY, raising=False)
monkeypatch.delenv(SUMMARY_ENVIRONMENT_KEY, raising=False)
environment_key = ENVIRONMENT_KEY if key == KEY else SUMMARY_ENVIRONMENT_KEY
value = INVALID_VALUE if source == "value" else 12000
if source == "environment":
monkeypatch.setenv(environment_key, INVALID_VALUE)
return {key: {"value": value, "ui_element": "number"}}
@pytest.mark.parametrize("source", ["value", "environment"])
def test_invalid_managed_length_is_not_replaced_by_default(monkeypatch, source):
snapshot = _invalid_snapshot(monkeypatch, source)
with pytest.raises(SettingsValueError) as raised:
_read_content_max_length_setting(snapshot)
assert raised.value.key == KEY
assert raised.value.source == source
assert INVALID_VALUE not in str(raised.value)
assert raised.value.__cause__ is None
assert raised.value.__context__ is None
def test_unsupported_length_type_is_not_replaced_by_default(monkeypatch):
monkeypatch.delenv(ENVIRONMENT_KEY, raising=False)
with pytest.raises(SettingsValueError) as raised:
_read_content_max_length_setting(
{KEY: {"value": 12000, "ui_element": "unsupported-widget"}}
)
assert raised.value.key == KEY
def _tool_case(monkeypatch, snapshot, mode, url):
payload = {
"status": "success",
"title": "Example",
"content": "Example content",
"url": url,
}
fetcher = MagicMock()
fetcher.return_value.__enter__.return_value.fetch.return_value = payload
monkeypatch.setattr(
"local_deep_research.content_fetcher.ContentFetcher", fetcher
)
collector = SearchResultsCollector([])
monkeypatch.setattr(
collector, "try_record_fetch", Mock(wraps=collector.try_record_fetch)
)
model = Mock()
model.invoke.return_value = SimpleNamespace(content="Relevant excerpt")
resolver = Mock(
side_effect=lambda target: (
payload if target.startswith("/library/document/") else None
)
)
tool = build_fetch_tool(
mode,
collector,
settings_snapshot=snapshot,
model=model,
overall_query="research topic",
library_resolver=resolver,
)
arguments = {"url": url}
if mode != "full":
arguments["focus"] = "research topic"
return SimpleNamespace(
tool=tool,
arguments=arguments,
fetcher=fetcher,
collector=collector,
model=model,
resolver=resolver,
)
@pytest.mark.parametrize("source", ["value", "environment"])
@pytest.mark.parametrize(
"mode", ["full", "summary_focus", "summary_focus_query"]
)
@pytest.mark.parametrize(
"url", ["https://example.com/paper", "/library/document/example"]
)
def test_invalid_length_stops_fetch_without_consuming_budget(
monkeypatch, source, mode, url
):
key = KEY if mode == "full" else SUMMARY_KEY
snapshot = _invalid_snapshot(monkeypatch, source, key)
case = _tool_case(monkeypatch, snapshot, mode, url)
result = case.tool.invoke(case.arguments)
assert result.startswith("Error fetching ")
assert key in result
assert INVALID_VALUE not in result
case.fetcher.assert_not_called()
case.resolver.assert_not_called()
case.collector.try_record_fetch.assert_not_called()
assert case.collector.get_fetch_count() == 0
case.model.invoke.assert_not_called()
@pytest.mark.parametrize(
"mode", ["full", "summary_focus", "summary_focus_query"]
)
@pytest.mark.parametrize(
"url", ["https://example.com/paper", "/library/document/example"]
)
def test_valid_length_allows_fetch_and_preserves_budget_accounting(
monkeypatch, mode, url
):
monkeypatch.delenv(ENVIRONMENT_KEY, raising=False)
monkeypatch.delenv(SUMMARY_ENVIRONMENT_KEY, raising=False)
snapshot = {
KEY: {"value": 12000, "ui_element": "number"},
SUMMARY_KEY: {"value": 4000, "ui_element": "number"},
# Leave room for the summary input plus instructions and response.
"llm.local_context_window_size": 32768,
}
case = _tool_case(monkeypatch, snapshot, mode, url)
_assert_successful_fetch(case, mode, url)
@pytest.mark.parametrize("source", ["value", "environment"])
@pytest.mark.parametrize(
"mode", ["full", "summary_focus", "summary_focus_query"]
)
@pytest.mark.parametrize(
"url", ["https://example.com/paper", "/library/document/example"]
)
def test_direct_tool_snapshot_does_not_read_inactive_length(
monkeypatch, source, mode, url
):
"""Direct tools read their own budget from a supplied partial snapshot.
This does not change strict validation when loading complete managed
settings; an invalid typed full-text setting still fails that load.
"""
active_key = KEY if mode == "full" else SUMMARY_KEY
inactive_key = SUMMARY_KEY if mode == "full" else KEY
snapshot = _invalid_snapshot(monkeypatch, source, inactive_key)
snapshot[active_key] = {
"value": 12000 if mode == "full" else 4000,
"ui_element": "number",
}
snapshot["llm.local_context_window_size"] = 32768
case = _tool_case(monkeypatch, snapshot, mode, url)
_assert_successful_fetch(case, mode, url)
def _assert_successful_fetch(case, mode, url):
result = case.tool.invoke(case.arguments)
assert result.startswith("[1] Title: Example")
expected_content = (
"Example content" if mode == "full" else "Relevant excerpt"
)
assert expected_content in result
case.resolver.assert_called_once_with(url)
if url.startswith("https://"):
case.fetcher.return_value.__enter__.return_value.fetch.assert_called_once_with(
url, max_length=12000 if mode == "full" else 4000
)
assert case.collector.get_fetch_count() == 1
else:
case.fetcher.assert_not_called()
assert case.collector.get_fetch_count() == 0
assert case.model.invoke.call_count == (0 if mode == "full" else 1)
@pytest.mark.parametrize("override,expected", [(None, 12000), ("15000", 15000)])
def test_valid_managed_length_retains_environment_precedence(
monkeypatch, override, expected
):
monkeypatch.delenv(ENVIRONMENT_KEY, raising=False)
if override is not None:
monkeypatch.setenv(ENVIRONMENT_KEY, override)
assert (
_read_content_max_length_setting(
{KEY: {"value": 12000, "ui_element": "number"}}
)
== expected
)
@pytest.mark.parametrize("snapshot", [None, {}, {KEY: "invalid"}, {KEY: -5}])
def test_missing_or_untyped_legacy_values_keep_fallback(monkeypatch, snapshot):
monkeypatch.delenv(ENVIRONMENT_KEY, raising=False)
assert _read_content_max_length_setting(snapshot) == CONTENT_MAX_LENGTH