Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion apps/server/src/provider/antigravityUsageLimits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")));
Expand Down
30 changes: 16 additions & 14 deletions apps/server/src/provider/antigravityUsageLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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;
Expand Down
Loading