diff --git a/src/agents/failover-error.ts b/src/agents/failover-error.ts index ee287d79484..5b3884b29f2 100644 --- a/src/agents/failover-error.ts +++ b/src/agents/failover-error.ts @@ -183,7 +183,19 @@ export function resolveFailoverReasonFromError(err: unknown): FailoverReason | n } const code = (getErrorCode(err) ?? "").toUpperCase(); - if (["ETIMEDOUT", "ESOCKETTIMEDOUT", "ECONNRESET", "ECONNABORTED"].includes(code)) { + if ( + [ + "ETIMEDOUT", + "ESOCKETTIMEDOUT", + "ECONNRESET", + "ECONNABORTED", + "ECONNREFUSED", + "ENETUNREACH", + "EHOSTUNREACH", + "ENETRESET", + "EAI_AGAIN", + ].includes(code) + ) { return "timeout"; } if (isTimeoutError(err)) { diff --git a/src/agents/model-fallback.test.ts b/src/agents/model-fallback.test.ts index cd0217faafc..14b705fffbd 100644 --- a/src/agents/model-fallback.test.ts +++ b/src/agents/model-fallback.test.ts @@ -751,6 +751,42 @@ describe("runWithModelFallback", () => { }); }); + it("falls back on ECONNREFUSED (local server down or remote unreachable)", async () => { + await expectFallsBackToHaiku({ + provider: "openai", + model: "gpt-4.1-mini", + firstError: Object.assign(new Error("connect ECONNREFUSED 127.0.0.1:11434"), { + code: "ECONNREFUSED", + }), + }); + }); + + it("falls back on ENETUNREACH (network disconnected)", async () => { + await expectFallsBackToHaiku({ + provider: "openai", + model: "gpt-4.1-mini", + firstError: Object.assign(new Error("connect ENETUNREACH"), { code: "ENETUNREACH" }), + }); + }); + + it("falls back on EHOSTUNREACH (host unreachable)", async () => { + await expectFallsBackToHaiku({ + provider: "openai", + model: "gpt-4.1-mini", + firstError: Object.assign(new Error("connect EHOSTUNREACH"), { code: "EHOSTUNREACH" }), + }); + }); + + it("falls back on EAI_AGAIN (DNS resolution failure)", async () => { + await expectFallsBackToHaiku({ + provider: "openai", + model: "gpt-4.1-mini", + firstError: Object.assign(new Error("getaddrinfo EAI_AGAIN api.openai.com"), { + code: "EAI_AGAIN", + }), + }); + }); + it("falls back on provider abort errors with request-aborted messages", async () => { await expectFallsBackToHaiku({ provider: "openai",