Skip to content

[WRONG BRANCH] fix(usage): recompute totals when capping adapter-estimated inputTokens - #286

Draft
luvs01 wants to merge 1 commit into
mainfrom
codex/fix-stale-totaltokens-after-capping-inputtokens
Draft

[WRONG BRANCH] fix(usage): recompute totals when capping adapter-estimated inputTokens#286
luvs01 wants to merge 1 commit into
mainfrom
codex/fix-stale-totaltokens-after-capping-inputtokens

Conversation

@luvs01

@luvs01 luvs01 commented Aug 15, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Adapter-provided estimated usage could be capped at the model context window for inputTokens while an explicit totalTokens field remained unchanged, violating the OcxUsage invariant that totalTokens = inputTokens + outputTokens and leaving persisted usage summaries inconsistent.

Description

  • When an adapter-estimated inputTokens value is reduced to the routed model's contextWindow, the code now recomputes totalTokens to inputTokens + outputTokens so the usage object remains internally consistent. The change is implemented in src/server/request-log.ts within finalizedUsage where the adapter-estimated branch is normalized.
  • Added a focused regression test that reproduces the stale-explicit-total scenario and verifies both the normalized usage object and the persisted attempt.totalTokens are updated; the test lives in tests/request-log-estimate-cap.test.ts.
  • Changes touch only the logging/usage normalization path and the focused unit test for the estimate cap behavior.

Testing

  • Ran the focused test: ./node_modules/.bin/bun test tests/request-log-estimate-cap.test.ts --timeout 10000 and it passed (all cases including the new regression).
  • Typecheck passed: ./node_modules/.bin/bun x tsc --noEmit succeeded and bun run privacy:scan returned "Privacy scan passed".
  • A full ./node_modules/.bin/bun run test run progressed through the suite and exercised the affected subsystems, but encountered unrelated environment-sensitive timeouts in some long-running integration tests; the focused coverage for this fix passed.

Codex Task

Summary by CodeRabbit

  • Bug Fixes
    • Corrected token usage totals when estimated input tokens exceed the model’s context limit.
    • Output token counts and estimated usage status are now preserved accurately.

@github-actions

Copy link
Copy Markdown

Deterministic PR hygiene checks passed.

@coderabbitai

coderabbitai Bot commented Aug 15, 2026

Copy link
Copy Markdown

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: 66c17642-9f86-4e62-b213-fa9ce95b5cc7

📥 Commits

Reviewing files that changed from the base of the PR and between 161c09f and 10e6055.

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

📝 Walkthrough

Walkthrough

The request log now recalculates totalTokens when estimated input tokens exceed the model context window. A regression test verifies the capped input, preserved output, recomputed total, and estimate metadata.

Changes

Usage total recalculation

Layer / File(s) Summary
Capped usage and regression coverage
src/server/request-log.ts, tests/request-log-estimate-cap.test.ts
At lines 1020–1024, capped estimated usage sets totalTokens to capped input tokens plus output tokens. Lines 34–50 add assertions for the recomputed total, capped input, preserved output, and estimate metadata.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: ⚪ Minimal · up to 10e60

The PR keeps usage totals consistent when capped input tokens are normalized and adds focused regression coverage; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: kartikkabadi, lidge-jun, ingwannu

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the fix for recomputing totals when capping adapter-estimated inputTokens, although the branch marker adds unnecessary noise.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-stale-totaltokens-after-capping-inputtokens

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

@github-actions github-actions Bot added the bug Something isn't working label Aug 15, 2026
@github-actions github-actions Bot changed the title fix(usage): recompute totals when capping adapter-estimated inputTokens [WRONG BRANCH] fix(usage): recompute totals when capping adapter-estimated inputTokens Aug 15, 2026
@github-actions

github-actions Bot commented Aug 15, 2026

Copy link
Copy Markdown

⏳ DRAFT

  • wrong target branch (main); retarget to dev.

What to do

  • Retarget this PR to dev — all contributions go to dev.

Its title has been prefixed with [WRONG BRANCH].
This pull request was already a draft. Its draft status will be preserved after every issue above is resolved.

@github-actions
github-actions Bot marked this pull request as draft August 15, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aardvark bug Something isn't working codex

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant