-
Notifications
You must be signed in to change notification settings - Fork 7
fix(opencode): broaden transport errno coverage and stop retrying permanent DNS failures #1467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 89 additions & 67 deletions
156
packages/opencode/src/session/stream-failure-classifier.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,85 +1,107 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { classifyStreamFailure } from "./stream-failure-classifier" | ||
| import { classifyStreamFailure, classifyBareTransportMessage } from "./stream-failure-classifier" | ||
|
|
||
| describe("classifyStreamFailure", () => { | ||
| describe("transport disconnect — retryable", () => { | ||
| test("ECONNRESET SystemError", () => { | ||
| const error = Object.assign(new Error("read ECONNRESET"), { code: "ECONNRESET", syscall: "read" }) | ||
| const result = classifyStreamFailure(error) | ||
| expect(result).toEqual({ | ||
| kind: "provider_transport_disconnect", | ||
| retryable: true, | ||
| code: "ECONNRESET", | ||
| }) | ||
| }) | ||
| // Every transport errno and its expected retryability. ENOTFOUND is the only | ||
| // permanent one (unresolved host); everything else is transient. Driven as a | ||
| // matrix so adding/renaming a code in TRANSPORT_CODES forces a matching row. | ||
| const TRANSPORT_CODE_MATRIX: ReadonlyArray<readonly [code: string, retryable: boolean]> = [ | ||
| ["ECONNRESET", true], | ||
| ["ECONNREFUSED", true], | ||
| ["ETIMEDOUT", true], | ||
| ["ECONNABORTED", true], | ||
| ["EPIPE", true], | ||
| ["EHOSTUNREACH", true], | ||
| ["ENETUNREACH", true], | ||
| ["EAI_AGAIN", true], | ||
| ["ENOTFOUND", false], | ||
| ["UND_ERR_SOCKET", true], | ||
| ["UND_ERR_CONNECT_TIMEOUT", true], | ||
| ["UND_ERR_HEADERS_TIMEOUT", true], | ||
| ["UND_ERR_BODY_TIMEOUT", true], | ||
| ] | ||
|
|
||
| test("UND_ERR_SOCKET — TypeError('terminated') with cause.code", () => { | ||
| const cause = Object.assign(new Error("other side closed"), { code: "UND_ERR_SOCKET" }) | ||
| const error = new TypeError("terminated", { cause }) | ||
| const result = classifyStreamFailure(error) | ||
| expect(result).toEqual({ | ||
| kind: "provider_transport_disconnect", | ||
| retryable: true, | ||
| code: "UND_ERR_SOCKET", | ||
| }) | ||
| describe("classifyStreamFailure — transport code matrix", () => { | ||
| for (const [code, retryable] of TRANSPORT_CODE_MATRIX) { | ||
| test(`top-level ${code} → transport disconnect (retryable=${retryable})`, () => { | ||
| const error = Object.assign(new Error(`failed: ${code}`), { code }) | ||
| expect(classifyStreamFailure(error)).toEqual({ kind: "provider_transport_disconnect", retryable, code }) | ||
| }) | ||
|
|
||
| test("ECONNREFUSED SystemError", () => { | ||
| const error = Object.assign(new Error("connect ECONNREFUSED"), { code: "ECONNREFUSED", syscall: "connect" }) | ||
| const result = classifyStreamFailure(error) | ||
| expect(result).toEqual({ | ||
| kind: "provider_transport_disconnect", | ||
| retryable: true, | ||
| code: "ECONNREFUSED", | ||
| }) | ||
| test(`${code} nested in the cause chain → transport disconnect (retryable=${retryable})`, () => { | ||
| const cause = Object.assign(new Error("inner"), { code }) | ||
| const error = new TypeError("fetch failed", { cause }) | ||
| expect(classifyStreamFailure(error)).toEqual({ kind: "provider_transport_disconnect", retryable, code }) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| test("ETIMEDOUT SystemError", () => { | ||
| const error = Object.assign(new Error("connect ETIMEDOUT"), { code: "ETIMEDOUT", syscall: "connect" }) | ||
| const result = classifyStreamFailure(error) | ||
| expect(result).toEqual({ | ||
| kind: "provider_transport_disconnect", | ||
| retryable: true, | ||
| code: "ETIMEDOUT", | ||
| }) | ||
| }) | ||
| describe("classifyStreamFailure — not a code-based transport error", () => { | ||
| test("generic Error returns undefined", () => { | ||
| expect(classifyStreamFailure(new Error("something broke"))).toBeUndefined() | ||
| }) | ||
|
|
||
| test("UND_ERR_SOCKET nested in cause chain", () => { | ||
| const innerCause = Object.assign(new Error("socket hang up"), { code: "UND_ERR_SOCKET" }) | ||
| const midError = new Error("fetch failed", { cause: innerCause }) | ||
| const outerError = new TypeError("terminated", { cause: midError }) | ||
| const result = classifyStreamFailure(outerError) | ||
| expect(result).toEqual({ | ||
| kind: "provider_transport_disconnect", | ||
| retryable: true, | ||
| code: "UND_ERR_SOCKET", | ||
| }) | ||
| }) | ||
| test("unknown errno code returns undefined", () => { | ||
| expect(classifyStreamFailure(Object.assign(new Error("x"), { code: "ESOMETHINGELSE" }))).toBeUndefined() | ||
| }) | ||
|
|
||
| describe("non-transport errors — not classified", () => { | ||
| test("generic Error returns undefined", () => { | ||
| expect(classifyStreamFailure(new Error("something broke"))).toBeUndefined() | ||
| }) | ||
| test("AbortError returns undefined", () => { | ||
| expect(classifyStreamFailure(new DOMException("The operation was aborted", "AbortError"))).toBeUndefined() | ||
| }) | ||
|
|
||
| test("TypeError without transport cause returns undefined", () => { | ||
| expect(classifyStreamFailure(new TypeError("Cannot read properties of undefined"))).toBeUndefined() | ||
| }) | ||
| test("non-Error values return undefined", () => { | ||
| expect(classifyStreamFailure("string error")).toBeUndefined() | ||
| expect(classifyStreamFailure(null)).toBeUndefined() | ||
| expect(classifyStreamFailure(42)).toBeUndefined() | ||
| }) | ||
|
|
||
| test("AbortError returns undefined", () => { | ||
| const error = new DOMException("The operation was aborted", "AbortError") | ||
| expect(classifyStreamFailure(error)).toBeUndefined() | ||
| }) | ||
| test("TypeError('terminated') without a transport-coded cause returns undefined", () => { | ||
| expect(classifyStreamFailure(new TypeError("terminated", { cause: new Error("unrelated") }))).toBeUndefined() | ||
| }) | ||
|
|
||
| test("non-Error values return undefined", () => { | ||
| expect(classifyStreamFailure("string error")).toBeUndefined() | ||
| expect(classifyStreamFailure(null)).toBeUndefined() | ||
| expect(classifyStreamFailure(42)).toBeUndefined() | ||
| test("a bare 'socket hang up' message is NOT code-classified (the bare-message fallback owns it)", () => { | ||
| // The message fallback was moved out of classifyStreamFailure so structured | ||
| // stream parsing can run first; the code path only matches errno codes. | ||
| expect(classifyStreamFailure(new Error("socket hang up"))).toBeUndefined() | ||
| }) | ||
|
|
||
| test("HTTP error (statusCode) with a transport-coded cause stays an API error, not transport", () => { | ||
| const cause = Object.assign(new Error("socket hang up"), { code: "UND_ERR_SOCKET" }) | ||
| const error = Object.assign(new Error("400 invalid request"), { statusCode: 400, cause }) | ||
| expect(classifyStreamFailure(error)).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe("classifyBareTransportMessage — anchored bare connection messages", () => { | ||
| test("exact 'socket hang up' → retryable transport", () => { | ||
| expect(classifyBareTransportMessage(new Error("socket hang up"))).toEqual({ | ||
| kind: "provider_transport_disconnect", | ||
| retryable: true, | ||
| code: "SOCKET_HANG_UP", | ||
| }) | ||
| }) | ||
|
|
||
| test("TypeError('terminated') without UND_ERR_SOCKET cause returns undefined", () => { | ||
| const error = new TypeError("terminated", { cause: new Error("unrelated") }) | ||
| expect(classifyStreamFailure(error)).toBeUndefined() | ||
| test("exact 'premature close' (surrounding whitespace trimmed) → retryable transport", () => { | ||
| expect(classifyBareTransportMessage(new Error(" premature close\n"))).toEqual({ | ||
| kind: "provider_transport_disconnect", | ||
| retryable: true, | ||
| code: "PREMATURE_CLOSE", | ||
| }) | ||
| }) | ||
|
|
||
| test("a longer message that merely CONTAINS the phrase is not a bare transport error", () => { | ||
| expect( | ||
| classifyBareTransportMessage(new Error("invalid request: socket hang up is not allowed")), | ||
| ).toBeUndefined() | ||
| }) | ||
|
|
||
| test("an HTTP error (statusCode) whose message is exactly 'socket hang up' is not bare transport", () => { | ||
| expect( | ||
| classifyBareTransportMessage(Object.assign(new Error("socket hang up"), { statusCode: 502 })), | ||
| ).toBeUndefined() | ||
| }) | ||
|
|
||
| test("non-Error value and unrelated message return undefined", () => { | ||
| expect(classifyBareTransportMessage("socket hang up")).toBeUndefined() | ||
| expect(classifyBareTransportMessage(new Error("something else"))).toBeUndefined() | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.