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.

20.2 KB
Raw Blame
"""Exponential-backoff retry wrapper for `safe_get`."""

import email.utils
import threading
import time
from datetime import datetime, timedelta, timezone
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from unittest.mock import MagicMock, patch

import pytest
import requests

from local_deep_research.security.safe_requests import (
    safe_get_with_retries,
)


def _mock_response(status_code=200, headers=None, content=b""):
    r = MagicMock(spec=requests.Response)
    r.status_code = status_code
    r.headers = headers or {}
    r.content = content
    return r


def _mock_response_body_raises(exc):
    """Response whose ``.content`` access raises ``exc`` on first read.

    Models the real production failure where ``safe_get`` returns a
    Response (headers received), then ``ChunkedEncodingError`` /
    ``ReadTimeout`` fires later when the body is consumed.
    """
    r = MagicMock(spec=requests.Response)
    r.status_code = 200
    r.headers = {}
    type(r).content = property(lambda self: (_ for _ in ()).throw(exc))
    return r


def test_first_attempt_success_returns_immediately():
    ok = _mock_response(200)
    with patch(
        "local_deep_research.security.safe_requests.safe_get",
        return_value=ok,
    ) as mock_get:
        resp = safe_get_with_retries("https://example.com/x")
    assert resp.status_code == 200
    assert mock_get.call_count == 1


def test_connection_error_retries_then_succeeds():
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[requests.ConnectionError("boom"), ok],
        ) as mock_get,
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        resp = safe_get_with_retries(
            "https://example.com/x", backoff_times=(0, 0, 0)
        )
    assert resp.status_code == 200
    assert mock_get.call_count == 2
    assert mock_sleep.called


def test_timeout_retries_then_gives_up():
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=requests.Timeout("slow"),
        ) as mock_get,
        patch("local_deep_research.security.safe_requests.time.sleep"),
    ):
        with pytest.raises(requests.Timeout):
            safe_get_with_retries(
                "https://example.com/x",
                max_retries=2,
                backoff_times=(0, 0, 0),
            )
    # 1 initial + 2 retries = 3 attempts total
    assert mock_get.call_count == 3


def test_http_500_retries_then_succeeds():
    bad = _mock_response(500)
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[bad, ok],
        ) as mock_get,
        patch("local_deep_research.security.safe_requests.time.sleep"),
    ):
        resp = safe_get_with_retries(
            "https://example.com/x", backoff_times=(0, 0, 0)
        )
    assert resp.status_code == 200
    assert mock_get.call_count == 2


def test_http_429_honors_retry_after():
    rate_limited = _mock_response(429, headers={"Retry-After": "7"})
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[rate_limited, ok],
        ),
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        safe_get_with_retries("https://example.com/x", backoff_times=(1, 1, 1))
    # Retry-After was 7, must override the 1-second schedule.
    assert mock_sleep.call_args[0][0] == 7


def test_ssrf_validation_error_is_not_retried():
    """ValueError means SSRF rejection — retrying would just re-fail."""
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=ValueError("URL failed security validation"),
        ) as mock_get,
        patch("local_deep_research.security.safe_requests.time.sleep"),
    ):
        with pytest.raises(ValueError):
            safe_get_with_retries(
                "http://169.254.169.254/", backoff_times=(0, 0, 0)
            )
    assert mock_get.call_count == 1


def test_http_404_is_not_retried():
    """4xx (other than 429) is caller's fault — no retry."""
    not_found = _mock_response(404)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            return_value=not_found,
        ) as mock_get,
        patch("local_deep_research.security.safe_requests.time.sleep"),
    ):
        resp = safe_get_with_retries(
            "https://example.com/x", backoff_times=(0, 0, 0)
        )
    assert resp.status_code == 404
    assert mock_get.call_count == 1


def test_retry_after_integer_is_capped_at_max():
    """A hostile Retry-After: 86400 must not pin the worker for a day."""
    rate_limited = _mock_response(429, headers={"Retry-After": "86400"})
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[rate_limited, ok],
        ),
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        safe_get_with_retries("https://example.com/x", backoff_times=(1, 1, 1))
    assert mock_sleep.call_args[0][0] == 300


def test_retry_after_http_date_form_is_parsed():
    """RFC 7231 HTTP-date form must be parsed, not silently ignored."""
    future = datetime.now(timezone.utc) + timedelta(seconds=60)
    http_date = email.utils.formatdate(future.timestamp(), usegmt=True)
    rate_limited = _mock_response(429, headers={"Retry-After": http_date})
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[rate_limited, ok],
        ),
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        safe_get_with_retries("https://example.com/x", backoff_times=(1, 1, 1))
    slept = mock_sleep.call_args[0][0]
    # 30s band absorbs clock jitter and test-harness latency.
    assert 45 <= slept <= 75, f"expected ~60s, got {slept}s"


@pytest.mark.parametrize("form", ["-0000", "no zone", "asctime"])
def test_retry_after_http_date_without_a_zone_is_read_as_utc(form):
    """An HTTP-date that names no zone is UTC, and must be waited for.

    ``email.utils`` parses these forms to a naive datetime. Subtracting
    the aware current time from it raised ``TypeError`` out of the retry
    loop, so the fetch failed instead of being retried.
    """
    future = datetime.now(timezone.utc) + timedelta(seconds=60)
    if form == "asctime":
        http_date = time.asctime(time.gmtime(future.timestamp()))
    else:
        http_date = email.utils.formatdate(future.timestamp())
        assert http_date.endswith(" -0000")
        if form == "no zone":
            http_date = http_date.removesuffix(" -0000")
    assert email.utils.parsedate_to_datetime(http_date).tzinfo is None
    rate_limited = _mock_response(429, headers={"Retry-After": http_date})
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[rate_limited, ok],
        ),
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        resp = safe_get_with_retries(
            "https://example.com/x", backoff_times=(1, 1, 1)
        )
    assert resp is ok
    slept = mock_sleep.call_args[0][0]
    # 30s band absorbs clock jitter and test-harness latency.
    assert 45 <= slept <= 75, f"expected ~60s, got {slept}s"


def test_retry_after_http_date_with_an_overflowing_field_uses_schedule():
    """A date field too large for ``datetime`` falls back to the schedule.

    Depending on the Python version, ``parsedate_to_datetime`` raises
    ``OverflowError`` directly or wraps it in ``ValueError``. Both must
    preserve the retry behavior.
    """
    huge = "Wed, 21 Oct 2015 99999999999999999999:28:00 GMT"
    rate_limited = _mock_response(429, headers={"Retry-After": huge})
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[rate_limited, ok],
        ),
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        resp = safe_get_with_retries(
            "https://example.com/x", backoff_times=(1, 2, 4)
        )
    assert resp is ok
    assert mock_sleep.call_args[0][0] == 1


@pytest.mark.parametrize("parse_error", [OverflowError, ValueError])
def test_retry_after_date_parser_error_uses_schedule(parse_error):
    """Keep both parser error forms covered on every supported Python."""
    http_date = "Wed, 21 Oct 2015 07:28:00 GMT"
    rate_limited = _mock_response(429, headers={"Retry-After": http_date})
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[rate_limited, ok],
        ) as mock_get,
        patch(
            "local_deep_research.security.safe_requests.email.utils.parsedate_to_datetime",
            side_effect=parse_error("invalid date field"),
        ) as mock_parse,
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        resp = safe_get_with_retries(
            "https://example.com/x", backoff_times=(1, 2, 4)
        )
    assert resp is ok
    assert mock_get.call_count == 2
    mock_parse.assert_called_once_with(http_date)
    mock_sleep.assert_called_once_with(1)
    rate_limited.close.assert_called_once()


def test_retry_after_unparseable_falls_back_to_schedule():
    """Garbage Retry-After falls back to the backoff schedule."""
    rate_limited = _mock_response(429, headers={"Retry-After": "garbage"})
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[rate_limited, ok],
        ),
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        safe_get_with_retries("https://example.com/x", backoff_times=(1, 2, 4))
    assert mock_sleep.call_args[0][0] == 1


def test_retry_after_negative_integer_is_clamped_to_zero():
    """time.sleep(-5) raises; Retry-After: -5 must be clamped to 0."""
    rate_limited = _mock_response(429, headers={"Retry-After": "-5"})
    ok = _mock_response(200)
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[rate_limited, ok],
        ),
        patch(
            "local_deep_research.security.safe_requests.time.sleep"
        ) as mock_sleep,
    ):
        safe_get_with_retries("https://example.com/x", backoff_times=(1, 1, 1))
    assert mock_sleep.call_args[0][0] == 0


