Skip to content

fix(text): carry formatBytes rounding into the next unit and precision band - #53273

Merged
nwparker merged 1 commit into
stablyai:mainfrom
AymanKhan9:fix/format-bytes-unit-rollover
Nov 30, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
AymanKhan9:fix/format-bytes-unit-rollover

Conversation

@AymanKhan9

@AymanKhan9 AymanKhan9 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Orca sometimes showed a file size like "1024 KB" when it should say "1.00 MB", or "10.00 KB" when it should say "10.0 KB". It picked the unit and the number of decimals first and rounded afterwards, so rounding could push the number past the limit. Now it rounds first and then picks the unit and decimals.

What Changed

Before: formatBytes (src/shared/text/format-bytes.ts) divided by 1024 until the value was under 1024. It chose the precision from that unrounded value and then called toFixed. A value like 1023.6 B or 1048575 B could round up to 1024 B or 1024 KB. A value like 9.996 KB stayed in the 2-decimal band and showed as 10.00 KB.

After: the value is rounded at its precision first. If the rounded value reaches 1024 and a larger unit exists, it moves to the next unit. The final precision is chosen from the rounded value. Results away from these boundaries are unchanged.

Input (bytes) Before After
1023.6 1024 B 1.00 KB
1048575 1024 KB 1.00 MB
1073741823 1024 MB 1.00 GB
10239 10.00 KB 10.0 KB
1536 / 512 1.50 KB / 512 B unchanged

Why

The bug is in the order of steps: the unit and precision were chosen before rounding. Rounding first and then choosing again fixes both mistakes in the one shared helper. Every size label in the app uses it (workspace storage, cleanup, diagnostics bundle, editor size gate, recent images, native-chat tool output).

Linked Issue

Fixes #53272

Visual Proof

N/A: this changes only the text of size labels, and only for values within rounding distance of a boundary. The before/after table above shows the exact strings, and the unit test covers them.

Testing

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Every added test fails without this change and avoids the test anti-patterns

  • New src/shared/text/format-bytes.test.ts covers rollover from B to KB, KB to MB and MB to GB, one precision-band case and two ordinary values. Without the fix it fails: 1023.6 gives 1024 B. The test passes with the fix (vitest, macOS).

  • The before/after strings above were reproduced by running the old and new functions side by side in Node.

  • Passed on the changed files: oxlint, oxfmt, the anti-slop and native code-quality oxlint configs, ast-grep, and check-changed-code-quality.

  • Platforms: pure string formatting with no platform-specific code.

Performance

This is a pure display helper and is not on any hot path. Callers format labels at render time, once per row or tool output. There is no polling, terminal stream, I/O, cache or fan-out. Micro-benchmark over 100k inputs, Node 24:

  • Typical random sizes: 58 ns/call before, 121 ns/call after.
  • Boundary-adversarial sizes: 59 ns/call before, 120 ns/call after.

The extra ~60 ns comes from one extra toFixed. For 1,000 labels that adds about 0.06 ms per render, which is negligible.

Risk

Level: Low

Output changes only for values within rounding distance of a unit or precision boundary. At the largest unit (PB) there is nowhere to roll over to, so it behaves as before. To back it out, revert the commit.

Review

  • Cross-platform: pure formatting, no platform code.
  • SSH/remote: none. This is a shared helper with no protocol change.
  • Agents and integrations: none.
  • Performance: see above.
  • UI quality: size labels now use the correct unit and number of decimals.
  • Security: none.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Original fix by @AymanKhan9 in #53273. A maintainer rebased it onto current main as a single commit and kept the original authorship.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

formatBytes now rounds values before formatting. If rounding reaches 1024 and a larger unit is available, it promotes the value to that unit and recalculates precision. Tests cover boundary rollover and representative output formats.

Severity of issue fixed: Low

Merge Risk: Minimal · up to 62fdf

This fixes byte-size displays such as "1024 KB" and "10.00 KB" by rounding before choosing the unit and precision. The change is small and tested, and it carries no identified merge risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed [#53272] requires that formatBytes round first, roll over to the next unit when the rounded value reaches 1024, and choose precision from the rounded value. At head, src/shared/text/format-bytes.ts ro…
Out of Scope Changes check Passed The whole-PR diff against merge base 53560a5 touches only src/shared/text/format-bytes.ts (rounding and unit rollover, with precision selection moved into the private precisionFor helper) and its new…
Title check Passed The title names the main change: formatBytes now rounds first, then carries over into the next unit and precision band. It also has the fix(text) scope. The title is 75 characters, which is within a r…
Description check Passed The description follows the template sections: ELI5, What Changed, Why, Linked Issue (Fixes #53272), Visual Proof (N/A with reason), Testing, Performance, Risk (Level: Low), Review, Agent skill bounda…
Full details: Linked Issues check

Explanation

[#53272] requires that formatBytes round first, roll over to the next unit when the rounded value reaches 1024, and choose precision from the rounded value. At head, src/shared/text/format-bytes.ts rounds with precisionFor(value, unitIndex) and then checks rounded >= 1024 with unitIndex < BYTE_UNITS.length - 1 before promoting the unit. It then recomputes precision with precisionFor(rounded, unitIndex) for the final output. Tracing the issue examples: 1023.6 gives '1.00 KB', 1048575 gives '1.00 MB', 1073741823 gives '1.00 GB', and 10239 gives '10.0 KB'. These match the expected outputs. The added src/shared/text/format-bytes.test.ts asserts these same cases, plus 1536 -> '1.50 KB' and 512 -> '512 B'. The issue's downstream displays (cleanup sizes, CLI 'Freed' line, size-limit copy) are not changed here. The fix is in the shared formatter they call, so no separate change is required. No unmet coding requirement is found.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against merge base 53560a5 touches only src/shared/text/format-bytes.ts (rounding and unit rollover, with precision selection moved into the private precisionFor helper) and its new test file src/shared/text/format-bytes.test.ts. Both changes implement the objective in #53272. The public signature of formatBytes is unchanged. No unrelated files or behavior changes were identified in the provided summary.

Full details: Title check

Explanation

The title names the main change: formatBytes now rounds first, then carries over into the next unit and precision band. It also has the fix(text) scope. The title is 75 characters, which is within a reasonable length. It is specific to the change and matches the raw summary and objectives.

Full details: Description check

Explanation

The description follows the template sections: ELI5, What Changed, Why, Linked Issue (Fixes #53272), Visual Proof (N/A with reason), Testing, Performance, Risk (Level: Low), Review, Agent skill boundary, Notes and Checklist. It explains the before/after behavior and the mechanism in detail. The description is complete for a small focused change. Minor gaps: the AI Disclosure section is absent, and the final checklist item (pnpm lint/typecheck/test/build) is unchecked. The 'Every added test fails without this change' claim is supported only by the author's own statement.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ac33bb89-703a-4d36-890b-b7ee24e84530
Commits

Reviewing files that changed from the base of the PR and between 53560a5 and 62fdf2b.

Files selected for processing: 2

  • src/shared/text/format-bytes.test.ts
  • src/shared/text/format-bytes.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

Precision was chosen before rounding, so values just under a boundary
rendered as "1024 KB" or "10.00 KB". Round first, then roll over to
the next unit and pick precision from the rounded value.

Fixes stablyai#53272
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.

[Bug]: formatBytes shows "1024 KB" instead of "1.00 MB" just below a unit boundary

2 participants