diff --git a/packages/antigravity/index.mjs b/packages/antigravity/index.mjs index df593ad..b6e0b18 100644 --- a/packages/antigravity/index.mjs +++ b/packages/antigravity/index.mjs @@ -1640,7 +1640,12 @@ async function complete(s, url, init, magpie) { return errorResponse(400, `Antigravity answered 429 to this system prompt: ${msg} — ${hint(system)}`) } if (res.status === 429 && l.project.credits && !usesCredits(magpie)) { - return new Response(withNote(text, CREDITS_NOTE), { status: 429, headers: { "content-type": "application/json" } }) + // the credits note only where the model's quota may be what ran + // out: a 429 with quota left is something else, and credits would + // not change it (plugins#1148) + const left = await quotaLeft(s, sent) + const note = left > 0 ? quotaLeftNote(left) : CREDITS_NOTE + return new Response(withNote(text, note), { status: 429, headers: { "content-type": "application/json" } }) } return new Response(text, { status: res.status, headers: { "content-type": res.headers.get("content-type") ?? "application/json" } }) } @@ -1656,6 +1661,22 @@ const CREDITS_OPTION = "use-ai-credits" const CREDITS_NOTE = "this account has Google AI credits; turn on the Use AI credits switch in magpie's Antigravity editor to spend them when the plan's quota is used up, as the app's Use AI Credits does" +// quotaLeft is the fraction of the model's quota fetchAvailableModels +// reads now, or -1 when it can't say. The cached list may be from before +// the quota ran out, so a failed read is unknown, not the cached value. +async function quotaLeft(s, model) { + try { + const mi = (await modelInfo(s, true)).infos.find((m) => m.id === model) + return mi ? mi.remaining : -1 + } catch { + return -1 + } +} + +function quotaLeftNote(left) { + return `this model still has ${Math.max(1, Math.round(left * 100))}% of its quota left, so this 429 is not the plan's quota running out, and Google AI credits would not change it` +} + function usesCredits(magpie) { try { return magpie?.option?.(PROVIDER, CREDITS_OPTION) === true diff --git a/packages/antigravity/package.json b/packages/antigravity/package.json index db6b20d..1a4bfcb 100644 --- a/packages/antigravity/package.json +++ b/packages/antigravity/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-antigravity-auth", - "version": "0.1.16", + "version": "0.1.17", "description": "Sign in to Antigravity with a Google account from OpenCode or magpie: Gemini, and the Claude and GPT-OSS models Google hosts for it", "type": "module", "main": "./opencode.mjs", diff --git a/packages/antigravity/requests.test.mjs b/packages/antigravity/requests.test.mjs index f973317..3f990bd 100644 --- a/packages/antigravity/requests.test.mjs +++ b/packages/antigravity/requests.test.mjs @@ -236,6 +236,40 @@ test("with the switch off, a refused request is not asked again, and the 429 say expect(j.error.message).toContain("Use AI credits") }) +// plugins#1148: a 429 came back while the model's quota wasn't used up, +// and the credits note read as if it were +const withQuota = (fraction) => ({ models: { "gemini-3-flash": { displayName: "Gemini 3 Flash", quotaInfo: fraction === undefined ? { resetTime: "2026-11-15T12:00:00Z" } : { remainingFraction: fraction, resetTime: "2026-11-15T12:00:00Z" } } } }) + +test("a 429 while the model's quota is left says so, not to spend credits (plugins#1148)", async () => { + let reads = 0 + fake({ loadCodeAssist: withCredits, fetchAvailableModels: () => (++reads === 1 ? withQuota(0.8) : withQuota(0.62)), streamGenerateContent: spentQuota }) + const { f } = await creditsFetch(false) + const res = await f(URL_("gemini-3-flash"), { method: "POST", body: ask() }) + expect(res.status).toBe(429) + const msg = (await res.json()).error.message + expect(msg).toContain("exhausted your capacity") + expect(msg).toContain("62% of its quota left") + expect(msg).not.toContain("Use AI credits") + expect(reads).toBe(2) +}) + +test("a 429 once the model's quota is used up still points at the credits switch", async () => { + let reads = 0 + fake({ loadCodeAssist: withCredits, fetchAvailableModels: () => (++reads === 1 ? withQuota(0.4) : withQuota(undefined)), streamGenerateContent: spentQuota }) + const { f } = await creditsFetch(false) + const msg = (await (await f(URL_("gemini-3-flash"), { method: "POST", body: ask() })).json()).error.message + expect(msg).toContain("Use AI credits") + expect(msg).not.toContain("quota left") +}) + +test("when the quota can't be read again, the 429 keeps the credits note", async () => { + let reads = 0 + fake({ loadCodeAssist: withCredits, fetchAvailableModels: () => (++reads === 1 ? withQuota(0.9) : new Response("boom", { status: 500 })), streamGenerateContent: spentQuota }) + const { f } = await creditsFetch(false) + const msg = (await (await f(URL_("gemini-3-flash"), { method: "POST", body: ask() })).json()).error.message + expect(msg).toContain("Use AI credits") +}) + test("an account with no credits is not asked again, with the switch on", async () => { const seen = fake({ loadCodeAssist: { cloudaicompanionProject: "ag-proj", paidTier: { id: "g1-pro-tier", name: "Google AI Pro" } }, fetchAvailableModels: models, streamGenerateContent: credited }) const { f } = await creditsFetch(true)