The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 23:30 UTC
fix(usage): Grok and Cursor usage limits show pace and the even-spend line (#21897)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Jaewook Oh committed
Nov 11, 2026 at 18:21 UTC
e2a781477afd2aea1741f720afa9067294e7a1b7
4 files changed
+106
-9
apps/server/src/provider/GrokProvider.test.ts
+60
-3
@@ -478,8 +478,8 @@ describe("Grok usage limits", () => {
478
};
479
480
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],
483
[
484
{
485
type: "USAGE_PERIOD_TYPE_MONTHLY",
@@ -489,12 +489,15 @@ describe("Grok usage limits", () => {
489
"monthly",
490
"Monthly",
491
"2026-10-01T00:00:00.000Z",
492
+ 30 * 24 * 60,
493
],
494
] as const) {
495
const response = { config: { currentPeriod } };
496
expect
497
.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
+ ]);
501
}
502
});
503
@@ -551,6 +554,60 @@ describe("Grok usage limits", () => {
554
).windows,
555
).toEqual([{ id: "subscription", kind: "monthly", label: "Monthly", usedPercent: 100 }]);
556
});
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
+ });
611
});
612
613
it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("readGrokAccount", (it) => {
packages/provider-cursor/src/server/usageLimits.test.ts
+34
@@ -42,6 +42,40 @@ describe("Cursor usage limits", () => {
42
);
43
});
44
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
+
79
it("does not invent unused allowance for absent buckets", () => {
80
expect(cursorUsageResponseToLimits({ planUsage: {} }, checkedAt).unavailable?.reason).toBe(
81
"unsupported",
packages/provider-cursor/src/server/usageLimits.ts
+7
@@ -16,6 +16,7 @@ import {
16
import { DEFAULT_CURSOR_API_ENDPOINT, exchangeCursorApiKey } from "./apiKey.ts";
17
18
const CursorUsageResponse = Schema.Struct({
19
+ billingCycleStart: Schema.optional(Schema.Union([Schema.String, Schema.Number])),
20
billingCycleEnd: Schema.optional(Schema.Union([Schema.String, Schema.Number])),
21
planUsage: Schema.optional(
22
Schema.Struct({
@@ -36,6 +37,11 @@ export function cursorUsageResponseToLimits(
37
Number(response.billingCycleEnd) > 0 && Option.isSome(reset)
38
? DateTime.formatIso(reset.value)
39
: 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;
45
const windows: ServerProviderUsageWindow[] = [];
46
if (response.planUsage) {
47
for (const { id, label } of CURSOR_USAGE_WINDOWS) {
@@ -47,6 +53,7 @@ export function cursorUsageResponseToLimits(
53
label,
54
usedPercent: clampPercent(usedPercent),
55
...(resetsAt ? { resetsAt } : {}),
56
+ ...(windowDurationMins > 0 ? { windowDurationMins } : {}),
57
});
58
}
59
}
packages/provider-grok/src/server/usageLimits.ts
+5
-6
@@ -69,13 +69,11 @@ export function grokUsageResponseToLimits(
69
const period = response.config?.currentPeriod;
70
const periodType = period?.type?.replace(/^USAGE_PERIOD_TYPE_/, "");
71
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);
75
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) {
77
// Grok omits a zero percentage while retaining the complete subscription period.
78
usedPercent = 0;
79
}
@@ -89,6 +87,7 @@ export function grokUsageResponseToLimits(
87
label: kind === "weekly" ? "Weekly" : kind === "monthly" ? "Monthly" : "Subscription",
88
usedPercent: clampPercent(usedPercent),
89
...(Option.isSome(reset) ? { resetsAt: DateTime.formatIso(reset.value) } : {}),
90
+ ...(windowDurationMins > 0 ? { windowDurationMins } : {}),
91
};
92
return makeUsageLimits({ checkedAt, windows: [window] });
93
}