fix: compact token formatter promotes to m before rounding - #394
Conversation
The web chat transcript's context readout could print an impossible "1000.0k" for values just below a million tokens: formatTokens tested its unit threshold before rounding, so 999_999 fell into the "k" branch and (value / 1000).toFixed(1) rounded it to "1000.0k". It now promotes to "m" at 999_950 — the boundary the half-up rounding actually crosses — matching the promote-before-rounding rule preview-core's formatBytes already uses. The existing test pinned the buggy "1000.0k"; corrected it and added boundary coverage (999_949 → 999.9k, 999_950 → 1.0m).
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Claude finished @Sma1lboy's task in 3m 34s —— View job Review: PR #394 — fix: compact token formatter promotes to m before roundingVerdict: Approve — clean, correctly-derived, well-tested fix. No blocking issues.
House rules
CorrectnessTwo independent agents hand-verified the IEEE-754 arithmetic (bash/node execution was sandboxed in this environment, so this was done by hand-tracing doubles): Should-fixNone. Nit
Note (already handled correctly by the PR, not a gap)
|
Codecov Report✅ All modified and coverable lines are covered by tests. 🚀 New features to boost your workflow:
|
Direction
Bugs / correctness — found by daily code review, not from an open issue.
Problem
The web chat transcript header shows a live context estimate via
formatTokens(packages/kobe-web/src/lib/history.ts). The function tested its unit threshold before rounding:Any value in
[999_950, 999_999]clears the1_000_000check (so it stays in the "k" branch) but then(value / 1_000).toFixed(1)rounds999.95…999.999up to1000.0, printing the impossible"1000.0k". This is a real, user-visible readout — long-context sessions routinely sit just under 1,000,000 tokens, and that's exactly the range that rendersctx 1000.0kin the transcript.The repo already had the correct precedent:
preview-core.ts'sformatBytesdeliberately promotes at1023.5, "not 1024… once v rounds up it would render as the next unit."Fix
Promote to
mat999_950— the smallest value that rounds up to1000.0k— so the boundary the half-up rounding actually crosses is the boundary the branch switches at. One-line threshold change, mirroring the establishedformatBytesrule.The existing test pinned the buggy behavior (
expect(formatTokens(999_999)).toBe("1000.0k"), with a comment rationalizing it as "just under 1m → still k"). Corrected it and added boundary coverage:999_949 → 999.9k,999_950 → 1.0m,999_999 → 1.0m.Verification
bun x vitest run test/history-usage.test.ts(kobe-web) — 6 passing.bun run lintandbun run typecheckfrom repo root — both green.server-session/web-state-routestest failures are pre-existing on cleanmain(env-dependent), confirmed by re-running with this change stashed.Patch changeset included.
Follow-ups (deliberately not in this slice)
fmtBytesinpackages/kobe/src/cli/doctor-cmd.ts:74has the identical root cause —fmtBytes(1_048_550)prints"1024.0 KB"instead of"1.0 MB". It lives in a different surface (kobe doctordiagnostics) with no existing test harness, so I left it out to keep this slice tight and surface it here rather than silently widen scope. Happy to fix it in a follow-up.Generated by Claude Code