~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
refactor(fetch): read fetch limits through the settings accessor (#7337)
Chanhyo Jung150fb0f80791
diff --git a/src/local_deep_research/advanced_search_system/tools/fetch/__init__.py b/src/local_deep_research/advanced_search_system/tools/fetch/__init__.pyindex 969b08bea9..ca9b6a02f2 100644--- a/src/local_deep_research/advanced_search_system/tools/fetch/__init__.py+++ b/src/local_deep_research/advanced_search_system/tools/fetch/__init__.py@@ -87,10 +87,45 @@ def _read_max_fetches_setting(Returns the configured non-negative integer limit, or default if missing/invalid.Negative values fall back to default, and values above 50 are clamped to 50.0 means unlimited fetches.++ Typed number rows use the canonical accessor's float conversion (fractional+ strings truncate) and environment priority. A non-empty+ ``LDR_SEARCH_FETCH_MAX_PER_TOPIC`` overrides such a row; an unparseable+ environment value falls back to the snapshot. A negative or non-finite+ resulting limit falls back to the caller's default.++ Raw values and sparse legacy rows retain snapshot-only int() coercion.+ An absent exact key keeps the caller's default even with an environment or+ thread-local value. Explicit ``max_fetches`` bypasses this fallback reader."""- if not settings_snapshot:+ key = "search.fetch.max_per_topic"+ # Missing snapshots keep the caller's default, not a thread-local value.+ if not settings_snapshot or key not in settings_snapshot:+ return default+ from local_deep_research.config.thread_settings import (+ get_setting_from_snapshot,+ )++ lookup_snapshot = settings_snapshot+ entry = settings_snapshot[key]+ if (+ isinstance(entry, dict)+ and "value" in entry+ and "ui_element" not in entry+ ):+ # Sparse legacy rows have no UI type. Preserve their int() coercion+ # instead of letting the accessor infer text for floats/booleans.+ raw = unwrap_setting(entry)+ if isinstance(raw, dict):+ return default+ lookup_snapshot = {key: raw}++ try:+ val = get_setting_from_snapshot(+ key, default=None, settings_snapshot=lookup_snapshot+ )+ except Exception:return default- val = settings_snapshot.get("search.fetch.max_per_topic")if val is None:return defaultunwrapped = unwrap_setting(val)@@ -99,7 +134,7 @@ def _read_max_fetches_setting(if limit < 0:return defaultreturn min(50, limit)- except (TypeError, ValueError):+ except (TypeError, ValueError, OverflowError):return defaultdiff --git a/tests/advanced_search_system/tools/test_fetch_modes.py b/tests/advanced_search_system/tools/test_fetch_modes.pyindex 559ceb5fdc..0450fb68ae 100644--- a/tests/advanced_search_system/tools/test_fetch_modes.py+++ b/tests/advanced_search_system/tools/test_fetch_modes.py@@ -11,12 +11,17 @@ from unittest.mock import MagicMock, patchimport pytest+from local_deep_research.config import thread_settings+from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (SearchResultsCollector,)from local_deep_research.utilities.search_utilities import (count_distinct_sources,)+from local_deep_research.advanced_search_system.tools import (+ fetch as fetch_module,+)from local_deep_research.advanced_search_system.tools.fetch import (FETCH_MODES,build_fetch_tool,@@ -1713,3 +1718,274 @@ def test_full_mode_fetch_puts_the_page_title_in_the_bibliography():assert "comprehensive guide" not in rendered# Recorded alongside: the stored citation title is untouched.assert all_links[0]["title"] == listing_title+++ "value",+ [1, {"value": 1}, {"value": 1, "ui_element": "number"}],+ ids=["raw", "legacy-wrapped", "typed-wrapped"],+)+def test_max_fetches_reader_uses_settings_accessor(value):+ snapshot = {"search.fetch.max_per_topic": value}+ original = {+ "search.fetch.max_per_topic": dict(value)+ if isinstance(value, dict)+ else value+ }+ with patch.object(+ thread_settings,+ "get_setting_from_snapshot",+ wraps=thread_settings.get_setting_from_snapshot,+ ) as accessor:+ assert fetch_module._read_max_fetches_setting(snapshot) == 1+ assert snapshot == original+ expected_lookup = (+ snapshot+ if isinstance(value, dict) and "ui_element" in value+ else {"search.fetch.max_per_topic": 1}+ )+ accessor.assert_called_once_with(+ "search.fetch.max_per_topic",+ default=None,+ settings_snapshot=expected_lookup,+ )++[email protected](autouse=True)+def _clear_max_fetches_environment(monkeypatch):+ monkeypatch.delenv("LDR_SEARCH_FETCH_MAX_PER_TOPIC", raising=False)++[email protected]("shape", ["raw", "legacy", "typed"])+ "value, legacy_expected, typed_expected",+ [+ (None, 9, 9),+ (0, 0, 0),+ ("3", 3, 3),+ (50, 50, 50),+ (51, 50, 50),+ (-1, 9, 9),+ ("invalid", 9, 9),+ (3.9, 3, 3),+ (False, 0, 0),+ (True, 1, 1),+ ("3.9", 9, 3),+ ("inf", 9, 9),+ ("-inf", 9, 9),+ ("nan", 9, 9),+ ("1e400", 9, 9),+ pytest.param("9" * 5000, 9, 9, id="very-long-number"),+ (float("inf"), 9, 9),+ (float("-inf"), 9, 9),+ (float("nan"), 9, 9),+ ],+)+def test_max_fetches_reader_preserves_snapshot_limits_and_default(+ shape, value, legacy_expected, typed_expected+):+ entry = value+ if shape == "legacy":+ entry = {"value": value}+ elif shape == "typed":+ entry = {"value": value, "ui_element": "number"}+ snapshot = {"search.fetch.max_per_topic": entry}+ original = {+ "search.fetch.max_per_topic": dict(entry)+ if isinstance(entry, dict)+ else entry+ }+ expected = typed_expected if shape == "typed" else legacy_expected+ assert (+ fetch_module._read_max_fetches_setting(snapshot, default=9) == expected+ )+ assert snapshot == original++[email protected]("shape", ["raw", "legacy", "typed"])+ "env_value, typed_expected",+ [+ ("7", 7),+ ("3.9", 3),+ ("0", 0),+ ("51", 50),+ ("-1", 9),+ ("invalid", 1),+ ("", 1),+ ("inf", 9),+ ("1e400", 9),+ ("nan", 9),+ pytest.param("9" * 5000, 9, id="very-long-env-number"),+ ],+)+def test_max_fetches_reader_pins_environment_precedence_by_snapshot_shape(+ shape, env_value, typed_expected, monkeypatch+):+ monkeypatch.setenv("LDR_SEARCH_FETCH_MAX_PER_TOPIC", env_value)+ entry = 1+ if shape == "legacy":+ entry = {"value": 1}+ elif shape == "typed":+ entry = {"value": 1, "ui_element": "number"}+ snapshot = {"search.fetch.max_per_topic": entry}+ # The canonical typed accessor applies environment priority; raw and+ # sparse legacy snapshots deliberately retain their historical behavior.+ expected = typed_expected if shape == "typed" else 1+ assert (+ fetch_module._read_max_fetches_setting(snapshot, default=9) == expected+ )+ assert snapshot == {"search.fetch.max_per_topic": entry}++[email protected]("env_value, expected", [("", 9), ("7", 7)])+def test_max_fetches_reader_typed_null_uses_canonical_environment_priority(+ env_value, expected, monkeypatch+):+ monkeypatch.setenv("LDR_SEARCH_FETCH_MAX_PER_TOPIC", env_value)+ snapshot = {+ "search.fetch.max_per_topic": {"value": None, "ui_element": "number"}+ }+ assert (+ fetch_module._read_max_fetches_setting(snapshot, default=9) == expected+ )+++ "entry",+ [+ {"value": 5, "ui_element": "bogus"},+ {"value": {"unexpected": 5}},+ {"ui_element": "number"},+ ],+)+def test_max_fetches_reader_invalid_rows_keep_caller_default(entry):+ assert (+ fetch_module._read_max_fetches_setting(+ {"search.fetch.max_per_topic": entry}, default=9+ )+ == 9+ )+++def test_max_fetches_reader_accessor_failure_keeps_caller_default():+ with patch.object(+ thread_settings,+ "get_setting_from_snapshot",+ side_effect=RuntimeError("Accessor failed"),+ ):+ assert (+ fetch_module._read_max_fetches_setting(+ {"search.fetch.max_per_topic": 1}, default=9+ )+ == 9+ )++[email protected]("snapshot", [None, {}, {"unrelated.setting": 1}])+def test_missing_max_fetches_snapshot_keeps_default_over_thread_context(+ snapshot, monkeypatch+):+ monkeypatch.setenv("LDR_SEARCH_FETCH_MAX_PER_TOPIC", "7")+ context = MagicMock()+ context.username = None+ context.get_setting.return_value = 7+ monkeypatch.setattr(+ thread_settings._thread_local,+ "settings_context",+ context,+ raising=False,+ )+ assert fetch_module._read_max_fetches_setting(snapshot, default=9) == 9+ context.get_setting.assert_not_called()+++ "mode", ["full", "summary_focus", "summary_focus_query"]+)+ "setting, override, attempts, expected",+ [+ (1, None, 3, 1),+ ({"value": 1}, None, 3, 1),+ (0, None, 3, 3),+ ({"value": 0}, None, 3, 3),+ (False, None, 3, 3),+ ({"value": False}, None, 3, 3),+ (51, None, 51, 50),+ ({"value": 51}, None, 51, 50),+ (-1, None, 3, 2),+ ({"value": -1}, None, 3, 2),+ (1, 0, 3, 3),+ ({"value": 0}, 2, 3, 2),+ ({"value": 1, "ui_element": "number"}, None, 3, 1),+ ({"value": 0, "ui_element": "number"}, None, 3, 3),+ ({"value": 51, "ui_element": "number"}, None, 51, 50),+ ({"value": "inf", "ui_element": "number"}, None, 3, 2),+ ({"value": 1, "ui_element": "number"}, 0, 3, 3),+ ],+)+def test_fetch_tools_apply_snapshot_limits_and_explicit_overrides(+ mode, setting, override, attempts, expected+):+ snapshot = {"search.fetch.max_per_topic": setting}+ collector = SearchResultsCollector([])+ tool = build_fetch_tool(+ mode,+ collector,+ model=_model_returning("Useful summary"),+ overall_query="Research question",+ settings_snapshot=snapshot,+ max_fetches=override,+ )+ assert tool is not None+ cm = _fetcher_cm()+ with patch(+ "local_deep_research.content_fetcher.ContentFetcher", return_value=cm+ ):+ for index in range(attempts):+ arguments = {"url": f"http://example.com/budget/{index}"}+ if mode != "full":+ arguments["focus"] = "Relevant evidence"+ tool.invoke(arguments)+ assert cm.__enter__.return_value.fetch.call_count == expected+++ "mode", ["full", "summary_focus", "summary_focus_query"]+)+ "env_value, override, attempts, expected",+ [+ ("7", None, 8, 7),+ ("inf", None, 3, 2),+ ("0", None, 3, 3),+ ("inf", 0, 3, 3),+ ("7", 2, 3, 2),+ ],+)+def test_fetch_tools_apply_typed_environment_limits_and_explicit_overrides(+ mode, env_value, override, attempts, expected, monkeypatch+):+ monkeypatch.setenv("LDR_SEARCH_FETCH_MAX_PER_TOPIC", env_value)+ tool = build_fetch_tool(+ mode,+ SearchResultsCollector([]),+ model=_model_returning("Useful summary"),+ overall_query="Research question",+ settings_snapshot={+ "search.fetch.max_per_topic": {"value": 1, "ui_element": "number"}+ },+ max_fetches=override,+ )+ assert tool is not None+ cm = _fetcher_cm()+ with patch(+ "local_deep_research.content_fetcher.ContentFetcher", return_value=cm+ ):+ for index in range(attempts):+ arguments = {"url": f"http://example.com/env-budget/{index}"}+ if mode != "full":+ arguments["focus"] = "Relevant evidence"+ tool.invoke(arguments)+ assert cm.__enter__.return_value.fetch.call_count == expected