Skip to content

Commit 047d492

Browse files
fix(server): Antigravity limits read the quota host the account is metered on (pingdotgg#32425)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7389423 commit 047d492

2 files changed

Lines changed: 54 additions & 14 deletions

File tree

‎apps/server/src/provider/antigravityUsageLimits.test.ts‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
6565
userId?: string;
6666
userInfoAt?: number;
6767
missingToken?: boolean;
68-
authMethod?: "oauth-personal" | "gemini-api-key";
68+
authMethod?: "oauth-personal" | "oauth-business" | "gemini-api-key";
6969
} = {},
7070
) {
7171
const fs = yield* FileSystem.FileSystem;
@@ -330,11 +330,12 @@ describe("Antigravity subscription limits", () => {
330330
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
331331
);
332332

333-
// Oracle: the existing endpoint merge keeps the most-used reading.
334-
it.effect("keeps the used endpoint reading and its reset over an untouched reading", () =>
333+
// Oracle: workspace sign-ins are metered on cloudcode-pa, so its reading wins even when less used.
334+
it.effect("reports a business account's quota from the endpoint it is metered on", () =>
335335
Effect.gen(function* () {
336336
yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-21T10:16:04Z")));
337337
const { probe } = yield* fixture({
338+
authMethod: "oauth-business",
338339
summaryByHost: {
339340
"daily-cloudcode-pa.googleapis.com": {
340341
buckets: [
@@ -383,9 +384,8 @@ describe("Antigravity subscription limits", () => {
383384
id: "3p-weekly",
384385
label: "Claude & GPT (shared) · Weekly",
385386
kind: "weekly",
386-
usedPercent: 50,
387+
usedPercent: 0,
387388
windowDurationMins: 10080,
388-
resetsAt: "2026-10-27T10:16:04.000Z",
389389
},
390390
]);
391391
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
@@ -474,7 +474,43 @@ describe("Antigravity subscription limits", () => {
474474
const byId = new Map(limits.windows.map((window) => [window.id, window]));
475475
expect(byId.get("gemini-weekly")?.label).toBe("Gemini · Weekly");
476476
expect(byId.get("3p-5h")?.usedPercent).toBeCloseTo(70);
477-
expect(byId.get("3p-weekly")?.usedPercent).toBe(100);
477+
expect(byId.get("3p-weekly")?.usedPercent).toBe(50);
478+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
479+
);
480+
481+
// Oracle: reported payloads for a personal sign-in, whose runtime meters on the daily endpoint.
482+
it.effect("reports a personal account's quota from the daily endpoint it is metered on", () =>
483+
Effect.gen(function* () {
484+
yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-11-07T12:00:00Z")));
485+
const argon = (remainingFraction: number, resetTime: string) => ({
486+
groups: [
487+
{
488+
displayName: "Gemini 4 Argon",
489+
buckets: [
490+
{
491+
bucketId: "gemini-argon-wtu-weekly",
492+
window: "weekly",
493+
remainingFraction,
494+
resetTime,
495+
},
496+
],
497+
},
498+
],
499+
});
500+
const { probe } = yield* fixture({
501+
authMethod: "oauth-personal",
502+
summaryByHost: {
503+
"daily-cloudcode-pa.googleapis.com": argon(0.8623059, "2026-11-14T08:41:35Z"),
504+
"cloudcode-pa.googleapis.com": argon(0, "2026-11-08T18:27:14Z"),
505+
},
506+
});
507+
expect((yield* probe).windows).toEqual([
508+
expect.objectContaining({
509+
id: "gemini-argon-wtu-weekly",
510+
usedPercent: expect.closeTo(13.77, 2),
511+
resetsAt: "2026-11-14T08:41:35.000Z",
512+
}),
513+
]);
478514
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
479515
);
480516

‎apps/server/src/provider/antigravityUsageLimits.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ import {
1515
makeUsageLimits,
1616
} from "@t3tools/provider-core/server/usageLimits";
1717

18-
const ENDPOINTS = [
19-
"https://daily-cloudcode-pa.googleapis.com",
20-
"https://cloudcode-pa.googleapis.com",
21-
];
18+
const DAILY_ENDPOINT = "https://daily-cloudcode-pa.googleapis.com";
19+
const PROD_ENDPOINT = "https://cloudcode-pa.googleapis.com";
20+
// The ACP runtime meters consumer tiers on the daily host and GCP-terms tiers on prod.
21+
// The sign-in method stands in for the tier.
22+
const ENDPOINTS = {
23+
"oauth-personal": [DAILY_ENDPOINT, PROD_ENDPOINT],
24+
"oauth-business": [PROD_ENDPOINT, DAILY_ENDPOINT],
25+
};
2226
const SUMMARY_WINDOW_MINS = new Map([
2327
["5h", 5 * 60],
2428
["weekly", 7 * 24 * 60],
@@ -269,7 +273,8 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")(
269273
accessToken = cached.accessToken;
270274
account = cached.account;
271275
}
272-
// Merge endpoints because quota groups differ; keep each bucket's most-used reading.
276+
const endpoints = ENDPOINTS[input.authMethod];
277+
// Merge endpoints because quota groups differ; the metered host's reading of a bucket wins.
273278
const fetchWindows = Effect.fn("AntigravityUsageProbe.fetchWindows")(function* <E>(
274279
operation: string,
275280
parse: (
@@ -278,7 +283,7 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")(
278283
) => Effect.Effect<ServerProviderUsageWindow[], E>,
279284
) {
280285
const responses = yield* Effect.forEach(
281-
ENDPOINTS,
286+
endpoints,
282287
(endpoint) =>
283288
Effect.gen(function* () {
284289
// Capture each request after auth, so refresh latency does not widen the tolerance.
@@ -302,8 +307,7 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")(
302307
);
303308
const windows = new Map<string, ServerProviderUsageWindow>();
304309
for (const window of responses.flat()) {
305-
const previous = windows.get(window.id);
306-
if (!previous || window.usedPercent > previous.usedPercent) windows.set(window.id, window);
310+
if (!windows.has(window.id)) windows.set(window.id, window);
307311
}
308312
return [...windows.values()];
309313
});

0 commit comments

Comments
 (0)