Skip to content

fix(usage): recompute normalized estimated totals - #1831

Draft
luvs01 wants to merge 1 commit into
lidge-jun:devfrom
luvs01:agent/recompute-capped-usage-totals
Draft

fix(usage): recompute normalized estimated totals#1831
luvs01 wants to merge 1 commit into
lidge-jun:devfrom
luvs01:agent/recompute-capped-usage-totals

Conversation

@luvs01

@luvs01 luvs01 commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Keep nested usage.totalTokens consistent when a request-side estimate replaces the logged input count.
  • Recompute the explicit total after an adapter-estimated input count is capped at the model context window.
  • Cover Kiro's combined and adapter-only paths plus Cursor's absolute-checkpoint semantics.

Exact base: b81314cd29b78fecb447df882dc4fc1a987434b9
Exact head: 7f028e369c8d09146615fca632c9725c37da6202

Why

This is a narrow follow-up to #1653. The context-window cap corrected inputTokens, but an existing explicit totalTokens could retain its pre-cap value. The same inconsistency was possible when a larger local estimate replaced the adapter's input count.

That left nested usage and the outer attempt total contradictory, and legacy/display aggregation could continue honoring the stale explicit total.

Behavior

  • Only estimated Kiro/Cursor accounting paths are affected.
  • A combined local estimate now stores totalTokens = combinedInputTokens + outputTokens.
  • An adapter-only estimate capped to the context window now stores totalTokens = contextWindow + outputTokens.
  • Existing provider-reported positive input precedence is unchanged.
  • Cursor's absolute checkpoint remains represented as input = checkpoint - output, so output is added exactly once.
  • contextTotalTokens, cache counters, routing, credentials, prompts, and request payloads are unchanged.

Verification

  • Bun 1.3.14 focused usage/request-log selection: 131 pass, 529 assertions.
  • Bun 1.4.0-canary.1 focused usage/request-log selection: 131 pass, 529 assertions.
  • bun run typecheck — pass on both runtimes.
  • bun run privacy:scan — pass on both runtimes.
  • git diff --check — pass.
  • Independent exact-diff correctness/compatibility/privacy review — CLEAN, no P0-P2 findings.
  • Codex Security diff scan fb434b9d-930e-4000-8220-f3e8a3b59162 — CLEAN, 0 findings, complete coverage of the changed production source.
  • Stable patch ID: 9aecddbc80a79cc26bbf8aa3d0c624e92355fe83.
  • Full repository suite is not claimed green; maintained exact-head CI remains required.

Review readiness checklist

This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:

  • All CI tests are green on my local testing.

  • I pushed my PR to the latest dev commit.

  • I resolved all correct Codex and CodeRabbit findings.

  • My PR is ready for review.

Summary by CodeRabbit

  • Bug Fixes

    • Corrected token usage totals when estimated input tokens are capped by the model context window.
    • Preserved output-token counts while preventing capped input estimates from being double-counted.
    • Improved usage reporting when combining estimates with provider and checkpoint data.
  • Tests

    • Added coverage for nested and overall token-total calculations across supported usage sources.

@github-actions

Copy link
Copy Markdown
Contributor

Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Aug 16, 2026
@coderabbitai

coderabbitai Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 382d794c-7118-4e88-a04e-3bbf0b420477

📥 Commits

Reviewing files that changed from the base of the PR and between b81314c and 7f028e3.

📒 Files selected for processing (2)
  • src/server/request-log.ts
  • tests/request-log-estimate-cap.test.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The request log now recomputes nested and outer token totals when input estimates are merged with provider usage or capped at the model context window. Tests cover adapter, local request, and Cursor checkpoint estimates.

Changes

Request-log token usage correction

Layer / File(s) Summary
Recompute merged and capped usage totals
src/server/request-log.ts
When provider usage and a local estimate coexist, the larger input count is used and totalTokens is recalculated with output tokens. Capped estimates also recalculate totalTokens.
Validate capped estimate totals
tests/request-log-estimate-cap.test.ts
Tests verify nested and outer totals for adapter estimates, local estimates with reported usage, and Cursor checkpoint data. They also verify that output tokens are preserved without double-counting.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: 🟡 Moderate · up to 7f028

The PR updates normalized usage totals, with focused tests and validation passing, but it remains draft and required exact-head CI has not yet been confirmed; it is not merge-ready until those checks pass.

Possibly related PRs

Suggested labels: bug

Suggested reviewers: kartikkabadi

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: recomputing normalized estimated usage totals.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

⏳ DRAFT

  • review readiness checklist open (2/4 boxes ticked).

What to do

  • Tick all four boxes in the PR description once you're done (currently 2/4).

Review readiness checklist

  • ⬜ All CI tests are green on my local testing.
  • ✅ I pushed my PR to the latest dev commit.
  • ✅ I resolved all correct Codex and CodeRabbit findings.
  • ⬜ My PR is ready for review.

2/4 boxes ticked.

This PR stays in draft until every box above is ticked.

luvs01 commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant