Extract shared token estimation helper, migrate all CHARS_PER_TOKEN sites#677
Draft
avs-io wants to merge 1 commit into
Draft
Extract shared token estimation helper, migrate all CHARS_PER_TOKEN sites#677avs-io wants to merge 1 commit into
avs-io wants to merge 1 commit into
Conversation
iamtoruk
approved these changes
Jul 16, 2026
iamtoruk
left a comment
Member
There was a problem hiding this comment.
Verified in depth: no stray CHARS_PER_TOKEN or hardcoded /4 sites remain, the ratio pin tests fail when the constant changes, the cursor-agent non-positive guard is preserved, and a 30-day all-provider report on real data is byte-identical between main and this branch, so the behavior-identical claim holds empirically. The two local test failures during verification are pre-existing on main (app/electron/cli.test.ts expects a built dist/cli.js) and unrelated.
This was referenced Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CHARS_PER_TOKENis defined independently in 7 files (src/context-tree.ts,src/context-budget.ts, and the kiro / cursor / warp / codex / cursor-agent providers), withMath.ceil(len / CHARS_PER_TOKEN)re-derived at each site. Any future refinement of the heuristic would silently apply to some providers and not others, skewing cross-provider comparisons. (#644)Fix
New leaf module
src/token-estimate.tsexportingCHARS_PER_TOKENandestimateTokensFromChars(chars); all 7 local constants deleted and every call site migrated. One additional hardcoded/ 4site insrc/context-tree-codex.ts(same heuristic, just never named) is migrated too.Behavior-identical by construction: every site already used ratio 4 with
Math.ceil, and the two sites with extra semantics keep them —cursor-agent's non-positive guard is preserved, andcontext-tree-codex's fractional decoded-size input goes through the sameceil(chars / 4).The module imports nothing from
src/, so providers stay lazy-loadable.Tests
tests/token-estimate.test.tspins the heuristic: ratio value, ceil rounding, zero/boundary inputs. The ratio can now only change in one place, guarded by one test.npx tsc --noEmitclean.npx vitest run— 130 files passed, 1713 tests passed, 0 failed.Closes #644