TypeScript
fix(server): hide rolling resets for untouched Antigravity quotas (#23558)
Fuksss1da04920a554
diff --git a/apps/server/src/provider/antigravityUsageLimits.test.ts b/apps/server/src/provider/antigravityUsageLimits.test.tsindex 7c0eeabb0a7..16e3c87ce1e 100644--- a/apps/server/src/provider/antigravityUsageLimits.test.ts+++ b/apps/server/src/provider/antigravityUsageLimits.test.ts@@ -1,9 +1,11 @@import * as NodeServices from "@effect/platform-node/NodeServices";import { describe, expect, it } from "@effect/vitest";import * as Context from "effect/Context";+import * as DateTime from "effect/DateTime";import * as Effect from "effect/Effect";import * as FileSystem from "effect/FileSystem";import * as Path from "effect/Path";+import * as TestClock from "effect/testing/TestClock";import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/http";import { makeAntigravityUsageProbe } from "./antigravityUsageLimits.ts";@@ -59,6 +61,7 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (tokenStatus?: number;refreshToken?: string;userId?: string;+ userInfoAt?: number;missingToken?: boolean;authMethod?: "oauth-personal" | "gemini-api-key";} = {},@@ -115,7 +118,13 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (}): Response.json(options.models ?? { models: {} }),);- }),+ }).pipe(+ Effect.tap(() =>+ request.url.endsWith("/userinfo") && options.userInfoAt !== undefined+ ? TestClock.setTime(options.userInfoAt)+ : Effect.void,+ ),+ ),);const usage = yield* makeAntigravityUsageProbe({profileDirectory,@@ -125,6 +134,288 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (});describe("Antigravity subscription limits", () => {+ // 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* () {+ yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-21T10:16:04Z")));+ const { probe } = yield* fixture({+ summary: {+ groups: [+ {+ buckets: [+ {+ bucketId: "3p-5h",+ window: "5h",+ remainingFraction: 1,+ resetTime: "2026-10-21T15:16:05Z",+ },+ {+ bucketId: "3p-weekly",+ window: "weekly",+ remainingFraction: 1,+ resetTime: "2026-10-28T10:16:05Z",+ },+ {+ bucketId: "gemini-5h",+ window: "5h",+ remainingFraction: 0.848104,+ resetTime: "2026-10-21T13:29:53Z",+ description:+ "You have used some of your 5-hour limit, it will fully refresh in 3 hours, 13 minutes.",+ },+ {+ bucketId: "gemini-weekly",+ window: "weekly",+ remainingFraction: 0.9620177,+ resetTime: "2026-10-28T08:29:53Z",+ description:+ "You have used some of your weekly limit, it will fully refresh in 6 days, 22 hours.",+ },+ ],+ },+ ],+ },+ });+ const limits = yield* probe;+ const byId = new Map(limits.windows.map((window) => [window.id, window]));+ expect(byId.get("3p-5h")).toEqual({+ id: "3p-5h",+ label: "Claude & GPT (shared) · 5-hour",+ kind: "session",+ usedPercent: 0,+ windowDurationMins: 300,+ });+ expect(byId.get("3p-weekly")).toEqual({+ id: "3p-weekly",+ label: "Claude & GPT (shared) · Weekly",+ kind: "weekly",+ usedPercent: 0,+ windowDurationMins: 10080,+ });+ expect(byId.get("gemini-5h")?.resetsAt).toBe("2026-10-21T13:29:53.000Z");+ expect(byId.get("gemini-weekly")?.resetsAt).toBe("2026-10-28T08:29:53.000Z");+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );++ // Oracle: measure the tolerance from the summary request, after auth.+ it.effect("uses summary request time after authentication for the reset tolerance", () =>+ Effect.gen(function* () {+ yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-21T10:16:02Z")));+ const { probe } = yield* fixture({+ userInfoAt: DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-21T10:16:04Z")),+ summary: {+ buckets: [+ {+ bucketId: "3p-5h",+ window: "5h",+ remainingFraction: 1,+ resetTime: "2026-10-21T15:17:03Z",+ },+ ],+ },+ });+ expect((yield* probe).windows[0]?.resetsAt).toBeUndefined();+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );++ // Oracle: keep descriptions, measured resets, and spent buckets.+ it.effect("keeps summary resets with a description, a distant reset, or no quota left", () =>+ Effect.gen(function* () {+ yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-21T10:16:04Z")));+ const { probe } = yield* fixture({+ summary: {+ buckets: [+ {+ bucketId: "described",+ window: "5h",+ remainingFraction: 1,+ description: "Reported reset",+ resetTime: "2026-10-21T15:16:04Z",+ },+ {+ bucketId: "distant",+ window: "weekly",+ remainingFraction: 1,+ resetTime: "2026-10-28T12:16:04Z",+ },+ {+ bucketId: "spent",+ window: "5h",+ remainingFraction: 0,+ resetTime: "2026-10-21T15:16:04Z",+ },+ {+ bucketId: "overfull",+ window: "5h",+ remainingFraction: 1.01,+ resetTime: "2026-10-21T15:16:04Z",+ },+ {+ bucketId: "empty-description",+ window: "5h",+ remainingFraction: 1,+ description: "",+ resetTime: "2026-10-21T15:16:04Z",+ },+ ],+ },+ });+ expect((yield* probe).windows.map(({ id, resetsAt }) => ({ id, resetsAt }))).toEqual([+ { id: "described", resetsAt: "2026-10-21T15:16:04.000Z" },+ { id: "empty-description", resetsAt: "2026-10-21T15:16:04.000Z" },+ { id: "overfull", resetsAt: "2026-10-21T15:16:04.000Z" },+ { id: "spent", resetsAt: "2026-10-21T15:16:04.000Z" },+ { id: "distant", resetsAt: "2026-10-28T12:16:04.000Z" },+ ]);+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );++ // Oracle: the 60-second tolerance applies to both known window lengths.+ it.effect("omits summary resets 59 seconds away but keeps resets 61 seconds away", () =>+ Effect.gen(function* () {+ yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-21T10:16:04Z")));+ const { probe } = yield* fixture({+ summary: {+ buckets: [+ {+ bucketId: "5h-before",+ window: "5h",+ remainingFraction: 1,+ resetTime: "2026-10-21T15:15:05Z",+ },+ {+ bucketId: "5h-after",+ window: "5h",+ remainingFraction: 1,+ resetTime: "2026-10-21T15:17:05Z",+ },+ {+ bucketId: "5h-edge",+ window: "5h",+ remainingFraction: 1,+ resetTime: "2026-10-21T15:17:04Z",+ },+ {+ bucketId: "weekly-after",+ window: "weekly",+ remainingFraction: 1,+ resetTime: "2026-10-28T10:17:03Z",+ },+ {+ bucketId: "weekly-before",+ window: "weekly",+ remainingFraction: 1,+ resetTime: "2026-10-28T10:15:03Z",+ },+ {+ bucketId: "weekly-edge",+ window: "weekly",+ remainingFraction: 1,+ resetTime: "2026-10-28T10:15:04Z",+ },+ ],+ },+ });+ expect((yield* probe).windows.map(({ id, resetsAt }) => ({ id, resetsAt }))).toEqual([+ { id: "5h-after", resetsAt: "2026-10-21T15:17:05.000Z" },+ { id: "5h-before", resetsAt: undefined },+ { id: "5h-edge", resetsAt: undefined },+ { id: "weekly-after", resetsAt: undefined },+ { id: "weekly-before", resetsAt: "2026-10-28T10:15:03.000Z" },+ { id: "weekly-edge", resetsAt: undefined },+ ]);+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );++ // Oracle: the existing endpoint merge keeps the most-used reading.+ it.effect("keeps the used endpoint reading and its reset over an untouched reading", () =>+ Effect.gen(function* () {+ yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-21T10:16:04Z")));+ const { probe } = yield* fixture({+ summaryByHost: {+ "daily-cloudcode-pa.googleapis.com": {+ buckets: [+ {+ bucketId: "3p-5h",+ window: "5h",+ remainingFraction: 1,+ resetTime: "2026-10-21T15:16:04Z",+ },+ {+ bucketId: "3p-weekly",+ window: "weekly",+ remainingFraction: 0.5,+ resetTime: "2026-10-27T10:16:04Z",+ },+ ],+ },+ "cloudcode-pa.googleapis.com": {+ buckets: [+ {+ bucketId: "3p-5h",+ window: "5h",+ remainingFraction: 0.5,+ resetTime: "2026-10-21T13:16:04Z",+ },+ {+ bucketId: "3p-weekly",+ window: "weekly",+ remainingFraction: 1,+ resetTime: "2026-10-28T10:16:04Z",+ },+ ],+ },+ },+ });+ expect((yield* probe).windows).toEqual([+ {+ id: "3p-5h",+ label: "Claude & GPT (shared) · 5-hour",+ kind: "session",+ usedPercent: 50,+ windowDurationMins: 300,+ resetsAt: "2026-10-21T13:16:04.000Z",+ },+ {+ id: "3p-weekly",+ label: "Claude & GPT (shared) · Weekly",+ kind: "weekly",+ usedPercent: 50,+ windowDurationMins: 10080,+ resetsAt: "2026-10-27T10:16:04.000Z",+ },+ ]);+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );++ // Oracle: model quotas have no evidence for the summary-only rule.+ it.effect("keeps full model fallback resets even at a full window from the request", () =>+ Effect.gen(function* () {+ yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-21T10:16:04Z")));+ const { probe } = yield* fixture({+ summaryStatus: 404,+ models: {+ models: {+ "gemini-a": {+ quotaInfo: { remainingFraction: 1, resetTime: "2026-10-21T15:16:04Z" },+ weeklyQuotaInfo: { remainingFraction: 1, resetTime: "2026-10-28T10:16:04Z" },+ },+ },+ },+ });+ expect(+ (yield* probe).windows.map(({ id, resetsAt, windowDurationMins }) => ({+ id,+ resetsAt,+ windowDurationMins,+ })),+ ).toEqual([+ { id: "gemini-weekly", resetsAt: "2026-10-28T10:16:04.000Z", windowDurationMins: 10080 },+ { id: "gemini-quota", resetsAt: "2026-10-21T15:16:04.000Z", windowDurationMins: undefined },+ ]);+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );+it.effect("reports separate Gemini and shared Claude/GPT five-hour and weekly limits", () =>Effect.gen(function* () {const { probe, requests } = yield* fixture();diff --git a/apps/server/src/provider/antigravityUsageLimits.ts b/apps/server/src/provider/antigravityUsageLimits.tsindex 75333ae8262..beecf7b53db 100644--- a/apps/server/src/provider/antigravityUsageLimits.ts+++ b/apps/server/src/provider/antigravityUsageLimits.ts@@ -19,6 +19,10 @@ const ENDPOINTS = ["https://daily-cloudcode-pa.googleapis.com","https://cloudcode-pa.googleapis.com",];+const SUMMARY_WINDOW_MINS = new Map([+ ["5h", 5 * 60],+ ["weekly", 7 * 24 * 60],+]);const MANUAL_REDIRECT: RequestInit = { redirect: "manual" };const TokenFile = Schema.Struct({client_id: Schema.NonEmptyString,@@ -40,6 +44,7 @@ const Bucket = Schema.Struct({bucketId: Schema.NonEmptyString,window: Schema.optional(Schema.String),displayName: Schema.optional(Schema.String),+ description: Schema.optional(Schema.String),disabled: Schema.optional(Schema.Boolean),});const Summary = Schema.Struct({@@ -96,7 +101,22 @@ function quotaWindow(};}-function summaryWindows(summary: typeof Summary.Type) {+function summaryResetTime(+ bucket: typeof Bucket.Type,+ windowDurationMins: number | undefined,+ requestedAt: number,+): string | undefined {+ // Untouched summary buckets report a rolling full-window reset.+ const untouched =+ bucket.remainingFraction === 1 &&+ bucket.description === undefined &&+ windowDurationMins !== undefined &&+ Math.abs(Date.parse(bucket.resetTime ?? "") - requestedAt - windowDurationMins * 60_000) <=+ 60_000;+ return untouched ? undefined : bucket.resetTime;+}++function summaryWindows(summary: typeof Summary.Type, requestedAt: number) {const windows = new Map<string, ServerProviderUsageWindow>();const groups = summary.groups?.some((group) => group.buckets?.length)? summary.groups@@ -111,12 +131,13 @@ function summaryWindows(summary: typeof Summary.Type) {: group.displayName?.trim() || bucket.bucketId;const weekly = bucket.window === "weekly";const session = bucket.window === "5h";+ const windowDurationMins = SUMMARY_WINDOW_MINS.get(bucket.window ?? "");const window = quotaWindow(bucket.bucketId,`${family} · ${weekly ? "Weekly" : session ? "5-hour" : bucket.displayName?.trim() || "Quota"}`,- bucket,+ { ...bucket, resetTime: summaryResetTime(bucket, windowDurationMins, requestedAt) },weekly ? "weekly" : session ? "session" : "other",- weekly ? 7 * 24 * 60 : session ? 5 * 60 : undefined,+ windowDurationMins,);if (window) windows.set(window.id, window);}@@ -248,25 +269,32 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")(// Merge endpoints because quota groups differ; keep each bucket's most-used reading.const fetchWindows = Effect.fn("AntigravityUsageProbe.fetchWindows")(function* <E>(operation: string,- parse: (response: unknown) => Effect.Effect<ServerProviderUsageWindow[], E>,+ parse: (+ response: unknown,+ requestedAt: number,+ ) => Effect.Effect<ServerProviderUsageWindow[], E>,) {const responses = yield* Effect.forEach(ENDPOINTS,(endpoint) =>- request(- HttpClientRequest.post(`${endpoint}/v1internal:${operation}`).pipe(- HttpClientRequest.setHeaders({- Authorization: `Bearer ${accessToken}`,- "User-Agent": `antigravity/hub/1.0.0 (aidev_client; os_type=${platform}; arch=${architecture}; cl=963137146)`,- }),- HttpClientRequest.bodyJsonUnsafe(- token.project_id ? { project: token.project_id } : {},+ Effect.gen(function* () {+ // Capture each request after auth, so refresh latency does not widen the tolerance.+ const requestedAt = yield* Clock.currentTimeMillis;+ return yield* request(+ HttpClientRequest.post(`${endpoint}/v1internal:${operation}`).pipe(+ HttpClientRequest.setHeaders({+ Authorization: `Bearer ${accessToken}`,+ "User-Agent": `antigravity/hub/1.0.0 (aidev_client; os_type=${platform}; arch=${architecture}; cl=963137146)`,+ }),+ HttpClientRequest.bodyJsonUnsafe(+ token.project_id ? { project: token.project_id } : {},+ ),),- ),- ).pipe(- Effect.flatMap(parse),- Effect.orElseSucceed(() => []),- ),+ ).pipe(+ Effect.flatMap((response) => parse(response, requestedAt)),+ Effect.orElseSucceed(() => []),+ );+ }),{ concurrency: "unbounded" },);const windows = new Map<string, ServerProviderUsageWindow>();@@ -276,8 +304,8 @@ export const makeAntigravityUsageProbe = Effect.fn("makeAntigravityUsageProbe")(}return [...windows.values()];});- const summary = yield* fetchWindows("retrieveUserQuotaSummary", (response) =>- decodeSummary(response).pipe(Effect.map(summaryWindows)),+ const summary = yield* fetchWindows("retrieveUserQuotaSummary", (response, requestedAt) =>+ decodeSummary(response).pipe(Effect.map((summary) => summaryWindows(summary, requestedAt))),);const windows = summary.length? summary