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.

4.8 KB
Raw Blame
"""Issue #7652: network failures must not land in filesystem_error, and the
token vocabulary must stay in lockstep with FailureClassifier's retry policy."""

import errno
import http.client
import os
import socket
import ssl

import pytest
import requests

from local_deep_research.library.download_management.failure_classifier import (
    FailureClassifier,
)
from local_deep_research.security.client_safe_errors import (
    CLIENT_SAFE_DOWNLOAD_MESSAGES,
    client_safe_download_message,
)


def _os(code):
    return OSError(code, os.strerror(code))


NETWORK_CASES = [
    (TimeoutError(errno.ETIMEDOUT, "Connection timed out"), "network_timeout"),
    (_os(errno.ENETUNREACH), "network_unavailable"),
    (_os(errno.ENETDOWN), "network_unavailable"),
    (_os(errno.EHOSTUNREACH), "network_unavailable"),
    (_os(errno.ENETRESET), "network_unavailable"),
    (
        ssl.SSLError(errno.ECONNRESET, "Connection reset by peer"),
        "network_unavailable",
    ),
    (ssl.SSLEOFError(8, "EOF"), "network_unavailable"),
    (requests.exceptions.SSLError("x"), "network_unavailable"),
    (socket.gaierror(-2, "Name or service not known"), "network_unavailable"),
    (socket.herror(1, "Unknown host"), "network_unavailable"),
    (http.client.RemoteDisconnected("closed"), "network_reset"),
    (
        ConnectionRefusedError(errno.ECONNREFUSED, "refused"),
        "network_unavailable",
    ),
    (BrokenPipeError(errno.EPIPE, "Broken pipe"), "network_reset"),
    (requests.exceptions.ReadTimeout("x"), "network_timeout"),
    (requests.exceptions.ConnectTimeout("x"), "network_timeout"),
]


@pytest.mark.parametrize("exc,token", NETWORK_CASES)
def test_network_errors_get_network_tokens(exc, token):
    assert client_safe_download_message(exc) == token


def test_real_filesystem_errno_still_filesystem():
    exc = OSError(errno.ENOSPC, os.strerror(errno.ENOSPC))
    assert client_safe_download_message(exc).startswith("filesystem_error:")


def test_non_canonical_strerror_does_not_leak():
    msg = client_safe_download_message(OSError(28, "leak: secret"))
    assert "secret" not in msg


@pytest.mark.parametrize(
    "token", sorted(set(CLIENT_SAFE_DOWNLOAD_MESSAGES.values()))
)
def test_every_display_token_is_classified(token):
    failure = FailureClassifier().classify_failure("str", details=token)
    assert failure.error_type != "unknown_error"


@pytest.mark.parametrize("exc,token", NETWORK_CASES)
def test_network_tokens_use_short_cooldown(exc, token):
    from datetime import timedelta

    failure = FailureClassifier().classify_failure(
        "str", details=client_safe_download_message(exc)
    )
    assert failure.retry_after is not None
    assert failure.retry_after <= timedelta(minutes=30)


def test_plain_epipe_matches_broken_pipe_error():
    plain = OSError(errno.EPIPE, os.strerror(errno.EPIPE))
    assert client_safe_download_message(plain) == client_safe_download_message(
        BrokenPipeError(errno.EPIPE, "Broken pipe")
    )


@pytest.mark.parametrize(
    "name,token",
    [
        ("WSAECONNREFUSED", "network_unavailable"),
        ("WSAENETUNREACH", "network_unavailable"),
        ("WSAEHOSTUNREACH", "network_unavailable"),
        ("WSAECONNRESET", "network_reset"),
        ("WSAETIMEDOUT", "network_timeout"),
    ],
)
def test_windows_wsa_errnos_get_network_tokens(name, token):
    code = getattr(errno, name, None)
    if code is None:
        pytest.skip(f"errno.{name} not defined on this platform")
    # A canonically-spelled WSA error must not become filesystem_error:*.
    try:
        strerror = os.strerror(code)
    except (ValueError, OverflowError):
        strerror = "x"
    assert client_safe_download_message(OSError(code, strerror)) == token


@pytest.mark.parametrize("bad_errno", [2**31, 2**40])
def test_out_of_range_errno_falls_back_to_class_name(bad_errno):
    # os.strerror raises OverflowError past the C int range; this used to
    # escape the token function.
    assert (
        client_safe_download_message(OSError(bad_errno, "x"))
        == "download_error:OSError"
    )


def test_strerror_value_error_falls_back_to_class_name(monkeypatch):
    def boom(_code):
        raise ValueError("unknown errno")

    monkeypatch.setattr(os, "strerror", boom)
    assert (
        client_safe_download_message(OSError(errno.ENOSPC, "No space"))
        == "download_error:OSError"
    )


@pytest.mark.parametrize(
    "token",
    [
        "filesystem_error:" + os.strerror(errno.ENOSPC),
        "filesystem_error:" + os.strerror(errno.EACCES),
        "download_error:OSError",
        "download_error:ValueError",
    ],
)
def test_filesystem_and_download_error_families_are_classified(token):
    failure = FailureClassifier().classify_failure("str", details=token)
    assert failure.error_type != "unknown_error"
    assert failure.retry_after is not None