Skip to content

fix: compact token formatter promotes to m before rounding - #394

Open
Sma1lboy wants to merge 1 commit into
mainfrom
claude/keen-einstein-3br61m
Open

fix: compact token formatter promotes to m before rounding#394
Sma1lboy wants to merge 1 commit into
mainfrom
claude/keen-einstein-3br61m

Conversation

@Sma1lboy

Copy link
Copy Markdown
Owner

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:

if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}m`
if (value >= 1_000)     return `${(value / 1_000).toFixed(1)}k`

Any value in [999_950, 999_999] clears the 1_000_000 check (so it stays in the "k" branch) but then (value / 1_000).toFixed(1) rounds 999.95…999.999 up to 1000.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 renders ctx 1000.0k in the transcript.

The repo already had the correct precedent: preview-core.ts's formatBytes deliberately promotes at 1023.5, "not 1024… once v rounds up it would render as the next unit."

Fix

Promote to m at 999_950 — the smallest value that rounds up to 1000.0k — so the boundary the half-up rounding actually crosses is the boundary the branch switches at. One-line threshold change, mirroring the established formatBytes rule.

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 lint and bun run typecheck from repo root — both green.
  • The 3 unrelated server-session / web-state-routes test failures are pre-existing on clean main (env-dependent), confirmed by re-running with this change stashed.

Patch changeset included.

Follow-ups (deliberately not in this slice)

fmtBytes in packages/kobe/src/cli/doctor-cmd.ts:74 has the identical root cause — fmtBytes(1_048_550) prints "1024.0 KB" instead of "1.0 MB". It lives in a different surface (kobe doctor diagnostics) 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

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).
@vercel

vercel Bot commented Aug 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
kobe-landing Ready Ready Preview Aug 10, 2026 4:33pm

Request Review

@claude

claude Bot commented Aug 10, 2026

Copy link
Copy Markdown

Claude finished @Sma1lboy's task in 3m 34s —— View job


Review: PR #394 — fix: compact token formatter promotes to m before rounding

Verdict: Approve — clean, correctly-derived, well-tested fix. No blocking issues.

  • Read CLAUDE.md (no packages/kobe/CLAUDE.md exists)
  • Gather PR diff and changed files
  • Run specialized review agents (code-reviewer, silent-failure-hunter, pr-test-analyzer, type-design-analyzer, comment-analyzer, code-simplifier)
  • Check kobe house rules
  • Synthesize findings

House rules

  • No AI/Anthropic/Claude attribution in commit or PR body. ✅
  • Changeset present (.changeset/token-count-unit-boundary.md), bump is patch, single-line body (no soft-wrap). ✅
  • File size cap: history.ts 104 lines, test file 78 lines — well under 500. ✅
  • Regression test on bug fix: the old test pinned the bug (expect(formatTokens(999_999)).toBe("1000.0k")) and has been corrected + a new boundary test (999_949→999.9k, 999_950→1.0m, 999_999→1.0m) was added. pr-test-analyzer hand-traced that both assertions genuinely fail pre-fix and pass post-fix — this satisfies the hard rule. ✅

Correctness

Two independent agents hand-verified the IEEE-754 arithmetic (bash/node execution was sandboxed in this environment, so this was done by hand-tracing doubles): 999_950/1000 rounds up to "1000.0" under the old code, 999_949/1000 correctly stays "999.9" — so 999_950 really is the exact smallest boundary, not an off-by-one. The cross-reference to formatBytes's 1023.5 promotion rule in packages/kobe/src/tui/ops/preview-core.ts:58 is verbatim accurate. Comments are accurate and proportionate; nothing to simplify.

Should-fix

None.

Nit

  • packages/kobe-web/src/lib/history.ts:96 — the promotion boundary (999_950) is a bare magic number; a named constant (M_PROMOTE_THRESHOLD or similar) would let the invariant travel with the value instead of living only in the comment. Optional, not blocking.

Note (already handled correctly by the PR, not a gap)

code-reviewer flagged that fmtBytes in packages/kobe/src/cli/doctor-cmd.ts:74 has the identical root cause (bytes in 1_048_525–1_048_575 would print "1024.0 KB"). The PR body already explicitly surfaces this as a deliberate, out-of-scope follow-up rather than silently leaving it — exactly what CLAUDE.md's scope guidance asks for. Worth a quick follow-up PR, but not a gap in this one.

@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
see 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants