antigravity 0.1.3: a refused credits ask no longer ends in "Body already used" (eric_li8983 on X)

With Use AI credits on, a 429 from the plan's quota was cancelled before the
second ask with credits; when that ask was refused too, the code read the
cancelled body and threw, so every retry came back as a 502 "Body already
used" and the quota's 429 was never told. The refusal is read first and told
when the credits ask fails or throws.

Run: bun test packages/antigravity (84 pass); the new test is red on the old
index.mjs (TypeError: Body already used), green on the new. Other packages that
cancel a body before a retry reassign the reply (read: kiro, minimax, nous,
atomcode, joycode, gcp-ai, factory); none read the cancelled one.
This commit is contained in:
yetone
2026-11-11 13:14:34 +08:00
parent 8adf0a7d79
commit 6c20f1dac7
3 changed files with 33 additions and 5 deletions
+17 -4
View File
@@ -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`)
+1 -1
View File
@@ -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",
+15
View File
@@ -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)