diff --git a/packages/antigravity/index.mjs b/packages/antigravity/index.mjs index 79446ac..c0ea258 100644 --- a/packages/antigravity/index.mjs +++ b/packages/antigravity/index.mjs @@ -1207,18 +1207,31 @@ async function complete(s, url, init, magpie) { signal: init?.signal, }) let res = await send(false) + let refusal if (res.status === 429 && l.project.credits && usesCredits(magpie) && !turnsAway(system)) { // the plan's quota is used up: the app asks before it spends AI // credits, and the switch is that answer. Credits are asked for only // after a refusal, so a request the plan covers never spends one. - await res.body?.cancel().catch(() => {}) - const again = await send(true) - if (again.ok) { + // + // The refusal is read before the second ask: when that ask fails too, + // the refusal is what the agent is told. Its body can't be read after + // it was cancelled ("Body already used", a 502 on every retry). + refusal = await res.text() + let again + try { + again = await send(true) + } catch (e) { + if (init?.signal?.aborted) throw e + } + if (again?.ok) { res = again + refusal = undefined + } else { + await again?.body?.cancel().catch(() => {}) } } if (!res.ok) { - const text = await res.text() + const text = refusal ?? (await res.text()) const msg = apiError(text, res.status) if (res.status === 401) return errorResponse(401, `Antigravity: ${msg}`, "kept") if (res.status === 404) return errorResponse(404, `model ${sent} not found`) diff --git a/packages/antigravity/package.json b/packages/antigravity/package.json index 6f2b7f5..ed19960 100644 --- a/packages/antigravity/package.json +++ b/packages/antigravity/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-antigravity-auth", - "version": "0.1.2", + "version": "0.1.3", "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 bbc4b4f..5a6b5f5 100644 --- a/packages/antigravity/requests.test.mjs +++ b/packages/antigravity/requests.test.mjs @@ -193,6 +193,21 @@ test("with Use AI credits on, a request the quota refuses is asked again with th expect(sent[1].json.enabled_credit_types).toEqual(["GOOGLE_ONE_AI"]) }) +test("when the asking with credits is refused too, the quota's 429 is what comes back, and a retry works the same", async () => { + const seen = fake({ + loadCodeAssist: withCredits, + fetchAvailableModels: models, + streamGenerateContent: (s) => (s.json.enabled_credit_types ? new Response(JSON.stringify({ error: { code: 403, message: "credits refused", status: "PERMISSION_DENIED" } }), { status: 403 }) : spentQuota()), + }) + const { f } = await creditsFetch(true) + for (let i = 0; i < 2; i++) { + const res = await f(URL_("gemini-3-flash"), { method: "POST", body: ask() }) + expect(res.status).toBe(429) + expect((await res.json()).error.message).toContain("exhausted your capacity") + } + expect(seen.filter((s) => s.name === "streamGenerateContent").length).toBe(4) +}) + test("a request the quota covers never asks for credits", async () => { const seen = fake({ loadCodeAssist: withCredits, fetchAvailableModels: models, streamGenerateContent: () => sse({ candidates: [] }) }) const { f } = await creditsFetch(true)