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.

95.8 KB
Raw Blame
"""Offline archive lookup and fetch behavior through the real tool pipeline."""

from contextlib import nullcontext
from functools import partial
from io import BytesIO
import uuid
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock

import pytest
import requests
from pydantic import ValidationError

from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
    SearchResultsCollector,
)
from local_deep_research.advanced_search_system.tools import archive
from local_deep_research.advanced_search_system.tools.fetch import (
    FETCH_MODES,
    build_fetch_tool,
)
from local_deep_research.security import clear_active_context
from local_deep_research.security.egress import policy
from local_deep_research.web_search_engines import wayback_client
from local_deep_research.web_search_engines.engine_availability import (
    SearchEngineAvailability,
)
from local_deep_research.web_search_engines.rate_limiting import RateLimitError


ORIGINAL = "https://example.com/history"
CAPTURED = "20240203112233"
FORMATTED_DATE = "2024-02-03 11:22:33 UTC"
API = "https://archive.org/wayback/available"
REPLAY = f"https://web.archive.org/web/{CAPTURED}/{ORIGINAL}"
_SESSION_REQUEST = requests.sessions.Session.request

# The fixture model is unregistered and has no configured context window:
# 8192 fallback tokens minus 6553 reply tokens leaves 1639 * 4 characters.
DEFAULT_SUMMARY_PAGE_LIMIT = 6556


MISMATCHED_TARGETS = [
    pytest.param(
        ORIGINAL, "https://unrelated.example/history", id="other_host"
    ),
    pytest.param(ORIGINAL, "https://example.com/other", id="other_path"),
    pytest.param(ORIGINAL, "https://example.com/History", id="path_case"),
    pytest.param(
        ORIGINAL + "?version=1", ORIGINAL + "?version=2", id="query_value"
    ),
    pytest.param(
        ORIGINAL + "?a=1&b=2", ORIGINAL + "?b=2&a=1", id="query_order"
    ),
    pytest.param(
        ORIGINAL + "?next=https%3A%2F%2Fother.example%2Fa",
        ORIGINAL + "?next=https://other.example/a",
        id="literal_query_encoding",
    ),
    pytest.param(
        "https://example.com:8443/history",
        "https://example.com:8444/history",
        id="nondefault_port",
    ),
    pytest.param(
        ORIGINAL,
        "http://example.com:443/history",
        id="wrong_scheme_default_port",
    ),
]


CANONICAL_TARGETS = [
    pytest.param("http://example.com/history", ORIGINAL, id="http_to_https"),
    pytest.param(ORIGINAL, "http://example.com/history", id="https_to_http"),
    pytest.param(
        "https://example.com:443/history", ORIGINAL, id="https_default_port"
    ),
    pytest.param(
        "http://example.com:80/history",
        "http://example.com/history",
        id="http_default_port",
    ),
    pytest.param("https://www.example.com/history", ORIGINAL, id="remove_www"),
    pytest.param(ORIGINAL, "https://www.example.com/history", id="add_www"),
    pytest.param(
        "HTTPS://WWW.Example.com:443/history", ORIGINAL, id="host_case"
    ),
    pytest.param(
        "https://example.com", "https://example.com/", id="empty_to_root_path"
    ),
    pytest.param(
        "https://example.com/", "https://example.com", id="root_to_empty_path"
    ),
    pytest.param(
        "https://www.example.com:443?next=https://other.example/a&version=1",
        "http://example.com:80/?next=https://other.example/a&version=1",
        id="combined_canonicalization_preserves_query",
    ),
]


def _available_payload(
    url=ORIGINAL, *, timestamp=CAPTURED, replay=None, **overrides
):
    closest = {
        "available": True,
        "status": "200",
        "timestamp": timestamp,
        "url": replay or f"http://web.archive.org/web/{timestamp}/{url}",
    }
    closest.update(overrides)
    return {"url": url, "archived_snapshots": {"closest": closest}}


def _response(payload=None, *, status=200):
    response = Mock()
    response.status_code = status
    response.headers = {}
    response.json.return_value = (
        _available_payload() if payload is None else payload
    )
    return response


@pytest.fixture(autouse=True)
def _offline(monkeypatch):
    clear_active_context()
    monkeypatch.setattr(
        requests.sessions.Session,
        "request",
        Mock(side_effect=AssertionError("Unexpected outbound request")),
    )
    yield
    clear_active_context()


@pytest.fixture
def runtime(monkeypatch):
    """Only HTTP, content extraction, and the summary model are substitutes."""
    response = _response()
    getter = Mock(return_value=response)
    monkeypatch.setattr(wayback_client, "safe_get", getter)
    fetcher = MagicMock()
    fetcher.fetch.return_value = {
        "status": "success",
        "title": "Historical page",
        "content": "Archived evidence from the selected capture.",
    }
    cm = MagicMock()
    cm.__enter__.return_value = fetcher
    cm.__exit__.return_value = False
    factory = Mock(return_value=cm)
    monkeypatch.setattr(
        "local_deep_research.content_fetcher.ContentFetcher", factory
    )
    model = Mock()
    model.invoke.return_value = SimpleNamespace(content="The archived fact.")
    return SimpleNamespace(
        response=response,
        getter=getter,
        fetcher=fetcher,
        factory=factory,
        model=model,
    )


def _arguments(mode, *, url=ORIGINAL, **extra):
    arguments = {"url": url, **extra}
    if mode != "full":
        arguments["focus"] = "What did the page say at that time?"
    return arguments


@pytest.mark.parametrize("mode", ["summary_focus", "summary_focus_query"])
@pytest.mark.parametrize("budget", [0, -1, True, 1.5, None, "invalid"])
def test_invalid_summary_budget_precedes_archive_lookup_and_quota(
    mode, budget, runtime
):
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        max_fetches=2,
        settings_snapshot={
            "search.fetch.summary_input_max_length": {
                "value": budget,
                "ui_element": "number",
            }
        },
    )

    message = tool.invoke(_arguments(mode))

    assert "must be a positive integer" in message
    assert "LDR_SEARCH_FETCH_SUMMARY_INPUT_MAX_LENGTH" in message
    assert collector.fetch_count == 0
    assert collector.results == []
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()
    runtime.model.invoke.assert_not_called()


@pytest.mark.parametrize("mode", ["summary_focus", "summary_focus_query"])
@pytest.mark.parametrize(
    ("key", "extra"),
    [
        ("llm.context_window_size", {"llm.context_window_unrestricted": False}),
        ("llm.local_context_window_size", {}),
        ("llm.max_tokens", {}),
    ],
)
def test_invalid_managed_model_budget_precedes_archive_lookup_and_quota(
    mode, key, extra, runtime
):
    collector = SearchResultsCollector([])
    invalid = "private-invalid-config-value"
    tool = archive.build_archive_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        max_fetches=2,
        settings_snapshot={
            **extra,
            key: {"value": invalid, "ui_element": "number"},
        },
    )

    message = tool.invoke(_arguments(mode))

    assert "Invalid setting" in message
    assert key in message
    assert invalid not in message
    assert collector.fetch_count == 0
    assert collector.results == []
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()
    runtime.model.invoke.assert_not_called()


def test_invalid_summary_budget_keeps_archive_full_mode_available(runtime):
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        "full",
        collector,
        settings_snapshot={
            "search.fetch.summary_input_max_length": {
                "value": "invalid",
                "ui_element": "number",
            }
        },
    )

    message = tool.invoke(_arguments("full"))

    assert "Archived evidence" in message
    assert collector.fetch_count == 1
    _assert_source(collector)
    runtime.getter.assert_called_once()
    runtime.model.invoke.assert_not_called()


def _assert_source(collector, *, replay=REPLAY, timestamp=CAPTURED):
    assert len(collector.results) == 1
    source = collector.results[0]
    assert source["link"] == replay
    assert source["original_url"] == ORIGINAL
    assert source["requested_url"] == ORIGINAL
    assert source["timestamp"] == timestamp
    assert source["formatted_date"] == FORMATTED_DATE
    assert source["source_type"] == "archive"
    assert collector.find_by_index(int(source["index"]))["link"] == replay
    assert collector.find_by_url(ORIGINAL) is None
    return source


@pytest.mark.parametrize(
    "replay",
    [
        pytest.param(
            "https://archive.org/web/20240203112233/https://example.com/history",
            id="archive_org_redirector",
        ),
        pytest.param(
            "https://web.archive.org/WEB/20240203112233/https://example.com/history",
            id="uppercase_web",
        ),
    ],
)
@pytest.mark.parametrize("mode", FETCH_MODES[1:])
def test_unsupported_direct_replay_explains_exact_capture_spelling(
    replay, mode, runtime
):
    collector = SearchResultsCollector([])
    tool = build_fetch_tool(mode, collector, model=runtime.model)

    output = tool.invoke(_arguments(mode, url=replay))

    assert "Unsupported archive replay capture URL" in output
    assert "https://web.archive.org/web/<timestamp>/<url>" in output
    assert "timestamp is 14 digits" in output
    assert "url is the original HTTP(S) URL" in output
    assert collector.results == []
    assert collector.fetch_count == 0
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()
    runtime.fetcher.fetch.assert_not_called()
    runtime.model.invoke.assert_not_called()


@pytest.mark.parametrize("timestamp", [None, "20240115"])
def test_lookup_uses_fixed_https_api_and_preserves_actual_capture(timestamp):
    response = _response()
    getter = Mock(return_value=response)

    snapshot = wayback_client.lookup_archive_snapshot(
        ORIGINAL, timestamp, request_get=getter
    )

    params = {"url": ORIGINAL}
    if timestamp is not None:
        params["timestamp"] = timestamp
    getter.assert_called_once_with(
        API, params=params, timeout=30, allow_redirects=False
    )
    assert snapshot == {
        "original_url": ORIGINAL,
        "requested_url": ORIGINAL,
        "url": REPLAY,
        "timestamp": CAPTURED,
        "formatted_date": FORMATTED_DATE,
        "available": True,
        "status": "200",
    }
    response.close.assert_called_once()


@pytest.mark.parametrize(("requested", "actual"), MISMATCHED_TARGETS)
def test_lookup_provenance_rejects_unrelated_replay_target(requested, actual):
    replay = f"http://web.archive.org/web/{CAPTURED}/{actual}"
    response = _response(_available_payload(requested, replay=replay))
    getter = Mock(return_value=response)

    with pytest.raises(ValueError, match="requested URL"):
        wayback_client.lookup_archive_snapshot(requested, request_get=getter)

    getter.assert_called_once_with(
        API, params={"url": requested}, timeout=30, allow_redirects=False
    )
    response.close.assert_called_once()


@pytest.mark.parametrize(("requested", "actual"), MISMATCHED_TARGETS)
@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
def test_archive_provenance_mismatch_never_fetches_or_registers_source(
    requested, actual, mode, runtime
):
    replay = f"http://web.archive.org/web/{CAPTURED}/{actual}"
    runtime.response.json.return_value = _available_payload(
        requested, replay=replay
    )
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        mode, collector, model=runtime.model
    )

    output = tool.invoke(_arguments(mode, url=requested))

    assert not output.startswith("[1]")
    assert "requested URL" in output
    assert collector.results == []
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()
    runtime.factory.assert_not_called()
    runtime.model.invoke.assert_not_called()


@pytest.mark.parametrize(("requested", "actual"), CANONICAL_TARGETS)
def test_lookup_canonical_target_keeps_actual_and_requested_urls(
    requested, actual
):
    response = _response(
        _available_payload(
            requested, replay=f"http://web.archive.org/web/{CAPTURED}/{actual}"
        )
    )
    getter = Mock(return_value=response)

    snapshot = wayback_client.lookup_archive_snapshot(
        requested, request_get=getter
    )

    assert snapshot["original_url"] == actual
    assert snapshot["requested_url"] == requested
    assert snapshot["url"] == f"https://web.archive.org/web/{CAPTURED}/{actual}"
    assert snapshot["timestamp"] == CAPTURED
    assert snapshot["formatted_date"] == FORMATTED_DATE
    getter.assert_called_once_with(
        API, params={"url": requested}, timeout=30, allow_redirects=False
    )
    response.close.assert_called_once()


@pytest.mark.parametrize(("requested", "actual"), CANONICAL_TARGETS)
@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
def test_archive_canonical_target_keeps_actual_provenance_in_citation_and_output(
    requested, actual, mode, runtime
):
    replay = f"https://web.archive.org/web/{CAPTURED}/{actual}"
    runtime.response.json.return_value = _available_payload(
        requested, replay=replay
    )
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        mode, collector, model=runtime.model
    )

    output = tool.invoke(_arguments(mode, url=requested))

    assert output.startswith("[1] ")
    assert f"URL: {replay}" in output
    assert f"Original URL: {actual}" in output
    assert f"Requested URL: {requested}" in output
    assert len(collector.results) == 1
    source = collector.results[0]
    assert source["link"] == replay
    assert source["original_url"] == actual
    assert source["requested_url"] == requested
    assert source["source_type"] == "archive"
    assert source["timestamp"] == CAPTURED
    assert source["formatted_date"] == FORMATTED_DATE
    assert collector.find_by_index(1)["original_url"] == actual
    assert collector.find_by_index(1)["requested_url"] == requested
    assert collector.fetch_count == 1
    runtime.fetcher.fetch.assert_called_once_with(
        replay,
        max_length=10_000 if mode == "full" else DEFAULT_SUMMARY_PAGE_LIMIT,
    )
    assert runtime.getter.call_args.kwargs["params"]["url"] == requested


@pytest.mark.parametrize("first_kind", ["ordinary", "archive"])
def test_regular_fetch_preserves_existing_archive_citation_metadata(
    first_kind, runtime
):
    actual = "http://www.example.com:80/history"
    replay = f"https://web.archive.org/web/{CAPTURED}/{actual}"
    runtime.response.json.return_value = _available_payload(
        ORIGINAL, replay=replay
    )
    collector = SearchResultsCollector([])
    ordinary = build_fetch_tool("full", collector, max_fetches=3)
    archived = archive.build_archive_fetch_tool(
        "full", collector, max_fetches=3
    )
    if first_kind == "ordinary":
        assert ordinary.invoke({"url": replay}).startswith("[1] ")

    assert archived.invoke({"url": ORIGINAL}).startswith("[1] ")
    metadata = {
        "source_type": "archive",
        "original_url": actual,
        "requested_url": ORIGINAL,
        "timestamp": CAPTURED,
        "formatted_date": FORMATTED_DATE,
    }
    assert all(
        collector.find_by_index(1)[key] == value
        for key, value in metadata.items()
    )

    output = ordinary.invoke({"url": replay})

    assert output.startswith("[1] ")
    assert len(collector.results) == 1
    assert collector.results[0]["link"] == replay
    assert all(
        collector.results[0][key] == value for key, value in metadata.items()
    )
    assert collector.fetch_count == (3 if first_kind == "ordinary" else 2)
    runtime.getter.assert_called_once()


@pytest.mark.parametrize(
    "payload",
    [
        {"archived_snapshots": {}},
        _available_payload(available=False),
        _available_payload(status="404"),
        _available_payload(status="500"),
    ],
    ids=["no_capture", "unavailable", "archived_404", "archived_500"],
)
def test_unavailable_lookup_returns_none_and_never_fetches_live(payload):
    response = _response(payload)
    getter = Mock(return_value=response)

    assert (
        wayback_client.lookup_archive_snapshot(ORIGINAL, request_get=getter)
        is None
    )
    getter.assert_called_once_with(
        API, params={"url": ORIGINAL}, timeout=30, allow_redirects=False
    )
    response.close.assert_called_once()


MALFORMED_PAYLOADS = [
    pytest.param([], id="non_object_response"),
    pytest.param({}, id="missing_snapshots"),
    pytest.param({"archived_snapshots": []}, id="non_object_snapshots"),
    pytest.param(
        {"archived_snapshots": {"closest": []}}, id="non_object_capture"
    ),
    pytest.param(
        _available_payload(timestamp="20240203"), id="short_timestamp"
    ),
    pytest.param(
        _available_payload(timestamp="20240230112233"), id="impossible_capture"
    ),
    pytest.param(_available_payload(timestamp=None), id="missing_timestamp"),
    pytest.param(
        _available_payload(
            replay="https://example.net/web/" + CAPTURED + "/" + ORIGINAL
        ),
        id="foreign_replay_host",
    ),
    pytest.param(
        _available_payload(
            replay=f"https://web.archive.org.evil.example/web/{CAPTURED}/{ORIGINAL}"
        ),
        id="lookalike_replay_host",
    ),
    pytest.param(
        _available_payload(
            replay=f"https://[email protected]/web/{CAPTURED}/{ORIGINAL}"
        ),
        id="credentialed_replay_host",
    ),
    pytest.param(
        _available_payload(
            replay=f"https://web.archive.org:8443/web/{CAPTURED}/{ORIGINAL}"
        ),
        id="foreign_replay_port",
    ),
    pytest.param(
        _available_payload(
            replay=f"https://web.archive.org/web/20240101000000/{ORIGINAL}"
        ),
        id="replay_timestamp_mismatch",
    ),
    pytest.param(
        _available_payload(
            replay=f"https://web.archive.org/web/{CAPTURED}/ftp://example.com/file"
        ),
        id="unsupported_embedded_scheme",
    ),
    pytest.param(
        _available_payload(
            replay=f"https://web.archive.org/web/{CAPTURED}/https://user:[email protected]/"
        ),
        id="credentialed_embedded_target",
    ),
    pytest.param(
        _available_payload(replay=f"https://web.archive.org/web/{CAPTURED}/"),
        id="missing_embedded_target",
    ),
    pytest.param(_available_payload(replay=API), id="api_is_not_replay"),
]


@pytest.mark.parametrize("payload", MALFORMED_PAYLOADS)
def test_malformed_lookup_closes_response_and_rejects_capture(payload):
    response = _response(payload)
    getter = Mock(return_value=response)

    with pytest.raises(ValueError):
        wayback_client.lookup_archive_snapshot(ORIGINAL, request_get=getter)

    getter.assert_called_once()
    response.close.assert_called_once()


def test_lookup_429_is_rate_limit_and_closes_response():
    response = _response(status=429)
    response.headers = {"Retry-After": "120"}
    getter = Mock(return_value=response)

    with pytest.raises(RateLimitError):
        wayback_client.lookup_archive_snapshot(ORIGINAL, request_get=getter)

    response.json.assert_not_called()
    response.close.assert_called_once()
    getter.assert_called_once()


@pytest.mark.parametrize("status", [302, 503])
def test_lookup_does_not_follow_failed_or_redirected_api(status):
    response = _response(status=status)
    if status == 503:
        response.raise_for_status.side_effect = requests.HTTPError("HTTP 503")
    getter = Mock(return_value=response)

    with pytest.raises((ValueError, requests.HTTPError)):
        wayback_client.lookup_archive_snapshot(ORIGINAL, request_get=getter)

    response.json.assert_not_called()
    response.close.assert_called_once()
    assert getter.call_args.kwargs["allow_redirects"] is False


def test_disabled_mode_returns_none_without_work(runtime):
    assert (
        archive.build_archive_fetch_tool("disabled", SearchResultsCollector([]))
        is None
    )
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()


@pytest.mark.parametrize(
    "mode", ["full", "summary_focus", "summary_focus_query"]
)
def test_archive_schema_matches_fetch_mode(mode, runtime):
    tool = archive.build_archive_fetch_tool(
        mode, SearchResultsCollector([]), model=runtime.model
    )

    assert tool.name == "fetch_archive"
    assert set(tool.args) == (
        {"url", "date"} if mode == "full" else {"url", "date", "focus"}
    )
    schema = tool.args_schema.model_json_schema()
    assert set(schema["required"]) == (
        {"url"} if mode == "full" else {"url", "focus"}
    )
    assert schema["properties"]["date"]["default"] is None
    if mode != "full":
        with pytest.raises(ValidationError):
            tool.invoke({"url": ORIGINAL})
    runtime.getter.assert_not_called()


@pytest.mark.parametrize("mode", ["summary_focus", "summary_focus_query"])
def test_summary_archive_requires_model(mode, runtime):
    with pytest.raises(ValueError, match="requires a model"):
        archive.build_archive_fetch_tool(mode, SearchResultsCollector([]))
    runtime.getter.assert_not_called()


def test_unknown_archive_mode_is_rejected(runtime):
    with pytest.raises(ValueError, match="Unknown fetch mode"):
        archive.build_archive_fetch_tool("unknown", SearchResultsCollector([]))
    runtime.getter.assert_not_called()


@pytest.mark.parametrize(
    "mode", ["full", "summary_focus", "summary_focus_query"]
)
@pytest.mark.parametrize("date", [None, "2024-01-15", "20240115", "2024-02-29"])
def test_successful_archive_fetch_keeps_replay_citation_and_nearest_date(
    mode, date, runtime
):
    collector = SearchResultsCollector([])
    overall_query = "How did the policy change over time?"
    tool = archive.build_archive_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        overall_query=overall_query,
        max_fetches=1,
    )
    arguments = _arguments(mode)
    if date is not None:
        arguments["date"] = date

    output = tool.invoke(arguments)

    assert output.startswith("[1] ")
    assert f"URL: {REPLAY}" in output
    assert FORMATTED_DATE in output
    assert ORIGINAL in output
    assert "Requested URL:" not in output
    _assert_source(collector)
    params = {"url": ORIGINAL}
    if date is not None:
        params["timestamp"] = date.replace("-", "")
        assert date in output
    runtime.getter.assert_called_once_with(
        API, params=params, timeout=30, allow_redirects=False
    )
    runtime.fetcher.fetch.assert_called_once_with(
        REPLAY,
        max_length=10_000 if mode == "full" else DEFAULT_SUMMARY_PAGE_LIMIT,
    )
    assert collector.fetch_count == 1
    if mode == "full":
        assert "Archived evidence" in output
        runtime.model.invoke.assert_not_called()
    else:
        assert "The archived fact." in output
        prompt = runtime.model.invoke.call_args.args[0]
        assert REPLAY in prompt
        assert "Archived evidence" in prompt
        assert arguments["focus"] in prompt
        if mode == "summary_focus":
            assert "Overall research question" not in prompt
            assert overall_query not in prompt
        else:
            assert f"Overall research question: {overall_query}" in prompt


@pytest.mark.parametrize(
    "url",
    [
        "https://example.com/wiki/Foo_(bar)",
        "https://example.com/path,segment;version?values=a,b;c",
        "https://example.com/page?next=https://other.example/a?x=1&y=2#part",
        "https://example.com:8443/path)?q=%2F%3F%26&next=https://other.example/a",
    ],
)
def test_absolute_url_punctuation_and_nested_query_are_preserved(url, runtime):
    runtime.response.json.return_value = _available_payload(url)
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool("full", collector)

    output = tool.invoke({"url": url})

    replay = f"https://web.archive.org/web/{CAPTURED}/{url}"
    assert runtime.getter.call_args.kwargs["params"]["url"] == url
    runtime.fetcher.fetch.assert_called_once_with(replay, max_length=10_000)
    assert collector.results[0]["original_url"] == url
    assert collector.results[0]["requested_url"] == url
    assert collector.results[0]["link"] == replay
    assert f"URL: {replay}" in output


@pytest.mark.parametrize(
    "url",
    [
        "",
        "historical information about a topic",
        "example.com/page",
        "https:///missing-host",
        "ftp://example.com/file",
        "file:///etc/passwd",
        "https://user:[email protected]/page",
        "https://example.com:bad/page",
        "https://example.com:99999/page",
        "https://example.com/path with spaces",
    ],
)
def test_invalid_original_url_never_uses_budget_or_transport(url, runtime):
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool("full", collector)

    output = tool.invoke({"url": url})

    assert not output.startswith("[1]")
    assert "secret" not in output
    assert collector.fetch_count == 0
    assert collector.results == []
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()


@pytest.mark.parametrize(
    "date",
    [
        "",
        "2024",
        "2024-2-03",
        "2024-13-01",
        "2023-02-29",
        "20240230",
        "2024-01-01T00:00:00",
        "20240203112233",
    ],
)
def test_invalid_calendar_date_never_uses_budget_or_transport(date, runtime):
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool("full", collector)

    output = tool.invoke({"url": ORIGINAL, "date": date})

    assert not output.startswith("[1]")
    assert collector.fetch_count == 0
    assert collector.results == []
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()


@pytest.mark.parametrize("payload", MALFORMED_PAYLOADS)
def test_malformed_snapshot_does_not_fetch_or_register_live_page(
    payload, runtime
):
    runtime.response.json.return_value = payload
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool("full", collector, max_fetches=1)

    output = tool.invoke({"url": ORIGINAL})

    assert not output.startswith("[1]")
    assert collector.results == []
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()
    runtime.factory.assert_not_called()


@pytest.mark.parametrize(
    "failure", ["no_capture", "unavailable", "429", "bad_json", "connection"]
)
def test_lookup_failure_consumes_one_slot_and_never_falls_back(
    failure, runtime
):
    if failure == "no_capture":
        runtime.response.json.return_value = {"archived_snapshots": {}}
    elif failure == "unavailable":
        runtime.response.json.return_value = _available_payload(available=False)
    elif failure == "429":
        runtime.response.status_code = 429
    elif failure == "bad_json":
        runtime.response.json.side_effect = ValueError("Invalid JSON")
    else:
        runtime.getter.side_effect = requests.ConnectionError(
            "Archive unavailable"
        )
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool("full", collector, max_fetches=1)

    first = tool.invoke({"url": ORIGINAL})
    second = tool.invoke({"url": ORIGINAL, "date": "2024-01-01"})

    assert not first.startswith("[1]")
    assert "budget exhausted" in second.lower()
    assert collector.fetch_count == 1
    assert collector.results == []
    runtime.getter.assert_called_once()
    runtime.factory.assert_not_called()


@pytest.mark.parametrize("mode", ["summary_focus", "summary_focus_query"])
@pytest.mark.parametrize("empty_stage", ["content", "summary"])
def test_empty_archive_content_or_summary_does_not_register_source(
    mode, empty_stage, runtime
):
    if empty_stage == "content":
        runtime.fetcher.fetch.return_value["content"] = " \n\t "
    else:
        runtime.model.invoke.return_value.content = " \n\t "
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        mode, collector, model=runtime.model, max_fetches=1
    )

    output = tool.invoke(_arguments(mode))

    assert "NOT RELEVANT" in output
    assert collector.results == []
    assert collector.fetch_count == 1
    runtime.fetcher.fetch.assert_called_once_with(
        REPLAY,
        max_length=10_000 if mode == "full" else DEFAULT_SUMMARY_PAGE_LIMIT,
    )
    if empty_stage == "content":
        runtime.model.invoke.assert_not_called()
    else:
        runtime.model.invoke.assert_called_once()


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
@pytest.mark.parametrize("failure", ["result", "exception"])
def test_replay_fetch_failure_never_substitutes_live_original(
    mode, failure, runtime
):
    if failure == "result":
        runtime.fetcher.fetch.return_value = {
            "status": "error",
            "error": "HTTP 404",
        }
    else:
        runtime.fetcher.fetch.side_effect = requests.ConnectionError(
            "Replay unavailable"
        )
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        mode, collector, model=runtime.model
    )

    output = tool.invoke(_arguments(mode))

    assert not output.startswith("[1]")
    assert collector.results == []
    assert collector.fetch_count == 1
    runtime.fetcher.fetch.assert_called_once_with(
        REPLAY,
        max_length=10_000 if mode == "full" else DEFAULT_SUMMARY_PAGE_LIMIT,
    )
    runtime.model.invoke.assert_not_called()


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
def test_archive_delegates_settings_limits_and_metadata_to_shared_fetch(
    mode, runtime, monkeypatch
):
    settings = {
        "search.fetch.content_max_length": {"value": 3000},
        "search.fetch.summary_input_max_length": {
            "value": 4000,
            "ui_element": "number",
        },
        "web.enable_javascript_rendering": {
            "value": True,
            "ui_element": "checkbox",
        },
    }
    context = object()
    monkeypatch.setattr(
        archive,
        "evaluate_engine",
        Mock(return_value=policy.Decision(True, "allowed")),
    )
    monkeypatch.setattr(
        policy,
        "evaluate_url",
        Mock(return_value=policy.Decision(True, "allowed")),
    )
    delegate = Mock(wraps=build_fetch_tool)
    monkeypatch.setattr(archive, "build_fetch_tool", delegate)
    body = "A" * 4000 + "EXCLUDED_TRAILING_EVIDENCE"
    runtime.fetcher.fetch.side_effect = lambda url, *, max_length: {
        "status": "success",
        "title": "Historical page",
        "content": body[:max_length],
    }
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        overall_query="Original question",
        settings_snapshot=settings,
        egress_context=context,
        max_fetches=1,
        subagent_id="archive_worker",
    )

    output = tool.invoke(_arguments(mode))

    runtime.fetcher.fetch.assert_called_once_with(
        REPLAY, max_length=3000 if mode == "full" else 4000
    )
    assert runtime.factory.call_args.kwargs["enable_js_rendering"] is True
    assert runtime.factory.call_args.kwargs["egress_context"] is context
    assert "EXCLUDED_TRAILING_EVIDENCE" not in output
    if mode == "full":
        assert "A" * 3000 in output
        assert "A" * 3001 not in output
    else:
        prompt = runtime.model.invoke.call_args.args[0]
        assert "A" * 4000 in prompt
        assert "A" * 4001 not in prompt
        assert "EXCLUDED_TRAILING_EVIDENCE" not in prompt
    delegate.assert_called_once()
    assert delegate.call_args.args == (mode, collector)
    kwargs = delegate.call_args.kwargs
    assert kwargs["record_fetch"] is False
    assert kwargs["settings_snapshot"] is settings
    assert kwargs["model"] is runtime.model
    assert kwargs["overall_query"] == "Original question"
    assert kwargs["egress_context"] is context
    assert kwargs["subagent_id"] == "archive_worker"
    assert kwargs["max_fetches"] == 1
    assert kwargs["source_metadata"] == {
        "source_type": "archive",
        "original_url": ORIGINAL,
        "requested_url": ORIGINAL,
        "timestamp": CAPTURED,
        "formatted_date": FORMATTED_DATE,
    }
    _assert_source(collector)
    assert collector.fetch_count == 1
    assert collector.get_fetch_count("archive_worker") == 1


@pytest.mark.parametrize("first_kind", ["archive", "ordinary"])
def test_archive_and_ordinary_fetch_use_the_same_quota(first_kind, runtime):
    collector = SearchResultsCollector([])
    tools = {
        "archive": archive.build_archive_fetch_tool(
            "full", collector, max_fetches=1
        ),
        "ordinary": build_fetch_tool("full", collector, max_fetches=1),
    }
    second_kind = "ordinary" if first_kind == "archive" else "archive"

    first = tools[first_kind].invoke({"url": ORIGINAL})
    second = tools[second_kind].invoke({"url": ORIGINAL})

    assert first.startswith("[1] ")
    assert "budget exhausted" in second.lower()
    assert collector.fetch_count == 1
    runtime.fetcher.fetch.assert_called_once()
    assert runtime.getter.call_count == (1 if first_kind == "archive" else 0)


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
def test_successful_archive_uses_one_slot_and_exhaustion_prevents_lookup(
    mode, runtime
):
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        mode, collector, model=runtime.model, max_fetches=1
    )

    first = tool.invoke(_arguments(mode))
    second = tool.invoke(_arguments(mode, date="2024-01-01"))

    assert first.startswith("[1] ")
    assert "budget exhausted" in second.lower()
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()
    runtime.fetcher.fetch.assert_called_once()


@pytest.mark.parametrize("limit", [1, {"value": 1}])
def test_archive_reads_shared_budget_from_settings(limit, runtime):
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        "full",
        collector,
        settings_snapshot={"search.fetch.max_per_topic": limit},
    )

    assert tool.invoke({"url": ORIGINAL}).startswith("[1] ")
    assert "budget exhausted" in tool.invoke({"url": ORIGINAL}).lower()
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()


def test_explicit_unlimited_archive_budget_overrides_settings(runtime):
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        "full",
        collector,
        max_fetches=0,
        settings_snapshot={"search.fetch.max_per_topic": 1},
    )

    for _ in range(3):
        assert tool.invoke({"url": ORIGINAL}).startswith("[1] ")

    assert collector.fetch_count == 3
    assert runtime.getter.call_count == runtime.fetcher.fetch.call_count == 3


def test_subagent_archive_budget_is_shared_with_its_ordinary_fetch_only(
    runtime,
):
    collector = SearchResultsCollector([])
    lead = build_fetch_tool(
        "full", collector, max_fetches=1, subagent_id="lead"
    )
    worker_archive = archive.build_archive_fetch_tool(
        "full", collector, max_fetches=1, subagent_id="worker"
    )
    worker_ordinary = build_fetch_tool(
        "full", collector, max_fetches=1, subagent_id="worker"
    )

    assert lead.invoke({"url": ORIGINAL}).startswith("[1] ")
    assert worker_archive.invoke({"url": ORIGINAL}).startswith("[2] ")
    assert (
        "budget exhausted" in worker_ordinary.invoke({"url": ORIGINAL}).lower()
    )
    assert collector.get_fetch_count("lead") == 1
    assert collector.get_fetch_count("worker") == 1
    assert collector.fetch_count == 2
    assert runtime.fetcher.fetch.call_count == 2
    runtime.getter.assert_called_once()


def test_distinct_snapshots_of_same_original_have_distinct_citations(runtime):
    second_timestamp = "20250304123456"
    runtime.response.json.side_effect = [
        _available_payload(),
        _available_payload(timestamp=second_timestamp),
    ]
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool("full", collector, max_fetches=2)

    first = tool.invoke({"url": ORIGINAL, "date": "2024-02-01"})
    second = tool.invoke({"url": ORIGINAL, "date": "2025-03-01"})

    assert first.startswith("[1] ")
    assert second.startswith("[2] ")
    assert len(collector.results) == 2
    assert [source["timestamp"] for source in collector.results] == [
        CAPTURED,
        second_timestamp,
    ]
    assert [source["link"] for source in collector.results] == [
        REPLAY,
        f"https://web.archive.org/web/{second_timestamp}/{ORIGINAL}",
    ]
    assert len(set(collector.sources)) == 2
    assert all(
        source["original_url"] == ORIGINAL for source in collector.results
    )
    assert all(
        source["source_type"] == "archive" for source in collector.results
    )
    assert collector.results[1]["formatted_date"] == "2025-03-04 12:34:56 UTC"
    assert collector.fetch_count == 2


@pytest.mark.parametrize(
    "flag", [False, 0, "0", "false", "off", "no", " false "]
)
@pytest.mark.parametrize("wrapped", [False, True], ids=["raw", "wrapped"])
def test_agent_enabled_flag_hides_archive_tool(flag, wrapped, runtime):
    setting = {"value": flag, "ui_element": "checkbox"} if wrapped else flag
    assert (
        archive.build_archive_fetch_tool(
            "full",
            SearchResultsCollector([]),
            settings_snapshot={
                "search.engine.web.wayback.agent_enabled": setting
            },
        )
        is None
    )
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()


def test_builder_policy_denial_hides_archive_tool(runtime, monkeypatch):
    context = object()
    gate = Mock(return_value=policy.Decision(False, "scope_mismatch"))
    monkeypatch.setattr(archive, "evaluate_engine", gate)

    assert (
        archive.build_archive_fetch_tool(
            "full", SearchResultsCollector([]), egress_context=context
        )
        is None
    )

    assert gate.call_args.args == ("wayback", context)
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()


def test_engine_policy_is_rechecked_before_each_archive_call(
    runtime, monkeypatch
):
    context = object()
    gate = Mock(return_value=policy.Decision(True, "allowed"))
    monkeypatch.setattr(archive, "evaluate_engine", gate)
    url_gate = Mock(return_value=policy.Decision(True, "allowed"))
    monkeypatch.setattr(policy, "evaluate_url", url_gate)
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        "full", collector, egress_context=context
    )
    gate.return_value = policy.Decision(False, "scope_mismatch")

    output = tool.invoke({"url": ORIGINAL})

    assert "egress policy" in output.lower()
    assert gate.call_count == 2
    assert collector.fetch_count == 0
    assert collector.results == []
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()
    url_gate.assert_not_called()


@pytest.mark.parametrize("denied_url", [None, ORIGINAL, API, REPLAY])
def test_url_policy_gates_before_lookup_and_replay_transport(
    denied_url, runtime, monkeypatch
):
    context = object()
    events = []
    monkeypatch.setattr(
        archive,
        "evaluate_engine",
        Mock(return_value=policy.Decision(True, "allowed")),
    )

    def evaluate_url(url, ctx):
        assert ctx is context
        events.append(("gate", url))
        return policy.Decision(
            url != denied_url,
            "scope_mismatch" if url == denied_url else "allowed",
        )

    def get(*args, **kwargs):
        events.append(("lookup", args[0]))
        return runtime.response

    def fetch(url, **kwargs):
        events.append(("fetch", url))
        return {
            "status": "success",
            "title": "Page",
            "content": "Archived evidence",
        }

    monkeypatch.setattr(policy, "evaluate_url", evaluate_url)
    runtime.getter.side_effect = get
    runtime.fetcher.fetch.side_effect = fetch
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        "full", collector, egress_context=context
    )

    output = tool.invoke({"url": ORIGINAL})

    if denied_url in {ORIGINAL, API}:
        assert collector.fetch_count == 0
        runtime.getter.assert_not_called()
        runtime.factory.assert_not_called()
    else:
        assert collector.fetch_count == 1
        assert events.index(("gate", ORIGINAL)) < events.index(("lookup", API))
        assert events.index(("gate", API)) < events.index(("lookup", API))
        runtime.getter.assert_called_once()
        if denied_url == REPLAY:
            runtime.factory.assert_not_called()
        else:
            assert events.index(("gate", REPLAY)) < events.index(
                ("fetch", REPLAY)
            )
            assert output.startswith("[1] ")
    if denied_url is not None:
        assert "egress policy" in output.lower()
        assert collector.results == []


def test_redirected_original_inside_capture_is_also_policy_gated(
    runtime, monkeypatch
):
    redirected = "http://www.example.com:80/history"
    replay = f"https://web.archive.org/web/{CAPTURED}/{redirected}"
    runtime.response.json.return_value = _available_payload(replay=replay)
    context = object()
    monkeypatch.setattr(
        archive,
        "evaluate_engine",
        Mock(return_value=policy.Decision(True, "allowed")),
    )
    urls = []

    def evaluate_url(url, ctx):
        urls.append(url)
        return policy.Decision(
            url != redirected,
            "scope_mismatch" if url == redirected else "allowed",
        )

    monkeypatch.setattr(policy, "evaluate_url", evaluate_url)
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        "full", collector, egress_context=context
    )

    output = tool.invoke({"url": ORIGINAL})

    assert "egress policy" in output.lower()
    assert urls == [ORIGINAL, API, redirected]
    assert collector.fetch_count == 1
    assert collector.results == []
    runtime.getter.assert_called_once()
    runtime.factory.assert_not_called()


def test_archive_cooldown_is_shared_and_prevents_new_lookup(runtime):
    availability = SearchEngineAvailability()
    collector = SearchResultsCollector([])
    runtime.response.status_code = 429
    runtime.response.headers = {"Retry-After": "120"}
    first_tool = archive.build_archive_fetch_tool(
        "full", collector, max_fetches=2, availability=availability
    )
    second_tool = archive.build_archive_fetch_tool(
        "full", collector, max_fetches=2, availability=availability
    )

    first = first_tool.invoke({"url": ORIGINAL})
    second = second_tool.invoke({"url": ORIGINAL, "date": "2024-01-01"})

    assert not first.startswith("[1]")
    assert not second.startswith("[1]")
    assert availability.state("wayback") is not None
    assert collector.fetch_count == 1
    assert collector.results == []
    runtime.getter.assert_called_once()
    runtime.factory.assert_not_called()


@pytest.fixture
def scripted_transport(monkeypatch):
    """Use real Requests redirect handling with a completely local adapter."""
    from local_deep_research.content_fetcher import fetcher as fetcher_module
    from local_deep_research.security import safe_requests

    routes = {}
    sent = []
    # Offline transport tests do not learn or wait on persisted host limits.
    # The downloader's real retry loop still runs against the adapter.
    rate = Mock()
    rate.apply_rate_limit.return_value = 0
    monkeypatch.setattr(
        "local_deep_research.research_library.downloaders.base.AdaptiveRateLimitTracker",
        Mock(return_value=rate),
    )
    # This fixture replaces the autouse request blocker with an adapter that
    # rejects anything not explicitly scripted. No socket or DNS is used.
    monkeypatch.setattr(requests.sessions.Session, "request", _SESSION_REQUEST)
    monkeypatch.setattr(
        safe_requests.ssrf_validator, "validate_url", Mock(return_value=True)
    )
    monkeypatch.setattr(
        safe_requests.dns_pinning,
        "pinned_request",
        lambda *args, **kwargs: nullcontext(),
    )
    monkeypatch.setattr(
        fetcher_module, "policy_aware_validate_url", Mock(return_value=True)
    )

    def send(adapter, request, **kwargs):
        sent.append(request.url)
        assert request.url in routes, f"Unscripted request: {request.url}"
        spec = routes[request.url]
        if "error" in spec:
            raise spec["error"]
        response = requests.Response()
        response.request = request
        response.url = request.url
        response.status_code = spec.get("status", 200)
        body = spec.get("body", b"")
        response.headers.update(
            {
                "Content-Type": "text/html; charset=utf-8",
                "Content-Length": str(len(body)),
                **spec.get("headers", {}),
            }
        )
        response.encoding = "utf-8"
        response.raw = BytesIO(body)
        response._content = body
        response._content_consumed = True
        if "body_error" in spec:

            class BrokenBody(BytesIO):
                def stream(self, *args, **kwargs):
                    raise spec["body_error"]

            response.raw = BrokenBody()
            response._content = False
            response._content_consumed = False
        return response

    monkeypatch.setattr(requests.adapters.HTTPAdapter, "send", send)
    return SimpleNamespace(routes=routes, sent=sent)


DENIED_REDIRECTS = [
    pytest.param(ORIGINAL, id="live_original"),
    pytest.param("https://other.example/history", id="foreign_host"),
    pytest.param(
        REPLAY.replace("https://web.archive", "http://web.archive"),
        id="http_downgrade",
    ),
    pytest.param(
        REPLAY.replace(CAPTURED, "20240203112234"), id="different_capture"
    ),
]


@pytest.mark.parametrize("redirect", DENIED_REDIRECTS)
def test_real_safe_session_blocks_redirect_outside_selected_capture(
    redirect, scripted_transport
):
    from local_deep_research.security.safe_requests import SafeSession

    scripted_transport.routes[REPLAY] = {
        "status": 302,
        "headers": {"Location": redirect},
    }
    with SafeSession() as session:
        session.url_guard = partial(
            wayback_client.validate_capture_fetch_url,
            timestamp=CAPTURED,
            expected_url=ORIGINAL,
        )
        with pytest.raises(ValueError, match="selected capture"):
            session.get(REPLAY, allow_redirects=True, timeout=30)

    assert scripted_transport.sent == [REPLAY]


@pytest.mark.parametrize("modifier", ["", "id_", "if_"])
def test_real_safe_session_allows_redirect_with_same_capture_timestamp(
    modifier, scripted_transport
):
    from local_deep_research.security.safe_requests import SafeSession

    redirect = f"https://web.archive.org/web/{CAPTURED}{modifier}/http://www.example.com:80/history"
    scripted_transport.routes.update(
        {
            REPLAY: {"status": 302, "headers": {"Location": redirect}},
            redirect: {"body": b"Archived final page"},
        }
    )
    with SafeSession() as session:
        session.url_guard = partial(
            wayback_client.validate_capture_fetch_url,
            timestamp=CAPTURED,
            expected_url=ORIGINAL,
        )
        with session.get(REPLAY, allow_redirects=True, timeout=30) as response:
            assert response.status_code == 200
            assert response.url == redirect
            assert response.text == "Archived final page"

    assert scripted_transport.sent == [REPLAY, redirect]


@pytest.mark.parametrize("redirect", DENIED_REDIRECTS)
def test_real_content_fetcher_archive_redirect_denial_leaves_no_citation(
    redirect, runtime, scripted_transport, monkeypatch
):
    from local_deep_research.content_fetcher.fetcher import ContentFetcher

    monkeypatch.setattr(
        "local_deep_research.content_fetcher.ContentFetcher", ContentFetcher
    )
    scripted_transport.routes[REPLAY] = {
        "status": 302,
        "headers": {"Location": redirect},
    }
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        "full",
        collector,
        max_fetches=1,
        settings_snapshot={"web.enable_javascript_rendering": True},
    )

    output = tool.invoke({"url": ORIGINAL})

    assert not output.startswith("[1]")
    assert collector.results == []
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()
    assert scripted_transport.sent == [REPLAY]


