Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions apps/server/src/provider/claudeUsageLimits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,65 @@ describe("claudeUsageResponseToLimits", () => {
});

describe("claudeRateLimitEventToUpdate", () => {
it("updates supported unified windows, omitting unnamed and malformed buckets", () => {
const info = {
status: "allowed" as const,
unifiedWindows: {
five_hour: { utilization: 0.25, resetsAt: 1_900_000_000 },
seven_day: { utilization: 0.5, resetsAt: 1_900_000_001 },
seven_day_overage_included: { utilization: 0.2, resetsAt: 1_900_000_002 },
unknown: { utilization: 1 },
constructor: { utilization: 1 },
toString: { utilization: 1 },
},
};
expect(
claudeRateLimitEventToUpdate(info, { overageIncluded: "Fable" })?.windows.map(
({ id, usedPercent, resetsAt }) => ({ id, usedPercent, resetsAt }),
),
).toEqual([
{ id: "five_hour", usedPercent: 25, resetsAt: "2030-03-17T17:46:40.000Z" },
{ id: "seven_day", usedPercent: 50, resetsAt: "2030-03-17T17:46:41.000Z" },
{ id: "seven_day_fable", usedPercent: 20, resetsAt: "2030-03-17T17:46:42.000Z" },
]);
expect(claudeRateLimitEventToUpdate(info, noNames)?.windows.map(({ id }) => id)).toEqual([
"five_hour",
"seven_day",
]);
const malformed = {
status: "allowed" as const,
unifiedWindows: {
five_hour: { utilization: NaN },
seven_day: { utilization: -1 },
seven_day_overage_included: null,
},
};
expect(claudeRateLimitEventToUpdate(malformed, { overageIncluded: "Fable" })).toBeUndefined();
});

it("keeps other unified windows when the legacy window takes precedence", () => {
const info = {
status: "allowed" as const,
rateLimitType: "five_hour" as const,
utilization: 0.75,
resetsAt: 1_900_000_010,
unifiedWindows: {
five_hour: { utilization: 0.25, resetsAt: 1_900_000_000 },
seven_day: { utilization: 0.5, resetsAt: 1_900_000_001 },
},
};
expect(
claudeRateLimitEventToUpdate(info, noNames)?.windows.map(({ id, usedPercent, resetsAt }) => ({
id,
usedPercent,
resetsAt,
})),
).toEqual([
{ id: "five_hour", usedPercent: 75, resetsAt: "2030-03-17T17:46:50.000Z" },
{ id: "seven_day", usedPercent: 50, resetsAt: "2030-03-17T17:46:41.000Z" },
]);
});

it("scales the 0–1 utilization and epoch-second reset onto the probe's window id", () => {
expect(
claudeRateLimitEventToUpdate(
Expand Down
34 changes: 23 additions & 11 deletions apps/server/src/provider/claudeUsageLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,31 @@ export function claudeRateLimitEventToUpdate(
info: SDKRateLimitInfo,
names: ClaudeScopedLimitNames,
): ProviderUsageLimitsUpdate | undefined {
const type: string | undefined = info.rateLimitType;
if (!type || typeof info.utilization !== "number") {
return undefined;
}
const usedPercent = info.utilization * 100;
const resetsAt = isoFromEpochSeconds(info.resetsAt);
if (type in WINDOWS) {
return { windows: [makeWindow(type, usedPercent, resetsAt)] };
const windows = new Map<string, ServerProviderUsageWindow>();
const add = (type: string, utilization: number, resetsAt: number | undefined) => {
if (!Number.isFinite(utilization) || utilization < 0) return;
const reset = isoFromEpochSeconds(resetsAt);
if (Object.hasOwn(WINDOWS, type)) {
windows.set(type, makeWindow(type, utilization * 100, reset));
} else if (type === OVERAGE_INCLUDED_EVENT_TYPE && names.overageIncluded) {
const window = scopedWindow(names.overageIncluded, utilization * 100, reset);
windows.set(window.id, window);
}
};
const unified = (info as { readonly unifiedWindows?: unknown }).unifiedWindows;
if (typeof unified === "object" && unified !== null && !Array.isArray(unified)) {
for (const [type, entry] of Object.entries(unified)) {
if (typeof entry !== "object" || entry === null) continue;
const { utilization, resetsAt } = entry as { utilization?: unknown; resetsAt?: unknown };
if (typeof utilization === "number") {
add(type, utilization, typeof resetsAt === "number" ? resetsAt : undefined);
}
}
}
if (type === OVERAGE_INCLUDED_EVENT_TYPE && names.overageIncluded) {
return { windows: [scopedWindow(names.overageIncluded, usedPercent, resetsAt)] };
if (info.rateLimitType && typeof info.utilization === "number") {
add(info.rateLimitType, info.utilization, info.resetsAt);
}
return undefined;
return windows.size === 0 ? undefined : { windows: [...windows.values()] };
}

/**
Expand Down
Loading