mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(core): forward retryAfterMs across provider session errors (#57589)
This commit is contained in:
committed by
GitHub
Unverified
parent
7d3752c14c
commit
80b8762913
@@ -20,18 +20,20 @@ const headerDetails = (headers: Headers.Headers) =>
|
||||
const normalizedHeaders = (headers: Headers.Headers) =>
|
||||
Object.fromEntries(Object.entries(headers).map(([key, value]) => [key.toLowerCase(), value]))
|
||||
|
||||
const clampRetryAfterMs = (millis: number) => Math.min(Number.MAX_SAFE_INTEGER, Math.max(0, Math.ceil(millis)))
|
||||
|
||||
const retryAfterMs = (headers: Record<string, string>) => {
|
||||
const millis = Number(headers["retry-after-ms"])
|
||||
if (Number.isFinite(millis)) return Math.max(0, millis)
|
||||
if (Number.isFinite(millis)) return clampRetryAfterMs(millis)
|
||||
|
||||
const value = headers["retry-after"]
|
||||
if (!value) return undefined
|
||||
|
||||
const seconds = Number(value)
|
||||
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1000)
|
||||
if (Number.isFinite(seconds)) return clampRetryAfterMs(seconds * 1000)
|
||||
|
||||
const date = Date.parse(value)
|
||||
if (!Number.isNaN(date)) return Math.max(0, date - Date.now())
|
||||
if (!Number.isNaN(date)) return clampRetryAfterMs(date - Date.now())
|
||||
return undefined
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Schema } from "effect"
|
||||
import { NonNegativeInt } from "@opencode/schema"
|
||||
import { Tool } from "@opencode/schema/tool"
|
||||
import { ModelID, ProviderID, RouteID } from "./ids.js"
|
||||
|
||||
@@ -12,7 +13,7 @@ export class HttpContext extends Schema.Class<HttpContext>("AI.HttpContext")({
|
||||
}) {}
|
||||
|
||||
export class HttpRateLimitDetails extends Schema.Class<HttpRateLimitDetails>("AI.HttpRateLimitDetails")({
|
||||
retryAfterMs: Schema.optional(Schema.Number),
|
||||
retryAfterMs: Schema.optional(NonNegativeInt),
|
||||
limit: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
remaining: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
reset: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
@@ -77,7 +78,7 @@ export class AuthenticationError extends Schema.TaggedError<AuthenticationError>
|
||||
|
||||
export class RateLimitError extends Schema.TaggedError<RateLimitError>("AI.Error.RateLimit")("RateLimit", {
|
||||
...ReasonFields,
|
||||
retryAfterMs: Schema.optional(Schema.Number),
|
||||
retryAfterMs: Schema.optional(NonNegativeInt),
|
||||
rateLimit: Schema.optional(HttpRateLimitDetails),
|
||||
}) {}
|
||||
|
||||
@@ -85,7 +86,7 @@ export class QuotaExceededError extends Schema.TaggedError<QuotaExceededError>("
|
||||
"QuotaExceeded",
|
||||
{
|
||||
...ReasonFields,
|
||||
retryAfterMs: Schema.optional(Schema.Number),
|
||||
retryAfterMs: Schema.optional(NonNegativeInt),
|
||||
},
|
||||
) {}
|
||||
|
||||
@@ -98,7 +99,7 @@ export class ProviderInternalError extends Schema.TaggedError<ProviderInternalEr
|
||||
"ProviderInternal",
|
||||
{
|
||||
...ReasonFields,
|
||||
retryAfterMs: Schema.optional(Schema.Number),
|
||||
retryAfterMs: Schema.optional(NonNegativeInt),
|
||||
},
|
||||
) {}
|
||||
|
||||
|
||||
@@ -64,6 +64,14 @@ describe("RequestExecutor", () => {
|
||||
)
|
||||
expect(RequestExecutor.httpFailure({ message: "No response", url: request.url }).reason.http).toBeUndefined()
|
||||
expect(RequestExecutor.httpFailure({ message: "No URL", status: 500 }).reason.http).toBeUndefined()
|
||||
expect(
|
||||
RequestExecutor.httpFailure({
|
||||
message: "Huge retry-after",
|
||||
url: request.url,
|
||||
status: 429,
|
||||
responseHeaders: { "Retry-After": "1e20" },
|
||||
}).reason,
|
||||
).toMatchObject({ _tag: "RateLimit", retryAfterMs: Number.MAX_SAFE_INTEGER })
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
@@ -35,17 +35,12 @@ export function toSessionError(cause: unknown): SessionError.Error {
|
||||
return providerError("provider.rate-limit", cause.reason)
|
||||
case "Authentication":
|
||||
return providerError("provider.auth", cause.reason)
|
||||
case "QuotaExceeded": {
|
||||
case "QuotaExceeded":
|
||||
if (!cause.reason.body?.includes("FreeUsageLimitError")) return providerError("provider.quota", cause.reason)
|
||||
const retryAfterMs = cause.reason.retryAfterMs
|
||||
return {
|
||||
...providerError("provider.free-limit", cause.reason),
|
||||
message: FREE_USAGE_LIMIT_MESSAGE,
|
||||
...(retryAfterMs !== undefined && Number.isFinite(retryAfterMs) && retryAfterMs >= 0
|
||||
? { retryAfterMs: Math.ceil(retryAfterMs) }
|
||||
: {}),
|
||||
}
|
||||
}
|
||||
case "ContentPolicy":
|
||||
return providerError("provider.content-filter", cause.reason)
|
||||
case "Transport":
|
||||
@@ -108,10 +103,12 @@ export function contentFilterError(summary: string, reason: FinishReasonDetails)
|
||||
|
||||
function providerError(type: string, reason: AIError["reason"]): SessionError.Error {
|
||||
const status = reason.http?.status
|
||||
const retryAfterMs = "retryAfterMs" in reason ? reason.retryAfterMs : undefined
|
||||
return {
|
||||
type,
|
||||
message: Object.entries(tokenSharingMessages).find(([code]) => reason.body?.includes(code))?.[1] ?? reason.message,
|
||||
...(status === undefined ? {} : { status }),
|
||||
...(reason.body === undefined ? {} : { response: { body: reason.body } }),
|
||||
...(retryAfterMs === undefined ? {} : { retryAfterMs }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,14 +33,23 @@ describe("toSessionError", () => {
|
||||
expect(toSessionError(llm(new RateLimitError({ message: "rate", retryAfterMs: 123 })))).toEqual({
|
||||
type: "provider.rate-limit",
|
||||
message: "rate",
|
||||
retryAfterMs: 123,
|
||||
})
|
||||
expect(toSessionError(llm(new AuthenticationError({ message: "auth" }))).type).toBe("provider.auth")
|
||||
expect(toSessionError(llm(new QuotaExceededError({ message: "quota" }))).type).toBe("provider.quota")
|
||||
expect(toSessionError(llm(new QuotaExceededError({ message: "quota", retryAfterMs: 456 })))).toEqual({
|
||||
type: "provider.quota",
|
||||
message: "quota",
|
||||
retryAfterMs: 456,
|
||||
})
|
||||
expect(toSessionError(llm(new ContentPolicyError({ message: "blocked" }))).type).toBe("provider.content-filter")
|
||||
expect(
|
||||
toSessionError(llm(new TransportError({ message: "transport", transport: "http", operation: "request" }))).type,
|
||||
).toBe("provider.transport")
|
||||
expect(toSessionError(llm(new ProviderInternalError({ message: "internal" }))).type).toBe("provider.internal")
|
||||
expect(toSessionError(llm(new ProviderInternalError({ message: "internal", retryAfterMs: 789 })))).toEqual({
|
||||
type: "provider.internal",
|
||||
message: "internal",
|
||||
retryAfterMs: 789,
|
||||
})
|
||||
expect(toSessionError(llm(new InvalidProviderOutputError({ message: "output" }))).type).toBe(
|
||||
"provider.invalid-output",
|
||||
)
|
||||
@@ -102,7 +111,7 @@ describe("toSessionError", () => {
|
||||
{ "retry-after": "45" },
|
||||
)
|
||||
expect(SessionRunnerRetry.isRetryable(paid)).toBe(true)
|
||||
expect(toSessionError(paid).type).toBe("provider.rate-limit")
|
||||
expect(toSessionError(paid)).toMatchObject({ type: "provider.rate-limit", retryAfterMs: 45_000 })
|
||||
|
||||
const go = fail(
|
||||
"https://opencode.ai/inference/go/openai/v1/chat/completions",
|
||||
@@ -110,7 +119,7 @@ describe("toSessionError", () => {
|
||||
{ "retry-after": "120" },
|
||||
)
|
||||
expect(SessionRunnerRetry.isRetryable(go)).toBe(false)
|
||||
expect(toSessionError(go).type).toBe("provider.quota")
|
||||
expect(toSessionError(go)).toMatchObject({ type: "provider.quota", retryAfterMs: 120_000 })
|
||||
|
||||
const other = fail(
|
||||
"https://api.openai.com/v1/chat/completions",
|
||||
@@ -118,7 +127,7 @@ describe("toSessionError", () => {
|
||||
{ "retry-after": "10" },
|
||||
)
|
||||
expect(SessionRunnerRetry.isRetryable(other)).toBe(true)
|
||||
expect(toSessionError(other).type).toBe("provider.rate-limit")
|
||||
expect(toSessionError(other)).toMatchObject({ type: "provider.rate-limit", retryAfterMs: 10_000 })
|
||||
})
|
||||
|
||||
test("preserves the permission rejection type without exposing internal fields", () => {
|
||||
|
||||
Reference in New Issue
Block a user