diff --git a/packages/antigravity/README.md b/packages/antigravity/README.md index cdc66b1..0b331ee 100644 --- a/packages/antigravity/README.md +++ b/packages/antigravity/README.md @@ -116,6 +116,7 @@ The plugin also changes the request itself, as the built-in did: |---|---| | 429 "Resource has been exhausted" to a system prompt with a turned-away line | 400 "turned this request away as from an unapproved channel", with what to change (it isn't a quota, so nothing is rested) | | 429 from a plan's quota, the account having Google AI credits | with the Use AI credits switch on (off by default), the request is asked again once with `enabled_credit_types: ["GOOGLE_ONE_AI"]`, as the app's Use AI Credits does; with it off, the 429 says the switch would spend them | +| Gemini 4's 400 INVALID_ARGUMENT after it ran (in the stream after thinking, or a slow 400) | asked again, up to twice. A stream's thinking is held back for up to 10 seconds, or until the answer starts, so a retry sends nothing twice; past 10 seconds the thinking streams live, and a 400 after that reaches the client as it is | | 401 | 401, sign-in kept | | 404 | 404 "model … not found" | | Refresh refused | 401, `x-magpie-sign-in: expired` | diff --git a/packages/antigravity/index.mjs b/packages/antigravity/index.mjs index 6fc5b45..aecb037 100644 --- a/packages/antigravity/index.mjs +++ b/packages/antigravity/index.mjs @@ -1224,7 +1224,11 @@ 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 } +// holdMs is the longest a stream's thinking is held back: past it what was +// held goes out and the rest streams live, so a long-thinking turn never +// leaves its client silent long enough to time out (plugins #1078). A 400 +// after that reaches the client as it is. +const retry400 = { floorMs: 3000, pauseMs: 300, holdMax: 512 * 1024, holdMs: 10_000 } const isGemini4 = (id) => /^gemini-4/i.test(String(id ?? "")) // badCall is whether a reply's body is Google's INVALID_ARGUMENT @@ -1251,6 +1255,7 @@ function showsAnswer(j) { // 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. +const LATE = Symbol("late") async function holdThoughts(res, signal) { const rd = res.body.getReader() const dec = new TextDecoder() @@ -1261,6 +1266,11 @@ async function holdThoughts(res, signal) { let release = false let bad = false let failure + // pending is a read still out when the hold's time ran out: the body + // takes it first, so no chunk is lost + let pending + let timer + const late = new Promise((r) => (timer = setTimeout(r, retry400.holdMs, LATE))) const scan = (final) => { let i while (!release && ((i = buf.indexOf("\n")) >= 0 || (final && buf))) { @@ -1275,7 +1285,13 @@ async function holdThoughts(res, signal) { } try { while (!release && !bad) { - const r = await rd.read() + const read = rd.read() + const r = await Promise.race([read, late]) + if (r === LATE) { + pending = read + release = true + break + } if (r.done) { done = true break @@ -1288,9 +1304,13 @@ async function holdThoughts(res, signal) { } if (done && !bad && !release) scan(true) } catch (e) { - if (signal?.aborted) throw e + if (signal?.aborted) { + clearTimeout(timer) + throw e + } failure = e } + clearTimeout(timer) if (bad) { await rd.cancel().catch(() => {}) return { bad: true } @@ -1302,7 +1322,9 @@ async function holdThoughts(res, signal) { if (failure) return ctl.error(failure) if (done) return ctl.close() try { - const r = await rd.read() + const read = pending ?? rd.read() + pending = undefined + const r = await read if (r.done) ctl.close() else ctl.enqueue(r.value) } catch (e) { diff --git a/packages/antigravity/package.json b/packages/antigravity/package.json index 8148638..b96e8ff 100644 --- a/packages/antigravity/package.json +++ b/packages/antigravity/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-antigravity-auth", - "version": "0.1.8", + "version": "0.1.9", "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 4ae51e7..1e757a9 100644 --- a/packages/antigravity/requests.test.mjs +++ b/packages/antigravity/requests.test.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(); _internal.retry400.pauseMs = 0; _internal.retry400.floorMs = 3000 }) +beforeEach(() => { reset(); _internal.retry400.pauseMs = 0; _internal.retry400.floorMs = 3000; _internal.retry400.holdMs = 10_000 }) afterEach(() => (globalThis.fetch = real)) const URL_ = (m, method = "streamGenerateContent") => `https://daily-cloudcode-pa.googleapis.com/v1beta/models/${m}:${method}?alt=sse` @@ -302,6 +302,38 @@ test("a fast 400 is the request's own and isn't asked again; nor is another mode expect(calls(seen)).toBe(2) }) +test("thinking held past holdMs goes out while the model still thinks, and nothing read is lost (#1078)", async () => { + _internal.retry400.holdMs = 50 + let ctl + const enc = new TextEncoder() + const body = new ReadableStream({ start: (c) => (ctl = c) }) + const seen = fake({ fetchAvailableModels: G4, streamGenerateContent: () => new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } }) }) + const f = await loaderFetch(auth()) + ctl.enqueue(enc.encode(dataOf(think) + "\r\n\r\n")) + const res = await f(URL_("gemini-4-carbon"), { method: "POST", body: ask() }) + expect(res.status).toBe(200) + const rd = res.body.getReader() + const dec = new TextDecoder() + let got = "" + while (!got.includes("hmm")) { + const r = await rd.read() + if (r.done) break + got += dec.decode(r.value, { stream: true }) + } + // the answer doesn't exist yet: the thinking came out on its own + expect(got).toContain("hmm") + expect(got).not.toContain("functionCall") + ctl.enqueue(enc.encode(dataOf(answer) + "\r\n\r\n")) + ctl.close() + for (;;) { + const r = await rd.read() + if (r.done) break + got += dec.decode(r.value, { stream: true }) + } + expect(got).toContain("functionCall") + expect(calls(seen)).toBe(1) +}) + 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