Skip to content

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

Merged
Wibias merged 1 commit into
lidge-jun:devfrom
luvs01:agent/recompute-capped-usage-totals
Aug 18, 2026
Merged

fix(usage): recompute normalized estimated totals#1831
Wibias merged 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: b5a6654786a2bafb0759dae92bbcfc47515de1c3
Exact head: 6df4cc6f734b87792a98af3bbc21388931c76165

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

  • Exact-head Bun 1.3.14: tests/request-log-estimate-cap.test.ts — 12 pass, 0 fail, 21 assertions.
  • Exact-head bun run typecheck, bun run privacy:scan, and git diff --check — passed.
  • The broader content-equivalent patch was previously validated on Bun 1.3.14 and Bun 1.4.0-canary.1: 131 pass, 529 assertions.
  • 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: 0e103fe0-daff-476d-b40d-f989a9a9da3f

📥 Commits

Reviewing files that changed from the base of the PR and between b5a6654 and 6df4cc6.

📒 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; 5 remain after this review.


📝 Walkthrough

Walkthrough

The request log now recomputes 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
Provider usage combined with a local estimate now selects the larger input count, recalculates totalTokens, and preserves the provider estimation status. Context-window capping also recalculates 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. Output tokens remain preserved without double-counting.

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

Merge Risk: ⚪ Minimal · up to 6df4c

This narrowly scoped change recomputes estimated usage totals for affected accounting paths, with targeted tests and required checks reported passing; no actionable merge-blocking risk remains beyond normal review.

Possibly related PRs

  • lidge-jun/opencodex#376: Updates token-usage estimation and total-token recomputation for Cursor checkpoints and request-local estimates.
  • lidge-jun/opencodex#1653: Introduces related request-log logic for capped input estimates and combined token totals.

Suggested reviewers: ingwannu

🚥 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 totals for estimated usage paths.
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

github-actions Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

✅ READY

  • all PR quality gates passed; the review readiness checklist is complete.

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.

4/4 boxes ticked.

This pull request is already Ready for Review.
The review-ready label marks this PR as ready; review automation runs independently.
Maintainers: @lidge-jun @Ingwannu @Wibias

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.

@Wibias
Wibias marked this pull request as ready for review August 17, 2026 03:56
@github-actions
github-actions Bot marked this pull request as draft August 17, 2026 04:01
@luvs01
luvs01 force-pushed the agent/recompute-capped-usage-totals branch from 7f028e3 to bdb4543 Compare August 17, 2026 11:40
@luvs01
luvs01 force-pushed the agent/recompute-capped-usage-totals branch from bdb4543 to 6df4cc6 Compare August 17, 2026 12:02

luvs01 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current dev b5a6654786a2bafb0759dae92bbcfc47515de1c3; exact head is 6df4cc6f734b87792a98af3bbc21388931c76165.

Exact-head validation passed: typecheck, 12 focused usage-cap tests (21 assertions), privacy scan, and diff check. Stable patch ID is unchanged. The local-all-CI, findings, and ready boxes remain unchecked until exact-head maintained CI and bot review complete.

Maintainer action requested: please approve the fork-gated Cross-platform CI run 32027835255 and React Doctor run 32027835277.

@github-actions
github-actions Bot marked this pull request as ready for review August 17, 2026 12:30
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@Wibias
Wibias merged commit f7c9e27 into lidge-jun:dev Aug 18, 2026
36 of 38 checks passed

Wibias commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Thanks @luvs01, merged. This is useful because usage accounting now stays internally consistent when an estimated input count is replaced or capped. Before this, inputTokens could be corrected while an older explicit totalTokens remained inflated, which could leave persisted logs and dashboards showing contradictory or overstated usage. Covering both Kiro and Cursor checkpoint paths gives us much more reliable accounting without changing real provider-reported usage. Nice narrow follow-up.

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

Labels

bug Something isn't working review-ready

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants