Skip to content
Open
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
1 change: 0 additions & 1 deletion apps/server/src/provider/Drivers/AntigravityDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ export const AntigravityDriver: ProviderDriver<
stampIdentity: classifyModels,
probe,
probeUsage: usage.probe,
clearUsage: usage.clear,
auth: { type: auth.authMethod, label: antigravityAuthLabel(auth.authMethod) },
supportsTextGeneration: isAntigravityTextGenerationAvailable(profileDirectory).pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Expand Down
110 changes: 63 additions & 47 deletions apps/server/src/provider/antigravityUsageLimits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ const summary = {
],
};

const cliToken = (refreshToken: string) =>
JSON.stringify({
token: {
access_token: "access-token",
refresh_token: refreshToken,
expiry: "2099-01-01T00:00:00Z",
},
});

const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
options: {
summary?: unknown;
Expand All @@ -65,11 +74,13 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
summaryStatus?: number;
/** Per-host summary statuses; hosts not listed fall back to `summaryStatus`. */
summaryStatusByHost?: Record<string, number>;
tokenStatus?: number;
refreshToken?: string;
userId?: string;
userInfoAt?: number;
/** The CLI is signed out: no antigravity-oauth-token file. */
missingToken?: boolean;
/** An acp_token.json left in the profile by a runtime below 1.5 or an earlier account. */
leftoverAcpToken?: boolean;
authMethod?: "oauth-personal" | "oauth-business" | "gemini-api-key";
keyringSecret?: string;
/** Reads the OS keyring instead of `keyringSecret`. */
Expand All @@ -80,18 +91,21 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
const path = yield* Path.Path;
const profileDirectory = yield* fs.makeTempDirectoryScoped();
yield* fs.makeDirectory(path.join(profileDirectory, "antigravity-acp"));
const tokenPath = path.join(profileDirectory, "antigravity-acp", "acp_token.json");
const writeToken = (refreshToken: string) =>
fs.writeFileString(
tokenPath,
if (options.leftoverAcpToken) {
yield* fs.writeFileString(
path.join(profileDirectory, "antigravity-acp", "acp_token.json"),
JSON.stringify({
client_id: "client-id",
client_secret: "client-secret",
refresh_token: refreshToken,
refresh_token: "stale-refresh",
project_id: "project-id",
}),
);
if (!options.missingToken) yield* writeToken(options.refreshToken ?? "refresh-a");
}
const cliTokenPath = path.join(profileDirectory, "antigravity-acp", "antigravity-oauth-token");
if (!options.missingToken) {
yield* fs.writeFileString(cliTokenPath, cliToken(options.refreshToken ?? "refresh-a"));
}
const requests: Array<{ url: string; body: string }> = [];
const http = HttpClient.make((request, url, _signal, fiber) =>
Effect.sync(() => {
Expand All @@ -100,16 +114,7 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
const body =
request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "";
requests.push({ url: request.url, body });
if (request.url.endsWith("/token")) {
expect(new URLSearchParams(body).get("grant_type")).toBe("refresh_token");
return HttpClientResponse.fromWeb(
request,
Response.json(
{ access_token: "access-token", expires_in: 3600 },
{ status: options.tokenStatus ?? 200 },
),
);
}
expect(request.url).not.toMatch(/\/token$/);
expect(request.headers.authorization).toBe("Bearer access-token");
if (request.url.endsWith("/userinfo")) {
return HttpClientResponse.fromWeb(
Expand All @@ -119,7 +124,7 @@ 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({});
expect(request.headers["user-agent"]).toMatch(/^antigravity\/hub\/2\.24\.0 /);
return HttpClientResponse.fromWeb(
request,
Expand All @@ -142,7 +147,7 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
authMethod: options.authMethod ?? "oauth-personal",
...(options.nativeKeyring ? {} : { readKeyringSecret: Effect.succeed(options.keyringSecret) }),
}).pipe(Effect.provideService(HttpClient.HttpClient, http));
return { ...usage, requests, writeToken, tokenPath };
return { ...usage, requests, cliTokenPath };
});

describe("Antigravity subscription limits", () => {
Expand Down Expand Up @@ -448,7 +453,6 @@ describe("Antigravity subscription limits", () => {
expect(byId.get("3p-5h")).toMatchObject({ kind: "session", windowDurationMins: 300 });
expect(byId.get("3p-5h")?.usedPercent).toBeCloseTo(56);
yield* probe;
expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(1);
expect(
requests.filter((request) => request.url.endsWith(":retrieveUserQuotaSummary")),
).toHaveLength(4);
Expand Down Expand Up @@ -568,22 +572,17 @@ describe("Antigravity subscription limits", () => {
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);

it.effect("re-reads changed credentials and clears cached access after sign-out", () =>
it.effect("re-reads a changed sign-in on every probe", () =>
Effect.gen(function* () {
const { probe, clear, requests, writeToken, tokenPath } = yield* fixture();
const fs = yield* FileSystem.FileSystem;
const { probe, cliTokenPath } = yield* fixture({ missingToken: true });
yield* fs.writeFileString(cliTokenPath, cliToken("refresh-a"));
const first = yield* probe;
yield* writeToken("refresh-b");
yield* fs.writeFileString(cliTokenPath, cliToken("refresh-b"));
const second = yield* probe;
expect(second.credentialFingerprint).not.toBe(first.credentialFingerprint);
expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(2);
yield* (yield* FileSystem.FileSystem).remove(tokenPath);
yield* fs.remove(cliTokenPath);
expect((yield* probe).unavailable?.reason).toBe("unsupported");
yield* writeToken("refresh-b");
yield* probe;
expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(3);
yield* clear;
yield* probe;
expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(4);
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);

Expand Down Expand Up @@ -653,24 +652,13 @@ describe("Antigravity subscription limits", () => {
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);

it.effect("returns a safe failure when OAuth refresh fails", () =>
Effect.gen(function* () {
const { probe } = yield* fixture({ tokenStatus: 401 });
const limits = yield* probe;
expect(limits.windows).toEqual([]);
expect(limits.unavailable?.reason).toBe("probeFailed");
expect(JSON.stringify(limits)).not.toMatch(/client-secret|refresh-a|access-token/);
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);

it.effect("reads the native CLI's stored sign-in when acp_token.json is absent", () =>
it.effect("reads the native CLI's stored sign-in file", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const { probe, tokenPath } = yield* fixture({ missingToken: true });
// The 1.5.x CLI never writes acp_token.json; without a keyring it stores this file instead.
const { probe, cliTokenPath } = yield* fixture({ missingToken: true });
// Without a keyring the CLI stores its sign-in in this file.
yield* fs.writeFileString(
path.join(path.dirname(tokenPath), "antigravity-oauth-token"),
cliTokenPath,
JSON.stringify({
token: {
access_token: "access-token",
Expand All @@ -691,6 +679,30 @@ describe("Antigravity subscription limits", () => {
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);

it.effect("ignores a leftover acp_token.json for the CLI's sign-in", () =>
Effect.gen(function* () {
// The CLI never writes or updates acp_token.json, so any copy is another account's.
const file = yield* fixture({ leftoverAcpToken: true, refreshToken: "refresh-b" });
const limits = yield* file.probe;
expect(limits.unavailable).toBeUndefined();
expect(limits.windows).toHaveLength(4);
expect(file.requests.map((request) => request.body).join("\n")).not.toContain(
"stale-refresh",
);

const keyring = yield* fixture({
authMethod: "oauth-business",
leftoverAcpToken: true,
missingToken: true,
keyringSecret: cliToken("refresh-b"),
});
expect((yield* keyring.probe).unavailable).toBeUndefined();
expect(keyring.requests.map((request) => request.body).join("\n")).not.toContain(
"stale-refresh",
);
}).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 Expand Up @@ -776,7 +788,11 @@ describe("Antigravity subscription limits", () => {

it.effect("does not make network requests for API keys or missing sign-ins", () =>
Effect.gen(function* () {
for (const options of [{ authMethod: "gemini-api-key" as const }, { missingToken: true }]) {
for (const options of [
{ authMethod: "gemini-api-key" as const },
{ missingToken: true },
{ missingToken: true, leftoverAcpToken: true },
]) {
const { probe, requests } = yield* fixture(options);
expect((yield* probe).unavailable?.reason).toBe("unsupported");
expect(requests).toEqual([]);
Expand Down
Loading
Loading