From 6df4cc6f734b87792a98af3bbc21388931c76165 Mon Sep 17 00:00:00 2001 From: luvs01 <27862058+luvs01@users.noreply.github.com> Date: Sun, 16 Aug 2026 19:28:09 +0900 Subject: [PATCH] fix(usage): recompute normalized estimated totals --- src/server/request-log.ts | 14 +++++-- tests/request-log-estimate-cap.test.ts | 51 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/server/request-log.ts b/src/server/request-log.ts index f4c4df6626..c69ea2a110 100644 --- a/src/server/request-log.ts +++ b/src/server/request-log.ts @@ -1004,10 +1004,14 @@ function finalizedUsage( const usageFallback = !finalUsage && estimate !== undefined ? { inputTokens: estimate, outputTokens: 0, estimated: true } : undefined; - const loggedUsage = finalUsage && estimate !== undefined + const combinedInputTokens = finalUsage && estimate !== undefined + ? Math.max(finalUsage.inputTokens, estimate) + : undefined; + const loggedUsage = finalUsage && combinedInputTokens !== undefined ? { ...finalUsage, - inputTokens: Math.max(finalUsage.inputTokens, estimate), + inputTokens: combinedInputTokens, + totalTokens: combinedInputTokens + finalUsage.outputTokens, estimated: true, } : finalUsage @@ -1017,7 +1021,11 @@ function finalizedUsage( // ESTIMATE via capEstimateAtContextWindow, and Math.max preserves a real // provider-reported count, so it needs no further reduction. ? (finalUsage.estimated && contextWindow !== undefined && finalUsage.inputTokens > contextWindow - ? { ...finalUsage, inputTokens: contextWindow } + ? { + ...finalUsage, + inputTokens: contextWindow, + totalTokens: contextWindow + finalUsage.outputTokens, + } : finalUsage) : usageFallback; const totalTokens = usageTotalTokens(loggedUsage); diff --git a/tests/request-log-estimate-cap.test.ts b/tests/request-log-estimate-cap.test.ts index b51f7aeb6b..27b7abed2d 100644 --- a/tests/request-log-estimate-cap.test.ts +++ b/tests/request-log-estimate-cap.test.ts @@ -31,6 +31,57 @@ describe("request log token-estimate context-window cap (codex-router PR #140)", expect(attempt.usage?.inputTokens).toBe(100_000); }); + test("capping adapter-estimated input recomputes an explicit total", () => { + const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); + finishRequestAttempt(attempt, 200, 10, { + inputTokens: 200_000, + outputTokens: 4, + totalTokens: 200_004, + estimated: true, + }); + expect(attempt.usage).toEqual({ + inputTokens: DEEPSEEK_WINDOW, + outputTokens: 4, + totalTokens: DEEPSEEK_WINDOW + 4, + estimated: true, + }); + expect(attempt.totalTokens).toBe(DEEPSEEK_WINDOW + 4); + }); + + test("combining a local estimate recomputes the nested and outer totals", () => { + const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); + noteAttemptSend(attempt, 200_000); + finishRequestAttempt(attempt, 200, 10, { + inputTokens: 50, + outputTokens: 4, + totalTokens: 54, + }); + expect(attempt.usage).toEqual({ + inputTokens: DEEPSEEK_WINDOW, + outputTokens: 4, + totalTokens: DEEPSEEK_WINDOW + 4, + estimated: true, + }); + expect(attempt.totalTokens).toBe(DEEPSEEK_WINDOW + 4); + }); + + test("caps an estimated Cursor checkpoint without double-adding output", () => { + const attempt = beginRequestAttempt(1, "cursor", "claude-4.6-opus-high", "cursor"); + finishRequestAttempt(attempt, 200, 10, { + inputTokens: 499_994, + outputTokens: 6, + totalTokens: 500_000, + estimated: true, + }); + expect(attempt.usage).toEqual({ + inputTokens: CURSOR_CLAUDE_WINDOW, + outputTokens: 6, + totalTokens: CURSOR_CLAUDE_WINDOW + 6, + estimated: true, + }); + expect(attempt.totalTokens).toBe(CURSOR_CLAUDE_WINDOW + 6); + }); + test("a positive provider-reported input count is never reduced by the cap", () => { const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); noteAttemptSend(attempt, 200_000); // estimate would exceed the window