From 8fdcaae970e62bb020ec43329040e46d10fe553c Mon Sep 17 00:00:00 2001 From: Mohamed Date: Sun, 22 Nov 2026 09:08:49 +0200 Subject: [PATCH] fix(server): Antigravity limits prefer the CLI's sign-in over a stale ACP token --- .../provider/antigravityUsageLimits.test.ts | 35 ++++++++++++++++++- .../src/provider/antigravityUsageLimits.ts | 30 ++++++++-------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/apps/server/src/provider/antigravityUsageLimits.test.ts b/apps/server/src/provider/antigravityUsageLimits.test.ts index eec70e2e44fce..829a20cd0ee10 100644 --- a/apps/server/src/provider/antigravityUsageLimits.test.ts +++ b/apps/server/src/provider/antigravityUsageLimits.test.ts @@ -74,6 +74,8 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* ( keyringSecret?: string; /** Reads the OS keyring instead of `keyringSecret`. */ nativeKeyring?: boolean; + /** The CLI sign-in supplies no project, so quota requests carry an empty body. */ + cliSignIn?: boolean; } = {}, ) { const fs = yield* FileSystem.FileSystem; @@ -119,7 +121,9 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* ( : Response.json({}, { status: 403 }), ); } - expect(JSON.parse(body)).toEqual(options.missingToken ? {} : { project: "project-id" }); + expect(JSON.parse(body)).toEqual( + options.missingToken || options.cliSignIn ? {} : { project: "project-id" }, + ); expect(request.headers["user-agent"]).toMatch(/^antigravity\/hub\/2\.24\.0 /); return HttpClientResponse.fromWeb( request, @@ -691,6 +695,35 @@ describe("Antigravity subscription limits", () => { }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); + it.effect("prefers the native CLI's sign-in over a leftover acp_token.json", () => + Effect.gen(function* () { + yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-09T12:00:00Z"))); + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const writeCliToken = (tokenPath: string, expiry: string) => + fs.writeFileString( + path.join(path.dirname(tokenPath), "antigravity-oauth-token"), + JSON.stringify({ + token: { access_token: "access-token", refresh_token: "refresh-a", expiry }, + }), + ); + + const signedIn = yield* fixture({ userId: "google-user-1", cliSignIn: true }); + yield* writeCliToken(signedIn.tokenPath, "2026-10-09T14:00:00Z"); + const limits = yield* signedIn.probe; + expect(limits.unavailable).toBeUndefined(); + expect(limits.windows).toHaveLength(4); + expect(signedIn.requests.some((request) => request.url.endsWith("/token"))).toBe(false); + + const expired = yield* fixture(); + yield* writeCliToken(expired.tokenPath, "2026-10-09T12:00:30Z"); + const expiredLimits = yield* expired.probe; + expect(expiredLimits.unavailable?.reason).toBe("probeFailed"); + expect(expiredLimits.unavailable?.message).toContain("expired"); + expect(expired.requests).toEqual([]); + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), + ); + it.effect("reads a Gemini Enterprise keyring sign-in and reports when it has expired", () => Effect.gen(function* () { yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-09T12:00:00Z"))); diff --git a/apps/server/src/provider/antigravityUsageLimits.ts b/apps/server/src/provider/antigravityUsageLimits.ts index fadf2207c31f2..56745f62f914b 100644 --- a/apps/server/src/provider/antigravityUsageLimits.ts +++ b/apps/server/src/provider/antigravityUsageLimits.ts @@ -357,21 +357,16 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")( let accessToken: string; let account: string; let projectId: string | undefined; - if (!(yield* fs.exists(tokenPath))) { + // The agent runs with the CLI's own sign-in. A leftover acp_token.json from an older + // integration can hold a different account, so only fall back to it when the CLI has none. + // Google account instances bypass the keyring, whose one entry every profile shares. + const stored = (yield* fs.exists(cliTokenPath)) + ? yield* fs.readFileString(cliTokenPath) + : input.authMethod === "oauth-business" + ? yield* readKeyringSecret.pipe(Effect.orElseSucceed(() => undefined)) + : undefined; + if (stored !== undefined && stored.length > 0) { cached = undefined; - // Google account instances bypass the keyring, whose one entry every profile shares. - const stored = (yield* fs.exists(cliTokenPath)) - ? yield* fs.readFileString(cliTokenPath) - : input.authMethod === "oauth-business" - ? yield* readKeyringSecret - : undefined; - if (stored === undefined) { - return makeUnavailableUsageLimits({ - checkedAt, - reason: "unsupported", - message: "Sign in with Google to see Antigravity limits.", - }); - } const { token } = yield* decodeCliToken(stored); if (!(Date.parse(token.expiry ?? "") > now + 60_000)) { return makeUnavailableUsageLimits({ @@ -390,6 +385,13 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")( Effect.flatMap(antigravityAccountFingerprint), Effect.catch(() => sha256(`antigravity-cli\0${token.refresh_token || accessToken}`)), ); + } else if (!(yield* fs.exists(tokenPath))) { + cached = undefined; + return makeUnavailableUsageLimits({ + checkedAt, + reason: "unsupported", + message: "Sign in with Google to see Antigravity limits.", + }); } else { const token = yield* fs.readFileString(tokenPath).pipe(Effect.flatMap(decodeTokenFile)); projectId = token.project_id;