@pytest.mark.parametrize("modifier", ["", "id_", "if_"])
def test_real_content_fetcher_extracts_same_capture_after_redirect_without_browser(
    modifier, runtime, scripted_transport, monkeypatch
):
    from local_deep_research.content_fetcher.fetcher import ContentFetcher

    monkeypatch.setattr(
        "local_deep_research.content_fetcher.ContentFetcher", ContentFetcher
    )
    browser = Mock(side_effect=AssertionError("Archive must use static HTML"))
    monkeypatch.setattr(
        "local_deep_research.research_library.downloaders.playwright_html"
        ".AutoHTMLDownloader",
        browser,
    )
    redirect = f"https://web.archive.org/web/{CAPTURED}{modifier}/http://www.example.com:80/history"
    paragraph = (
        "Archived policy evidence is retained from the historical page. "
        "The published policy describes the original service and its limits. "
        "This capture records the information before the policy changed. "
    )
    html = (
        "<html><head><title>Historical policy</title></head><body><main><article>"
        "<h1>Historical policy</h1>"
        + "".join(f"<p>{paragraph}</p>" for _ in range(4))
        + '<img src="https://live-resources.example/image.png">'
        + '<script src="https://live-resources.example/app.js"></script>'
        + "</article></main></body></html>"
    ).encode()
    scripted_transport.routes.update(
        {
            REPLAY: {"status": 302, "headers": {"Location": redirect}},
            redirect: {"body": html},
        }
    )
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool(
        "full",
        collector,
        max_fetches=1,
        settings_snapshot={"web.enable_javascript_rendering": True},
    )

    output = tool.invoke({"url": ORIGINAL})

    assert output.startswith("[1] ")
    assert "Archived policy evidence" in output
    assert collector.results[0]["link"] == REPLAY
    assert collector.results[0]["timestamp"] == CAPTURED
    assert collector.fetch_count == 1
    assert scripted_transport.sent[:2] == [REPLAY, redirect]
    assert set(scripted_transport.sent) == {REPLAY, redirect}
    runtime.getter.assert_called_once()
    browser.assert_not_called()


@pytest.mark.parametrize(("requested", "actual"), MISMATCHED_TARGETS)
def test_real_safe_session_blocks_same_timestamp_target_mismatch(
    requested, actual, scripted_transport
):
    from local_deep_research.security.safe_requests import SafeSession

    initial = f"https://web.archive.org/web/{CAPTURED}/{requested}"
    redirect = f"https://web.archive.org/web/{CAPTURED}/{actual}"
    scripted_transport.routes[initial] = {
        "status": 302,
        "headers": {"Location": redirect},
    }
    with SafeSession() as session:
        session.url_guard = partial(
            wayback_client.validate_capture_fetch_url,
            timestamp=CAPTURED,
            expected_url=requested,
        )
        with pytest.raises(ValueError, match="captured URL"):
            session.get(initial, allow_redirects=True, timeout=30)

    assert scripted_transport.sent == [initial]


@pytest.mark.parametrize(("requested", "actual"), MISMATCHED_TARGETS)
def test_real_content_fetcher_blocks_same_timestamp_target_mismatch_without_citation(
    requested, actual, runtime, scripted_transport, monkeypatch
):
    from local_deep_research.content_fetcher.fetcher import ContentFetcher

    monkeypatch.setattr(
        "local_deep_research.content_fetcher.ContentFetcher", ContentFetcher
    )
    initial = f"https://web.archive.org/web/{CAPTURED}/{requested}"
    redirect = f"https://web.archive.org/web/{CAPTURED}/{actual}"
    runtime.response.json.return_value = _available_payload(requested)
    scripted_transport.routes[initial] = {
        "status": 302,
        "headers": {"Location": redirect},
    }
    collector = SearchResultsCollector([])
    tool = archive.build_archive_fetch_tool("full", collector, max_fetches=1)

    output = tool.invoke({"url": requested})

    assert not output.startswith("[1]")
    assert collector.results == []
    assert collector.fetch_count == 1
    assert scripted_transport.sent == [initial]
    runtime.getter.assert_called_once()


@pytest.fixture
def real_replay(runtime, scripted_transport, monkeypatch):
    from local_deep_research.content_fetcher.fetcher import ContentFetcher

    monkeypatch.setattr(
        "local_deep_research.content_fetcher.ContentFetcher", ContentFetcher
    )
    browser = Mock(side_effect=AssertionError("Archive must use static HTML"))
    monkeypatch.setattr(
        "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
        browser,
    )
    return SimpleNamespace(transport=scripted_transport, browser=browser)


def _historical_html():
    paragraph = (
        "Archived policy evidence describes the original service and its limits. "
        "This historical capture records the published information before it changed. "
    )
    return (
        "<html><head><title>Historical policy</title></head><body><article>"
        + "".join(f"<p>{paragraph}</p>" for _ in range(8))
        + "<p>ARCHIVED_FACT: the fee was 10 units.</p></article></body></html>"
    ).encode()


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
@pytest.mark.parametrize(
    "reference", ["[1]", REPLAY], ids=["citation", "replay_url"]
)
@pytest.mark.parametrize(
    "reset", [False, True], ids=["same_section", "new_section"]
)
@pytest.mark.parametrize("redirect", DENIED_REDIRECTS)
def test_ordinary_reread_retains_archive_boundary(
    mode, reference, reset, redirect, runtime, real_replay
):
    transport = real_replay.transport
    transport.routes[REPLAY] = {"body": _historical_html()}
    collector = SearchResultsCollector([])
    archived = archive.build_archive_fetch_tool(
        "full", collector, max_fetches=3
    )
    assert archived.invoke({"url": ORIGINAL}).startswith("[1] ")
    original_source = collector.find_by_index(1)
    if reset:
        collector.reset()
    transport.sent.clear()
    transport.routes[REPLAY] = {
        "status": 302,
        "headers": {"Location": redirect},
    }
    transport.routes[ORIGINAL] = {"body": b"LIVE_FACT: the fee is 100 units."}
    availability = SearchEngineAvailability()
    ordinary = build_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        max_fetches=3,
        availability=availability,
        settings_snapshot={"web.enable_javascript_rendering": True},
    )

    output = ordinary.invoke(_arguments(mode, url=reference))

    assert not output.startswith("[1]")
    assert "LIVE_FACT" not in output
    assert transport.sent == [REPLAY]
    assert collector.find_by_index(1) == original_source
    assert collector.fetch_count == (1 if reset else 2)
    assert availability.state("wayback") is None
    runtime.model.invoke.assert_not_called()
    real_replay.browser.assert_not_called()


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
@pytest.mark.parametrize(
    "reference", ["[1]", REPLAY], ids=["citation", "replay_url"]
)
def test_ordinary_reread_echoes_archive_provenance_in_new_section(
    mode, reference, runtime, real_replay
):
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.web_search_engines.wayback_client import (
        validate_capture_fetch_url,
    )

    real_replay.transport.routes[REPLAY] = {"body": _historical_html()}
    collector = SearchResultsCollector([])
    archived = archive.build_archive_fetch_tool("full", collector)
    assert archived.invoke({"url": ORIGINAL}).startswith("[1] ")
    collector.reset()
    runtime.model.invoke.return_value.content = collector.find_by_index(1)[
        "snippet"
    ]
    ordinary = build_fetch_tool(mode, collector, model=runtime.model)

    output = ordinary.invoke(_arguments(mode, url=reference))

    assert output.startswith("[1] ")
    assert f"Captured: {FORMATTED_DATE}" in output
    assert f"Original URL: {ORIGINAL}" in output
    source = _assert_source(collector)
    rendered = format_links_to_markdown(
        collector.results, uncited_mode="disabled"
    )
    assert REPLAY in rendered
    assert FORMATTED_DATE in rendered
    validate_capture_fetch_url(source["link"], CAPTURED, source["original_url"])
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()
    real_replay.browser.assert_not_called()


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
@pytest.mark.parametrize(
    "spec,delay,attempts",
    [
        ({"status": 429, "headers": {"Retry-After": "120"}}, 120, 3),
        ({"status": 503, "headers": {"Retry-After": "120"}}, 120, 3),
        ({"status": 403}, 300, 1),
        (
            {"error": requests.ConnectionError("Replay connection failed")},
            60,
            3,
        ),
        ({"error": requests.Timeout("Replay timed out")}, 60, 3),
    ],
    ids=["429", "503", "403", "connection", "timeout"],
)
def test_terminal_replay_outage_disables_archive_fetch_and_ordinary_rereads(
    mode, spec, delay, attempts, runtime, real_replay
):
    availability = SearchEngineAvailability()
    success = Mock(wraps=availability.record_success)
    availability.record_success = success
    collector = SearchResultsCollector([])
    real_replay.transport.routes[REPLAY] = spec
    archived = archive.build_archive_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        availability=availability,
        max_fetches=3,
    )

    first = archived.invoke(_arguments(mode))
    state = availability.state("wayback")
    assert state is not None
    assert state.failure.cooldown_seconds == delay
    assert first.startswith("wayback")
    assert collector.results == []
    assert collector.fetch_count == 1
    assert real_replay.transport.sent == [REPLAY] * attempts
    success.assert_not_called()

    second = archived.invoke(_arguments(mode))
    assert second == state.describe()
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()
    # A tracked citation must also observe this run-wide outage before
    # using another network slot through the ordinary fetch tool.
    collector.find_or_add_result(
        {
            "link": REPLAY,
            "title": "Archived source",
            "snippet": "Previously archived evidence",
            "source_type": "archive",
            "original_url": ORIGINAL,
            "timestamp": CAPTURED,
        },
        engine_name="wayback",
    )
    ordinary = build_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        availability=availability,
        max_fetches=3,
    )
    assert ordinary.invoke(_arguments(mode, url="[1]")) == state.describe()
    assert collector.fetch_count == 1
    assert real_replay.transport.sent == [REPLAY] * attempts
    real_replay.browser.assert_not_called()


@pytest.mark.parametrize("status", [404, 410])
def test_missing_replay_is_a_document_failure_not_an_archive_outage(
    status, runtime, real_replay
):
    real_replay.transport.routes[REPLAY] = {"status": status}
    availability = SearchEngineAvailability()
    collector = SearchResultsCollector([])
    archived = archive.build_archive_fetch_tool(
        "full", collector, availability=availability, max_fetches=2
    )

    for _ in range(2):
        assert not archived.invoke({"url": ORIGINAL}).startswith("[1]")
        assert availability.state("wayback") is None
    assert collector.results == []
    assert collector.fetch_count == 2
    assert real_replay.transport.sent == [REPLAY, REPLAY]
    assert runtime.getter.call_count == 2


def test_healthy_lookup_does_not_reset_repeated_replay_outage_streak(
    runtime, real_replay
):
    clock = [0.0]
    # A tracker no longer ends cooldowns after a streak by default (#6930),
    # so give it an explicit cap: a healthy availability lookup must not
    # reset the streak of failing replay reads before that cap is reached.
    # Production (langgraph_agent_strategy.py) builds SearchEngineAvailability
    # without max_consecutive_cooldowns, so this pins the opt-in cap only.
    availability = SearchEngineAvailability(
        clock=lambda: clock[0], max_consecutive_cooldowns=3
    )
    collector = SearchResultsCollector([])
    real_replay.transport.routes[REPLAY] = {
        "status": 429,
        "headers": {"Retry-After": "120"},
    }
    archived = archive.build_archive_fetch_tool(
        "full", collector, availability=availability, max_fetches=0
    )

    for attempt in range(4):
        archived.invoke({"url": ORIGINAL})
        state = availability.state("wayback")
        assert state is not None
        if attempt < 3:
            assert state.deadline is not None
            clock[0] += 121
        else:
            assert state.deadline is None
            assert "3 cooldowns in a row" in state.describe()
    assert collector.fetch_count == 4
    assert runtime.getter.call_count == 4
    assert real_replay.transport.sent == [REPLAY] * 12


