Skip to content

Commit e2a7814

Browse files
wookiistclaudemaria-rcksjuliusmarminge
authored
fix(usage): Grok and Cursor usage limits show pace and the even-spend line (#21897)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 25631c3 commit e2a7814

4 files changed

Lines changed: 106 additions & 9 deletions

File tree

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

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,8 @@ describe("Grok usage limits", () => {
478478
};
479479

480480
it("shows zero usage for complete weekly and monthly periods with no percentage", () => {
481-
for (const [currentPeriod, kind, label, resetsAt] of [
482-
[weeklyPeriod, "weekly", "Weekly", "2026-09-18T03:10:30.159Z"],
481+
for (const [currentPeriod, kind, label, resetsAt, windowDurationMins] of [
482+
[weeklyPeriod, "weekly", "Weekly", "2026-09-18T03:10:30.159Z", 7 * 24 * 60],
483483
[
484484
{
485485
type: "USAGE_PERIOD_TYPE_MONTHLY",
@@ -489,12 +489,15 @@ describe("Grok usage limits", () => {
489489
"monthly",
490490
"Monthly",
491491
"2026-10-01T00:00:00.000Z",
492+
30 * 24 * 60,
492493
],
493494
] as const) {
494495
const response = { config: { currentPeriod } };
495496
expect
496497
.soft(grokUsageResponseToLimits(response, checkedAt).windows)
497-
.toEqual([{ id: "subscription", kind, label, usedPercent: 0, resetsAt }]);
498+
.toEqual([
499+
{ id: "subscription", kind, label, usedPercent: 0, resetsAt, windowDurationMins },
500+
]);
498501
}
499502
});
500503

@@ -551,6 +554,60 @@ describe("Grok usage limits", () => {
551554
).windows,
552555
).toEqual([{ id: "subscription", kind: "monthly", label: "Monthly", usedPercent: 100 }]);
553556
});
557+
558+
it("measures the window from the reported period bounds", () => {
559+
const window = (type: string, start: string, end: string) =>
560+
grokUsageResponseToLimits(
561+
{ config: { creditUsagePercent: 4, currentPeriod: { type, start, end } } },
562+
checkedAt,
563+
).windows[0];
564+
expect(
565+
window(
566+
"USAGE_PERIOD_TYPE_WEEKLY",
567+
"2026-10-14T20:51:37.352+00:00",
568+
"2026-10-21T20:51:37.352+00:00",
569+
),
570+
).toEqual({
571+
id: "subscription",
572+
kind: "weekly",
573+
label: "Weekly",
574+
usedPercent: 4,
575+
resetsAt: "2026-10-21T20:51:37.352Z",
576+
windowDurationMins: 7 * 24 * 60,
577+
});
578+
expect(
579+
window(
580+
"USAGE_PERIOD_TYPE_MONTHLY",
581+
"2026-02-01T00:00:00.000+00:00",
582+
"2026-03-01T00:00:00.000+00:00",
583+
)?.windowDurationMins,
584+
).toBe(28 * 24 * 60);
585+
});
586+
587+
it("leaves the window length unknown when the period start is unusable", () => {
588+
const end = "2026-10-21T20:51:37.352+00:00";
589+
for (const start of [
590+
undefined,
591+
null,
592+
42,
593+
"invalid",
594+
"2026-02-30T00:00:00.000Z",
595+
end,
596+
"2026-10-28T20:51:37.352+00:00",
597+
]) {
598+
const [window] = grokUsageResponseToLimits(
599+
{
600+
config: {
601+
creditUsagePercent: 4,
602+
currentPeriod: { type: "USAGE_PERIOD_TYPE_WEEKLY", start, end },
603+
},
604+
},
605+
checkedAt,
606+
).windows;
607+
expect(window?.resetsAt).toBe("2026-10-21T20:51:37.352Z");
608+
expect(window).not.toHaveProperty("windowDurationMins");
609+
}
610+
});
554611
});
555612

556613
it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("readGrokAccount", (it) => {

‎packages/provider-cursor/src/server/usageLimits.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@ describe("Cursor usage limits", () => {
4242
);
4343
});
4444

45+
it("reports the billing-cycle length so pace can be computed", () => {
46+
const planUsage = { totalPercentUsed: 40, autoPercentUsed: 30, apiPercentUsed: 20 };
47+
const durations = (billingCycleStart: string | number | undefined, billingCycleEnd: string) =>
48+
cursorUsageResponseToLimits(
49+
{ billingCycleStart, billingCycleEnd, planUsage },
50+
checkedAt,
51+
).windows.map((window) => window.windowDurationMins);
52+
expect(durations("1794045847072", "1796637847072")).toEqual([43_200, 43_200, 43_200]);
53+
expect(durations(1_796_637_847_072, "1799316247072")).toEqual([44_640, 44_640, 44_640]);
54+
});
55+
56+
it("omits the cycle length when the start is missing or invalid", () => {
57+
for (const billingCycleStart of [undefined, "", "0", "not-a-number", "1796637847072"]) {
58+
expect(
59+
cursorUsageResponseToLimits(
60+
{
61+
billingCycleStart,
62+
billingCycleEnd: "1796637847072",
63+
planUsage: { totalPercentUsed: 40 },
64+
},
65+
checkedAt,
66+
).windows,
67+
).toEqual([
68+
{
69+
id: "totalPercentUsed",
70+
kind: "monthly",
71+
label: "Overall",
72+
usedPercent: 40,
73+
resetsAt: "2026-12-07T10:04:07.072Z",
74+
},
75+
]);
76+
}
77+
});
78+
4579
it("does not invent unused allowance for absent buckets", () => {
4680
expect(cursorUsageResponseToLimits({ planUsage: {} }, checkedAt).unavailable?.reason).toBe(
4781
"unsupported",

‎packages/provider-cursor/src/server/usageLimits.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { DEFAULT_CURSOR_API_ENDPOINT, exchangeCursorApiKey } from "./apiKey.ts";
1717

1818
const CursorUsageResponse = Schema.Struct({
19+
billingCycleStart: Schema.optional(Schema.Union([Schema.String, Schema.Number])),
1920
billingCycleEnd: Schema.optional(Schema.Union([Schema.String, Schema.Number])),
2021
planUsage: Schema.optional(
2122
Schema.Struct({
@@ -36,6 +37,11 @@ export function cursorUsageResponseToLimits(
3637
Number(response.billingCycleEnd) > 0 && Option.isSome(reset)
3738
? DateTime.formatIso(reset.value)
3839
: undefined;
40+
const cycleStart = Number(response.billingCycleStart);
41+
const windowDurationMins =
42+
resetsAt && cycleStart > 0
43+
? Math.round((Number(response.billingCycleEnd) - cycleStart) / 60_000)
44+
: 0;
3945
const windows: ServerProviderUsageWindow[] = [];
4046
if (response.planUsage) {
4147
for (const { id, label } of CURSOR_USAGE_WINDOWS) {
@@ -47,6 +53,7 @@ export function cursorUsageResponseToLimits(
4753
label,
4854
usedPercent: clampPercent(usedPercent),
4955
...(resetsAt ? { resetsAt } : {}),
56+
...(windowDurationMins > 0 ? { windowDurationMins } : {}),
5057
});
5158
}
5259
}

‎packages/provider-grok/src/server/usageLimits.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ export function grokUsageResponseToLimits(
6969
const period = response.config?.currentPeriod;
7070
const periodType = period?.type?.replace(/^USAGE_PERIOD_TYPE_/, "");
7171
const kind = periodType === "WEEKLY" ? "weekly" : periodType === "MONTHLY" ? "monthly" : "other";
72+
const start = typeof period?.start === "string" ? parsePeriodBoundary(period.start) : NaN;
73+
const end = parsePeriodBoundary(period?.end);
74+
const windowDurationMins = Math.round((end - start) / 60_000);
7275
let usedPercent = response.config?.creditUsagePercent;
73-
if (
74-
usedPercent === undefined &&
75-
(kind === "weekly" || kind === "monthly") &&
76-
typeof period?.start === "string" &&
77-
parsePeriodBoundary(period?.start) < parsePeriodBoundary(period?.end)
78-
) {
76+
if (usedPercent === undefined && (kind === "weekly" || kind === "monthly") && start < end) {
7977
// Grok omits a zero percentage while retaining the complete subscription period.
8078
usedPercent = 0;
8179
}
@@ -89,6 +87,7 @@ export function grokUsageResponseToLimits(
8987
label: kind === "weekly" ? "Weekly" : kind === "monthly" ? "Monthly" : "Subscription",
9088
usedPercent: clampPercent(usedPercent),
9189
...(Option.isSome(reset) ? { resetsAt: DateTime.formatIso(reset.value) } : {}),
90+
...(windowDurationMins > 0 ? { windowDurationMins } : {}),
9291
};
9392
return makeUsageLimits({ checkedAt, windows: [window] });
9493
}

0 commit comments

Comments
 (0)