fix(usage): compute cost fallback lazily to stop event-loop stall

normalizeCliproxyUsageHistoryDetail passed calculateHistoryDetailCost (a
~6ms model-pricing lookup) as an eager default argument to
normalizePersistedNumber, so it ran for every record even when a persisted
cost was already present. A few thousand records turned into a multi-second
synchronous stall that wedged the bar summary aggregator and the dashboard
startup prewarm. Compute the fallback only when cost is actually missing.
This commit is contained in:
Tam Nhu Tran
2026-06-08 09:30:55 -04:00
parent 5c3b92971b
commit f0734348d0
@@ -151,16 +151,20 @@ export function normalizeCliproxyUsageHistoryDetail(
const outputTokens = normalizePersistedNumber(candidate.outputTokens);
const cacheReadTokens = normalizePersistedNumber(candidate.cacheReadTokens);
const requestCount = Math.max(1, normalizePersistedNumber(candidate.requestCount, 1));
const cost = normalizePersistedNumber(
candidate.cost,
calculateHistoryDetailCost(
candidate.model,
provider,
inputTokens,
outputTokens,
cacheReadTokens
)
);
// Compute the cost fallback lazily. calculateHistoryDetailCost is ~6ms/call
// (model-pricing lookup); passing it as an eager default argument ran it for
// every record even when a persisted cost was already present, turning a few
// thousand records into a multi-second event-loop stall.
const cost =
typeof candidate.cost === 'number' && Number.isFinite(candidate.cost)
? candidate.cost
: calculateHistoryDetailCost(
candidate.model,
provider,
inputTokens,
outputTokens,
cacheReadTokens
);
const accountId =
typeof candidate.accountId === 'string' && candidate.accountId.length > 0