Skip to content

Commit 02da6cc

Browse files
authored
Merge pull request #1871 from yansigit/codex/command-code-zero-reset-at
fix(quota): ignore Command Code epoch-zero reset timestamps
2 parents 9830ab1 + 1c838d2 commit 02da6cc

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

‎src/providers/quota.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,27 @@ function providerLabel(providerId: string): string {
237237
}
238238

239239
function normalizeResetAt(value: unknown): number | undefined {
240-
if (typeof value === "number" && Number.isFinite(value)) return value > 10_000_000_000 ? value : value * 1000;
240+
if (typeof value === "number" && Number.isFinite(value)) return epochMillis(value);
241241
if (typeof value === "string" && value.trim()) {
242242
const trimmed = value.trim();
243243
// Cursor Connect RPC returns billingCycleEnd as a unix-ms decimal string ("1771077734000").
244244
// Date.parse treats that as invalid; numeric epoch strings must be handled explicitly.
245-
if (/^\d+(\.\d+)?$/.test(trimmed)) {
245+
if (/^[+-]?\d+(\.\d+)?$/.test(trimmed)) {
246246
const numeric = Number(trimmed);
247-
if (Number.isFinite(numeric)) return numeric > 10_000_000_000 ? numeric : numeric * 1000;
247+
return epochMillis(numeric);
248248
}
249249
const parsed = Date.parse(trimmed);
250-
return Number.isFinite(parsed) ? parsed : undefined;
250+
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
251251
}
252252
return undefined;
253253
}
254254

255+
/** Unix 0 / negative values are sentinels, not reset clocks (Command Code fiveHour.resetAt: 0). */
256+
function epochMillis(value: number): number | undefined {
257+
if (!Number.isFinite(value) || value <= 0) return undefined;
258+
return value > 10_000_000_000 ? value : value * 1000;
259+
}
260+
255261
function toFiniteNumber(value: unknown): number | undefined {
256262
if (typeof value === "number" && Number.isFinite(value)) return value;
257263
if (typeof value === "string" && value.trim()) {

‎tests/command-code-quota.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,66 @@ describe("Command Code provider quota", () => {
118118
expect(JSON.stringify(result)).not.toContain("commandcode-secret");
119119
});
120120

121+
test("omits a zero Command Code window reset instead of treating it as the Unix epoch", async () => {
122+
globalThis.fetch = (async (input: RequestInfo | URL) => {
123+
const url = String(input);
124+
if (url === "https://api.commandcode.ai/alpha/whoami"
125+
|| url === "https://api.commandcode.ai/alpha/billing/subscriptions"
126+
|| url === "https://api.commandcode.ai/alpha/usage/summary") {
127+
return new Response("down", { status: 500 });
128+
}
129+
if (url !== "https://api.commandcode.ai/alpha/billing/credits") {
130+
throw new Error(`unexpected Command Code quota probe: ${url}`);
131+
}
132+
return new Response(JSON.stringify({
133+
windowLimits: {
134+
fiveHour: { cap: 14, used: 0, resetAt: 0 },
135+
weekly: { cap: 35, used: 34.9522404823, resetAt: 1_787_184_801_319 },
136+
},
137+
}), { status: 200, headers: { "content-type": "application/json" } });
138+
}) as typeof fetch;
139+
140+
const result = await fetchProviderQuotaReports(commandCodeConfig(), true);
141+
142+
expect(result.reports[0]?.quota).toEqual({
143+
fiveHourPercent: 0,
144+
weeklyPercent: 99.86354423514285,
145+
weeklyResetAt: 1_787_184_801_319,
146+
updatedAt: expect.any(Number),
147+
});
148+
expect(result.reports[0]?.quota).not.toHaveProperty("fiveHourResetAt");
149+
});
150+
151+
test("omits a signed numeric Command Code window reset instead of Date.parse-ing it", async () => {
152+
globalThis.fetch = (async (input: RequestInfo | URL) => {
153+
const url = String(input);
154+
if (url === "https://api.commandcode.ai/alpha/whoami"
155+
|| url === "https://api.commandcode.ai/alpha/billing/subscriptions"
156+
|| url === "https://api.commandcode.ai/alpha/usage/summary") {
157+
return new Response("down", { status: 500 });
158+
}
159+
if (url !== "https://api.commandcode.ai/alpha/billing/credits") {
160+
throw new Error(`unexpected Command Code quota probe: ${url}`);
161+
}
162+
return new Response(JSON.stringify({
163+
windowLimits: {
164+
fiveHour: { cap: 14, used: 0, resetAt: "-1" },
165+
weekly: { cap: 35, used: 34.9522404823, resetAt: 1_787_184_801_319 },
166+
},
167+
}), { status: 200, headers: { "content-type": "application/json" } });
168+
}) as typeof fetch;
169+
170+
const result = await fetchProviderQuotaReports(commandCodeConfig(), true);
171+
172+
expect(result.reports[0]?.quota).toEqual({
173+
fiveHourPercent: 0,
174+
weeklyPercent: 99.86354423514285,
175+
weeklyResetAt: 1_787_184_801_319,
176+
updatedAt: expect.any(Number),
177+
});
178+
expect(result.reports[0]?.quota).not.toHaveProperty("fiveHourResetAt");
179+
});
180+
121181
test("keeps rolling windows when whoami and usage summary fail", async () => {
122182
const seen: string[] = [];
123183
globalThis.fetch = (async (input: RequestInfo | URL) => {

0 commit comments

Comments
 (0)