diff --git a/apps/server/src/provider/antigravityUsageLimits.test.ts b/apps/server/src/provider/antigravityUsageLimits.test.ts index f40f228fad6ef..5f1bff2326a32 100644 --- a/apps/server/src/provider/antigravityUsageLimits.test.ts +++ b/apps/server/src/provider/antigravityUsageLimits.test.ts @@ -127,6 +127,132 @@ describe("Antigravity usage limits", () => { }).pipe(Effect.scoped), ); + // regression: #48279 weekly-exhausted Antigravity groups mark 5h disabled with a stale fraction + it.effect( + "treats a disabled 5-hour bucket as exhausted and inherits its group's weekly reset", + () => + Effect.gen(function* () { + if (yield* HostProcess.isWindows) return; + const payload = { + status: "SUCCESS", + command: { + name: "usage", + data: { + groups: [ + { + name: "Gemini Models", + buckets: [ + { + id: "gemini-weekly", + name: "Weekly Limit Remaining", + window: "weekly", + remaining_fraction: 0, + reset_time: "2026-12-08T11:55:08Z", + }, + { + id: "gemini-5h", + name: "Five Hour Limit Remaining", + window: "5h", + disabled: true, + remaining_fraction: 0.9899866580963135, + }, + ], + }, + { + name: "Claude and GPT models", + buckets: [ + { + id: "3p-weekly", + name: "Weekly Limit Remaining", + window: "weekly", + remaining_fraction: 0.1, + reset_time: "2026-12-04T16:32:52Z", + }, + { + id: "3p-5h", + name: "Five Hour Limit Remaining", + window: "5h", + remaining_fraction: 1, + reset_time: "1970-01-01T00:00:00Z", + }, + ], + }, + ], + }, + }, + }; + const limits = yield* probeWith({ + script: `printf '%s' ${shellQuote(JSON.stringify(payload))}`, + }); + expect(limits.unavailable).toBeUndefined(); + expect(limits.windows).toEqual([ + { + id: "3p-5h", + kind: "session", + label: "Claude and GPT models · 5-hour", + usedPercent: 0, + windowDurationMins: 300, + }, + { + id: "gemini-5h", + kind: "session", + label: "Gemini Models · 5-hour", + usedPercent: 100, + resetsAt: "2026-12-08T11:55:08.000Z", + }, + { + id: "3p-weekly", + kind: "weekly", + label: "Claude and GPT models · Weekly", + usedPercent: 90, + resetsAt: "2026-12-04T16:32:52.000Z", + windowDurationMins: 10080, + }, + { + id: "gemini-weekly", + kind: "weekly", + label: "Gemini Models · Weekly", + usedPercent: 100, + resetsAt: "2026-12-08T11:55:08.000Z", + windowDurationMins: 10080, + }, + ]); + + const withoutFraction = { + status: "SUCCESS", + command: { + name: "usage", + data: { + groups: [ + { + name: "Claude and GPT models", + buckets: [ + { + id: "3p-5h", + name: "Five Hour Limit Remaining", + window: "5h", + disabled: true, + }, + ], + }, + ], + }, + }, + }; + const standalone = yield* probeWith({ + script: `printf '%s' ${shellQuote(JSON.stringify(withoutFraction))}`, + }); + expect(standalone.windows).toEqual([ + { + id: "3p-5h", + kind: "session", + label: "Claude and GPT models · 5-hour", + usedPercent: 100, + }, + ]); + }).pipe(Effect.scoped), + ); + it.effect.each([ { name: "a runtime older than 1.5.22", diff --git a/apps/server/src/provider/antigravityUsageLimits.ts b/apps/server/src/provider/antigravityUsageLimits.ts index d9d19c0f44ab3..a42506aa8ffa7 100644 --- a/apps/server/src/provider/antigravityUsageLimits.ts +++ b/apps/server/src/provider/antigravityUsageLimits.ts @@ -39,6 +39,7 @@ const UsageBucket = Schema.Struct({ id: Schema.String, name: Schema.optional(Schema.String), window: Schema.optional(Schema.String), + disabled: Schema.optional(Schema.Boolean), remaining_fraction: Schema.optional(Schema.Number), reset_time: Schema.optional(Schema.String), }); @@ -67,6 +68,22 @@ const WINDOWS: Record< weekly: { kind: "weekly", label: "Weekly", mins: 7 * 24 * 60 }, }; +function parseResetTime(resetTime: string | undefined): string | undefined { + const reset = DateTime.make(resetTime ?? ""); + // An untouched bucket reports the Unix epoch: its window has not started. + return Option.isSome(reset) && DateTime.toEpochMillis(reset.value) > 0 + ? DateTime.formatIso(reset.value) + : undefined; +} + +function usedPercentOf(bucket: typeof UsageBucket.Type): number | null { + if (bucket.disabled === true) return 100; + const fraction = bucket.remaining_fraction; + return fraction !== undefined && Number.isFinite(fraction) + ? clampPercent((1 - fraction) * 100) + : null; +} + /** Maps `agy usage --json` output; groups such as Gemini and Claude/GPT are metered apart. */ function antigravityUsageOutputToLimits( stdout: string, @@ -82,23 +99,28 @@ function antigravityUsageOutputToLimits( } const windows: ServerProviderUsageWindow[] = []; for (const group of decoded.value.command.data.groups) { - for (const bucket of group.buckets ?? []) { - const fraction = bucket.remaining_fraction; - if (fraction === undefined || !Number.isFinite(fraction)) continue; + const buckets = group.buckets ?? []; + const spentWeeklyReset = buckets + .filter((bucket) => bucket.window === "weekly" && usedPercentOf(bucket) === 100) + .map((bucket) => parseResetTime(bucket.reset_time)) + .find((reset) => reset !== undefined); + for (const bucket of buckets) { + const usedPercent = usedPercentOf(bucket); + if (usedPercent === null) continue; + const disabled = bucket.disabled === true; const window = WINDOWS[bucket.window ?? ""]; - const reset = DateTime.make(bucket.reset_time ?? ""); - // An untouched bucket reports the Unix epoch: its window has not started. + // A weekly-disabled 5h bucket omits reset_time and keeps a stale remaining_fraction. const resetsAt = - Option.isSome(reset) && DateTime.toEpochMillis(reset.value) > 0 - ? DateTime.formatIso(reset.value) - : undefined; + disabled && window?.kind === "session" + ? (spentWeeklyReset ?? parseResetTime(bucket.reset_time)) + : parseResetTime(bucket.reset_time); windows.push({ id: bucket.id, kind: window?.kind ?? "other", label: `${group.name?.trim() || "Antigravity"} · ${window?.label ?? (bucket.name?.trim() || "Quota")}`, - usedPercent: clampPercent((1 - fraction) * 100), + usedPercent, ...(resetsAt ? { resetsAt } : {}), - ...(window ? { windowDurationMins: window.mins } : {}), + ...(window && !disabled ? { windowDurationMins: window.mins } : {}), }); } } diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 61c89ffd1cc3e..6f6e4c84d3c60 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1403,6 +1403,72 @@ describe("pooled account columns", () => { ); expect(keys(pool!)).toEqual([["b", "a"]]); }); + + // regression: #48279 Antigravity 5h buckets stay empty when their own group's weekly bucket is spent + it("clamps an Antigravity 5-hour pool only when its own group's weekly pool is exhausted", () => { + const antigravity = ProviderDriverKind.make("antigravity"); + const weeklyReset = "2026-09-08T11:55:08.000Z"; + const otherWeeklyReset = "2026-09-04T16:32:52.000Z"; + const agyAccount = (key: string, windows: LimitAccount["limits"]["windows"]): LimitAccount => ({ + ...account(key, windows), + driver: antigravity, + }); + + const [geminiBlocked] = collectLimitPools( + [ + agyAccount("a", [ + { + id: "gemini-5h", + kind: "session", + label: "Gemini Models · 5-hour", + usedPercent: 1, + windowDurationMins: 300, + }, + { + id: "gemini-weekly", + kind: "weekly", + label: "Gemini Models · Weekly", + usedPercent: 100, + resetsAt: weeklyReset, + windowDurationMins: 7 * 24 * 60, + }, + { + id: "3p-5h", + kind: "session", + label: "Claude and GPT models · 5-hour", + usedPercent: 0, + windowDurationMins: 300, + }, + { + id: "3p-weekly", + kind: "weekly", + label: "Claude and GPT models · Weekly", + usedPercent: 90, + resetsAt: otherWeeklyReset, + windowDurationMins: 7 * 24 * 60, + }, + ]), + ], + now, + ); + const byId = new Map(geminiBlocked!.windows.map((entry) => [entry.id, entry])); + expect(byId.get("gemini-5h")).toMatchObject({ + remainingPercent: 0, + usedPercent: 100, + pace: null, + resets: [{ at: Date.parse(weeklyReset), restoresPercent: 100 }], + }); + expect(byId.get("gemini-5h")?.columns[0]?.window).toMatchObject({ + usedPercent: 100, + resetsAt: weeklyReset, + windowDurationMins: undefined, + }); + expect(byId.get("3p-5h")).toMatchObject({ + remainingPercent: 100, + usedPercent: 0, + resets: [], + }); + }); }); describe("/usage-limits", () => { diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 79473f6771eb0..0eff6eb4abfdc 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -632,15 +632,26 @@ function poolKey(driver: ServerProvider["driver"], window: ServerProviderUsageWi return driver === "codex" ? window.kind : `${window.kind}:${window.id}`; } +function isBlockingWeeklyWindow( + driver: ServerProvider["driver"], + session: ServerProviderUsageWindow, + candidate: ServerProviderUsageWindow, +): boolean { + if (candidate.usedPercent < 100) return false; + if (driver === "claudeAgent") return candidate.id === "seven_day"; + if (driver === "codex") return candidate.kind === "weekly"; + if (driver === "antigravity") { + const pool = session.id.match(/^(.+)-5h$/)?.[1]; + return candidate.kind === "weekly" && pool !== undefined && candidate.id === `${pool}-weekly`; + } + return false; +} + function poolMember(account: LimitAccount, reported: ServerProviderUsageWindow, now: number) { const spentWeekly = reported.kind === "session" - ? account.limits.windows.find( - (window) => - window.usedPercent >= 100 && - (account.driver === "claudeAgent" - ? window.id === "seven_day" - : account.driver === "codex" && window.kind === "weekly"), + ? account.limits.windows.find((window) => + isBlockingWeeklyWindow(account.driver, reported, window), ) : undefined; if (!spentWeekly) return { account, window: reported, refillPercent: reported.usedPercent }; @@ -650,22 +661,23 @@ function poolMember(account: LimitAccount, reported: ServerProviderUsageWindow, const cutoff = Math.max(now, Date.parse(account.limits.checkedAt)); let resetsAt: string | undefined; let refillPercent = 0; - // A stale or missing clock cannot tell us when blocked quota becomes usable. - if ( - weeklyReset !== null && - sessionReset !== null && - weeklyReset > cutoff && - sessionReset > cutoff - ) { - if (sessionReset <= weeklyReset) { + // A stale or missing clock cannot tell us when blocked quota becomes usable, + // except Antigravity omits the 5h clock when disabled or untouched so weeklyReset governs. + if (weeklyReset !== null && weeklyReset > cutoff) { + if (account.driver === "antigravity" && reported.resetsAt === undefined) { resetsAt = spentWeekly.resetsAt; refillPercent = 100; - } else if (reported.usedPercent >= 100) { - resetsAt = reported.resetsAt; - refillPercent = 100; - } else { - resetsAt = spentWeekly.resetsAt; - refillPercent = 100 - Math.max(0, Math.min(100, reported.usedPercent)); + } else if (sessionReset !== null && sessionReset > cutoff) { + if (sessionReset <= weeklyReset) { + resetsAt = spentWeekly.resetsAt; + refillPercent = 100; + } else if (reported.usedPercent >= 100) { + resetsAt = reported.resetsAt; + refillPercent = 100; + } else { + resetsAt = spentWeekly.resetsAt; + refillPercent = 100 - Math.max(0, Math.min(100, reported.usedPercent)); + } } } return {