Skip to content

Commit 0e7deee

Browse files
sunlit-dengsallyom
andauthored
fix(failover): classify transient Undici error codes (#123042)
* fix(failover): classify transient Undici error codes * fix(failover): share transient network code policy Signed-off-by: sallyom <[email protected]> --------- Signed-off-by: sallyom <[email protected]> Co-authored-by: sallyom <[email protected]>
1 parent aeff737 commit 0e7deee

5 files changed

Lines changed: 40 additions & 13 deletions

File tree

‎src/agents/embedded-agent-helpers/assistant-message-failures.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ describe("classifyAssistantFailoverReason", () => {
2525
expect(classifyAssistantFailoverReason(opencodeGoStalledStreamError)).toBe("timeout");
2626
});
2727

28+
it.each([
29+
"ENOTFOUND",
30+
"UND_ERR_CONNECT_TIMEOUT",
31+
"UND_ERR_DNS_RESOLVE_FAILED",
32+
"UND_ERR_CONNECT",
33+
"UND_ERR_SOCKET",
34+
"UND_ERR_HEADERS_TIMEOUT",
35+
"UND_ERR_BODY_TIMEOUT",
36+
])("classifies structured %s assistant errors as timeouts", (errorCode) => {
37+
expect(
38+
classifyAssistantFailoverReason({
39+
...opencodeGoStalledStreamError,
40+
provider: "demo-provider",
41+
errorCode,
42+
errorMessage: "provider connection closed",
43+
}),
44+
).toBe("timeout");
45+
});
46+
2847
it("does not classify caller-aborted assistant messages as provider failover", () => {
2948
expect(
3049
classifyAssistantFailoverReason({

‎src/agents/failover/classification-rules.ts‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
2+
import { isTransientNetworkError } from "../../infra/retryable-network-errors.js";
23
import {
34
extractLeadingHttpStatus,
45
parseApiErrorInfo,
@@ -9,18 +10,9 @@ import {
910
isRateLimitErrorMessage,
1011
} from "./message-patterns.js";
1112
import type { FailoverClassification, FailoverReason, FailoverSignal } from "./signal.js";
12-
const TIMEOUT_ERROR_CODES = new Set([
13-
"ETIMEDOUT",
14-
"ESOCKETTIMEDOUT",
15-
"ECONNRESET",
16-
"ECONNABORTED",
17-
"ECONNREFUSED",
18-
"ENETUNREACH",
19-
"EHOSTUNREACH",
13+
const FAILOVER_TIMEOUT_ERROR_CODES = new Set([
2014
"EHOSTDOWN",
2115
"ENETRESET",
22-
"EPIPE",
23-
"EAI_AGAIN",
2416
"ERR_STREAM_PREMATURE_CLOSE",
2517
]);
2618
const NO_BODY_HTTP_WRAPPER_RE =
@@ -342,7 +334,10 @@ export function classifyFailoverReasonFromCode(raw: string | undefined): Failove
342334
case "OVERLOADED_ERROR":
343335
return "overloaded";
344336
default:
345-
return TIMEOUT_ERROR_CODES.has(normalized) ? "timeout" : null;
337+
return FAILOVER_TIMEOUT_ERROR_CODES.has(normalized) ||
338+
isTransientNetworkError({ code: normalized })
339+
? "timeout"
340+
: null;
346341
}
347342
}
348343
export function classifyCoreFailoverReasonFromErrorType(

‎src/agents/failover/retry-evidence.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { parseRetryAfterHttpDateMs } from "@openclaw/ai/internal/retry-after";
22
import milliseconds from "ms";
3+
import { isTransientNetworkError } from "../../infra/retryable-network-errors.js";
34
import {
45
extractErrorHttpStatus,
56
extractLeadingHttpStatus,
@@ -48,12 +49,13 @@ function resolveRetrySignalStatus(signal: Pick<FailoverSignal, "message" | "stat
4849

4950
/** Narrow evidence that replaying the same assistant request may succeed within this session. */
5051
export function hasTransientRetryEvidence(
51-
signal: Pick<FailoverSignal, "message" | "status">,
52+
signal: Pick<FailoverSignal, "code" | "message" | "status">,
5253
): boolean {
5354
const status = resolveRetrySignalStatus(signal);
5455
return (
5556
(status !== undefined && RETRYABLE_HTTP_STATUS_CODES.has(status)) ||
56-
TRANSIENT_RETRY_EVIDENCE_RE.test(signal.message ?? "")
57+
TRANSIENT_RETRY_EVIDENCE_RE.test(signal.message ?? "") ||
58+
isTransientNetworkError({ code: signal.code })
5759
);
5860
}
5961

‎src/llm/utils/retry.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ describe("isRetryableAssistantError", () => {
5555
).toBe(false);
5656
});
5757

58+
it("retries a structured transient Undici error", () => {
59+
expect(
60+
isRetryableAssistantError({
61+
...errorMessage("provider connection closed"),
62+
errorCode: "UND_ERR_HEADERS_TIMEOUT",
63+
}),
64+
).toBe(true);
65+
});
66+
5867
it.each([
5968
"An error occurred while processing your request. You can retry your request.",
6069
"The system encountered an unexpected error. Try your request again.",

‎src/llm/utils/retry.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export function isRetryableAssistantError(message: AssistantMessage): boolean {
3636
const signal = {
3737
message: errorMessage,
3838
provider: message.provider,
39+
code: message.errorCode,
40+
errorType: message.errorType,
3941
...(status === undefined ? {} : { status }),
4042
};
4143
const classification = classifyFailoverSignal(signal);

0 commit comments

Comments
 (0)