-
Notifications
You must be signed in to change notification settings - Fork 14
fix(session): unify transport error classification for stream disconnect recovery #941
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
5e9406a
fix(session): unify transport error classification for stream disconn…
Astro-Han 646676c
fix(session): use || for empty-string fallback, try-catch in cause tr…
Astro-Han 3c1b115
test(processor): assert failed reasoning is removed on transport retry
Astro-Han 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
85 changes: 85 additions & 0 deletions
85
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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { classifyStreamFailure } 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", | ||
| }) | ||
| }) | ||
|
|
||
| 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", | ||
| }) | ||
| }) | ||
|
|
||
| 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("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", | ||
| }) | ||
| }) | ||
|
|
||
| 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", | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("non-transport errors — not classified", () => { | ||
| test("generic Error returns undefined", () => { | ||
| expect(classifyStreamFailure(new Error("something broke"))).toBeUndefined() | ||
| }) | ||
|
|
||
| test("TypeError without transport cause returns undefined", () => { | ||
| expect(classifyStreamFailure(new TypeError("Cannot read properties of undefined"))).toBeUndefined() | ||
| }) | ||
|
|
||
| test("AbortError returns undefined", () => { | ||
| const error = new DOMException("The operation was aborted", "AbortError") | ||
| expect(classifyStreamFailure(error)).toBeUndefined() | ||
| }) | ||
|
|
||
| test("non-Error values return undefined", () => { | ||
| expect(classifyStreamFailure("string error")).toBeUndefined() | ||
| expect(classifyStreamFailure(null)).toBeUndefined() | ||
| expect(classifyStreamFailure(42)).toBeUndefined() | ||
| }) | ||
|
|
||
| test("TypeError('terminated') without UND_ERR_SOCKET cause returns undefined", () => { | ||
| const error = new TypeError("terminated", { cause: new Error("unrelated") }) | ||
| expect(classifyStreamFailure(error)).toBeUndefined() | ||
| }) | ||
| }) | ||
| }) |
34 changes: 34 additions & 0 deletions
34
packages/opencode/src/session/stream-failure-classifier.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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| export type TransportDisconnect = { | ||
| kind: "provider_transport_disconnect" | ||
| retryable: true | ||
| code: string | ||
| } | ||
|
|
||
| const TRANSPORT_CODES = new Set(["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "UND_ERR_SOCKET"]) | ||
|
|
||
| export function classifyStreamFailure(error: unknown): TransportDisconnect | undefined { | ||
| if (!(error instanceof Error)) return undefined | ||
|
|
||
| const topCode = (error as { code?: string }).code | ||
| if (typeof topCode === "string" && TRANSPORT_CODES.has(topCode)) { | ||
| return { kind: "provider_transport_disconnect", retryable: true, code: topCode } | ||
| } | ||
|
|
||
| const code = findTransportCodeInCause(error.cause) | ||
| if (code) { | ||
| return { kind: "provider_transport_disconnect", retryable: true, code } | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| function findTransportCodeInCause(cause: unknown, depth = 0): string | undefined { | ||
| if (depth > 4 || !cause || typeof cause !== "object") return undefined | ||
| try { | ||
| const code = (cause as { code?: string }).code | ||
| if (typeof code === "string" && TRANSPORT_CODES.has(code)) return code | ||
| return findTransportCodeInCause((cause as { cause?: unknown }).cause, depth + 1) | ||
| } catch { | ||
| return undefined | ||
| } | ||
| } | ||
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
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.