# ---------------------------------------------------------------------------
# consume_body=True: retry on body-stream transients
# ---------------------------------------------------------------------------


def test_consume_body_default_does_not_read_body():
    """Default (``consume_body=False``) must not touch ``response.content``.

    The wrapper has historically returned the response without reading
    the body. Callers that stream large bodies (e.g., NDJSON line-by-line)
    rely on this — a regression that pre-reads would balloon memory.
    """
    body_marker = MagicMock()
    body_marker.__bool__ = MagicMock(side_effect=AssertionError("touched"))
    resp = _mock_response(200)
    type(resp).content = property(lambda self: body_marker)

    with patch(
        "local_deep_research.security.safe_requests.safe_get",
        return_value=resp,
    ):
        # Should not raise — default behavior leaves .content untouched.
        safe_get_with_retries("https://example.com/x")


def test_consume_body_retries_on_chunked_encoding_error():
    """Mid-stream ``ChunkedEncodingError`` retries the whole fetch."""
    bad = _mock_response_body_raises(
        requests.exceptions.ChunkedEncodingError(
            "IncompleteRead(835082 bytes read, 1262437 more expected)"
        )
    )
    ok = _mock_response(200, content=b"final")
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[bad, ok],
        ) as mock_get,
        patch("local_deep_research.security.safe_requests.time.sleep"),
    ):
        resp = safe_get_with_retries(
            "https://example.com/x",
            consume_body=True,
            backoff_times=(0, 0, 0),
        )
    assert resp.content == b"final"
    assert mock_get.call_count == 2
    bad.close.assert_called_once()


def test_consume_body_retries_on_read_timeout():
    """``ReadTimeout`` (Timeout but NOT ConnectionError) is also retried.

    Pinning this explicitly: the obvious ``except ConnectionError``
    catch would silently miss ReadTimeout (it's a Timeout subclass,
    not a ConnectionError subclass), so the implementation must list
    ``Timeout`` separately.
    """
    bad = _mock_response_body_raises(
        requests.exceptions.ReadTimeout("body read stalled")
    )
    ok = _mock_response(200, content=b"final")
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            side_effect=[bad, ok],
        ) as mock_get,
        patch("local_deep_research.security.safe_requests.time.sleep"),
    ):
        resp = safe_get_with_retries(
            "https://example.com/x",
            consume_body=True,
            backoff_times=(0, 0, 0),
        )
    assert resp.content == b"final"
    assert mock_get.call_count == 2


def test_consume_body_gives_up_after_max_retries():
    """Persistent body-read failures eventually surface to the caller."""
    bad = _mock_response_body_raises(
        requests.exceptions.ChunkedEncodingError("broken")
    )
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            return_value=bad,
        ) as mock_get,
        patch("local_deep_research.security.safe_requests.time.sleep"),
    ):
        with pytest.raises(requests.exceptions.ChunkedEncodingError):
            safe_get_with_retries(
                "https://example.com/x",
                consume_body=True,
                max_retries=2,
                backoff_times=(0, 0, 0),
            )
    # 1 initial + 2 retries = 3 attempts
    assert mock_get.call_count == 3


def test_consume_body_does_not_retry_value_error_from_body_guard():
    """``ValueError`` from oversized-body guard must NOT be retried.

    A 1 GB+ response isn't a transient network error — retrying just
    burns more bandwidth on the same outcome. The guard's ValueError
    must propagate immediately on the first attempt.
    """
    bad = _mock_response_body_raises(
        ValueError("Response body too large: >1073741825 bytes")
    )
    with (
        patch(
            "local_deep_research.security.safe_requests.safe_get",
            return_value=bad,
        ) as mock_get,
        patch("local_deep_research.security.safe_requests.time.sleep"),
    ):
        with pytest.raises(ValueError):
            safe_get_with_retries(
                "https://example.com/x",
                consume_body=True,
                backoff_times=(0, 0, 0),
            )
    # Single attempt — no retry on ValueError.
    assert mock_get.call_count == 1


def test_consume_body_returns_cached_body_to_caller():
    """After a successful retry, ``response.content`` is cached.

    The point of consuming inside the loop is so the caller doesn't
    repeat the read (and risk a second transient). Verify the body
    is available without a second read.
    """
    ok = _mock_response(200, content=b"hello world")
    with patch(
        "local_deep_research.security.safe_requests.safe_get",
        return_value=ok,
    ):
        resp = safe_get_with_retries("https://example.com/x", consume_body=True)
    assert resp.content == b"hello world"


# ---------------------------------------------------------------------------
# consume_body=True through the real safe_get (nothing patched)
# ---------------------------------------------------------------------------
#
# The consume_body tests above patch safe_get, so they cannot see whether
# the body is still unread when safe_get returns. safe_get reads the body
# itself unless stream=True is passed, and a ChunkedEncodingError raised
# there is neither a ConnectionError nor a Timeout, so it would escape the
# wrapper unretried. These run the real safe_get over a real requests
# transport against a server on 127.0.0.1.

_BODY = b"0123456789abcdef" * 256  # 4 KiB


@pytest.fixture
def loopback():
    """Start a 127.0.0.1 server that answers from a script.

    ``serve(script)`` returns ``(url, seen)``. ``script`` names one
    response per request, in order, and its last entry repeats:
    ``"ok-length"`` / ``"ok-chunked"`` send ``_BODY`` with a
    Content-Length or chunked framing, ``"broken-length"`` /
    ``"broken-chunked"`` promise all of it but send a third and hang up
    (the client raises ``ChunkedEncodingError`` mid-body), and
    ``"busy"`` sends ``_BODY`` with status 503. ``seen`` lists the
    request paths received.
    """
    servers = []

    def serve(script):
        seen = []
        lock = threading.Lock()

        class Handler(BaseHTTPRequestHandler):
            protocol_version = "HTTP/1.1"

            def do_GET(self):
                with lock:
                    seen.append(self.path)
                    kind = script[min(len(seen), len(script)) - 1]
                self.close_connection = True
                body = _BODY[: len(_BODY) // 3] if "broken" in kind else _BODY
                self.send_response(503 if kind == "busy" else 200)
                self.send_header("Connection", "close")
                if kind.endswith("chunked"):
                    self.send_header("Transfer-Encoding", "chunked")
                    self.end_headers()
                    self.wfile.write(b"%x\r\n" % len(_BODY) + body)
                    if "broken" not in kind:
                        self.wfile.write(b"\r\n0\r\n\r\n")
                else:
                    self.send_header("Content-Length", str(len(_BODY)))
                    self.end_headers()
                    self.wfile.write(body)
                self.wfile.flush()

            def log_message(self, *args):
                pass

        server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
        thread = threading.Thread(target=server.serve_forever, daemon=True)
        thread.start()
        servers.append((server, thread))
        return f"http://127.0.0.1:{server.server_port}/data", seen

    yield serve
    for server, thread in servers:
        server.shutdown()
        server.server_close()
        thread.join(timeout=2)


@pytest.mark.parametrize("framing", ["length", "chunked"])
def test_consume_body_retries_mid_body_break_through_real_safe_get(
    loopback, framing
):
    """A body that breaks mid-read is fetched again and returned.

    Red if the wrapper stops passing ``stream=True``: safe_get then reads
    the body before returning and the ``ChunkedEncodingError`` escapes
    after one request.
    """
    url, seen = loopback([f"broken-{framing}", f"ok-{framing}"])

    response = safe_get_with_retries(
        url,
        allow_localhost=True,
        timeout=5,
        consume_body=True,
        backoff_times=(0,),
    )

    assert len(seen) == 2
    assert response.status_code == 200
    # Read and cached inside the wrapper: closing the transport loses
    # nothing.
    response.close()
    assert response.content == _BODY


def test_consume_body_final_retryable_status_returns_read_body(loopback):
    """The last attempt's 503 comes back with its body already read.

    ``stream=True`` makes safe_get hand over an unread body, so a
    retryable status returned before the consume_body read would leave
    it unread on the response.
    """
    url, seen = loopback(["busy"])

    response = safe_get_with_retries(
        url,
        allow_localhost=True,
        timeout=5,
        consume_body=True,
        max_retries=1,
        backoff_times=(0,),
    )

    assert len(seen) == 2
    assert response.status_code == 503
    response.close()
    assert response.content == _BODY