Skip to content

[Duplicate Code] budgetResult fields merge and cache-read rollup warning duplicated in HTTP and WebSocket token trackers #4711

Closed

Description

@github-actions

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_mismatch warning block (~18 lines each), and (2) the budgetResult field-merge block that copies effective_tokens_this_response, effective_tokens_total, model_multiplier, ai_credits_this_response, and ai_credits_total onto the usage record (~15 lines each). Together these represent ~66 duplicate lines across the two files.
  • Locations:
    • containers/api-proxy/token-tracker-http.js lines 251–269 (cache warning) and lines 297–312 (budget fields)
    • containers/api-proxy/token-tracker-ws.js lines 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 the token_budget_usage billing 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–312
  • containers/api-proxy/token-tracker-ws.js — lines 203–219 and 246–261
  • containers/api-proxy/token-tracker-shared.js (new) or containers/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

  1. added a commit that references this issue on Jun 11, 2026
    bffaf07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions