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.

69.9 KB
Raw Blame
"""Unit tests for ``advanced_search_system.tools.fetch.build_fetch_tool``.

Pins the mode dispatch and the prompt-content contract for the two
summary variants (focus-only vs focus + overall query). Avoids any real
HTTP — patches ``ContentFetcher`` and the model.
"""

from __future__ import annotations

from unittest.mock import MagicMock, patch

import 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,
)
from local_deep_research.settings.exceptions import SettingsValueError


def _fetcher_cm(*, status="success", title="Page", content="Body text"):
    fetcher = MagicMock()
    fetcher.fetch.return_value = {
        "status": status,
        "title": title,
        "content": content,
    }
    cm = MagicMock()
    cm.__enter__.return_value = fetcher
    cm.__exit__.return_value = False
    return cm


def _model_returning(text: str):
    """A model whose ``invoke`` returns an object with ``.content == text``."""
    msg = MagicMock()
    msg.content = text
    model = MagicMock()
    model.invoke.return_value = msg
    return model


def test_fetch_modes_constant_lists_all_supported_values():
    assert FETCH_MODES == (
        "disabled",
        "full",
        "summary_focus",
        "summary_focus_query",
    )


def test_disabled_mode_returns_none():
    assert build_fetch_tool("disabled", SearchResultsCollector([])) is None


def test_full_mode_returns_tool_with_url_only_signature():
    tool = build_fetch_tool("full", SearchResultsCollector([]))
    assert tool is not None
    assert "url" in tool.args
    assert "focus" not in tool.args


def test_summary_focus_requires_model():
    with pytest.raises(ValueError, match="summary_focus"):
        build_fetch_tool("summary_focus", SearchResultsCollector([]))


def test_summary_focus_query_requires_model():
    with pytest.raises(ValueError, match="summary_focus_query"):
        build_fetch_tool("summary_focus_query", SearchResultsCollector([]))


def test_summary_focus_tool_calls_model_with_focus_only_prompt():
    collector = SearchResultsCollector([])
    model = _model_returning("relevant fact 1")
    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
        overall_query="should not appear",
    )
    assert tool is not None
    assert "focus" in tool.args

    cm = _fetcher_cm(title="T", content="page body")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "http://example.com/", "focus": "year of X"})

    model.invoke.assert_called_once()
    prompt = model.invoke.call_args[0][0]
    # focus-only mode must NOT mention the overall query, even if one was passed
    assert "Why this page was fetched: year of X" in prompt
    assert "Overall research question" not in prompt
    # Output is prefixed with citation index so the agent can cite consistently
    assert out.startswith("[1] ")
    assert "relevant fact 1" in out


def test_summary_focus_query_includes_overall_query_in_prompt():
    collector = SearchResultsCollector([])
    model = _model_returning("relevant fact 2")
    tool = build_fetch_tool(
        "summary_focus_query",
        collector,
        model=model,
        overall_query="When did Liepmann receive the Prandtl-Ring Award?",
    )
    cm = _fetcher_cm(title="T", content="page body")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        tool.invoke({"url": "http://example.com/", "focus": "year"})

    prompt = model.invoke.call_args[0][0]
    assert (
        "Overall research question: When did Liepmann receive the Prandtl-Ring Award?"
        in prompt
    )
    assert "Why this page was fetched: year" in prompt


def test_summary_focus_query_with_empty_overall_query_falls_back_to_focus_only():
    """An empty overall_query should not produce a stale 'Overall research question:' line."""
    collector = SearchResultsCollector([])
    model = _model_returning("ok")
    tool = build_fetch_tool(
        "summary_focus_query",
        collector,
        model=model,
        overall_query="",  # empty
    )
    cm = _fetcher_cm()
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        tool.invoke({"url": "http://example.com/", "focus": "x"})

    prompt = model.invoke.call_args[0][0]
    assert "Overall research question" not in prompt


def test_unknown_mode_raises_with_valid_modes_listed():
    with pytest.raises(ValueError, match="Unknown fetch mode"):
        build_fetch_tool("magic", SearchResultsCollector([]))


# ---- issue #3826: web.enable_javascript_rendering plumbing ----


def _captured_content_fetcher_kwargs(invoke_target):
    """Run *invoke_target* with ContentFetcher patched and return its
    constructor kwargs from the call.

    The patched factory returns a context manager whose ``fetch`` succeeds
    so the tool body runs end-to-end up to the call we care about.
    """
    cm = _fetcher_cm()
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ) as factory:
        invoke_target()
    assert factory.call_args is not None
    return factory.call_args.kwargs


def test_full_mode_passes_js_off_when_snapshot_disables_it():
    """Full-mode fetch tool must pass enable_js_rendering=False to
    ContentFetcher when the snapshot disables JS rendering."""
    collector = SearchResultsCollector([])
    snapshot = {
        "web.enable_javascript_rendering": {
            "value": False,
            "ui_element": "checkbox",
        }
    }
    tool = build_fetch_tool("full", collector, settings_snapshot=snapshot)
    kwargs = _captured_content_fetcher_kwargs(
        lambda: tool.invoke({"url": "http://example.com/"})
    )
    assert kwargs.get("enable_js_rendering") is False


def test_full_mode_passes_js_on_when_snapshot_enables_it():
    """When the snapshot opts in, JS rendering is forwarded to ContentFetcher."""
    collector = SearchResultsCollector([])
    snapshot = {
        "web.enable_javascript_rendering": {
            "value": True,
            "ui_element": "checkbox",
        }
    }
    tool = build_fetch_tool("full", collector, settings_snapshot=snapshot)
    kwargs = _captured_content_fetcher_kwargs(
        lambda: tool.invoke({"url": "http://example.com/"})
    )
    assert kwargs.get("enable_js_rendering") is True


def test_full_mode_defaults_to_js_off_without_snapshot():
    """No snapshot, no thread-local context → JS disabled (safe default)."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector)
    kwargs = _captured_content_fetcher_kwargs(
        lambda: tool.invoke({"url": "http://example.com/"})
    )
    assert kwargs.get("enable_js_rendering") is False


def test_fetch_error_scrubs_credentialed_url_from_exception():
    """A fetch exception that embeds a credentialed URL must be scrubbed
    before the tool return reaches the agent/LLM and the user-visible
    research output (credential-leak follow-up to #4625). Full detail stays
    in the server log."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector)

    fetcher = MagicMock()
    fetcher.fetch.side_effect = Exception(
        "ConnectionError for https://user:[email protected]:8080"
    )
    cm = MagicMock()
    cm.__enter__.return_value = fetcher
    cm.__exit__.return_value = False

    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "http://example.com/"})

    assert "SUPERSECRET123" not in out  # credential scrubbed
    assert "Error fetching" in out  # still a useful error for the agent


def test_failed_fetch_scrubs_credentials_from_result_error():
    """A non-success fetch result carries ContentFetcher's raw error string,
    which can embed a credentialed URL. It must be scrubbed before the
    'Failed to fetch' tool return reaches the agent/LLM (#4633)."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector)

    fetcher = MagicMock()
    fetcher.fetch.return_value = {
        "status": "error",
        "error": "blocked: https://user:[email protected]",
    }
    cm = MagicMock()
    cm.__enter__.return_value = fetcher
    cm.__exit__.return_value = False

    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "http://example.com/"})

    assert "SUPERSECRET999" not in out  # credential scrubbed
    assert "Failed to fetch" in out  # still a useful error for the agent


def test_summary_mode_forwards_js_setting():
    """Summary-mode tool also forwards the JS toggle from the snapshot."""
    collector = SearchResultsCollector([])
    model = _model_returning("ok")
    snapshot = {
        "web.enable_javascript_rendering": {
            "value": False,
            "ui_element": "checkbox",
        }
    }
    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
        settings_snapshot=snapshot,
    )
    kwargs = _captured_content_fetcher_kwargs(
        lambda: tool.invoke({"url": "http://example.com/", "focus": "x"})
    )
    assert kwargs.get("enable_js_rendering") is False


# ---------------------------------------------------------------------------
# Egress policy: an out-of-scope URL is a RECOVERABLE tool message, not a
# raised exception. Re-raising used to abort a pooled subagent (and depended
# on each agent's tool-error layer); returning a message lets the lead agent
# and subagents handle it identically and stay in-scope. The URL is never
# fetched either way — only the reporting changes.
# ---------------------------------------------------------------------------


def test_full_mode_egress_denial_returns_message_not_raises():
    from local_deep_research.security.egress.policy import Decision

    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, egress_context=object())

    with patch(
        "local_deep_research.security.egress.policy.evaluate_url",
        return_value=Decision(False, "scope_mismatch_private_only"),
    ):
        out = tool.invoke({"url": "https://public.example.com/x"})

    assert isinstance(out, str)
    assert "Cannot fetch" in out
    assert "scope_mismatch_private_only" in out  # the reason is surfaced
    assert "skip external URLs" in out  # the adapt-and-stay-local hint
    # The denied URL must never be registered as a citation.
    assert collector.results == []


def test_summary_mode_egress_denial_returns_message_not_raises():
    from local_deep_research.security.egress.policy import Decision

    collector = SearchResultsCollector([])
    model = _model_returning("should-never-be-called")
    tool = build_fetch_tool(
        "summary_focus", collector, model=model, egress_context=object()
    )

    with patch(
        "local_deep_research.security.egress.policy.evaluate_url",
        return_value=Decision(False, "scope_mismatch_private_only"),
    ):
        out = tool.invoke({"url": "https://public.example.com/x", "focus": "y"})

    assert isinstance(out, str)
    assert "Cannot fetch" in out
    assert "scope_mismatch_private_only" in out
    # The gate fires BEFORE any fetch/summarise work.
    model.invoke.assert_not_called()
    assert collector.results == []


# ---------------------------------------------------------------------------
# Library-document and citation-marker URL pre-resolution (A3).
#
# The library RAG engine and the collection engine emit
# ``/library/document/<uuid>[/pdf]`` as a result's citation URL; agents
# sometimes paste a bare ``[N]`` citation marker back into ``fetch_content``
# instead of the actual URL. Both shapes used to be rejected by the egress
# policy as ``unsupported_scheme`` and the agent saw no page content. The
# pre-resolution pass routes library URLs to a local DB read and rewrites
# citation markers via SearchResultsCollector.find_by_index.
# ---------------------------------------------------------------------------


def _library_doc_resolver(url):
    """Minimal library resolver for tests: returns content for one URL."""
    if url == "/library/document/doc-1":
        return {
            "title": "Doc One",
            "content": "Body of document one.",
            "url": url,
            "snippet": "Body of document one.",
        }
    if url == "/library/document/doc-1/pdf":
        return {
            "title": "Doc One (PDF)",
            "content": "Body of document one.",
            "url": url,
            "snippet": "Body of document one.",
        }
    return None


def test_full_mode_resolves_library_document_url_locally():
    """A ``/library/document/<uuid>`` URL fetches from the resolver and
    registers the citation; ContentFetcher is NOT called and the egress
    policy is NOT consulted (the fetch is a local DB read)."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool(
        "full",
        collector,
        library_resolver=_library_doc_resolver,
        egress_context=object(),
    )

    cm = _fetcher_cm()  # would fail if ContentFetcher were called
    with (
        patch(
            "local_deep_research.content_fetcher.ContentFetcher",
            return_value=cm,
        ) as factory,
        patch(
            "local_deep_research.security.egress.policy.evaluate_url"
        ) as mock_evaluate_url,
    ):
        out = tool.invoke({"url": "/library/document/doc-1"})

    factory.assert_not_called()  # local read, no HTTP
    mock_evaluate_url.assert_not_called()  # egress bypass locked directly
    assert "Title: Doc One" in out
    assert "URL: /library/document/doc-1" in out
    assert "Body of document one." in out
    # The library URL is registered as a citation so the agent can cite it.
    assert len(collector.results) == 1
    assert collector.results[0]["link"] == "/library/document/doc-1"


def test_full_mode_resolves_library_pdf_url_locally():
    """The ``/pdf`` suffix is just a hint to render the doc as PDF; the
    fetch tool reads the same Document row regardless of suffix."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool(
        "full", collector, library_resolver=_library_doc_resolver
    )

    cm = _fetcher_cm()
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "/library/document/doc-1/pdf"})

    assert "Title: Doc One (PDF)" in out
    assert "URL: /library/document/doc-1/pdf" in out
    assert "Body of document one." in out


def test_full_mode_falls_through_when_library_resolver_returns_none():
    """An unknown library UUID (resolver returns None) falls through to
    the egress policy and is rejected as ``unsupported_scheme`` — same as
    the pre-fix behaviour for malformed library URLs."""
    from local_deep_research.security.egress.policy import Decision

    collector = SearchResultsCollector([])

    def _always_none(_url):
        return None

    tool = build_fetch_tool(
        "full",
        collector,
        library_resolver=_always_none,
        egress_context=object(),
    )

    with patch(
        "local_deep_research.security.egress.policy.evaluate_url",
        return_value=Decision(False, "unsupported_scheme"),
    ):
        out = tool.invoke({"url": "/library/document/missing"})

    assert "Cannot fetch" in out
    assert "unsupported_scheme" in out


def test_full_mode_without_library_resolver_falls_through_to_egress():
    """When no library_resolver is passed, the tool preserves the
    pre-A3 behaviour: library URLs hit the egress policy unchanged."""
    from local_deep_research.security.egress.policy import Decision

    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, egress_context=object())

    with patch(
        "local_deep_research.security.egress.policy.evaluate_url",
        return_value=Decision(False, "unsupported_scheme"),
    ):
        out = tool.invoke({"url": "/library/document/abc"})

    assert "Cannot fetch" in out
    assert "unsupported_scheme" in out


def test_full_mode_citation_marker_resolves_to_citation_url():
    """A bare ``[N]`` citation marker is rewritten to the citation's
    stored URL and the rewritten URL is what ContentFetcher sees (and
    what the egress gate evaluates, if any)."""
    collector = SearchResultsCollector([])
    collector.add_results(
        [
            {
                "title": "Some Paper",
                "link": "https://example.com/paper",
                "snippet": "snippet",
            }
        ],
        engine_name="web",
    )

    tool = build_fetch_tool("full", collector)

    cm = _fetcher_cm(title="Some Paper", content="body")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "[1]"})

    # The resolved URL (not the citation marker) was fetched.
    cm.__enter__.return_value.fetch.assert_called_once()
    fetched_url = cm.__enter__.return_value.fetch.call_args.args[0]
    assert fetched_url == "https://example.com/paper"
    assert "Title: Some Paper" in out


def test_full_mode_unknown_citation_marker_returns_helpful_error():
    """A well-formed ``[N]`` with no matching citation must produce an
    actionable error, not the generic egress-denial message. The error
    must mention the marker and steer the agent toward the citation's URL."""
    collector = SearchResultsCollector([])  # no citations tracked
    tool = build_fetch_tool("full", collector)

    cm = _fetcher_cm()
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ) as factory:
        out = tool.invoke({"url": "[9999]"})

    factory.assert_not_called()
    assert "No registered citation matches [9999]" in out
    assert "URL" in out  # tells the agent what to do instead


def test_full_mode_citation_with_library_url_resolves_locally():
    """A citation whose source is a library doc URL resolves through the
    library resolver (recursive: [N] → /library/document/<uuid> → content).
    ContentFetcher is NOT called."""
    collector = SearchResultsCollector([])
    collector.add_results(
        [
            {
                "title": "Library Doc",
                "link": "/library/document/doc-1",
                "snippet": "snippet",
            }
        ],
        engine_name="library",
    )
    tool = build_fetch_tool(
        "full", collector, library_resolver=_library_doc_resolver
    )

    cm = _fetcher_cm()
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ) as factory:
        out = tool.invoke({"url": "[1]"})

    factory.assert_not_called()
    assert "Title: Doc One" in out
    assert "URL: /library/document/doc-1" in out


def test_summary_mode_resolves_library_document_url_locally():
    """Summary mode routes the resolved document content through the LLM
    summariser the same way a fetched HTTP page would."""
    collector = SearchResultsCollector([])
    model = _model_returning("summary of doc 1")
    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
        library_resolver=_library_doc_resolver,
        egress_context=object(),
    )

    cm = _fetcher_cm()
    with (
        patch(
            "local_deep_research.content_fetcher.ContentFetcher",
            return_value=cm,
        ) as factory,
        patch(
            "local_deep_research.security.egress.policy.evaluate_url"
        ) as mock_evaluate_url,
    ):
        out = tool.invoke(
            {"url": "/library/document/doc-1", "focus": "what does it say"}
        )

    factory.assert_not_called()
    mock_evaluate_url.assert_not_called()
    model.invoke.assert_called_once()
    # The prompt was built from the library document content.
    prompt = model.invoke.call_args.args[0]
    assert "Body of document one." in prompt
    assert "Page title: Doc One" in prompt
    assert "Page URL: /library/document/doc-1" in prompt
    assert "summary of doc 1" in out


def test_summary_mode_citation_marker_resolves_to_citation_url():
    """Summary mode also rewrites citation markers to the citation URL;
    the egress gate (if any) then evaluates the resolved URL."""
    collector = SearchResultsCollector([])
    collector.add_results(
        [
            {
                "title": "Some Paper",
                "link": "https://example.com/paper",
                "snippet": "snippet",
            }
        ],
        engine_name="web",
    )
    model = _model_returning("summary")
    tool = build_fetch_tool("summary_focus", collector, model=model)

    cm = _fetcher_cm(title="T", content="body")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "[1]", "focus": "year"})

    cm.__enter__.return_value.fetch.assert_called_once()
    fetched_url = cm.__enter__.return_value.fetch.call_args.args[0]
    assert fetched_url == "https://example.com/paper"
    assert "summary" in out


def test_summary_mode_unknown_citation_marker_short_circuits_with_error():
    """Summary mode also short-circuits on an unknown citation marker so
    no LLM round-trip is spent on a guaranteed-fail call."""
    collector = SearchResultsCollector([])
    model = _model_returning("should-not-be-called")
    tool = build_fetch_tool("summary_focus", collector, model=model)

    cm = _fetcher_cm()
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "[42]", "focus": "anything"})

    cm.__enter__.return_value.fetch.assert_not_called()
    model.invoke.assert_not_called()
    assert "No registered citation matches [42]" in out


def test_summary_mode_library_doc_with_empty_content_returns_not_relevant():
    """A library document with no extractable text mirrors the existing
    HTTP-fetcher behaviour: NOT RELEVANT, no LLM call, no registration
    (so the agent can retry with a different focus)."""

    def _empty_resolver(_url):
        return {
            "title": "Empty Doc",
            "content": "",
            "url": _url,
            "snippet": "",
        }

    collector = SearchResultsCollector([])
    model = _model_returning("should-not-be-called")
    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
        library_resolver=_empty_resolver,
    )

    cm = _fetcher_cm()
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke(
            {"url": "/library/document/empty", "focus": "anything"}
        )

    model.invoke.assert_not_called()
    assert "NOT RELEVANT" in out
    assert "/library/document/empty" in out
    # Empty citation must NOT be registered — the agent should be free to
    # retry with a different focus instead of seeing it as already-cached
    # with an empty body.
    assert collector.results == []


def test_inputs_that_look_like_citation_but_are_not_pass_through():
    """Inputs that LOOK like a citation but aren't a well-formed ``[N]``
    (e.g. ``[1, 2]`` or ``[]`` or ``[abc]``) must NOT short-circuit with
    the citation error. The fetch tool should fall through to the egress
    policy and produce a normal denial."""
    from local_deep_research.security.egress.policy import Decision

    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, egress_context=object())

    for bad in ("[1, 2]", "[]", "[abc]"):
        with patch(
            "local_deep_research.security.egress.policy.evaluate_url",
            return_value=Decision(False, "unsupported_scheme"),
        ):
            out = tool.invoke({"url": bad})
        assert "Cannot fetch" in out, (
            f"expected denial for {bad!r}, got {out!r}"
        )


def test_library_doc_content_length_capped_at_max_length():
    """A library document whose content exceeds CONTENT_MAX_LENGTH is truncated."""
    from local_deep_research.advanced_search_system.tools.fetch import (
        CONTENT_MAX_LENGTH,
    )

    long_text = "A" * (CONTENT_MAX_LENGTH + 5000)

    def _large_resolver(_url):
        return {
            "title": "Large Doc",
            "content": long_text,
            "url": _url,
            "snippet": "A" * 200,
        }

    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, library_resolver=_large_resolver)

    out = tool.invoke({"url": "/library/document/large-doc"})
    assert out.startswith(
        "[1] Title: Large Doc\nURL: /library/document/large-doc\n\n"
    )
    body = out.split("\n\n", 1)[1]
    assert len(body) == CONTENT_MAX_LENGTH


def test_circular_citation_marker_prevents_unbounded_recursion():
    """If a citation marker points to a link that cycles back to itself, circular detection prevents recursion."""
    collector = SearchResultsCollector([])
    collector.add_results(
        [{"title": "Loop", "link": "[1]", "snippet": "Looping citation"}],
        engine_name="fetch",
    )
    tool = build_fetch_tool("full", collector)

    out = tool.invoke({"url": "[1]"})
    assert "Circular citation reference detected" in out


def test_multi_node_citation_cycle_reports_circular_reference():
    """A [1] -> [2] -> [1] cycle is detected before the depth cap."""
    collector = SearchResultsCollector([])
    collector.add_results(
        [{"title": "First", "link": "[2]", "snippet": "points to 2"}],
        engine_name="fetch",
    )
    collector.add_results(
        [{"title": "Second", "link": "[1]", "snippet": "points to 1"}],
        engine_name="fetch",
    )
    tool = build_fetch_tool("full", collector)

    out = tool.invoke({"url": "[1]"})

    assert "Circular citation reference detected for '[1]'" in out
    assert "depth limit exceeded" not in out


def test_deep_acyclic_citation_chain_reports_depth_limit():
    """A six-hop acyclic chain gets a precise depth-limit error."""
    collector = SearchResultsCollector([])
    for index in range(1, 7):
        link = f"[{index + 1}]" if index < 6 else "https://example.com/final"
        collector.add_results(
            [
                {
                    "title": f"Citation {index}",
                    "link": link,
                    "snippet": f"points to {link}",
                }
            ],
            engine_name="fetch",
        )
    tool = build_fetch_tool("full", collector)

    out = tool.invoke({"url": "[1]"})

    assert "Citation resolution depth limit exceeded for '[6]'" in out
    assert "Circular citation reference detected" not in out


def test_citation_marker_rewrite_denied_by_egress_policy_names_resolved_url():
    """When a citation marker [N] rewrites to an external URL and that resolved URL is
    denied by policy, the denial message names the resolved URL, not the marker."""
    from local_deep_research.security.egress.policy import Decision

    collector = SearchResultsCollector([])
    collector.add_results(
        [
            {
                "title": "Secret Doc",
                "link": "https://forbidden-external.com/secret",
                "snippet": "snip",
            }
        ],
        engine_name="web",
    )
    tool = build_fetch_tool("full", collector, egress_context=object())

    with patch(
        "local_deep_research.security.egress.policy.evaluate_url",
        return_value=Decision(False, "scope_mismatch_private_only"),
    ):
        out = tool.invoke({"url": "[1]"})

    assert "Cannot fetch https://forbidden-external.com/secret" in out
    assert "Cannot fetch [1]" not in out
    assert "scope_mismatch_private_only" in out


def test_citation_marker_with_empty_url_field_returns_error():
    """A citation marker [N] whose source citation lacks a link/url field returns a clear error."""
    collector = SearchResultsCollector([])
    collector.add_results(
        [{"title": "No URL Paper", "snippet": "no link given"}],
        engine_name="web",
    )
    tool = build_fetch_tool("full", collector)

    out = tool.invoke({"url": "[1]"})
    assert "Citation [1] has no URL field." in out


def test_summary_mode_citation_with_library_url_resolves_locally():
    """Summary mode citation marker [N] pointing to a library doc resolves locally without HTTP or egress gate."""
    collector = SearchResultsCollector([])
    collector.add_results(
        [
            {
                "title": "Library Doc Citation",
                "link": "/library/document/doc-1",
                "snippet": "snip",
            }
        ],
        engine_name="library",
    )
    model = _model_returning("summary of cited doc")
    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
        library_resolver=_library_doc_resolver,
        egress_context=object(),
    )

    cm = _fetcher_cm()
    with (
        patch(
            "local_deep_research.content_fetcher.ContentFetcher",
            return_value=cm,
        ) as factory,
        patch(
            "local_deep_research.security.egress.policy.evaluate_url"
        ) as mock_evaluate_url,
    ):
        out = tool.invoke({"url": "[1]", "focus": "facts"})

    factory.assert_not_called()
    mock_evaluate_url.assert_not_called()
    model.invoke.assert_called_once()
    assert "summary of cited doc" in out


def test_multi_level_citation_chain_resolves_deepest_target():
    """Multi-level citation chains ([1] -> [2] -> external URL or library doc) resolve recursively."""
    collector = SearchResultsCollector([])
    collector.add_results(
        [{"title": "First", "link": "[2]", "snippet": "points to 2"}],
        engine_name="web",
    )
    collector.add_results(
        [
            {
                "title": "Second",
                "link": "https://example.com/deep-page",
                "snippet": "deep",
            }
        ],
        engine_name="web",
    )

    tool = build_fetch_tool("full", collector)

    cm = _fetcher_cm(title="Deep Page", content="deep content")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "[1]"})

    cm.__enter__.return_value.fetch.assert_called_once()
    fetched_url = cm.__enter__.return_value.fetch.call_args.args[0]
    assert fetched_url == "https://example.com/deep-page"
    assert "Title: Deep Page" in out


def test_summary_fetch_tool_handles_llm_exception():
    """Summary-mode fetch tool catches LLM model.invoke exceptions and returns a scrubbed error string."""
    collector = SearchResultsCollector([])
    model = MagicMock()
    model.invoke.side_effect = RuntimeError("LLM rate limit reached")

    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
    )

    cm = _fetcher_cm(title="Page Title", content="Page text to summarize.")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke(
            {"url": "https://example.com/page", "focus": "key facts"}
        )

    assert "Error summarizing https://example.com/page" in out
    assert "LLM rate limit reached" in out


def test_summary_fetch_tool_handles_empty_summary():
    """Summary-mode fetch tool returns NOT RELEVANT when LLM returns an empty summary."""
    collector = SearchResultsCollector([])
    model = _model_returning("")

    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
    )

    cm = _fetcher_cm(title="Unrelated Page", content="Unrelated page content.")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke(
            {"url": "https://example.com/unrelated", "focus": "specific topic"}
        )

    assert (
        "NOT RELEVANT (no spans matched focus at https://example.com/unrelated)"
        in out
    )


def test_summary_fetch_tool_handles_empty_content():
    """Summary-mode fetch tool returns NOT RELEVANT without invoking LLM when page content is empty."""
    collector = SearchResultsCollector([])
    model = MagicMock()

    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
    )

    cm = _fetcher_cm(title="Empty Page", content="   \n   ")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke(
            {"url": "https://example.com/empty", "focus": "anything"}
        )

    model.invoke.assert_not_called()
    assert (
        "NOT RELEVANT (no extractable content at https://example.com/empty)"
        in out
    )


def test_non_string_url_returns_none_in_try_resolve_url():
    """_try_resolve_url returns None immediately if url is not a string."""
    from local_deep_research.advanced_search_system.tools.fetch import (
        _try_resolve_url,
    )

    assert _try_resolve_url(12345, None, None) is None


# NOTE: two tests were removed here alongside the code they covered — one
# for a ``getattr`` guard on ``find_by_url`` and one for a pre-#5381 bare-int
# ``add_results`` contract. Both exercised speculative generality in
# ``_register_in_collector``'s fallback that this branch had added and then
# reverted: no collector in any repo we can see has that shape, and the
# fallback's own docstring forbids growing it without a real consumer.
# Keeping tests for deliberately-removed code would just re-document it.


def test_register_in_collector_assigns_new_index():
    from local_deep_research.advanced_search_system.tools.fetch import (
        _register_in_collector,
    )

    collector = SearchResultsCollector([])
    idx = _register_in_collector(
        collector, "http://a.com", "A", "body text for snippet"
    )
    assert idx == 1
    assert collector.find_by_url("http://a.com") == 1


def test_register_in_collector_fast_path_reuses_without_reappend():
    """Already-tracked URL and SAME text returns the existing index and does
    not grow ``_results``.

    The search hit below carries the excerpt the fetch derives from the
    page body, so the fetch is a genuine duplicate under the
    ``(url, snippet)`` dedup key introduced for #5894. The companion test
    covers the other half — a fetch whose text is new.
    """
    from local_deep_research.advanced_search_system.tools.fetch import (
        _register_in_collector,
    )

    collector = SearchResultsCollector([])
    collector.add_results(
        [{"title": "A", "link": "http://a.com", "snippet": "fetched body"}],
        engine_name="web",
    )
    before = len(collector.results)
    idx = _register_in_collector(collector, "http://a.com", "A", "fetched body")
    assert idx == 1
    assert len(collector.results) == before


def test_register_in_collector_keeps_a_fetched_excerpt_that_is_new():
    """Fetching a cited page whose text differs from the search snippet
    must NOT discard that text.

    Driven through the real ``_register_in_collector`` — the helper the
    ``fetch_content`` tool actually calls — because the first attempt at
    #5894 changed ``add_results`` only and left this path collapsing on
    the URL alone, so the fetched excerpt was still lost. No test in that
    attempt exercised the combination.

    The paired assertion is the point: the same-text case above must keep
    reusing, or this pair would be satisfied by a collector that never
    dedupes at all.
    """
    from local_deep_research.advanced_search_system.tools.fetch import (
        _register_in_collector,
    )

    all_links = []
    collector = SearchResultsCollector(all_links)
    collector.add_results(
        [{"title": "A", "link": "http://a.com", "snippet": "search snippet"}],
        engine_name="web",
    )

    idx = _register_in_collector(
        collector, "http://a.com", "A", "an entirely different page body"
    )

    assert idx == 2, "the fetched excerpt was collapsed onto the search hit"
    assert len(all_links) == 2
    snippets = [entry["snippet"] for entry in all_links]
    assert "search snippet" in snippets
    assert "an entirely different page body" in snippets
    # One source, two excerpts: the URL still resolves to its first
    # citation and the source is counted once.
    assert collector.find_by_url("http://a.com") == 1
    assert count_distinct_sources(all_links) == 1


class _ListReturningCustomCollector:
    """A custom collector returning a 2-LIST rather than a tuple.

    Unpacking is duck-typed, so any 2-item iterable must keep working —
    narrowing the fallback to ``isinstance(outcome, tuple)`` would trade the
    bare-int compatibility hole for a list/generator one.
    """

    def __init__(self, index_value="1"):
        self._index_value = index_value

    def add_results(self, results, engine_name="web"):
        return [0, [dict(r, index=self._index_value) for r in results]]

    def find_by_url(self, url):
        return None


def test_register_in_collector_accepts_two_item_list_return():
    from local_deep_research.advanced_search_system.tools.fetch import (
        _register_in_collector,
    )

    collector = _ListReturningCustomCollector(index_value="7")
    assert _register_in_collector(collector, "http://a.com", "A", "body") == 7


class _TupleReturningCustomCollector:
    """A custom collector on the post-#5381 tuple contract but without the
    fetch fast path — the other population the fallback must serve."""

    def __init__(self, index_value="1", url_lookup=None):
        self._index_value = index_value
        self._url_lookup = url_lookup
        self.added = []

    def add_results(self, results, engine_name="web"):
        self.added.extend(results)
        return 0, [dict(r, index=self._index_value) for r in results]

    def find_by_url(self, url):
        return self._url_lookup


def test_register_in_collector_tuple_contract_uses_indexed_copy():
    """A custom collector on the tuple contract yields its assigned index."""
    from local_deep_research.advanced_search_system.tools.fetch import (
        _register_in_collector,
    )

    collector = _TupleReturningCustomCollector(index_value="7")
    assert not hasattr(collector, "find_or_add_result")

    idx = _register_in_collector(collector, "http://a.com", "A", "body")

    assert idx == 7


def test_register_in_collector_falls_back_on_bad_index():
    """A non-int index falls through to ``find_by_url`` instead of raising."""
    from local_deep_research.advanced_search_system.tools.fetch import (
        _register_in_collector,
    )

    collector = _TupleReturningCustomCollector(
        index_value="not-an-int", url_lookup=4
    )
    idx = _register_in_collector(collector, "http://a.com", "A", "body")

    assert idx == 4


def test_register_in_collector_raises_when_unresolvable():
    """No usable index anywhere is a hard error, not a silent bad citation."""
    from local_deep_research.advanced_search_system.tools.fetch import (
        _register_in_collector,
    )

    collector = _TupleReturningCustomCollector(
        index_value=None, url_lookup=None
    )
    with pytest.raises(RuntimeError, match="Failed to register fetched URL"):
        _register_in_collector(collector, "http://a.com", "A", "body")


# ---------------------------------------------------------------------------
# Fetch budget cap per topic/subsection (#5815).
# ---------------------------------------------------------------------------


def test_full_mode_enforces_fetch_budget_cap():
    """When the fetch budget is exhausted, full-mode fetch returns a message
    without calling ContentFetcher or making network calls."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, max_fetches=2)

    cm = _fetcher_cm(title="Page", content="Page content")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out1 = tool.invoke({"url": "https://example.com/1"})
        out2 = tool.invoke({"url": "https://example.com/2"})
        out3 = tool.invoke({"url": "https://example.com/3"})

    assert "Title: Page" in out1
    assert out1.endswith("(1 of 2 page fetches used)")
    assert "Title: Page" in out2
    assert out2.endswith("(2 of 2 page fetches used)")
    assert "Fetch content budget exhausted (2/2) for this topic." in out3
    assert "Rely on search snippets" in out3
    # Only 2 network fetches were performed
    assert cm.__enter__.return_value.fetch.call_count == 2
    assert collector.fetch_count == 2


@pytest.mark.parametrize("fetch_mode", ["summary_focus", "summary_focus_query"])
def test_summary_mode_enforces_fetch_budget_cap(fetch_mode):
    """When the fetch budget is exhausted, summary-mode fetch (both summary_focus
    and summary_focus_query) returns a message without calling ContentFetcher
    or model.invoke."""
    collector = SearchResultsCollector([])
    model = _model_returning("Extracted summary")
    tool = build_fetch_tool(
        fetch_mode,
        collector,
        model=model,
        overall_query="What is quantum computing?",
        max_fetches=2,
    )

    cm = _fetcher_cm(title="Page", content="Page content")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out1 = tool.invoke({"url": "https://example.com/1", "focus": "fact 1"})
        out2 = tool.invoke({"url": "https://example.com/2", "focus": "fact 2"})
        out3 = tool.invoke({"url": "https://example.com/3", "focus": "fact 3"})

    assert "Extracted summary" in out1
    assert out1.endswith("(1 of 2 page fetches used)")
    assert "Extracted summary" in out2
    assert out2.endswith("(2 of 2 page fetches used)")
    assert "Fetch content budget exhausted (2/2) for this topic." in out3
    assert cm.__enter__.return_value.fetch.call_count == 2
    assert model.invoke.call_count == 2
    assert collector.fetch_count == 2


def test_fetch_budget_cap_zero_allows_unlimited():
    """Setting max_fetches=0 disables the cap and allows unlimited fetches."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, max_fetches=0)

    cm = _fetcher_cm(title="Page", content="Page content")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        for i in range(10):
            out = tool.invoke({"url": f"https://example.com/{i}"})
            assert "Title: Page" in out

    assert cm.__enter__.return_value.fetch.call_count == 10
    assert collector.fetch_count == 10


def test_fetch_budget_cap_resets_with_collector_reset():
    """Calling collector.reset() resets the fetch count, allowing new fetches."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, max_fetches=1)

    cm = _fetcher_cm(title="Page", content="Page content")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out1 = tool.invoke({"url": "https://example.com/1"})
        out2 = tool.invoke({"url": "https://example.com/2"})
        assert "Title: Page" in out1
        assert "Fetch content budget exhausted (1/1)" in out2

        collector.reset()
        assert collector.fetch_count == 0

        out3 = tool.invoke({"url": "https://example.com/3"})
        assert "Title: Page" in out3
        assert collector.fetch_count == 1


def test_fetch_budget_reads_from_settings_snapshot():
    """When max_fetches is not explicitly passed, the limit is read from settings_snapshot."""
    collector = SearchResultsCollector([])
    settings = {"search.fetch.max_per_topic": {"value": 1}}
    tool = build_fetch_tool("full", collector, settings_snapshot=settings)

    cm = _fetcher_cm(title="Page", content="Page content")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out1 = tool.invoke({"url": "https://example.com/1"})
        out2 = tool.invoke({"url": "https://example.com/2"})

    assert "Title: Page" in out1
    assert "Fetch content budget exhausted (1/1)" in out2


def test_fetch_budget_skipped_for_collector_without_try_record_fetch():
    """Collectors lacking try_record_fetch (or None) skip budget enforcement."""

    class LegacyCollector:
        def __init__(self):
            self.results = []

        def find_or_add_result(self, result, engine_name):
            return 1

    collector = LegacyCollector()
    tool = build_fetch_tool("full", collector, max_fetches=1)

    cm = _fetcher_cm(title="Page", content="Page content")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out1 = tool.invoke({"url": "https://example.com/1"})
        out2 = tool.invoke({"url": "https://example.com/2"})

    assert "Title: Page" in out1
    assert "Title: Page" in out2


def test_fetch_budget_consumed_on_failed_fetch():
    """Option A accounting: failed network fetches consume a budget slot."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, max_fetches=1)

    cm = _fetcher_cm(status="error")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out1 = tool.invoke({"url": "https://example.com/fail"})
        out2 = tool.invoke({"url": "https://example.com/next"})

    assert "Failed to fetch" in out1
    assert collector.fetch_count == 1
    assert "Fetch content budget exhausted (1/1) for this topic." in out2


def test_fetch_budget_consumed_on_guard_empty_content():
    """Option A accounting: empty page content (Guard 1 returning NOT RELEVANT)
    consumes a budget slot because the invocation expended an agent turn."""
    collector = SearchResultsCollector([])
    model = _model_returning("summary")
    tool = build_fetch_tool(
        "summary_focus", collector, model=model, max_fetches=1
    )

    cm = _fetcher_cm(title="Empty", content="   \n  ")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out1 = tool.invoke(
            {"url": "https://example.com/empty", "focus": "fact"}
        )
        out2 = tool.invoke({"url": "https://example.com/next", "focus": "fact"})

    assert "NOT RELEVANT (no extractable content" in out1
    assert collector.fetch_count == 1
    assert "Fetch content budget exhausted (1/1) for this topic." in out2


def test_fetch_budget_consumed_on_guard_no_matching_spans():
    """Option A accounting: empty summary from model (Guard 2 returning NOT RELEVANT)
    consumes a budget slot because the invocation expended an agent turn."""
    collector = SearchResultsCollector([])
    model = _model_returning("")  # Empty response => Guard 2
    tool = build_fetch_tool(
        "summary_focus", collector, model=model, max_fetches=1
    )

    cm = _fetcher_cm(title="Page", content="Some page text")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out1 = tool.invoke(
            {"url": "https://example.com/mismatch", "focus": "unrelated fact"}
        )
        out2 = tool.invoke({"url": "https://example.com/next", "focus": "fact"})

    assert "NOT RELEVANT (no spans matched focus" in out1
    assert collector.fetch_count == 1
    assert "Fetch content budget exhausted (1/1) for this topic." in out2


def test_fetch_tool_description_contains_budget_cap():
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, max_fetches=2)
    assert "(Up to 2 page fetches allowed per topic.)" in tool.description

    tool_unlimited = build_fetch_tool("full", collector, max_fetches=0)
    assert "page fetches allowed per topic" not in tool_unlimited.description


def test_policy_denied_fetch_does_not_consume_budget():
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector, max_fetches=1)

    with patch(
        "local_deep_research.advanced_search_system.tools.fetch._enforce_url_policy"
    ) as mock_enforce:
        from local_deep_research.security.egress.policy import PolicyDeniedError

        mock_enforce.side_effect = PolicyDeniedError(
            MagicMock(reason="host_not_allowed"), target="https://blocked.com"
        )

        out = tool.invoke({"url": "https://blocked.com"})
        assert (
            "Cannot fetch https://blocked.com: blocked by egress policy" in out
        )
        assert collector.fetch_count == 0

    # Budget slot remains available
    cm = _fetcher_cm(title="Allowed", content="Allowed content")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out2 = tool.invoke({"url": "https://allowed.com"})
        assert "Title: Allowed" in out2
        assert out2.endswith("(1 of 1 page fetches used)")
        assert collector.fetch_count == 1


def test_library_resolution_does_not_consume_budget():
    collector = SearchResultsCollector([])
    tool = build_fetch_tool(
        "full",
        collector,
        max_fetches=1,
    )
    with patch(
        "local_deep_research.advanced_search_system.tools.fetch._try_resolve_url",
        return_value=(
            "result",
            {
                "title": "Local Doc",
                "content": "Local document text",
                "status": "success",
            },
        ),
    ):
        out = tool.invoke({"url": "/library/document/12345"})
        assert "Title: Local Doc" in out
        assert "(1 of 1 page fetches used)" not in out
        assert collector.fetch_count == 0

    # Budget slot is still intact for network fetch
    cm = _fetcher_cm(title="Network", content="Network page")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out2 = tool.invoke({"url": "https://example.com/net"})
        assert "Title: Network" in out2
        assert out2.endswith("(1 of 1 page fetches used)")
        assert collector.fetch_count == 1


