fix(usage): render codex usage when rate-limit response has null secondary window

Some Codex plans (e.g. Plus) return only a weekly window in `primary` with
`secondary: null`. applyCodexUsageToAccount dereferenced `secondary.usedPercent`
unconditionally, throwing a TypeError that refreshActiveCodexUsage swallowed at
debug level, so usage never applied and the dashboard row stayed blank (-1).

Guard the null secondary, and when a lone window is weekly (windowDurationMins
> 1440) route it into the 7d slot leaving 5h unknown. Accounts that report both
windows are unchanged. Adds a regression test for the secondary:null case.
This commit is contained in:
Codex
2026-07-24 23:15:12 +09:00
parent a9095d95a8
commit 6ab38ca461
2 changed files with 68 additions and 8 deletions

View File

@@ -157,6 +157,48 @@ describe('codex-usage-collector fallback account usage', () => {
]);
});
it('handles a lone weekly window (secondary: null) without throwing and routes it to 7d', async () => {
createDefaultCodexAuth(tempHome);
const childProcess = await import('child_process');
vi.mocked(childProcess.spawn).mockImplementation(((
_cmd: string,
_args: readonly string[] | undefined,
_opts?: { env?: Record<string, string> },
) => {
// Some plans (e.g. Plus) report only a weekly window in `primary` with
// `secondary: null`. This previously threw on `secondary.usedPercent`.
return createFakeChildProcess({
codex: {
limitName: 'Codex',
primary: {
usedPercent: 10,
windowDurationMins: 10080, // 7 days
resetsAt: new Date(Date.now() + 4 * 86_400_000).toISOString(),
},
secondary: null,
},
}) as never;
}) as unknown as typeof childProcess.spawn);
const rotation = await import('./codex-token-rotation.js');
const usage = await import('./codex-usage-collector.js');
rotation.initCodexTokenRotation();
const result = await usage.refreshActiveCodexUsage();
// Usage must be applied (not swallowed by a TypeError), and the lone weekly
// window is shown as 7d while the missing 5h window renders as unknown.
expect(result.fetchedAt).toEqual(expect.any(String));
expect(result.rows).toEqual([
expect.objectContaining({
name: 'Codex',
h5pct: -1,
d7pct: 10,
}),
]);
});
it('finds codex via ~/.hermes/node/bin when running under bun', async () => {
createDefaultCodexAuth(tempHome);
const childProcess = await import('child_process');