Skip to content

Commit fd54723

Browse files
anandgupta42claude
andcommitted
fix(analyst): read the upstream error code inside the gateway wrapper when classifying an overflow
Narrowing overflow detection to the provider's own code dropped the one place a real upstream code lives behind the LiteLLM / Altimate Base gateway: `error.provider_specific_fields.error.code` under a generic outer `error.code`. That path is now one of the three consulted (upstream wrapper, `error.code`, root `code`); arbitrary nested diagnostic fields are still ignored. Test: a wrapped upstream `context_length_exceeded` / `content_length_exceeded` is a `ContextOverflowError`; the nested-diagnostic and top-level cases are unchanged. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 76a5828 commit fd54723

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,20 @@ export namespace MessageV2 {
568568
return responseErrorCode(responseBody, isEchoPolicyCode)
569569
}
570570

571-
/** The provider's own error code (`error.code` or root `code`), never a nested diagnostic field. */
571+
/**
572+
* The provider's own error code: `error.code`, the root `code`, or the upstream code that the
573+
* LiteLLM / Altimate Base gateway wraps under `error.provider_specific_fields.error.code`.
574+
* Arbitrary nested diagnostic fields are never consulted.
575+
*/
572576
function providerErrorCode(responseBody: string | undefined): string | undefined {
573577
if (!responseBody) return undefined
574578
try {
575-
const body = JSON.parse(responseBody) as { code?: unknown; error?: { code?: unknown } }
576-
const code = body?.error?.code ?? body?.code
577-
return typeof code === "string" ? code : undefined
579+
const body = JSON.parse(responseBody) as {
580+
code?: unknown
581+
error?: { code?: unknown; provider_specific_fields?: { error?: { code?: unknown } } }
582+
}
583+
const candidates = [body?.error?.provider_specific_fields?.error?.code, body?.error?.code, body?.code]
584+
return candidates.find((code): code is string => typeof code === "string")
578585
} catch {
579586
return undefined
580587
}

‎packages/opencode/test/session/message-v2.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,27 @@ describe("session.message-v2.fromError", () => {
15711571
})
15721572
}
15731573

1574+
for (const code of ["context_length_exceeded", "content_length_exceeded"]) {
1575+
test(`an upstream ${code} wrapped by the gateway under provider_specific_fields is still an overflow`, () => {
1576+
const result = MessageV2.fromError(new APICallError({
1577+
message: "Request failed",
1578+
url: "https://example.com",
1579+
requestBodyValues: {},
1580+
statusCode: 400,
1581+
isRetryable: false,
1582+
responseBody: JSON.stringify({
1583+
error: {
1584+
code: "invalid_request_error",
1585+
message: "upstream request failed",
1586+
provider_specific_fields: { error: { code, message: "maximum context length exceeded" } },
1587+
},
1588+
}),
1589+
}), { providerID })
1590+
1591+
expect(result.name).toBe("ContextOverflowError")
1592+
})
1593+
}
1594+
15741595
for (const code of ["context_length_exceeded", "content_length_exceeded"]) {
15751596
test(`a nested diagnostic ${code} never masks a top-level policy denial`, () => {
15761597
const result = MessageV2.fromError(new APICallError({

0 commit comments

Comments
 (0)