def test_collector_without_try_record_fetch_warns_once(loguru_caplog):
    import local_deep_research.advanced_search_system.tools.fetch as fetch_mod

    fetch_mod._warned_collector_missing_fetch_cap = False

    with loguru_caplog.at_level("WARNING"):
        fetch_mod._warn_collector_lacks_fetch_recording()
        fetch_mod._warn_collector_lacks_fetch_recording()

    records = [
        r
        for r in loguru_caplog.records
        if "does not implement try_record_fetch" in r.message
    ]
    assert len(records) == 1


def test_read_max_fetches_setting_clamping():
    from local_deep_research.advanced_search_system.tools.fetch import (
        _read_max_fetches_setting,
        DEFAULT_MAX_FETCHES_PER_TOPIC,
    )

    assert _read_max_fetches_setting(None) == DEFAULT_MAX_FETCHES_PER_TOPIC
    assert _read_max_fetches_setting({}) == DEFAULT_MAX_FETCHES_PER_TOPIC
    assert (
        _read_max_fetches_setting({"search.fetch.max_per_topic": {"value": 5}})
        == 5
    )
    assert (
        _read_max_fetches_setting({"search.fetch.max_per_topic": {"value": 0}})
        == 0
    )
    assert (
        _read_max_fetches_setting(
            {"search.fetch.max_per_topic": {"value": 100}}
        )
        == 50
    )
    assert (
        _read_max_fetches_setting({"search.fetch.max_per_topic": {"value": -5}})
        == DEFAULT_MAX_FETCHES_PER_TOPIC
    )
    assert (
        _read_max_fetches_setting(
            {"search.fetch.max_per_topic": {"value": "invalid"}}
        )
        == DEFAULT_MAX_FETCHES_PER_TOPIC
    )


def test_read_content_max_length_setting_defaults_and_clamping():
    from local_deep_research.advanced_search_system.tools.fetch import (
        CONTENT_MAX_LENGTH,
        CONTENT_MAX_LENGTH_MAX,
        CONTENT_MAX_LENGTH_MIN,
        _read_content_max_length_setting,
    )

    assert _read_content_max_length_setting(None) == CONTENT_MAX_LENGTH
    assert _read_content_max_length_setting({}) == CONTENT_MAX_LENGTH
    assert (
        _read_content_max_length_setting(
            {"search.fetch.content_max_length": {"value": 5000}}
        )
        == 5000
    )
    # Raw-int snapshot form (no {"value": ...} wrapper).
    assert (
        _read_content_max_length_setting(
            {"search.fetch.content_max_length": 2000}
        )
        == 2000
    )
    assert (
        _read_content_max_length_setting(
            {"search.fetch.content_max_length": {"value": 100000}}
        )
        == CONTENT_MAX_LENGTH_MAX
    )
    assert (
        _read_content_max_length_setting(
            {"search.fetch.content_max_length": {"value": 500}}
        )
        == CONTENT_MAX_LENGTH_MIN
    )
    assert (
        _read_content_max_length_setting(
            {"search.fetch.content_max_length": {"value": -5}}
        )
        == CONTENT_MAX_LENGTH
    )
    assert (
        _read_content_max_length_setting(
            {"search.fetch.content_max_length": {"value": "invalid"}}
        )
        == CONTENT_MAX_LENGTH
    )


def test_library_doc_respects_content_max_length_setting():
    """A snapshot-configured cap truncates library docs instead of the default."""
    long_text = "A" * 20000

    def _large_resolver(_url):
        return {
            "title": "Large Doc",
            "content": long_text,
            "url": _url,
            "snippet": "A" * 200,
        }

    collector = SearchResultsCollector([])
    tool = build_fetch_tool(
        "full",
        collector,
        library_resolver=_large_resolver,
        settings_snapshot={"search.fetch.content_max_length": {"value": 3000}},
    )

    out = tool.invoke({"url": "/library/document/large-doc"})
    body = out.split("\n\n", 1)[1]
    assert len(body) == 3000


