Repository navigation
[Duplicate Code] budgetResult fields merge and cache-read rollup warning duplicated in HTTP and WebSocket token trackers #4711
Copy link
Copy link
Closed
Labels
Description
Duplicate Code Opportunity
Summary
- Pattern: Two distinct multi-line blocks are copy-pasted verbatim between the HTTP and WebSocket token-tracker modules: (1) the
token_cache_read_rollup_mismatchwarning block (~18 lines each), and (2) thebudgetResultfield-merge block that copieseffective_tokens_this_response,effective_tokens_total,model_multiplier,ai_credits_this_response, andai_credits_totalonto the usage record (~15 lines each). Together these represent ~66 duplicate lines across the two files. - Locations:
containers/api-proxy/token-tracker-http.jslines 251–269 (cache warning) and lines 297–312 (budget fields)containers/api-proxy/token-tracker-ws.jslines 203–219 (cache warning) and lines 246–261 (budget fields)
- Impact: Adding a new budget field (e.g.
ai_credits_cached_input) or changing the mismatch-warning log structure requires the same edit in both files. Prior art: a similar finding ([Duplicate Code] token_budget_usage billing log block duplicated in HTTP and WebSocket response handlers #4497) was resolved for thetoken_budget_usagebilling log block; these two blocks were introduced after that fix and were not extracted.
Evidence
Cache-read rollup mismatch warning — HTTP (lines 251–268)
if (observedCacheReadTokens > 0 && normalized.cache_read_tokens === 0) {
logRequest('warn', 'token_cache_read_rollup_mismatch', {
request_id: requestId,
provider,
model: model || 'unknown',
observed_cache_read_tokens: observedCacheReadTokens,
rolled_up_cache_read_tokens: normalized.cache_read_tokens,
streaming,
});
diag('CACHE_READ_ROLLUP_MISMATCH', {
request_id: requestId,
provider,
model: model || 'unknown',
observed_cache_read_tokens: observedCacheReadTokens,
rolled_up_cache_read_tokens: normalized.cache_read_tokens,
streaming,
});
}Cache-read rollup mismatch warning — WS (lines 203–219, identical except streaming: true, transport: 'websocket')
if (observedCacheReadTokens > 0 && normalized.cache_read_tokens === 0) {
logRequest('warn', 'token_cache_read_rollup_mismatch', { ..., streaming: true, transport: 'websocket' });
diag('CACHE_READ_ROLLUP_MISMATCH', { ..., streaming: true, transport: 'websocket' });
}Budget-fields merge — HTTP (lines 297–312)
if (budgetResult) {
if (budgetResult.effective_tokens_this_response != null)
record.effective_tokens_this_response = budgetResult.effective_tokens_this_response;
if (budgetResult.effective_tokens_total != null)
record.effective_tokens_total = budgetResult.effective_tokens_total;
if (budgetResult.model_multiplier != null)
record.model_multiplier = budgetResult.model_multiplier;
if (budgetResult.ai_credits_this_response != null)
record.ai_credits_this_response = budgetResult.ai_credits_this_response;
if (budgetResult.ai_credits_total != null)
record.ai_credits_total = budgetResult.ai_credits_total;
}Budget-fields merge — WS (lines 246–261, byte-for-byte identical)
if (budgetResult) {
if (budgetResult.effective_tokens_this_response != null)
record.effective_tokens_this_response = budgetResult.effective_tokens_this_response;
// ... (identical)
}Suggested Refactoring
Add two small helpers to containers/api-proxy/token-tracker-shared.js (new file) or directly to token-parsers.js:
/**
* Copies all non-null budgetResult fields onto the usage log record.
* `@param` {object} record - mutable token usage record
* `@param` {object|undefined} budgetResult - return value of the onUsage callback
*/
function applyBudgetResultToRecord(record, budgetResult) {
if (!budgetResult) return;
const fields = ['effective_tokens_this_response', 'effective_tokens_total',
'model_multiplier', 'ai_credits_this_response', 'ai_credits_total'];
for (const field of fields) {
if (budgetResult[field] != null) record[field] = budgetResult[field];
}
}
/**
* Logs a warning when the rolling cache-read max is higher than the final
* rolled-up value, which indicates a streaming aggregation mismatch.
*/
function warnCacheReadRollupMismatch({ requestId, provider, model, observedCacheReadTokens, normalized, streaming, transport }) {
if (observedCacheReadTokens <= 0 || normalized.cache_read_tokens !== 0) return;
const fields = { request_id: requestId, provider, model: model || 'unknown',
observed_cache_read_tokens: observedCacheReadTokens,
rolled_up_cache_read_tokens: 0, streaming };
if (transport) fields.transport = transport;
logRequest('warn', 'token_cache_read_rollup_mismatch', fields);
diag('CACHE_READ_ROLLUP_MISMATCH', fields);
}Both token-tracker-http.js and token-tracker-ws.js then call these helpers, eliminating the duplication. The HTTP tracker passes no transport; the WS tracker passes transport: 'websocket'.
Affected Files
containers/api-proxy/token-tracker-http.js— lines 251–269 and 297–312containers/api-proxy/token-tracker-ws.js— lines 203–219 and 246–261containers/api-proxy/token-tracker-shared.js(new) orcontainers/api-proxy/token-parsers.js— helpers to add
Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-06-10
Generated by Duplicate Code Detector · 670.7 AIC · ⊞ 26.9K · ◷
- expires on Jul 10, 2026, 10:29 PM UTC
Activity
refactor: extract shared token-tracker budget helpers (#4780)