antigravity 0.1.6: gemini-4's rejected call (400 INVALID_ARGUMENT after the model ran) is asked again, up to twice (shzutbi1 on X)
Gemini 4 now and then builds a call Google's validator rejects; the same request passes on a resend. A 400 INVALID_ARGUMENT that took 3s or more, or an error chunk in the stream before any non-thinking part, is sent again for gemini-4 models only. Thinking-only chunks are held back until the first answer part, so a retry never repeats thinking to the client; once an answer has begun the stream goes through as it is (a reply can't be taken back). A fast 400 and other models are returned as before. Run: bun test in packages/antigravity on mica, 78 pass. Broken by hand: RETRY_BAD_CALL=0 turns 4 new tests red. Not tried: a real Antigravity account (the 400 is intermittent). Cost: thinking shows once the first answer part (or the end) arrives, not live.
This commit is contained in:
+150
-21
@@ -1183,6 +1183,102 @@ function unwrap(body) {
|
||||
|
||||
const RESPONSE_HEADERS = { "content-type": "text/event-stream", "cache-control": "no-cache" }
|
||||
|
||||
// ---- Gemini 4's rejected calls --------------------------------------------------------------
|
||||
|
||||
const RETRY_BAD_CALL = 2
|
||||
// floorMs is how long a 400 must have taken to count as the backend's own
|
||||
// rejection of what the model made: a request that is wrong in itself is
|
||||
// refused at once, and isn't sent again
|
||||
const retry400 = { floorMs: 3000, pauseMs: 300, holdMax: 512 * 1024 }
|
||||
const isGemini4 = (id) => /^gemini-4/i.test(String(id ?? ""))
|
||||
|
||||
// badCall is whether a reply's body is Google's INVALID_ARGUMENT
|
||||
function badCall(text) {
|
||||
const j = parse(text)
|
||||
const e = isObj(j) ? (isObj(j.error) ? j.error : isObj(j.response?.error) ? j.response.error : undefined) : undefined
|
||||
if (e) return e.status === "INVALID_ARGUMENT" || (e.code === 400 && /invalid argument/i.test(String(e.message ?? "")))
|
||||
return /INVALID_ARGUMENT|request contains an invalid argument/i.test(String(text))
|
||||
}
|
||||
|
||||
// showsAnswer is whether a stream chunk carries something the client shows
|
||||
// beyond thinking: text, a call, a file
|
||||
function showsAnswer(j) {
|
||||
const c = isObj(j?.response) ? j.response : j
|
||||
for (const p of c?.candidates?.[0]?.content?.parts ?? []) {
|
||||
if (!isObj(p) || p.thought) continue
|
||||
if (p.functionCall || p.inlineData || p.fileData || p.executableCode || (typeof p.text === "string" && p.text !== "")) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// holdThoughts reads a stream until it shows its first non-thinking part,
|
||||
// and holds what it read back until then. An INVALID_ARGUMENT error that
|
||||
// comes first sends nothing to the client, so the caller can ask again
|
||||
// ({bad: true}). Once an answer has begun the client has it, and the
|
||||
// stream goes through as it is: a reply can't be taken back.
|
||||
async function holdThoughts(res, signal) {
|
||||
const rd = res.body.getReader()
|
||||
const dec = new TextDecoder()
|
||||
const held = []
|
||||
let size = 0
|
||||
let buf = ""
|
||||
let done = false
|
||||
let release = false
|
||||
let bad = false
|
||||
let failure
|
||||
const scan = (final) => {
|
||||
let i
|
||||
while (!release && ((i = buf.indexOf("\n")) >= 0 || (final && buf))) {
|
||||
const l = i >= 0 ? buf.slice(0, i) : buf
|
||||
buf = i >= 0 ? buf.slice(i + 1) : ""
|
||||
if (!l.startsWith("data:")) continue
|
||||
const j = parse(l.slice(5).trim())
|
||||
if (!isObj(j)) continue
|
||||
if ((j.error || j.response?.error) && badCall(JSON.stringify(j))) bad = true
|
||||
else if (showsAnswer(j)) release = true
|
||||
}
|
||||
}
|
||||
try {
|
||||
while (!release && !bad) {
|
||||
const r = await rd.read()
|
||||
if (r.done) {
|
||||
done = true
|
||||
break
|
||||
}
|
||||
held.push(r.value)
|
||||
size += r.value.length
|
||||
buf += dec.decode(r.value, { stream: true })
|
||||
scan(false)
|
||||
if (size > retry400.holdMax) release = true
|
||||
}
|
||||
if (done && !bad && !release) scan(true)
|
||||
} catch (e) {
|
||||
if (signal?.aborted) throw e
|
||||
failure = e
|
||||
}
|
||||
if (bad) {
|
||||
await rd.cancel().catch(() => {})
|
||||
return { bad: true }
|
||||
}
|
||||
let i = 0
|
||||
const body = new ReadableStream({
|
||||
async pull(ctl) {
|
||||
if (i < held.length) return ctl.enqueue(held[i++])
|
||||
if (failure) return ctl.error(failure)
|
||||
if (done) return ctl.close()
|
||||
try {
|
||||
const r = await rd.read()
|
||||
if (r.done) ctl.close()
|
||||
else ctl.enqueue(r.value)
|
||||
} catch (e) {
|
||||
ctl.error(e)
|
||||
}
|
||||
},
|
||||
cancel: (r) => rd.cancel(r),
|
||||
})
|
||||
return { bad: false, body }
|
||||
}
|
||||
|
||||
// complete sends a Gemini request at url (…/models/<id>:<method>) to
|
||||
// Antigravity for the account s
|
||||
async function complete(s, url, init, magpie) {
|
||||
@@ -1223,29 +1319,60 @@ async function complete(s, url, init, magpie) {
|
||||
body: JSON.stringify(envelope(req, sent, l.project.id, credits, init?.headers)),
|
||||
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.
|
||||
//
|
||||
// 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
|
||||
const attempt = async () => {
|
||||
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.
|
||||
//
|
||||
// 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 (again?.ok) {
|
||||
res = again
|
||||
refusal = undefined
|
||||
} else {
|
||||
await again?.body?.cancel().catch(() => {})
|
||||
return { res, refusal }
|
||||
}
|
||||
// Gemini 4 now and then fails a request the same request later passes:
|
||||
// its backend rejects a call the model built itself (a 400
|
||||
// INVALID_ARGUMENT after the model ran, or in the stream after thinking).
|
||||
// Such a reply is asked again, up to RETRY_BAD_CALL times. A 400 that
|
||||
// comes back fast is the request's own fault and is returned as it is.
|
||||
const again400 = isGemini4(sent) || isGemini4(model)
|
||||
let res, refusal
|
||||
for (let n = 0; ; n++) {
|
||||
const t0 = Date.now()
|
||||
;({ res, refusal } = await attempt())
|
||||
const left = again400 && n < RETRY_BAD_CALL && !init?.signal?.aborted
|
||||
if (!left) break
|
||||
if (!res.ok) {
|
||||
if (res.status !== 400) break
|
||||
const text = refusal ?? (await res.text())
|
||||
refusal = text
|
||||
if (!badCall(text) || Date.now() - t0 < retry400.floorMs) break
|
||||
await sleep(retry400.pauseMs)
|
||||
continue
|
||||
}
|
||||
const held = await holdThoughts(res, init?.signal)
|
||||
if (held.bad) {
|
||||
await sleep(retry400.pauseMs)
|
||||
continue
|
||||
}
|
||||
res = new Response(held.body, { status: 200, headers: res.headers })
|
||||
break
|
||||
}
|
||||
if (!res.ok) {
|
||||
const text = refusal ?? (await res.text())
|
||||
@@ -1755,6 +1882,8 @@ export const _internal = {
|
||||
creditsOf,
|
||||
entitlements,
|
||||
complete,
|
||||
retry400,
|
||||
badCall,
|
||||
usage,
|
||||
tokens,
|
||||
projects,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-antigravity-auth",
|
||||
"version": "0.1.5",
|
||||
"version": "0.1.6",
|
||||
"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",
|
||||
|
||||
@@ -5,7 +5,7 @@ import { afterEach, beforeEach, expect, test } from "bun:test"
|
||||
import { AntigravityAuthPlugin, _internal } from "./index.mjs"
|
||||
import { auth, fake, real, reset } from "./fake.mjs"
|
||||
|
||||
beforeEach(reset)
|
||||
beforeEach(() => { reset(); _internal.retry400.pauseMs = 0; _internal.retry400.floorMs = 3000 })
|
||||
afterEach(() => (globalThis.fetch = real))
|
||||
|
||||
const URL_ = (m, method = "streamGenerateContent") => `https://daily-cloudcode-pa.googleapis.com/v1beta/models/${m}:${method}?alt=sse`
|
||||
@@ -258,3 +258,79 @@ test("host session headers reach the envelope's sessionId without leaking upstre
|
||||
expect(calls[0].json.request.sessionId).not.toBe(calls[1].json.request.sessionId)
|
||||
expect(calls[0].headers["x-magpie-session"]).toBeUndefined()
|
||||
})
|
||||
|
||||
// ---- Gemini 4's rejected calls ----------------------------------------------------------
|
||||
|
||||
const G4 = { models: { "gemini-4-carbon": { displayName: "Gemini 4 Carbon" }, "gemini-3-flash": { displayName: "Gemini 3 Flash" } } }
|
||||
const bad400 = () => new Response(JSON.stringify({ error: { code: 400, message: "Request contains an invalid argument.", status: "INVALID_ARGUMENT" } }), { status: 400, headers: { "content-type": "application/json" } })
|
||||
const think = { candidates: [{ content: { role: "model", parts: [{ text: "hmm", thought: true }] } }] }
|
||||
const answer = { candidates: [{ content: { role: "model", parts: [{ functionCall: { name: "ls", args: {} } }] }, finishReason: "STOP" }] }
|
||||
const raw = (...lines) => new Response(lines.join("\r\n\r\n") + "\r\n\r\n", { status: 200, headers: { "content-type": "text/event-stream" } })
|
||||
const errChunk = "data: " + JSON.stringify({ error: { code: 400, message: "Request contains an invalid argument.", status: "INVALID_ARGUMENT" } })
|
||||
const dataOf = (c) => "data: " + JSON.stringify({ response: c })
|
||||
const calls = (seen) => seen.filter((s) => s.name === "streamGenerateContent").length
|
||||
|
||||
test("gemini-4's slow 400 INVALID_ARGUMENT is asked again, and the retry's reply comes back", async () => {
|
||||
_internal.retry400.floorMs = 0
|
||||
let n = 0
|
||||
const seen = fake({ fetchAvailableModels: G4, streamGenerateContent: () => (n++ < 1 ? bad400() : sse(answer)) })
|
||||
const f = await loaderFetch(auth())
|
||||
const res = await f(URL_("gemini-4-carbon"), { method: "POST", body: ask() })
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.text()).toContain("functionCall")
|
||||
expect(calls(seen)).toBe(2)
|
||||
})
|
||||
|
||||
test("a gemini-4 400 that keeps coming is asked twice more, then returned", async () => {
|
||||
_internal.retry400.floorMs = 0
|
||||
const seen = fake({ fetchAvailableModels: G4, streamGenerateContent: bad400 })
|
||||
const f = await loaderFetch(auth())
|
||||
const res = await f(URL_("gemini-4-carbon"), { method: "POST", body: ask() })
|
||||
expect(res.status).toBe(400)
|
||||
expect((await res.json()).error.status).toBe("INVALID_ARGUMENT")
|
||||
expect(calls(seen)).toBe(3)
|
||||
})
|
||||
|
||||
test("a fast 400 is the request's own and isn't asked again; nor is another model's", async () => {
|
||||
const seen = fake({ fetchAvailableModels: G4, streamGenerateContent: bad400 })
|
||||
const f = await loaderFetch(auth())
|
||||
_internal.retry400.floorMs = 3000
|
||||
expect((await f(URL_("gemini-4-carbon"), { method: "POST", body: ask() })).status).toBe(400)
|
||||
expect(calls(seen)).toBe(1)
|
||||
_internal.retry400.floorMs = 0
|
||||
expect((await f(URL_("gemini-3-flash"), { method: "POST", body: ask() })).status).toBe(400)
|
||||
expect(calls(seen)).toBe(2)
|
||||
})
|
||||
|
||||
test("an INVALID_ARGUMENT in the stream after thinking only is asked again, and no thinking is sent twice", async () => {
|
||||
_internal.retry400.floorMs = 3000
|
||||
let n = 0
|
||||
const seen = fake({ fetchAvailableModels: G4, streamGenerateContent: () => (n++ < 1 ? raw(dataOf(think), dataOf(think), errChunk) : sse(think, answer)) })
|
||||
const f = await loaderFetch(auth())
|
||||
const res = await f(URL_("gemini-4-carbon"), { method: "POST", body: ask() })
|
||||
const text = await res.text()
|
||||
expect(text).not.toContain("INVALID_ARGUMENT")
|
||||
expect(text.split("hmm").length - 1).toBe(1)
|
||||
expect(text).toContain("functionCall")
|
||||
expect(calls(seen)).toBe(2)
|
||||
})
|
||||
|
||||
test("once an answer has begun the stream is not asked again, and its error goes through", async () => {
|
||||
let n = 0
|
||||
const seen = fake({ fetchAvailableModels: G4, streamGenerateContent: () => (n++ < 1 ? raw(dataOf(think), dataOf({ candidates: [{ content: { role: "model", parts: [{ text: "Hel" }] } }] }), errChunk) : sse(answer)) })
|
||||
const f = await loaderFetch(auth())
|
||||
const text = await (await f(URL_("gemini-4-carbon"), { method: "POST", body: ask() })).text()
|
||||
expect(text).toContain("Hel")
|
||||
expect(text).toContain("INVALID_ARGUMENT")
|
||||
expect(calls(seen)).toBe(1)
|
||||
})
|
||||
|
||||
test("a generateContent reply is asked again the same way", async () => {
|
||||
let n = 0
|
||||
const seen = fake({ fetchAvailableModels: G4, streamGenerateContent: () => (n++ < 1 ? raw(dataOf(think), errChunk) : sse(answer)) })
|
||||
const f = await loaderFetch(auth())
|
||||
const res = await f(URL_("gemini-4-carbon", "generateContent"), { method: "POST", body: ask() })
|
||||
expect(res.status).toBe(200)
|
||||
expect(JSON.stringify(await res.json())).toContain("functionCall")
|
||||
expect(calls(seen)).toBe(2)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user