def test_http_fetch_receives_configured_max_length():
    """The snapshot cap is forwarded to ContentFetcher as max_length."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool(
        "full",
        collector,
        settings_snapshot={"search.fetch.content_max_length": {"value": 4200}},
    )

    cm = _fetcher_cm(title="Network", content="Network page")
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": "https://example.com/net"})

    assert "Title: Network" in out
    fetcher = cm.__enter__.return_value
    fetcher.fetch.assert_called_once_with(
        "https://example.com/net", max_length=4200
    )


def test_explicit_content_max_length_overrides_snapshot():
    """An explicit arg wins over the snapshot for library-doc truncation."""
    long_text = "B" * 20000

    def _large_resolver(_url):
        return {
            "title": "Large Doc",
            "content": long_text,
            "url": _url,
            "snippet": "B" * 200,
        }

    collector = SearchResultsCollector([])
    tool = build_fetch_tool(
        "full",
        collector,
        library_resolver=_large_resolver,
        settings_snapshot={"search.fetch.content_max_length": {"value": 3000}},
        content_max_length=1500,
    )

    out = tool.invoke({"url": "/library/document/large-doc"})
    body = out.split("\n\n", 1)[1]
    assert len(body) == 1500


def test_summary_mode_respects_summary_input_max_length_setting():
    """Summary mode truncates library-doc text before LLM extraction."""
    long_text = "C" * 20000

    def _large_resolver(_url):
        return {
            "title": "Large Doc",
            "content": long_text,
            "url": _url,
            "snippet": "C" * 200,
        }

    collector = SearchResultsCollector([])
    model = _model_returning("relevant excerpt")
    tool = build_fetch_tool(
        "summary_focus",
        collector,
        model=model,
        library_resolver=_large_resolver,
        settings_snapshot={
            "search.fetch.summary_input_max_length": {"value": 3000}
        },
    )

    out = tool.invoke(
        {"url": "/library/document/large-doc", "focus": "key fact"}
    )
    assert "relevant excerpt" in out
    assert model.invoke.call_count == 1
    prompt = model.invoke.call_args[0][0]
    assert "C" * 3000 in prompt
    assert "C" * 3001 not in prompt


def test_http_fetch_real_fetcher_truncates_to_configured_max_length():
    """End-to-end: snapshot cap flows through the real ContentFetcher."""
    from local_deep_research.content_fetcher.url_classifier import URLType

    long_text = "D" * 20000
    long_bytes = long_text.encode("utf-8")

    download_result = MagicMock()
    download_result.is_success = True
    download_result.content = long_bytes
    download_result.skip_reason = None

    mock_dl = MagicMock()
    mock_dl.download_with_result.return_value = download_result
    mock_dl.get_metadata.return_value = {"title": "Large Page"}

    collector = SearchResultsCollector([])
    tool = build_fetch_tool(
        "full",
        collector,
        settings_snapshot={"search.fetch.content_max_length": {"value": 2500}},
    )

    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_mock,
        patch(
            "local_deep_research.content_fetcher.fetcher.ContentFetcher._get_downloader",
            return_value=mock_dl,
        ),
    ):
        classifier_mock.classify.return_value = URLType.HTML
        classifier_mock.get_source_name.return_value = "Web Page"
        out = tool.invoke({"url": "https://example.com/large"})

    assert "[... content truncated ...]" in out
    body = out.split("\n\n", 1)[1]
    # Strip the budget footer ("(N of M page fetches used)") if present.
    content_only = body.split("\n\n(", 1)[0]
    # Real ContentFetcher appends "\n\n[... content truncated ...]" after cap.
    assert content_only.startswith("D" * 2500)
    assert len(content_only) == 2500 + len("\n\n[... content truncated ...]")


def test_full_mode_fetch_puts_the_page_title_in_the_bibliography():
    """The whole production chain for #6024, entered at the tool and with
    only the HTTP fetcher stubbed: a source the agent found by search and
    then read keeps the title the search engine scraped out of its result
    listing, because ``find_or_add_result`` folds the fetch onto the existing
    citation and upgrades only the link spelling.
    """
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )

    listing_title = (
        "Caste in India: a comprehensive guide to its origins, Vedic "
        "foundations, Manusmriti and modern implications | 2026 | Archive"
    )
    page_title = "Caste system in India - Wikipedia"
    url = "http://example.com/caste"

    all_links = []
    collector = SearchResultsCollector(all_links)
    collector.add_results(
        [{"title": listing_title, "link": url, "snippet": "paradigmatic"}],
        engine_name="searxng",
    )

    tool = build_fetch_tool("full", collector)
    with patch(
        "local_deep_research.content_fetcher.ContentFetcher",
        return_value=_fetcher_cm(title=page_title, content="Body text"),
    ):
        tool.invoke({"url": url})

    rendered = format_links_to_markdown(all_links)
    assert page_title in rendered
    assert "comprehensive guide" not in rendered
    # Recorded alongside: the stored citation title is untouched.
    assert all_links[0]["title"] == listing_title


@pytest.mark.parametrize(
    "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,
    )


@pytest.fixture(autouse=True)
def _clear_max_fetches_environment(monkeypatch):
    monkeypatch.delenv("LDR_SEARCH_FETCH_MAX_PER_TOPIC", raising=False)


@pytest.mark.parametrize("shape", ["raw", "legacy", "typed"])
@pytest.mark.parametrize(
    "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, SettingsValueError),
        (3.9, 3, 3),
        (False, 0, SettingsValueError),
        (True, 1, SettingsValueError),
        ("3.9", 9, 3),
        ("inf", 9, SettingsValueError),
        ("-inf", 9, SettingsValueError),
        ("nan", 9, SettingsValueError),
        ("1e400", 9, SettingsValueError),
        pytest.param("9" * 5000, 9, SettingsValueError, id="very-long-number"),
        (float("inf"), 9, SettingsValueError),
        (float("-inf"), 9, SettingsValueError),
        (float("nan"), 9, SettingsValueError),
    ],
)
def test_max_fetches_reader_preserves_snapshot_limits_and_default(
    shape, value, legacy_expected, typed_expected
):
    # A typed row that is not a finite number is an invalid configured
    # setting: it raises instead of selecting the default. Raw and sparse
    # legacy rows keep their int() coercion and fallback.
    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
    if expected is SettingsValueError:
        with pytest.raises(SettingsValueError) as raised:
            fetch_module._read_max_fetches_setting(snapshot, default=9)
        assert raised.value.key == "search.fetch.max_per_topic"
        assert raised.value.source == "value"
    else:
        assert (
            fetch_module._read_max_fetches_setting(snapshot, default=9)
            == expected
        )
    assert snapshot == original


@pytest.mark.parametrize("shape", ["raw", "legacy", "typed"])
@pytest.mark.parametrize(
    "env_value, typed_expected",
    [
        ("7", 7),
        ("3.9", 3),
        ("0", 0),
        ("51", 50),
        ("-1", 9),
        ("invalid", SettingsValueError),
        ("", 1),
        ("inf", SettingsValueError),
        ("1e400", SettingsValueError),
        ("nan", SettingsValueError),
        pytest.param("9" * 5000, SettingsValueError, 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, and an
    # override it cannot convert raises instead of falling back to the
    # snapshot; raw and sparse legacy snapshots deliberately retain their
    # historical behavior.
    expected = typed_expected if shape == "typed" else 1
    if expected is SettingsValueError:
        with pytest.raises(SettingsValueError) as raised:
            fetch_module._read_max_fetches_setting(snapshot, default=9)
        assert raised.value.source == "environment"
        assert env_value not in str(raised.value)
    else:
        assert (
            fetch_module._read_max_fetches_setting(snapshot, default=9)
            == expected
        )
    assert snapshot == {"search.fetch.max_per_topic": entry}


@pytest.mark.parametrize("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
    )


@pytest.mark.parametrize(
    "entry",
    [
        {"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_unsupported_type_is_not_replaced_by_default():
    with pytest.raises(SettingsValueError) as raised:
        fetch_module._read_max_fetches_setting(
            {"search.fetch.max_per_topic": {"value": 5, "ui_element": "bogus"}},
            default=9,
        )
    assert raised.value.key == "search.fetch.max_per_topic"
    assert raised.value.reason == "unsupported setting type"


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
        )


@pytest.mark.parametrize("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()


@pytest.mark.parametrize(
    "mode", ["full", "summary_focus", "summary_focus_query"]
)
@pytest.mark.parametrize(
    "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"}, 0, 3, 3),
        ({"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


@pytest.mark.parametrize(
    "mode", ["full", "summary_focus", "summary_focus_query"]
)
@pytest.mark.parametrize(
    "env_value, override, attempts, expected",
    [
        ("7", None, 8, 7),
        ("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


@pytest.mark.parametrize(
    "mode", ["full", "summary_focus", "summary_focus_query"]
)
@pytest.mark.parametrize(
    "entry, env_value, source",
    [
        ({"value": "inf", "ui_element": "number"}, None, "value"),
        ({"value": 1, "ui_element": "number"}, "inf", "environment"),
        ({"value": 1, "ui_element": "unsupported-widget"}, None, "value"),
    ],
    ids=["typed-row", "typed-override", "unsupported-type"],
)
def test_fetch_tools_refuse_an_invalid_typed_limit(
    mode, entry, env_value, source, monkeypatch
):
    # Without an explicit max_fetches the builder reads the setting. An
    # invalid typed limit must stop the build, not become the default 2.
    if env_value is not None:
        monkeypatch.setenv("LDR_SEARCH_FETCH_MAX_PER_TOPIC", env_value)
    with pytest.raises(SettingsValueError) as raised:
        build_fetch_tool(
            mode,
            SearchResultsCollector([]),
            model=_model_returning("Useful summary"),
            overall_query="Research question",
            settings_snapshot={"search.fetch.max_per_topic": entry},
        )
    assert raised.value.key == "search.fetch.max_per_topic"
    assert raised.value.source == source
    assert raised.value.__context__ is None