Skip to content

The agent that grows with you

fix(vercel): retry httpx2 transport errors from the sandbox SDK

Maciej Lisiewski7528243600f3
diff --git a/tests/tools/test_vercel_sandbox_transient_errors.py b/tests/tools/test_vercel_sandbox_transient_errors.py
new file mode 100644
index 00000000000..1a7f3cd9570
--- /dev/null
+++ b/tests/tools/test_vercel_sandbox_transient_errors.py
@@ -0,0 +1,63 @@
+"""Vercel sandbox retries treat transport failures from the SDK's HTTP stack as transient.
+
+vercel >= 0.12 raises httpx2 exceptions, which do not subclass httpx's.
+"""
+
+import socket
+
+import httpx
+import pytest
+
+from tools.environments.vercel_sandbox import _is_transient_vercel_error, _retry_vercel_call
+
+httpx2 = pytest.importorskip("httpx2")
+
+
+def _refused_url() -> str:
+ with socket.socket() as sock:
+ sock.bind(("127.0.0.1", 0))
+ port = sock.getsockname()[1]
+ return f"http://127.0.0.1:{port}/"
+
+
[email protected]("client_mod", [httpx, httpx2], ids=["httpx", "httpx2"])
+def test_connection_refused_is_transient(client_mod):
+ with pytest.raises(Exception) as caught, client_mod.Client(timeout=5) as client:
+ client.get(_refused_url())
+
+ assert _is_transient_vercel_error(caught.value)
+
+
+def test_wrapped_httpx2_transport_error_is_transient():
+ try:
+ try:
+ raise httpx2.ReadError("connection reset")
+ except httpx2.ReadError as inner:
+ raise RuntimeError("sandbox create failed") from inner
+ except RuntimeError as outer:
+ assert _is_transient_vercel_error(outer)
+
+
+def test_httpx2_connect_error_is_retried():
+ calls = []
+
+ def flaky():
+ calls.append(1)
+ if len(calls) < 3:
+ raise httpx2.ConnectError("connection refused")
+ return "ok"
+
+ assert _retry_vercel_call("sandbox create", flaky, attempts=3) == "ok"
+ assert len(calls) == 3
+
+
+def test_non_transport_error_is_not_retried():
+ calls = []
+
+ def broken():
+ calls.append(1)
+ raise ValueError("bad request payload")
+
+ with pytest.raises(ValueError):
+ _retry_vercel_call("sandbox create", broken, attempts=3)
+ assert len(calls) == 1
diff --git a/tools/environments/vercel_sandbox.py b/tools/environments/vercel_sandbox.py
index 1009fd8dd10..9c809738ea9 100644
--- a/tools/environments/vercel_sandbox.py
+++ b/tools/environments/vercel_sandbox.py
@@ -80,6 +80,21 @@ def _extract_status_code(exc: BaseException) -> int | None:
return value
return None
+@cache
+def _transport_error_types() -> tuple[type[BaseException], ...]:
+ """Network exceptions of every HTTP stack the SDK may raise from.
+
+ vercel >= 0.12 moved the sandbox client from httpx to httpx2, a separate
+ distribution whose exceptions do not subclass httpx's.
+ """
+ types: list[type[BaseException]] = [httpx.NetworkError, httpx.ProtocolError, httpx.ReadError]
+ try:
+ import httpx2
+ except ImportError:
+ return tuple(types)
+ return (*types, httpx2.NetworkError, httpx2.ProtocolError, httpx2.ReadError)
+
+
def _is_transient_vercel_error(exc: BaseException) -> bool:
"""True when any exception in the cause/context chain looks retryable."""
seen: set[int] = set()
@@ -90,7 +105,7 @@ def _is_transient_vercel_error(exc: BaseException) -> bool:
status = next((c for c in codes if isinstance(c, int)), None)
name = type(error).__name__.lower()
if (status in _TRANSIENT_STATUS_CODES
- or isinstance(error, (httpx.NetworkError, httpx.ProtocolError, httpx.ReadError))
+ or isinstance(error, _transport_error_types())
or "ratelimit" in name or "servererror" in name):
return True
error = error.__cause__ or error.__context__