# A research run binds one shared content cache, and fetch paths without an
# explicit cache resolve it. Archive reads must not use it: an entry for the
# same replay URL can come from an unguarded fetch whose redirects left the
# capture, and the archive tool needs the read's own transport failure.
_CACHED_REPLAY_TEXT = "CACHED_FACT: an unguarded fetch stored this text."


@pytest.fixture
def bound_research_cache(monkeypatch):
    """Bind a real run cache the way AdvancedSearchSystem does."""
    from local_deep_research.utilities.research_content_cache import (
        ResearchContentCache,
        bind_cache_to_research,
        unbind_cache_from_research,
    )
    from local_deep_research.utilities.thread_context import (
        clear_search_context,
        set_search_context,
    )

    research_id = f"archive-cache-{uuid.uuid4().hex}"
    cache = ResearchContentCache()
    bind_cache_to_research(research_id, cache)
    set_search_context({"research_id": research_id})
    # The fetch tool's own cache gate validates through the egress module;
    # keep it offline like the fetcher gate that scripted_transport replaces.
    monkeypatch.setattr(
        "local_deep_research.security.egress.fetch.policy_aware_validate_url",
        Mock(return_value=True),
    )
    yield cache
    clear_search_context()
    unbind_cache_from_research(research_id, cache=cache)


def _content_counters(cache):
    """Counters that any cached-page read, store or merged fetch changes."""
    stats = cache.stats()
    return {key: stats[key] for key in ("hits", "misses", "puts", "entries")}


def _seed_replay_entry(cache):
    # put() files the entry under content_cache_key(REPLAY), the same key
    # every production read and store of this replay URL computes.
    cache.put(REPLAY, _CACHED_REPLAY_TEXT, title="Cached copy", url_out=REPLAY)
    return _content_counters(cache)


def _capture_guard():
    return partial(
        wayback_client.validate_capture_fetch_url,
        timestamp=CAPTURED,
        expected_url=ORIGINAL,
    )


def test_guarded_fetcher_skips_cached_replay_and_raises_its_own_429(
    bound_research_cache, real_replay
):
    from local_deep_research.content_fetcher.fetcher import ContentFetcher

    before = _seed_replay_entry(bound_research_cache)
    real_replay.transport.routes[REPLAY] = {
        "status": 429,
        "headers": {"Retry-After": "120"},
    }

    with ContentFetcher(url_guard=_capture_guard()) as fetcher:
        with pytest.raises(requests.HTTPError) as raised:
            fetcher.fetch(REPLAY)

    assert raised.value.response.status_code == 429
    assert real_replay.transport.sent == [REPLAY] * 3
    assert _content_counters(bound_research_cache) == before
    assert bound_research_cache.get(REPLAY)["content"] == _CACHED_REPLAY_TEXT
    real_replay.browser.assert_not_called()


def test_guarded_fetcher_does_not_store_its_capture_in_the_run_cache(
    bound_research_cache, real_replay
):
    from local_deep_research.content_fetcher.fetcher import ContentFetcher

    before = _content_counters(bound_research_cache)
    real_replay.transport.routes[REPLAY] = {"body": _historical_html()}

    with ContentFetcher(url_guard=_capture_guard()) as fetcher:
        result = fetcher.fetch(REPLAY)

    assert result["status"] == "success"
    assert "ARCHIVED_FACT" in result["content"]
    # The page read and its metadata read both go to the capture itself.
    assert set(real_replay.transport.sent) == {REPLAY}
    assert _content_counters(bound_research_cache) == before
    assert bound_research_cache.get(REPLAY) is None


def test_unguarded_fetcher_serves_the_same_cached_replay(
    bound_research_cache, real_replay
):
    """Control: the seeded entry is reachable, so only the guard skips it."""
    from local_deep_research.content_fetcher.fetcher import ContentFetcher

    before = _seed_replay_entry(bound_research_cache)

    with ContentFetcher() as fetcher:
        result = fetcher.fetch(REPLAY)

    assert result["status"] == "success"
    assert result["content"] == _CACHED_REPLAY_TEXT
    assert real_replay.transport.sent == []
    after = _content_counters(bound_research_cache)
    assert after["hits"] == before["hits"] + 1
    assert after["puts"] == before["puts"]
    real_replay.browser.assert_not_called()


def test_archive_tool_skips_cached_replay_and_classifies_its_429(
    bound_research_cache, runtime, real_replay
):
    before = _seed_replay_entry(bound_research_cache)
    availability = SearchEngineAvailability()
    collector = SearchResultsCollector([])
    real_replay.transport.routes[REPLAY] = {
        "status": 429,
        "headers": {"Retry-After": "120"},
    }
    archived = archive.build_archive_fetch_tool(
        "full", collector, availability=availability, max_fetches=3
    )

    output = archived.invoke({"url": ORIGINAL})

    state = availability.state("wayback")
    assert state is not None
    assert state.failure.cooldown_seconds == 120
    assert output.startswith("wayback")
    assert "CACHED_FACT" not in output
    assert collector.results == []
    assert real_replay.transport.sent == [REPLAY] * 3
    assert _content_counters(bound_research_cache) == before
    runtime.getter.assert_called_once()
    real_replay.browser.assert_not_called()


def test_archive_tool_does_not_probe_or_store_the_run_cache(
    bound_research_cache, runtime
):
    # The shared fetch path alone, with runtime's stub fetcher: its log
    # probe and write-back would use the run cache around any successful
    # read, so the stub succeeds and the cache must stay untouched.
    before = _content_counters(bound_research_cache)
    collector = SearchResultsCollector([])
    archived = archive.build_archive_fetch_tool(
        "full", collector, max_fetches=3
    )

    output = archived.invoke({"url": ORIGINAL})

    assert output.startswith("[1] ")
    runtime.fetcher.fetch.assert_called_once()
    assert runtime.fetcher.fetch.call_args.args[0] == REPLAY
    options = runtime.factory.call_args.kwargs
    assert options["content_cache"] is None
    assert options["url_guard"] is not None
    assert _content_counters(bound_research_cache) == before
    assert bound_research_cache.get(REPLAY) is None
    runtime.getter.assert_called_once()


def test_unguarded_fetch_path_serves_the_same_cached_replay(
    bound_research_cache, real_replay
):
    """Control: the tools' shared fetch path reaches the seeded entry."""
    from local_deep_research.advanced_search_system.tools.fetch import (
        _fetch_raw_content,
    )

    before = _seed_replay_entry(bound_research_cache)

    result, url, error, _count = _fetch_raw_content(
        REPLAY, None, SearchResultsCollector([]), None, {}, "full"
    )

    assert error is None
    assert url == REPLAY
    assert result["status"] == "success"
    assert result["content"] == _CACHED_REPLAY_TEXT
    assert real_replay.transport.sent == []
    after = _content_counters(bound_research_cache)
    assert after["hits"] > before["hits"]
    assert after["puts"] == before["puts"]
    real_replay.browser.assert_not_called()


def test_collector_reuse_keeps_archive_metadata_after_section_reset(runtime):
    collector = SearchResultsCollector([])
    archived = archive.build_archive_fetch_tool("full", collector)
    archived.invoke({"url": ORIGINAL})
    source = collector.find_by_index(1)
    collector.reset()

    index = collector.find_or_add_result(
        {"link": REPLAY, "title": "Plain title", "snippet": source["snippet"]},
        engine_name="fetch",
    )

    assert index == 1
    echoed = _assert_source(collector)
    assert echoed["title"] == source["title"]


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
@pytest.mark.parametrize(
    "reference", ["[1]", REPLAY], ids=["citation", "replay_url"]
)
@pytest.mark.parametrize(
    "spec,delay,attempts",
    [
        ({"status": 429, "headers": {"Retry-After": "120"}}, 120, 3),
        ({"status": 503, "headers": {"Retry-After": "120"}}, 120, 3),
        (
            {"error": requests.ConnectionError("Replay connection failed")},
            60,
            3,
        ),
    ],
    ids=["429", "503", "connection"],
)
def test_ordinary_archive_reread_reports_outage_to_shared_cooldown(
    mode, reference, spec, delay, attempts, runtime, real_replay
):
    transport = real_replay.transport
    transport.routes[REPLAY] = {"body": _historical_html()}
    availability = SearchEngineAvailability()
    collector = SearchResultsCollector([])
    archived = archive.build_archive_fetch_tool(
        "full", collector, availability=availability
    )
    assert archived.invoke({"url": ORIGINAL}).startswith("[1] ")
    collector.reset()
    transport.sent.clear()
    transport.routes[REPLAY] = spec
    ordinary = build_fetch_tool(
        mode, collector, model=runtime.model, availability=availability
    )

    ordinary.invoke(_arguments(mode, url=reference))

    state = availability.state("wayback")
    assert state is not None
    assert state.failure.cooldown_seconds == delay
    assert collector.results == []
    assert collector.fetch_count == 1
    assert transport.sent == [REPLAY] * attempts
    assert archived.invoke({"url": ORIGINAL}) == state.describe()
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()
    real_replay.browser.assert_not_called()


@pytest.mark.parametrize(
    "first,second",
    [
        ("https://example.com/history", "https://example.com/history/"),
        (
            "https://example.com/history?utm_source=first",
            "https://example.com/history?utm_source=second",
        ),
    ],
    ids=["target_path", "target_query"],
)
def test_collector_and_sources_keep_distinct_replay_targets(first, second):
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )
    from local_deep_research.web_search_engines.wayback_client import (
        validate_capture_fetch_url,
    )

    collector = SearchResultsCollector([])
    replays = []
    for index, target in enumerate((first, second), 1):
        replay = f"https://web.archive.org/web/{CAPTURED}/{target}"
        replays.append(replay)
        assigned = collector.find_or_add_result(
            {
                "link": replay,
                "title": "Historical policy",
                "snippet": "Identical archived excerpt",
                "source_type": "archive",
                "original_url": target,
                "timestamp": CAPTURED,
                "formatted_date": FORMATTED_DATE,
            },
            engine_name="wayback",
        )
        assert assigned == index
        assert collector.find_by_url(replay) == index

    rendered = format_links_to_markdown(
        collector.results, uncited_mode="disabled"
    )
    displayed = [
        line.removeprefix("   URL: ")
        for line in rendered.splitlines()
        if line.startswith("   URL: ")
    ]
    assert displayed == replays
    for replay, target in zip(displayed, (first, second)):
        validate_capture_fetch_url(replay, CAPTURED, target)


@pytest.mark.parametrize("source_type", [None, "web", "html"])
def test_successful_archive_upgrades_prior_generic_citation(
    source_type, runtime
):
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )

    collector = SearchResultsCollector([])
    generic = {
        "link": REPLAY,
        "title": "Generic search title",
        "snippet": runtime.fetcher.fetch.return_value["content"],
        "original_url": "https://unrelated.example/old-metadata",
    }
    if source_type is not None:
        generic["source_type"] = source_type
    assert collector.find_or_add_result(generic, engine_name="web") == 1
    archived = archive.build_archive_fetch_tool("full", collector)

    assert archived.invoke({"url": ORIGINAL}).startswith("[1] ")
    source = _assert_source(collector)
    assert FORMATTED_DATE in source["title"]
    assert FORMATTED_DATE in format_links_to_markdown(
        collector.results, uncited_mode="disabled"
    )
    collector.reset()
    ordinary = build_fetch_tool("full", collector)
    assert ordinary.invoke({"url": "[1]"}).startswith("[1] ")
    _assert_source(collector)
    # The tracked capture is guarded even though its initial search hit
    # was not classified as an archive source.
    runtime.factory.call_args.kwargs["url_guard"](REPLAY)
    with pytest.raises(ValueError):
        runtime.factory.call_args.kwargs["url_guard"](ORIGINAL)


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
@pytest.mark.parametrize(
    "error",
    [
        requests.exceptions.ChunkedEncodingError("Body transfer aborted"),
        requests.exceptions.ContentDecodingError("Body decoding aborted"),
    ],
    ids=["chunked_transfer", "content_decoding"],
)
def test_aborted_replay_body_is_a_shared_archive_transport_outage(
    mode, error, runtime, real_replay
):
    real_replay.transport.routes[REPLAY] = {"body_error": error}
    availability = SearchEngineAvailability()
    collector = SearchResultsCollector([])
    archived = archive.build_archive_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        availability=availability,
        max_fetches=3,
    )

    first = archived.invoke(_arguments(mode))
    state = availability.state("wayback")
    assert state is not None
    assert state.failure.cooldown_seconds == 60
    assert "Could not read the provider response" in first
    assert archived.invoke(_arguments(mode)) == state.describe()
    assert collector.results == []
    assert collector.fetch_count == 1
    runtime.getter.assert_called_once()
    assert real_replay.transport.sent == [REPLAY]
    runtime.model.invoke.assert_not_called()


@pytest.mark.parametrize(
    "reference", ["[1]", REPLAY], ids=["generic_citation", "replay_url"]
)
@pytest.mark.parametrize(
    "reset", [False, True], ids=["same_section", "new_section"]
)
def test_reread_finds_verified_capture_after_different_search_excerpt(
    reference, reset, runtime, real_replay
):
    transport = real_replay.transport
    collector = SearchResultsCollector([])
    assert (
        collector.find_or_add_result(
            {
                "link": REPLAY,
                "title": "Search listing title",
                "snippet": "A different search excerpt",
                "source_type": "web",
            },
            engine_name="web",
        )
        == 1
    )
    transport.routes[REPLAY] = {"body": _historical_html()}
    archived = archive.build_archive_fetch_tool(
        "full", collector, max_fetches=3
    )
    assert archived.invoke({"url": ORIGINAL}).startswith("[2] ")
    first = collector.find_by_index(1)
    second = collector.find_by_index(2)
    assert first["snippet"] != second["snippet"]
    assert collector.find_by_url(REPLAY) == 1
    if reset:
        collector.reset()
    transport.sent.clear()
    transport.routes[REPLAY] = {
        "status": 302,
        "headers": {"Location": ORIGINAL},
    }
    transport.routes[ORIGINAL] = {"body": b"LIVE_FACT: the fee is 100 units."}
    ordinary = build_fetch_tool("full", collector, max_fetches=3)

    output = ordinary.invoke({"url": reference})

    assert not output.startswith("[")
    assert "LIVE_FACT" not in output
    assert transport.sent == [REPLAY]
    assert collector.find_by_index(1) == first
    assert collector.find_by_index(2) == second
    assert collector.fetch_count == (1 if reset else 2)
    runtime.getter.assert_called_once()
    real_replay.browser.assert_not_called()


@pytest.mark.parametrize("same_excerpt", [False, True])
@pytest.mark.parametrize("cited", ["[1]", "[2]"])
def test_verified_capture_date_survives_prose_citing_one_excerpt(
    same_excerpt, cited, runtime
):
    from local_deep_research.utilities.search_utilities import (
        format_links_to_markdown,
    )

    collector = SearchResultsCollector([])
    content = runtime.fetcher.fetch.return_value["content"]
    collector.find_or_add_result(
        {
            "link": REPLAY,
            "title": "Generic fetched title",
            "snippet": content if same_excerpt else "Different fetched excerpt",
        },
        engine_name="fetch",
    )
    archived = archive.build_archive_fetch_tool("full", collector)
    archived.invoke({"url": ORIGINAL})
    if same_excerpt and cited == "[2]":
        cited = "[1]"

    rendered = format_links_to_markdown(
        collector.results, prose=f"Evidence {cited}.", uncited_mode="strict"
    )

    assert FORMATTED_DATE in rendered
    assert REPLAY in rendered
    assert len(collector.results) == (1 if same_excerpt else 2)
    assert collector.find_by_index(1)["snippet"] == (
        content if same_excerpt else "Different fetched excerpt"
    )
    collector.reset()
    _, indexed = collector.add_results(
        [
            {
                "link": REPLAY,
                "title": "New search title",
                "snippet": "New excerpt",
            }
        ]
    )
    assert indexed[0]["formatted_date"] == FORMATTED_DATE
    assert indexed[0]["source_type"] == "archive"
    assert FORMATTED_DATE in format_links_to_markdown(
        collector.results, uncited_mode="disabled"
    )


@pytest.mark.parametrize(
    "mode", ["full", "summary_focus", "summary_focus_query"]
)
@pytest.mark.parametrize(
    "missing", ["original_url", "timestamp", "formatted_date", "all", "nulls"]
)
@pytest.mark.parametrize("reference,reset", [(REPLAY, False), ("[1]", True)])
def test_partial_archive_provenance_is_recovered_before_fetch(
    mode, missing, reference, reset, runtime
):
    requested = "http://www.example.com/history"
    row = {
        "link": REPLAY,
        "title": "Historical page",
        "snippet": runtime.fetcher.fetch.return_value["content"],
        "source_type": "archive",
        "original_url": ORIGINAL,
        "requested_url": requested,
        "timestamp": CAPTURED,
        "formatted_date": FORMATTED_DATE,
    }
    fields = ("original_url", "timestamp", "formatted_date")
    for field in fields if missing in {"all", "nulls"} else [missing]:
        if missing == "nulls":
            row[field] = None
        else:
            row.pop(field)
    collector = SearchResultsCollector([])
    collector.add_results([row], engine_name="wayback")
    if reset:
        collector.reset()
    ordinary = build_fetch_tool(mode, collector, model=runtime.model)

    output = ordinary.invoke(_arguments(mode, url=reference))

    assert output.startswith("[")
    assert f"Original URL: {ORIGINAL}" in output
    assert f"Requested URL: {requested}" in output
    assert f"Captured: {FORMATTED_DATE}" in output
    assert collector.fetch_count == 1
    recovered = collector.find_by_index(int(output[1 : output.index("]")]))
    assert recovered["original_url"] == ORIGINAL
    assert recovered["requested_url"] == requested
    assert recovered["timestamp"] == CAPTURED
    assert recovered["formatted_date"] == FORMATTED_DATE
    guard = runtime.factory.call_args.kwargs["url_guard"]
    guard(REPLAY)
    with pytest.raises(ValueError):
        guard(ORIGINAL)
    runtime.fetcher.fetch.assert_called_once_with(
        REPLAY,
        max_length=10_000 if mode == "full" else DEFAULT_SUMMARY_PAGE_LIMIT,
    )
    runtime.getter.assert_not_called()
    assert runtime.model.invoke.call_count == (0 if mode == "full" else 1)


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
@pytest.mark.parametrize(
    "url,metadata",
    [
        pytest.param(ORIGINAL, {}, id="non_replay"),
        pytest.param(REPLAY, {"timestamp": "20240101000000"}, id="wrong_time"),
        pytest.param(
            REPLAY, {"original_url": ORIGINAL + "/other"}, id="wrong_target"
        ),
    ],
)
def test_partial_archive_provenance_never_overrides_capture_constraints(
    mode, url, metadata, runtime
):
    collector = SearchResultsCollector([])
    collector.add_results(
        [{"link": url, "source_type": "archive", **metadata}],
        engine_name="wayback",
    )
    before = dict(collector.find_by_index(1))
    ordinary = build_fetch_tool(mode, collector, model=runtime.model)

    output = ordinary.invoke(_arguments(mode, url="[1]"))

    assert not output.startswith("[")
    assert collector.find_by_index(1) == before
    assert collector.fetch_count == 0
    runtime.factory.assert_not_called()
    runtime.model.invoke.assert_not_called()
    runtime.getter.assert_not_called()


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

    collector = SearchResultsCollector([])
    index = _register_in_collector(
        collector, REPLAY, "", "Historical evidence", {"source_type": "archive"}
    )

    assert index == 1
    source = _assert_source(collector)
    assert source["title"] == f"{ORIGINAL} (archived {FORMATTED_DATE})"


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
def test_non_capture_archive_page_keeps_ordinary_fetch_behavior(mode, runtime):
    url = "https://web.archive.org/about/"
    collector = SearchResultsCollector([])
    ordinary = build_fetch_tool(mode, collector, model=runtime.model)

    output = ordinary.invoke(_arguments(mode, url=url))

    assert output.startswith("[1]")
    assert "Captured:" not in output
    assert runtime.factory.call_args.kwargs.get("url_guard") is None
    assert collector.fetch_count == 1
    runtime.fetcher.fetch.assert_called_once_with(
        url, max_length=10_000 if mode == "full" else DEFAULT_SUMMARY_PAGE_LIMIT
    )
    runtime.getter.assert_not_called()


@pytest.mark.parametrize("mode", ["full", "summary_focus_query"])
@pytest.mark.parametrize("modifier", ["", "id_", "if_"])
def test_first_direct_replay_read_is_confined_without_archive_lookup(
    mode, modifier, runtime, real_replay
):
    replay = f"https://web.archive.org/web/{CAPTURED}{modifier}/{ORIGINAL}"
    real_replay.transport.routes[replay] = {
        "status": 302,
        "headers": {"Location": ORIGINAL},
    }
    real_replay.transport.routes[ORIGINAL] = {
        "body": b"LIVE_FACT: current page"
    }
    collector = SearchResultsCollector([])
    ordinary = build_fetch_tool(mode, collector, model=runtime.model)

    output = ordinary.invoke(_arguments(mode, url=replay))

    assert not output.startswith("[")
    assert "LIVE_FACT" not in output
    assert collector.results == []
    assert collector.fetch_count == 1
    assert real_replay.transport.sent == [replay]
    runtime.getter.assert_not_called()
    runtime.model.invoke.assert_not_called()
    real_replay.browser.assert_not_called()


