Skip to content
Merged
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
24 changes: 21 additions & 3 deletions packages/antigravity-plugin/src/wire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export type CloudCodeError = {
message?: string
reason?: string
resetTime?: string
retryDelay?: string
}

type QuotaWindow = {
Expand Down Expand Up @@ -322,20 +323,35 @@ export function explainCloudCodeError(input: {
input.error.reason?.toLowerCase() === "quota_exhausted" ||
(input.error.status === "RESOURCE_EXHAUSTED" && fiveHour !== undefined && fiveHour.remainingFraction <= 0) ||
(input.error.status === "RESOURCE_EXHAUSTED" && messageIsLongQuota(input.error.message))
const headline =
// Cloud Code answers some request content with an instant bare RESOURCE_EXHAUSTED while quota is untouched.
// Retrying sends the same content again, so fail once instead of treating it as a throttle.
const group = fiveHour ? `${fiveHour.id.split(":")[0]}:` : undefined
const rejected =
!quota &&
!input.error.reason &&
!input.error.retryDelay &&
input.error.status === "RESOURCE_EXHAUSTED" &&
fiveHour !== undefined &&
fiveHour.remainingFraction >= 0.95 &&
windows.every((window) => !window.id.startsWith(group ?? "") || window.remainingFraction > 0)
const status =
input.error.status && input.error.message
? `${input.error.status}: ${input.error.message}`
: (input.error.status ?? input.error.message ?? "")
const headline = rejected
? `${status} This is likely not quota: at least 95% of this model's quota is unused, so Google probably rejected this request's content. Not retrying; changing the prompt or switching models may help.`
: status
const readout = windows.map(
(window) =>
`${window.id} remainingFraction=${JSON.stringify(window.remainingFraction)}${window.resetTime ? ` resetTime=${window.resetTime}` : ""}`,
)
const fallbackReset = windows.length === 0 && input.error.resetTime ? [`resetTime=${input.error.resetTime}`] : []
const message = [headline, ...readout, ...fallbackReset].filter((part) => part.length > 0).join(" ")
const reason = quota ? "QUOTA_EXHAUSTED" : input.error.reason
const reason = quota ? "QUOTA_EXHAUSTED" : rejected ? "CONTENT_BLOCKED" : input.error.reason
return {
message,
quota,
rejected,
body: JSON.stringify({
error: {
code: input.httpStatus,
Expand Down Expand Up @@ -370,6 +386,7 @@ function cloudCodeError(text: string): CloudCodeError | undefined {
...(message ? { message } : {}),
...(details.reason ? { reason: details.reason } : {}),
...(details.resetTime ? { resetTime: details.resetTime } : {}),
...(details.retryDelay ? { retryDelay: details.retryDelay } : {}),
}
}

Expand All @@ -385,7 +402,7 @@ function nestedGoogleError(message: string) {

function errorDetails(error: Record<string, unknown> | undefined) {
const details = error && Array.isArray(error.details) ? error.details : []
return details.reduce<{ reason?: string; resetTime?: string }>((acc, detail) => {
return details.reduce<{ reason?: string; resetTime?: string; retryDelay?: string }>((acc, detail) => {
if (!isRecord(detail)) return acc
const metadata = isRecord(detail.metadata) ? detail.metadata : undefined
return {
Expand All @@ -395,6 +412,7 @@ function errorDetails(error: Record<string, unknown> | undefined) {
textField(metadata?.quotaResetTimeStamp) ??
textField(metadata?.quotaResetTimestamp) ??
textField(metadata?.resetTime),
retryDelay: acc.retryDelay ?? textField(detail.retryDelay),
}
}, {})
}
Expand Down
67 changes: 67 additions & 0 deletions packages/antigravity-plugin/test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,73 @@ describe("Cloud Code 429 bodies", () => {
).toBe(true)
})

const fullQuota = (remainingFraction: number) => ({
groups: [{ displayName: "Gemini models", buckets: [{ window: "5h", remainingFraction, resetTime: reset }] }],
})

test("fails a bare RESOURCE_EXHAUSTED once when the model's 5h quota is nearly full", () => {
const explained = GoogleAntigravityWire.explainCloudCodeError({
error: { status: "RESOURCE_EXHAUSTED", message: "Resource has been exhausted (e.g. check quota)." },
summary: fullQuota(0.9998),
httpStatus: 429,
modelID: "gemini-3.8-flash-low",
})
expect(explained?.quota).toBe(false)
expect(explained?.rejected).toBe(true)
expect(explained?.message).toContain("likely not quota: at least 95% of this model's quota is unused")
expect(explained?.message).toContain("gemini:5h remainingFraction=0.9998")
expect(JSON.parse(explained?.body ?? "{}").error.details).toEqual([
{ "@type": "type.googleapis.com/google.rpc.ErrorInfo", reason: "CONTENT_BLOCKED" },
])
})

test.each([
{ modelID: "gemini-3.8-flash-low", group: "Gemini models", fraction: 0.95, rejected: true },
{ modelID: "claude-sonnet-4-6", group: "Claude + GPT", fraction: 1, rejected: true },
{ modelID: "claude-sonnet-4-6", group: "Gemini models", fraction: 1, rejected: false },
])("reads the failed model's own group for $modelID", ({ modelID, group, fraction, rejected }) => {
expect(
GoogleAntigravityWire.explainCloudCodeError({
error: { status: "RESOURCE_EXHAUSTED", message: "Resource has been exhausted (e.g. check quota)." },
summary: { groups: [{ displayName: group, buckets: [{ window: "5h", remainingFraction: fraction }] }] },
httpStatus: 429,
modelID,
})?.rejected,
).toBe(rejected)
})

test.each([
{ name: "an explicit reason", error: { reason: "RATE_LIMIT_EXCEEDED" }, summary: fullQuota(1) },
{ name: "partly used quota", error: {}, summary: fullQuota(0.5) },
{ name: "no quota summary", error: {}, summary: undefined },
{ name: "a retry delay", error: { retryDelay: "30s" }, summary: fullQuota(1) },
{ name: "5h just under the threshold", error: {}, summary: fullQuota(0.9499) },
{
name: "an exhausted weekly window",
error: {},
summary: {
groups: [
{
displayName: "Gemini models",
buckets: [
{ window: "5h", remainingFraction: 1 },
{ window: "weekly", remainingFraction: 0 },
],
},
],
},
},
])("keeps a bare throttle retryable with $name", ({ error, summary }) => {
expect(
GoogleAntigravityWire.explainCloudCodeError({
error: { status: "RESOURCE_EXHAUSTED", message: "Resource has been exhausted (e.g. check quota).", ...error },
summary,
httpStatus: 429,
modelID: "gemini-3.8-flash-low",
})?.rejected,
).toBe(false)
})

test("ignores success-shaped bodies", () => {
expect(GoogleAntigravityWire.parseCloudCodeError('data: {"response":{"candidates":[]}}\n\n')).toBeUndefined()
})
Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/session-antigravity-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,24 @@ for (const modelID of ["gemini-4-argon-high", "gemini-3.8-flash-high"] as const)
)
}
}

it.effect("a bare RESOURCE_EXHAUSTED with nearly full quota classifies as a non-retryable content rejection", () =>
Effect.sync(() => {
const explained = GoogleAntigravityWire.explainCloudCodeError({
error: { status: "RESOURCE_EXHAUSTED", message: "Resource has been exhausted (e.g. check quota)." },
summary: { groups: [{ displayName: "Gemini models", buckets: [{ window: "5h", remainingFraction: 0.9998 }] }] },
httpStatus: 429,
modelID: "gemini-4-carbon-high",
})
const error = RequestExecutor.httpFailure({
message: explained!.body,
url: GoogleAntigravityWire.generateURL,
status: 429,
responseHeaders: { "content-type": "application/json" },
responseBody: explained!.body,
})
expect(error.reason._tag).toBe("ContentPolicy")
expect(isRetryable(error)).toBe(false)
expect(error.message).toContain("likely not quota")
}),
)
Loading