fix(usage): rebuild corrupt per-source usage cache records instead of failing every scan (#42418)

A null or non-object entry in a per-source usage sidecar made the Codex,
Muse and OpenCode scans throw, and since a failed scan never rewrites the
sidecar the failure repeated on every refresh. Records missing their
sessions/dailyAggregates projections were either reused as zero usage
(Muse/OpenCode) or crashed the Codex reuse and incremental-resume paths.

- readUsageSourceCache drops entries that are not plain objects.
- Muse and OpenCode normalizers drop records lacking projection arrays so
  the source is reparsed.
- Codex requires both projection arrays before reusing or resuming a
  cached rollout record.

Fixes #42413

Co-authored-by: Neil <neil@stably.ai>
This commit is contained in:
Shani Singh
2026-11-14 14:23:59 -08:00
committed by GitHub
co-authored by Neil
parent 5ddac03e1b
commit 38c0c22b2e
7 changed files with 95 additions and 19 deletions
@@ -8,11 +8,18 @@ import type {
export function normalizeOpenCodeUsagePersistedDatabases(
databases: OpenCodeUsagePersistedDatabase[] | undefined
): OpenCodeUsagePersistedDatabase[] {
return (databases ?? []).map((database) => ({
...database,
sessions: (database.sessions ?? []).map(normalizeSessionCost),
dailyAggregates: (database.dailyAggregates ?? []).map(normalizeDailyAggregateCost)
}))
// A record missing a projection would be reused as zero usage; drop it so the scanner rereads the database.
return (databases ?? []).flatMap((database) =>
Array.isArray(database.sessions) && Array.isArray(database.dailyAggregates)
? [
{
...database,
sessions: database.sessions.map(normalizeSessionCost),
dailyAggregates: database.dailyAggregates.map(normalizeDailyAggregateCost)
}
]
: []
)
}
export function normalizeDailyAggregateCost(