diff --git a/src/util/exponentialBackoff.test.ts b/src/util/exponentialBackoff.test.ts index 05fd9f30a..bd0e96c4d 100644 --- a/src/util/exponentialBackoff.test.ts +++ b/src/util/exponentialBackoff.test.ts @@ -13,7 +13,9 @@ function isRetryableError(error: any): boolean { if ( error.code === "ECONNRESET" || error.code === "ENOTFOUND" || - error.code === "ETIMEDOUT" + error.code === "ETIMEDOUT" || + error.code === "EPIPE" || + error.code === "ECONNREFUSED" ) { return true; } @@ -30,6 +32,21 @@ function isRetryableError(error: any): boolean { return true; } + // Anthropic specific errors + const lower = error.message?.toLowerCase(); + if (lower?.includes("overloaded")) { + return true; + } + + // Check for premature close errors by message content + if (lower?.includes("premature close") || + lower?.includes("premature end") || + lower?.includes("connection reset") || + lower?.includes("socket hang up") || + lower?.includes("aborted")) { + return true; + } + return false; } @@ -96,6 +113,51 @@ describe("exponentialBackoff utilities", () => { expect(isRetryableError(error)).toBe(true); }); + it("should return true for EPIPE errors", () => { + const error = { code: "EPIPE" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for ECONNREFUSED errors", () => { + const error = { code: "ECONNREFUSED" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for Anthropic overloaded errors", () => { + const error = { message: "The service is overloaded" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for premature close errors", () => { + const error = { message: "Error: Premature close" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for premature end errors", () => { + const error = { message: "Premature end of response" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for connection reset errors", () => { + const error = { message: "Connection reset by peer" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for socket hang up errors", () => { + const error = { message: "socket hang up" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for aborted connection errors", () => { + const error = { message: "Request aborted" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should handle case insensitive error messages", () => { + const error = { message: "PREMATURE CLOSE" }; + expect(isRetryableError(error)).toBe(true); + }); + it("should return false for 400 Bad Request", () => { const error = { status: 400 }; expect(isRetryableError(error)).toBe(false); diff --git a/src/util/exponentialBackoff.ts b/src/util/exponentialBackoff.ts index d594b488c..cff81418f 100644 --- a/src/util/exponentialBackoff.ts +++ b/src/util/exponentialBackoff.ts @@ -30,34 +30,75 @@ const DEFAULT_OPTIONS: Required = { hiddenRetries: 2, }; +/** + * Checks if the error is a network-related error + */ +function isNetworkError(error: any): boolean { + const networkCodes = [ + "ECONNRESET", + "ENOTFOUND", + "ETIMEDOUT", + "EPIPE", + "ECONNREFUSED", + ]; + return networkCodes.includes(error.code); +} + +/** + * Checks if the HTTP status code is retryable + */ +function isRetryableHttpStatus(status: number): boolean { + // 429 (Too Many Requests), 502 (Bad Gateway), 503 (Service Unavailable), 504 (Gateway Timeout) + return status === 429 || status === 502 || status === 503 || status === 504; +} + +/** + * Checks if the error type indicates a server issue + */ +function isServerError(error: any): boolean { + return error.type === "server_error" || error.type === "rate_limit_exceeded"; +} + +/** + * Checks if the error message indicates a connection issue + */ +function isConnectionError(errorMessage: string): boolean { + const connectionErrorPatterns = [ + "premature close", + "premature end", + "connection reset", + "socket hang up", + "aborted", + "overloaded", + ]; + + return connectionErrorPatterns.some((pattern) => + errorMessage.includes(pattern), + ); +} + /** * Determines if an error is retryable based on the error type and status code */ function isRetryableError(error: any): boolean { // Network errors are retryable - if ( - error.code === "ECONNRESET" || - error.code === "ENOTFOUND" || - error.code === "ETIMEDOUT" - ) { + if (isNetworkError(error)) { return true; } // HTTP status codes that are retryable - if (error.status) { - const status = error.status; - // 429 (Too Many Requests), 502 (Bad Gateway), 503 (Service Unavailable), 504 (Gateway Timeout) - return status === 429 || status === 502 || status === 503 || status === 504; - } - - // OpenAI specific errors - if (error.type === "server_error" || error.type === "rate_limit_exceeded") { + if (error.status && isRetryableHttpStatus(error.status)) { return true; } - // Anthropic specific errors - const lower = error.message?.toLowerCase(); - if (lower?.includes("overloaded")) { + // OpenAI/API specific errors + if (isServerError(error)) { + return true; + } + + // Check for connection issues by message content + const errorMessage = error.message?.toLowerCase(); + if (errorMessage && isConnectionError(errorMessage)) { return true; }