Skip to content
Merged
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
30 changes: 28 additions & 2 deletions apps/server/src/provider/antigravityUsageLimits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
userId?: string;
userInfoAt?: number;
missingToken?: boolean;
/** Writes the token file of agy 1.5+ instead of the legacy `acp_token.json`. */
agyToken?: boolean;
authMethod?: "oauth-personal" | "oauth-business" | "gemini-api-key";
} = {},
) {
Expand All @@ -83,7 +85,15 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
project_id: "project-id",
}),
);
if (!options.missingToken) yield* writeToken(options.refreshToken ?? "refresh-a");
if (options.agyToken) {
yield* fs.writeFileString(
path.join(profileDirectory, "antigravity-acp", "antigravity-oauth-token"),
JSON.stringify({
token: { access_token: "stale", refresh_token: options.refreshToken ?? "refresh-agy" },
auth_method: "consumer",
}),
);
} else if (!options.missingToken) yield* writeToken(options.refreshToken ?? "refresh-a");
const requests: Array<{ url: string; body: string }> = [];
const http = HttpClient.make((request, url, _signal, fiber) =>
Effect.sync(() => {
Expand Down Expand Up @@ -111,7 +121,7 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
: Response.json({}, { status: 403 }),
);
}
expect(JSON.parse(body)).toEqual({ project: "project-id" });
expect(JSON.parse(body)).toEqual(options.agyToken ? {} : { project: "project-id" });
expect(request.headers["user-agent"]).toMatch(/^antigravity\/hub\/2\.24\.0 /);
return HttpClientResponse.fromWeb(
request,
Expand All @@ -137,6 +147,22 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
});

describe("Antigravity subscription limits", () => {
// Regression: agy 1.5 writes `antigravity-oauth-token` without client credentials, and the
// probe reported "Sign in with Google" for a signed-in account.
it.effect("reads limits from the agy 1.5 token file with agy's OAuth client", () =>
Effect.gen(function* () {
const { probe, requests } = yield* fixture({ agyToken: true });
const limits = yield* probe;
expect(limits.unavailable).toBeUndefined();
expect(limits.windows.map((window) => window.id)).toContain("gemini-5h");
const refresh = new URLSearchParams(
requests.find((request) => request.url.endsWith("/token"))?.body,
);
expect(refresh.get("refresh_token")).toBe("refresh-agy");
expect(refresh.get("client_id")).toMatch(/^1071006060591-/);
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);

// Oracle: the 10:16 UTC payload and its narrow Workaround rule.
it.effect("omits rolling resets for untouched summary buckets and keeps used Gemini resets", () =>
Effect.gen(function* () {
Expand Down
33 changes: 30 additions & 3 deletions apps/server/src/provider/antigravityUsageLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ const TokenFile = Schema.Struct({
refresh_token: Schema.NonEmptyString,
project_id: Schema.optional(Schema.String),
});
// agy 1.5+ stores its Google sign-in without client credentials; it refreshes the token with
// its own installed-app OAuth client, which ships in every agy binary and is public by design.
const AGY_OAUTH_CLIENT = {
client_id: "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com",
client_secret: "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",
};
const AgyTokenFile = Schema.Struct({
token: Schema.Struct({ refresh_token: Schema.NonEmptyString }),
});
const TokenResponse = Schema.Struct({
access_token: Schema.NonEmptyString,
expires_in: Schema.Number.check(Schema.isGreaterThan(0)),
Expand Down Expand Up @@ -78,6 +87,7 @@ const Models = Schema.Struct({
});

const decodeTokenFile = Schema.decodeEffect(Schema.fromJsonString(TokenFile));
const decodeAgyTokenFile = Schema.decodeEffect(Schema.fromJsonString(AgyTokenFile));
const decodeTokenResponse = Schema.decodeUnknownEffect(TokenResponse);
const decodeUserInfo = Schema.decodeUnknownEffect(UserInfo);
const decodeSummary = Schema.decodeUnknownEffect(Summary);
Expand Down Expand Up @@ -217,7 +227,24 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")(
const crypto = yield* Digest.Digest;
const platform = yield* HostProcess.Platform;
const architecture = yield* HostProcess.Architecture;
const tokenPath = path.join(input.profileDirectory, "antigravity-acp", "acp_token.json");
const acpDirectory = path.join(input.profileDirectory, "antigravity-acp");
const agyTokenPath = path.join(acpDirectory, "antigravity-oauth-token");
const legacyTokenPath = path.join(acpDirectory, "acp_token.json");
// Prefer agy's current token file; a legacy one may linger from an older agy with a revoked token.
const readToken = Effect.gen(function* () {
if (yield* fs.exists(agyTokenPath)) {
const file = yield* fs.readFileString(agyTokenPath).pipe(Effect.flatMap(decodeAgyTokenFile));
const token: typeof TokenFile.Type = {
...AGY_OAUTH_CLIENT,
refresh_token: file.token.refresh_token,
};
return token;
}
if (yield* fs.exists(legacyTokenPath)) {
return yield* fs.readFileString(legacyTokenPath).pipe(Effect.flatMap(decodeTokenFile));
}
return undefined;
});
let cached:
| { fingerprint: string; account: string; accessToken: string; expiresAt: number }
| undefined;
Expand All @@ -236,15 +263,15 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")(
if (input.authMethod !== "oauth-personal" && input.authMethod !== "oauth-business") {
return makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" });
}
if (!(yield* fs.exists(tokenPath))) {
const token = yield* readToken;
if (token === undefined) {
cached = undefined;
return makeUnavailableUsageLimits({
checkedAt,
reason: "unsupported",
message: "Sign in with Google to see Antigravity limits.",
});
}
const token = yield* fs.readFileString(tokenPath).pipe(Effect.flatMap(decodeTokenFile));
const fingerprint = yield* sha256(
`${token.client_id}\0${token.refresh_token}\0${token.project_id ?? ""}`,
);
Expand Down