Skip to content

Commit 7533630

Browse files
committed
fix(error_classifier): classify connect/DNS failure messages on generic exception types
Port from anomalyco/opencode#40707: connection-establishment and DNS failure messages wrapped in generic exceptions (RuntimeError from local shims, MCP bridges, SDKs re-raising without chaining) fell through to FailoverReason.unknown, which misses the retry loop's eager transport fallback — the full retry budget burned against a dead endpoint before provider fallback. New _CONNECTION_MESSAGE_PATTERNS (connect refused, no route, network unreachable, DNS phrasings across Python/glibc/macOS/Node, fetch failed, Envoy upstream connect error) classify as retryable timeout via _classify_by_message, mirroring _TIMEOUT_MESSAGE_PATTERNS. Mid-stream disconnect strings are deliberately excluded — they keep their _SERVER_DISCONNECT_PATTERNS routing (large-session compression).
1 parent ff7c951 commit 7533630

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎agent/error_classifier.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,43 @@ def _model_id_missing_known_prefix(model: str, provider: str) -> bool:
575575
"upstream timed out",
576576
]
577577

578+
# Connection-establishment / DNS failure message patterns. These surface
579+
# when the exception TYPE is generic (RuntimeError/Exception from a local
580+
# shim, MCP bridge, subprocess wrapper, or an SDK that re-raises without
581+
# chaining) so the _TRANSPORT_ERROR_TYPES check never fires, and the error
582+
# carries no HTTP status. Without message-level matching they fall through
583+
# to FailoverReason.unknown, which misses the transport eager-fallback path
584+
# in the retry loop (unknown retries the same dead endpoint for the full
585+
# budget before fallback). Ported from anomalyco/opencode#40707, which hit
586+
# the same bug shape: serialized midstream errors matched by type only.
587+
#
588+
# Deliberately EXCLUDES mid-stream disconnect strings ("connection reset by
589+
# peer", "peer closed connection", "unexpected eof", "socket hang up") —
590+
# those belong to _SERVER_DISCONNECT_PATTERNS, whose classification step
591+
# runs later and routes large sessions to context-overflow compression.
592+
# A connection that was never established cannot be a server-side overflow
593+
# rejection, so these are safe to classify as plain retryable transport.
594+
_CONNECTION_MESSAGE_PATTERNS = [
595+
# TCP connect failures
596+
"connection refused",
597+
"econnrefused",
598+
"no route to host",
599+
"network is unreachable",
600+
"network unreachable",
601+
# DNS resolution failures (Python, glibc, macOS, Node bridge phrasings)
602+
"name or service not known",
603+
"temporary failure in name resolution",
604+
"nodename nor servname provided",
605+
"getaddrinfo failed",
606+
"getaddrinfo enotfound",
607+
"eai_again",
608+
# Node/undici bridge generic network failure (MCP servers, local shims)
609+
"fetch failed",
610+
"failed to fetch",
611+
# Envoy/proxy upstream connect failure (cloud gateways)
612+
"upstream connect error",
613+
]
614+
578615
# Transport error type names
579616
_TRANSPORT_ERROR_TYPES = frozenset({
580617
"ReadTimeout", "ConnectTimeout", "PoolTimeout",
@@ -1794,6 +1831,16 @@ def _classify_by_message(
17941831
if any(p in error_msg for p in _TIMEOUT_MESSAGE_PATTERNS):
17951832
return result_fn(FailoverReason.timeout, retryable=True)
17961833

1834+
# Connection-establishment / DNS failure message patterns — same shim
1835+
# problem as the timeout patterns above: the wrapping exception type is
1836+
# generic, so _TRANSPORT_ERROR_TYPES never matches and the error would
1837+
# fall through to FailoverReason.unknown. Classified as timeout (the
1838+
# transport bucket) so the retry loop's eager transport fallback and
1839+
# client rebuild apply. Never routes to compression: a connection that
1840+
# was never established is not a context-overflow signal.
1841+
if any(p in error_msg for p in _CONNECTION_MESSAGE_PATTERNS):
1842+
return result_fn(FailoverReason.timeout, retryable=True)
1843+
17971844
return None
17981845

17991846

‎tests/agent/test_error_classifier.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,67 @@ def test_stale_breaker_runtime_error_triggers_fallback_not_retry(self):
11681168
assert result.should_compress is False
11691169

11701170

1171+
# ── Test: connection/DNS failure message patterns on generic exception types ──
1172+
# Port of anomalyco/opencode#40707 (expand retryable error patterns): errors
1173+
# whose TYPE is generic (RuntimeError/Exception from local shims, MCP bridges,
1174+
# re-raising SDKs) but whose MESSAGE carries a connection-establishment or DNS
1175+
# failure must classify as retryable transport, not FailoverReason.unknown.
1176+
1177+
class TestConnectionMessagePatterns:
1178+
"""Generic-typed connect/DNS failures route to the transport bucket."""
1179+
1180+
@pytest.mark.parametrize("message", [
1181+
"connect ECONNREFUSED 127.0.0.1:11434",
1182+
"Connection refused by proxy",
1183+
"getaddrinfo failed",
1184+
"getaddrinfo ENOTFOUND api.example.com",
1185+
"[Errno -3] Temporary failure in name resolution",
1186+
"[Errno 8] nodename nor servname provided, or not known",
1187+
"getaddrinfo EAI_AGAIN openrouter.ai",
1188+
"Name or service not known",
1189+
"No route to host",
1190+
"[Errno 101] Network is unreachable",
1191+
"fetch failed",
1192+
"TypeError: Failed to fetch",
1193+
"upstream connect error or disconnect/reset before headers",
1194+
])
1195+
def test_generic_exception_with_connect_failure_message_is_timeout(self, message):
1196+
# RuntimeError — NOT in _TRANSPORT_ERROR_TYPES, not a ConnectionError
1197+
# subclass, no status code. Without message matching this falls to
1198+
# FailoverReason.unknown and misses the eager transport fallback.
1199+
result = classify_api_error(RuntimeError(message))
1200+
assert result.reason == FailoverReason.timeout, message
1201+
assert result.retryable is True
1202+
assert result.should_compress is False
1203+
1204+
def test_connect_failure_never_routes_to_compression_on_large_session(self):
1205+
# A connection that was never established is not an overflow signal,
1206+
# even when the session is huge (the disconnect+large-session
1207+
# heuristic must not apply to connect-phase failures).
1208+
result = classify_api_error(
1209+
RuntimeError("connect ECONNREFUSED 10.0.0.5:443"),
1210+
approx_tokens=180000, context_length=200000, num_messages=400,
1211+
)
1212+
assert result.reason == FailoverReason.timeout
1213+
assert result.should_compress is False
1214+
1215+
def test_midstream_disconnect_patterns_still_use_disconnect_path(self):
1216+
# "connection reset by peer" is deliberately NOT in the connect-phase
1217+
# list — it stays on the _SERVER_DISCONNECT_PATTERNS path, which
1218+
# routes large sessions to context-overflow compression.
1219+
result = classify_api_error(
1220+
RuntimeError("Connection reset by peer"),
1221+
approx_tokens=180000, context_length=200000, num_messages=400,
1222+
)
1223+
assert result.reason == FailoverReason.context_overflow
1224+
assert result.should_compress is True
1225+
1226+
def test_plain_unknown_error_still_unknown(self):
1227+
# Guard against over-matching: an unrelated message stays unknown.
1228+
result = classify_api_error(RuntimeError("something exploded"))
1229+
assert result.reason == FailoverReason.unknown
1230+
1231+
11711232
# ── Test: throttle vs overflow disambiguation + new overflow shapes ─────
11721233
# Port of anomalyco/opencode#37848 (expand context overflow patterns +
11731234
# rate-limit exclusion guard).

0 commit comments

Comments
 (0)