fix: read Claude usage utilization as a percent, never a fraction
The status dashboard showed Claude 5h at 100% right after a reset. The /api/oauth/usage payload at 2026-11-04 15:01 KST was five_hour=1 (1%), seven_day=18, but toPct() treated any value <= 1 as a 0-1 fraction and multiplied it by 100. A freshly reset window passes through 1% (and often 1.0 exactly), so the dashboard read 100% every time that happened. The earlier rollover fixes (91203d8,97cc670) addressed stale values around resets_at and never touched this conversion. The same heuristic was in getCachedClaudeUsageLimitResetAt, so a 1% window also counted as exhausted when picking a usage-limit resume time. The API always returns 0-100 (cache history: 0, 1, 12, 18, 53, 99), so both places now use the value as a percent. Tests that used fractional fixtures (0.2/0.4/0.7) now use percents; new tests cover 1 -> 1%, 0.4 -> 0%, 100 -> 100%, and a 1% window not counted as exhausted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -246,7 +246,7 @@ describe('getCachedClaudeUsageLimitResetAt', () => {
|
||||
fetchedAt: 1,
|
||||
usage: {
|
||||
five_hour: { utilization: 100, resets_at: '2026-10-01T12:00:00Z' },
|
||||
seven_day: { utilization: 0.4, resets_at: '2026-10-05T00:00:00Z' },
|
||||
seven_day: { utilization: 40, resets_at: '2026-10-05T00:00:00Z' },
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -257,6 +257,23 @@ describe('getCachedClaudeUsageLimitResetAt', () => {
|
||||
).toEqual(new Date('2026-10-01T12:00:00Z'));
|
||||
});
|
||||
|
||||
it('does not read a 1% window as exhausted', async () => {
|
||||
readJsonFileMock.mockReturnValue({
|
||||
'account-0': {
|
||||
fetchedAt: 1,
|
||||
usage: {
|
||||
five_hour: { utilization: 1, resets_at: '2026-10-01T12:00:00Z' },
|
||||
seven_day: { utilization: 0.5, resets_at: '2026-10-05T00:00:00Z' },
|
||||
},
|
||||
},
|
||||
});
|
||||
const { getCachedClaudeUsageLimitResetAt } =
|
||||
await import('./claude-usage.js');
|
||||
expect(
|
||||
getCachedClaudeUsageLimitResetAt(Date.parse('2026-10-01T10:00:00Z')),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when no window is exhausted', async () => {
|
||||
readJsonFileMock.mockReturnValue({
|
||||
'account-0': {
|
||||
|
||||
@@ -21,6 +21,11 @@ const USAGE_CACHE_FILE = path.join(DATA_DIR, 'claude-usage-cache.json');
|
||||
|
||||
const PROFILE_ENDPOINT = 'https://api.anthropic.com/api/oauth/profile';
|
||||
|
||||
/**
|
||||
* `utilization` is a percentage (0-100) exactly as /api/oauth/usage returns
|
||||
* it, e.g. 1 means 1%. It is never a 0-1 fraction: reading values <= 1 as a
|
||||
* fraction showed a fresh window at 1% as 100% on the dashboard.
|
||||
*/
|
||||
export interface ClaudeUsageData {
|
||||
five_hour?: { utilization: number; resets_at: string };
|
||||
seven_day?: { utilization: number; resets_at: string };
|
||||
@@ -115,10 +120,6 @@ function tokenSuffix(token: string): string {
|
||||
return token.slice(-8);
|
||||
}
|
||||
|
||||
function utilizationPct(utilization: number): number {
|
||||
return utilization > 1 ? utilization : utilization * 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset time of the exhausted usage window (5h or 7d, whichever ends later)
|
||||
* from the last successful usage fetch, or null when no cached window is at
|
||||
@@ -131,7 +132,7 @@ export function getCachedClaudeUsageLimitResetAt(
|
||||
const usage = usageDiskCache[USAGE_CACHE_KEY]?.usage;
|
||||
let latest: number | null = null;
|
||||
for (const window of [usage?.five_hour, usage?.seven_day]) {
|
||||
if (!window || utilizationPct(window.utilization) < 100) continue;
|
||||
if (!window || window.utilization < 100) continue;
|
||||
const resetMs = Date.parse(window.resets_at);
|
||||
if (!Number.isFinite(resetMs) || resetMs <= now) continue;
|
||||
if (latest == null || resetMs > latest) latest = resetMs;
|
||||
|
||||
@@ -95,11 +95,11 @@ describe('dashboard Claude usage rows', () => {
|
||||
usage: {
|
||||
// Future resets: a passed reset now reads as a rolled-over (0%) window.
|
||||
five_hour: {
|
||||
utilization: 0.4,
|
||||
utilization: 40,
|
||||
resets_at: new Date(Date.now() + 3_600_000).toISOString(),
|
||||
},
|
||||
seven_day: {
|
||||
utilization: 0.7,
|
||||
utilization: 70,
|
||||
resets_at: new Date(Date.now() + 86_400_000).toISOString(),
|
||||
},
|
||||
},
|
||||
@@ -111,6 +111,21 @@ describe('dashboard Claude usage rows', () => {
|
||||
expect(rows[0]).toMatchObject({ name: 'Claude max', h5pct: 40, d7pct: 70 });
|
||||
});
|
||||
|
||||
it('reads utilization as a percentage, so a fresh window at 1% is not 100%', () => {
|
||||
const resets = (ms: number) => new Date(Date.now() + ms).toISOString();
|
||||
const rowFor = (h5: number, d7: number) =>
|
||||
buildClaudeUsageRows({
|
||||
usage: {
|
||||
five_hour: { utilization: h5, resets_at: resets(3_600_000) },
|
||||
seven_day: { utilization: d7, resets_at: resets(86_400_000) },
|
||||
},
|
||||
})[0];
|
||||
// Live API payload of 2026-11-04 15:01 KST: five_hour 1, seven_day 18.
|
||||
expect(rowFor(1, 18)).toMatchObject({ h5pct: 1, d7pct: 18 });
|
||||
expect(rowFor(0, 0.4)).toMatchObject({ h5pct: 0, d7pct: 0 });
|
||||
expect(rowFor(0.6, 100)).toMatchObject({ h5pct: 1, d7pct: 100 });
|
||||
});
|
||||
|
||||
it('renders nothing without a usage snapshot', () => {
|
||||
expect(buildClaudeUsageRows(null)).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -68,12 +68,6 @@ export function mergeClaudeDashboardUsage(
|
||||
};
|
||||
}
|
||||
|
||||
function toPct(utilization: number): number {
|
||||
return utilization > 1
|
||||
? Math.round(utilization)
|
||||
: Math.round(utilization * 100);
|
||||
}
|
||||
|
||||
export function buildClaudeUsageRows(
|
||||
snapshot: ClaudeUsageSnapshot | null,
|
||||
): UsageRow[] {
|
||||
@@ -84,9 +78,13 @@ export function buildClaudeUsageRows(
|
||||
return [
|
||||
{
|
||||
name: profile ? `Claude ${profile.planType}` : 'Claude',
|
||||
h5pct: h5 ? currentWindowPct(toPct(h5.utilization), h5.resets_at) : -1,
|
||||
h5pct: h5
|
||||
? currentWindowPct(Math.round(h5.utilization), h5.resets_at)
|
||||
: -1,
|
||||
h5reset: h5 ? formatResetRemaining(h5.resets_at) : '',
|
||||
d7pct: d7 ? currentWindowPct(toPct(d7.utilization), d7.resets_at) : -1,
|
||||
d7pct: d7
|
||||
? currentWindowPct(Math.round(d7.utilization), d7.resets_at)
|
||||
: -1,
|
||||
d7reset: d7 ? formatResetRemaining(d7.resets_at) : '',
|
||||
error: snapshot.usageRateLimited ? '429' : undefined,
|
||||
},
|
||||
|
||||
@@ -189,11 +189,11 @@ describe('buildWebUsageRowsForSnapshot', () => {
|
||||
claudeUsage: {
|
||||
usage: {
|
||||
five_hour: {
|
||||
utilization: 0.2,
|
||||
utilization: 20,
|
||||
resets_at: '2026-04-26T12:00:00.000Z',
|
||||
},
|
||||
seven_day: {
|
||||
utilization: 0.4,
|
||||
utilization: 40,
|
||||
resets_at: '2026-04-27T12:00:00.000Z',
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user