Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/opencode/src/provider/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export type ParsedStreamError =
responseBody: string
}

// OpenRouter relays upstream overflow as a typeless `{ code: 502, message }` chunk.
// OpenRouter relays upstream failures as typeless `{ code: 5xx, message }` chunks.
function isOverflow(body: { error?: { code?: string; message?: string }; message?: string }) {
return body.error?.code === "context_length_exceeded" || isContextOverflow(body.error?.message ?? body.message ?? "")
}
Expand All @@ -117,6 +117,14 @@ export function parseStreamError(input: unknown): ParsedStreamError | undefined
responseBody,
}
}
if (typeof body.code === "number" && body.code >= 500) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Codes above 599 are not 5xx, but this condition marks every numeric code ≥500 as retryable and sends it through the session retry loop. Restrict the check to integer codes from 500 through 599.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/opencode/src/provider/error.ts, line 120:

<comment>Codes above 599 are not 5xx, but this condition marks every numeric code ≥500 as retryable and sends it through the session retry loop. Restrict the check to integer codes from 500 through 599.</comment>

<file context>
@@ -117,6 +117,14 @@ export function parseStreamError(input: unknown): ParsedStreamError | undefined
       responseBody,
     }
   }
+  if (typeof body.code === "number" && body.code >= 500) {
+    return {
+      type: "api_error",
</file context>
Suggested change
if (typeof body.code === "number" && body.code >= 500) {
if (typeof body.code === "number" && Number.isInteger(body.code) && body.code >= 500 && body.code < 600) {

return {
type: "api_error",
message: typeof body.message === "string" ? body.message : "Server error.",
isRetryable: true,
responseBody,
}
}
if (body.type !== "error") return

switch (body?.error?.code) {
Expand Down
18 changes: 18 additions & 0 deletions packages/opencode/test/session/message-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,24 @@ describe("session.message-v2.fromError", () => {
})
})

test("serializes OpenRouter typeless 5xx chunks as retryable APIError", () => {
const input = {
code: 502,
message: "Stream ended before a terminal response event",
metadata: { error_type: "provider_unavailable" },
}
const result = MessageV2.fromError(input, { providerID })

expect(result).toStrictEqual({
name: "APIError",
data: {
message: input.message,
isRetryable: true,
responseBody: JSON.stringify(input),
},
})
})

test("serializes response error codes", () => {
const cases = [
{
Expand Down
Loading