@pytest.mark.parametrize("modifier", ["", "id_", "if_"])
def test_first_direct_replay_success_keeps_capture_metadata_without_lookup(
    modifier, runtime, real_replay
):
    replay = f"https://web.archive.org/web/{CAPTURED}{modifier}/{ORIGINAL}"
    real_replay.transport.routes[replay] = {"body": _historical_html()}
    collector = SearchResultsCollector([])
    ordinary = build_fetch_tool("full", collector)

    output = ordinary.invoke({"url": replay})

    assert output.startswith("[1] ")
    assert f"Captured: {FORMATTED_DATE}" in output
    _assert_source(collector, replay=replay)
    runtime.getter.assert_not_called()
    real_replay.browser.assert_not_called()


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
@pytest.mark.parametrize(
    "replay",
    [
        REPLAY.replace("https://web.archive.org", "http://web.archive.org"),
        REPLAY.replace(CAPTURED, "20240230112233"),
        REPLAY.replace(CAPTURED, CAPTURED + "im_"),
        REPLAY.replace("https://", "https://user:password@", 1),
        REPLAY.replace("web.archive.org", "web.archive.org:8443"),
        REPLAY.replace("web.archive.org", "web.archive.org."),
        REPLAY.replace("web.archive.org", "archive.org"),
        REPLAY.replace("/web/", "/WEB/"),
        REPLAY.replace("/web/", "//web/"),
        REPLAY.replace("/web/", "/web//"),
        REPLAY.replace("web.archive.org", "%77eb.archive.org"),
        REPLAY.replace("/web/", "/%77eb/"),
        REPLAY.replace(CAPTURED, "%32" + CAPTURED[1:]),
        REPLAY.replace("/web/", "/about/../web/"),
        REPLAY.replace(CAPTURED, "2024"),
        REPLAY.replace(CAPTURED, "*"),
    ],
    ids=[
        "http_capture",
        "invalid_date",
        "unsupported_modifier",
        "credentials",
        "port",
        "trailing_dot_host",
        "redirector_host",
        "uppercase_replay_path",
        "double_leading_slash",
        "double_capture_slash",
        "prepared_host",
        "prepared_path",
        "prepared_timestamp",
        "prepared_dot_segments",
        "partial_timestamp",
        "wildcard_timestamp",
    ],
)
def test_invalid_direct_capture_never_uses_transport_or_budget(
    mode, replay, runtime
):
    collector = SearchResultsCollector([])
    ordinary = build_fetch_tool(mode, collector, model=runtime.model)

    assert not ordinary.invoke(_arguments(mode, url=replay)).startswith("[1]")
    assert collector.results == []
    assert collector.fetch_count == 0
    runtime.getter.assert_not_called()
    runtime.factory.assert_not_called()
    runtime.model.invoke.assert_not_called()


@pytest.fixture
def chain_replay(runtime, scripted_transport, monkeypatch):
    from local_deep_research.content_fetcher.fetcher import ContentFetcher
    from local_deep_research.research_library.downloaders.playwright_html import (
        AutoHTMLDownloader,
    )

    monkeypatch.setattr(
        "local_deep_research.content_fetcher.ContentFetcher", ContentFetcher
    )
    auto = Mock(wraps=AutoHTMLDownloader)
    monkeypatch.setattr(
        "local_deep_research.research_library.downloaders.playwright_html.AutoHTMLDownloader",
        auto,
    )
    browser = Mock(side_effect=AssertionError("Unexpected browser rendering"))
    monkeypatch.setattr(
        "local_deep_research.research_library.downloaders.playwright_html.PlaywrightHTMLDownloader",
        browser,
    )
    return SimpleNamespace(
        transport=scripted_transport, auto=auto, browser=browser
    )


def _chained_collector(target=REPLAY, *, length=2, verified=False):
    entries = [
        {
            "index": str(index),
            "link": f"[{index + 1}]" if index < length else target,
            "title": f"Chained source {index}",
            "snippet": f"Discovery excerpt {index}",
        }
        for index in range(1, length + 1)
    ]
    if verified:
        entries[-1].update(
            source_type="archive",
            original_url=ORIGINAL,
            requested_url=ORIGINAL,
            timestamp=CAPTURED,
            formatted_date=FORMATTED_DATE,
        )
    return SearchResultsCollector(entries)


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
@pytest.mark.parametrize(
    "length", [2, 4], ids=["two_markers", "depth_boundary"]
)
@pytest.mark.parametrize("verified", [False, True], ids=["generic", "archive"])
def test_chained_archive_read_cannot_follow_live_redirect(
    mode, length, verified, runtime, chain_replay
):
    collector = _chained_collector(length=length, verified=verified)
    collector.reset()
    before = collector.find_by_index(length)
    chain_replay.transport.routes[REPLAY] = {
        "status": 302,
        "headers": {"Location": ORIGINAL},
    }
    chain_replay.transport.routes[ORIGINAL] = {
        "body": _historical_html().replace(b"ARCHIVED_FACT", b"LIVE_FACT")
    }
    availability = SearchEngineAvailability()
    ordinary = build_fetch_tool(
        mode, collector, model=runtime.model, availability=availability
    )

    output = ordinary.invoke(_arguments(mode, url="[1]"))

    assert not output.startswith("[")
    assert "selected capture" in output
    assert "LIVE_FACT" not in output
    assert collector.results == []
    assert collector.find_by_index(length) == before
    assert collector.fetch_count == 1
    assert chain_replay.transport.sent == [REPLAY]
    assert availability.state("wayback") is None
    runtime.getter.assert_not_called()
    runtime.model.invoke.assert_not_called()
    chain_replay.auto.assert_not_called()
    chain_replay.browser.assert_not_called()


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
@pytest.mark.parametrize("verified", [False, True], ids=["generic", "archive"])
def test_chained_archive_success_keeps_terminal_capture_provenance(
    mode, verified, runtime, chain_replay
):
    collector = _chained_collector(verified=verified)
    collector.reset()
    chain_replay.transport.routes[REPLAY] = {"body": _historical_html()}
    ordinary = build_fetch_tool(mode, collector, model=runtime.model)

    output = ordinary.invoke(_arguments(mode, url="[1]"))

    assert output.startswith("[3] ")
    assert f"Original URL: {ORIGINAL}" in output
    assert f"Captured: {FORMATTED_DATE}" in output
    assert collector.fetch_count == 1
    source = _assert_source(collector)
    assert source["index"] == "3"
    assert set(chain_replay.transport.sent) == {REPLAY}
    runtime.getter.assert_not_called()
    chain_replay.auto.assert_not_called()
    chain_replay.browser.assert_not_called()


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
def test_chained_archive_outage_respects_shared_retry_after(
    mode, runtime, chain_replay
):
    collector = _chained_collector()
    collector.reset()
    chain_replay.transport.routes[REPLAY] = {
        "status": 429,
        "headers": {"Retry-After": "400"},
    }
    availability = SearchEngineAvailability()
    ordinary = build_fetch_tool(
        mode, collector, model=runtime.model, availability=availability
    )

    output = ordinary.invoke(_arguments(mode, url="[1]"))

    state = availability.state("wayback")
    assert state is not None
    assert state.failure.cooldown_seconds == 400
    assert output == state.describe()
    assert ordinary.invoke(_arguments(mode, url="[1]")) == state.describe()
    assert chain_replay.transport.sent == [REPLAY] * 3
    assert collector.results == []
    assert collector.fetch_count == 1
    runtime.getter.assert_not_called()
    runtime.model.invoke.assert_not_called()


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
@pytest.mark.parametrize("denial", ["engine", ORIGINAL, REPLAY])
def test_chained_archive_policy_denial_precedes_transport_and_quota(
    mode, denial, runtime, chain_replay, monkeypatch
):
    collector = _chained_collector()
    collector.reset()

    def decision(target):
        return SimpleNamespace(allowed=target != denial, reason="chain_policy")

    monkeypatch.setattr(
        policy, "evaluate_engine", lambda *args, **kwargs: decision("engine")
    )
    monkeypatch.setattr(
        policy, "evaluate_url", lambda url, *args, **kwargs: decision(url)
    )
    ordinary = build_fetch_tool(
        mode, collector, model=runtime.model, egress_context=object()
    )

    output = ordinary.invoke(_arguments(mode, url="[1]"))

    assert "blocked by egress policy" in output
    assert collector.fetch_count == 0
    assert collector.results == []
    assert chain_replay.transport.sent == []
    runtime.getter.assert_not_called()
    runtime.model.invoke.assert_not_called()


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
@pytest.mark.parametrize(
    "target,length,message",
    [
        ("[1]", 2, "Circular citation reference"),
        (REPLAY, 5, "Citation resolution depth limit"),
        ("[99]", 2, "No registered citation matches [99]"),
        ("", 2, "Citation [2] has no URL field"),
        (REPLAY.replace(CAPTURED, "20240230112233"), 2, "Error fetching"),
    ],
    ids=[
        "cycle",
        "depth",
        "missing_citation",
        "missing_url",
        "invalid_capture",
    ],
)
def test_chained_resolution_errors_keep_existing_limits_without_quota(
    mode, target, length, message, runtime, chain_replay
):
    collector = _chained_collector(target, length=length)
    collector.reset()
    ordinary = build_fetch_tool(mode, collector, model=runtime.model)

    output = ordinary.invoke(_arguments(mode, url="[1]"))

    assert message in output
    assert collector.results == []
    assert collector.fetch_count == 0
    assert chain_replay.transport.sent == []
    runtime.getter.assert_not_called()
    runtime.model.invoke.assert_not_called()


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
def test_chained_live_read_survives_archive_cooldown(
    mode, runtime, chain_replay
):
    from local_deep_research.web_search_engines.engine_availability import (
        SearchFailure,
    )

    collector = _chained_collector(ORIGINAL)
    collector.reset()
    chain_replay.transport.routes[ORIGINAL] = {"body": _historical_html()}
    availability = SearchEngineAvailability()
    availability.disable("wayback", SearchFailure("Archive unavailable", 400))
    ordinary = build_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        availability=availability,
        settings_snapshot={"web.enable_javascript_rendering": False},
    )

    output = ordinary.invoke(_arguments(mode, url="[1]"))

    assert output.startswith("[3] ")
    assert "Captured:" not in output
    assert collector.fetch_count == 1
    assert set(chain_replay.transport.sent) == {ORIGINAL}
    assert collector.find_by_index(3).get("source_type") != "archive"
    runtime.getter.assert_not_called()


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
def test_chained_library_read_resolves_once_without_transport_or_quota(
    mode, runtime, monkeypatch
):
    local = "/library/document/0123456789abcdef0123456789abcdef"
    collector = _chained_collector(local, length=4)
    collector.reset()
    resolver = Mock(
        return_value={"title": "Local policy", "content": "LOCAL_FACT " * 400}
    )
    gate = Mock(side_effect=AssertionError("Local read must skip egress"))
    monkeypatch.setattr(policy, "evaluate_url", gate)
    ordinary = build_fetch_tool(
        mode,
        collector,
        model=runtime.model,
        library_resolver=resolver,
        egress_context=object(),
        content_max_length=1000,
    )

    output = ordinary.invoke(_arguments(mode, url="[1]"))

    assert output.startswith("[5] ")
    assert "Captured:" not in output
    assert collector.fetch_count == 0
    resolver.assert_called_once_with(local)
    gate.assert_not_called()
    runtime.factory.assert_not_called()
    runtime.getter.assert_not_called()
    if mode == "full":
        assert output.endswith(("LOCAL_FACT " * 400)[:1000])
    else:
        prompt = runtime.model.invoke.call_args.args[0]
        assert ("LOCAL_FACT " * 400)[:1000] in prompt
        assert ("LOCAL_FACT " * 400)[:1001] not in prompt


@pytest.mark.parametrize("mode", FETCH_MODES[1:])
def test_chained_archive_read_uses_one_shared_budget(
    mode, runtime, chain_replay
):
    collector = _chained_collector()
    collector.reset()
    chain_replay.transport.routes[REPLAY] = {
        "status": 302,
        "headers": {"Location": ORIGINAL},
    }
    ordinary = build_fetch_tool(
        mode, collector, model=runtime.model, max_fetches=1
    )

    ordinary.invoke(_arguments(mode, url="[1]"))
    output = ordinary.invoke(_arguments(mode, url="[2]"))

    assert "budget exhausted (1/1)" in output
    assert collector.fetch_count == 1
    assert chain_replay.transport.sent == [REPLAY]
    assert collector.results == []
    runtime.getter.assert_not_called()
    runtime.model.invoke.assert_not_called()