Skip to content

fix(ui): keep an approved payload size consistent with its own unit - #864

Open
ANIRUDDHA ADAK (aniruddhaadak80) wants to merge 1 commit into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/approval-bytes-boundary
Open

ANIRUDDHA ADAK (aniruddhaadak80) wants to merge 1 commit into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/approval-bytes-boundary

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

What

formatApprovalBytes in frontend/ui/src/components/permission-actions.ts branches on the raw byte count but prints a value rounded to one decimal:

const bytes = Math.max(0, Math.trunc(value))
const exact = new Intl.NumberFormat("en-US").format(bytes)
if (!Number.isFinite(value) || bytes < 1024) return `${exact} bytes`
if (bytes < 1024 * 1024) return `${Number((bytes / 1024).toFixed(1))} KB (${exact} bytes)`
return `${Number((bytes / (1024 * 1024)).toFixed(1))} MB (${exact} bytes)`

Why it matters

This is the exactness surface of a permission card. The function feeds the Payload exact row and the egress total_bytes row — the numbers a user reads to decide whether to authorize data leaving the machine.

1048400 -> 1023.8 KB (1,048,400 bytes)
1048575 -> 1024 KB (1,048,575 bytes)      <- one byte under 1 MiB
1048576 -> 1 MB (1,048,576 bytes)
NaN      -> NaN bytes
Infinity -> ∞ bytes

The unit contradicts the count in the same string. Number("1024.0") is 1024, so from 1,048,525 bytes — 51 bytes before the real boundary — the label reads a full megabyte in KB.

The finite check runs after the formatting. exact is computed on line 2 and !Number.isFinite(value) is tested on line 3, so a non-finite value is already formatted by the time it is rejected, producing "NaN bytes" and "∞ bytes". Neither call site validates first: hostedScientific() does check payload_bytes, but the egress total_bytes reaches the formatter unguarded.

Verification

New permission-actions.test.ts — the module had none; message-part-permission.test.ts covers the component, not the formatter. Two of five cases fail before:

Expected: "1 MB (1,048,575 bytes)"
Received: "1024 KB (1,048,575 bytes)"
(fail) promotes a rounded KB value instead of labelling it 1024 KB

Expected: "unknown size"
Received: "NaN bytes"
(fail) reports a size it cannot count as unknown rather than as text
(pass) promotes a rounded KB value instead of labelling it 1024 KB
(pass) switches units at the byte where the rounded value would read 1024
(pass) keeps the exact count and ordinary unit boundaries
(pass) reports a size it cannot count as unknown rather than as text
(pass) a negative size is not rendered
 5 pass
 0 fail

switches units at the byte where the rounded value would read 1024 pins 1,048,524 → 1023.9 KB against 1,048,525 → 1 MB — the exact byte where the one-decimal KB form first reads 1024. That is the case a hand-picked 1023_950-style constant gets wrong, so the guard tests the condition it is guarding rather than a magic number. keeps the exact count and ordinary unit boundaries covers 0 / 1023 / 1024 / 1,048,400 so the change cannot have shifted the normal path.

The change

 export function formatApprovalBytes(value: number) {
-  const bytes = Math.max(0, Math.trunc(value))
-  const exact = new Intl.NumberFormat("en-US").format(bytes)
-  if (!Number.isFinite(value) || bytes < 1024) return `${exact} bytes`
-  if (bytes < 1024 * 1024) return `${Number((bytes / 1024).toFixed(1))} KB (${exact} bytes)`
+  // The exact count is the point of this label, so a value that cannot be
+  // counted is named as such instead of being formatted into "NaN bytes".
+  if (!Number.isFinite(value)) return "unknown size"
+  const bytes = Math.max(0, Math.trunc(value))
+  const exact = new Intl.NumberFormat("en-US").format(bytes)
+  if (bytes < 1024) return `${exact} bytes`
+  // A rounded KB value reaches 1024 just below the next unit (from 1,048,525
+  // bytes), which contradicted the exact count printed beside it. Promote
+  // those to MB rather than printing "1024 KB".
+  if (bytes < 1024 * 1024 && Number((bytes / 1024).toFixed(1)) < 1024)
+    return `${Number((bytes / 1024).toFixed(1))} KB (${exact} bytes)`
   return `${Number((bytes / (1024 * 1024)).toFixed(1))} MB (${exact} bytes)`
 }

message-part-permission.test.ts is unaffected: 14 pass / 0 fail across both files.

Touched files are Prettier-clean (verified on LF-normalized copies; this Windows checkout's core.autocrlf=true makes Prettier flag every file repo-wide).

Fixes #863

@vercel

vercel Bot commented Sep 30, 2026

Copy link
Copy Markdown

ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

The branch tested the raw byte count but printed one rounded to a decimal, so from 1048525 bytes a single megabyte was labelled 1024 KB beside an exact count that read as less. The finite check also ran after formatting, so NaN reached the label as text.

This branch has not been deployed

No deployments
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.

An approved payload size just under a megabyte is labelled 1024 KB, and NaN renders as text

1 participant