From c9813c236dd3ee09a5a8e110c8348987c704b92c Mon Sep 17 00:00:00 2001 From: "Nick T." <73005126+tiliakoos@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:17:31 -0500 Subject: [PATCH 1/2] fix(mobile): client storage sizes no longer read "1024 KB" formatBytes picked the unit and decimal precision before rounding and stopped at GB, so 1,048,575 bytes read "1024 KB" and 1 TiB read "1024.0 GB". Pick the unit and precision after rounding through TB. Re-lands #28648. Co-authored-by: only21mil <263261067+only21mil@users.noreply.github.com> --- apps/mobile/src/lib/formatBytes.test.ts | 18 +++ apps/mobile/src/lib/formatBytes.ts | 18 ++- ...lient-storage-does-not-read-1024-kb.e2e.ts | 111 ++++++++++++++++++ 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 apps/mobile/src/lib/formatBytes.test.ts create mode 100644 scripts/scenarios/client-storage-does-not-read-1024-kb.e2e.ts diff --git a/apps/mobile/src/lib/formatBytes.test.ts b/apps/mobile/src/lib/formatBytes.test.ts new file mode 100644 index 000000000000..4a091eea29b7 --- /dev/null +++ b/apps/mobile/src/lib/formatBytes.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { formatBytes } from "./formatBytes"; + +describe("formatBytes", () => { + it.each([ + [0, "0 B"], + [1_023, "1023 B"], + [1_024, "1.0 KB"], + [1_047_552, "1023 KB"], + [1_048_575, "1.0 MB"], + [1_073_741_823, "1.0 GB"], + [10_230, "10 KB"], + [1024 ** 4, "1.0 TB"], + ])("formats %d bytes as %s from the rounded value", (bytes, label) => { + expect(formatBytes(bytes)).toBe(label); + }); +}); diff --git a/apps/mobile/src/lib/formatBytes.ts b/apps/mobile/src/lib/formatBytes.ts index 709e73f8fb86..500816841f51 100644 --- a/apps/mobile/src/lib/formatBytes.ts +++ b/apps/mobile/src/lib/formatBytes.ts @@ -1,7 +1,17 @@ +const UNITS = ["KB", "MB", "GB", "TB"] as const; + +function formatScaled(value: number): string { + return Number(value.toFixed(1)) < 10 ? value.toFixed(1) : value.toFixed(0); +} + export function formatBytes(bytes: number): string { if (bytes < 1024) return `${bytes} B`; - if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(bytes < 10 * 1024 ? 1 : 0)} KB`; - if (bytes < 1024 ** 3) - return `${(bytes / (1024 * 1024)).toFixed(bytes < 10 * 1024 * 1024 ? 1 : 0)} MB`; - return `${(bytes / 1024 ** 3).toFixed(1)} GB`; + let value = bytes / 1024; + let unitIndex = 0; + // Pick the unit after rounding, so 1023.9 KB reads "1.0 MB", not "1024 KB". + while (Number(formatScaled(value)) >= 1024 && unitIndex < UNITS.length - 1) { + value /= 1024; + unitIndex += 1; + } + return `${formatScaled(value)} ${UNITS[unitIndex]}`; } diff --git a/scripts/scenarios/client-storage-does-not-read-1024-kb.e2e.ts b/scripts/scenarios/client-storage-does-not-read-1024-kb.e2e.ts new file mode 100644 index 000000000000..5c50142091fa --- /dev/null +++ b/scripts/scenarios/client-storage-does-not-read-1024-kb.e2e.ts @@ -0,0 +1,111 @@ +import { expect } from "e2e"; + +import { MOBILE, MOBILE_T3_HOME } from "../e2e.config.ts"; +import { environmentId, makeState, runPromise } from "./t3.ts"; +import { IOS, test } from "./t3-mobile.ts"; + +const APP_ID = "com.t3tools.t3code.dev"; +const SCHEME = "t3code-dev"; +const TARGET_BYTES = 1_048_320; +const BYTES_PER_CHAR = 2; +const TURN_OVERHEAD = 10_590; +const DRIFT_BYTES = 32; + +function parseClearSize(label: string): { readonly lo: number; readonly hi: number } { + const match = /^Clear (\d+(?:\.\d+)?) (B|KB|MB)$/.exec(label.trim()); + if (match === null) throw new Error(`Unexpected Client Storage button label: ${label}`); + const value = Number(match[1]); + const unit = match[2]; + if (unit === "B") return { lo: value, hi: value + 1 }; + const scale = unit === "MB" ? 1024 * 1024 : 1024; + const halfStep = match[1]?.includes(".") ? 0.05 : 0.5; + return { lo: (value - halfStep) * scale, hi: (value + halfStep) * scale }; +} + +test( + 'Client storage sizes just under 1 MB read "1.0 MB" instead of "1024 KB"', + IOS, + async ({ t3, app, screen, device }) => { + const project = await t3.project("demo"); + const pad = await t3.thread({ + project, + title: "Calibration", + messages: ["seed"], + }); + const { cli } = makeState(MOBILE_T3_HOME, { T3CODE_PORT: String(MOBILE.serverPort) }); + + // Start from an empty cache so repeated local simulator runs match a fresh device. + await app.clearState().catch(() => {}); + const [, bulk] = await Promise.all([ + t3.open(), + t3.thread({ + project, + title: "Bulk cache", + messages: Array.from({ length: 4 }, (_, index) => `bulk-${index} ${"a".repeat(104_000)}`), + }), + ]); + await screen.getByText("Bulk cache", { exact: false }).first().waitFor({ timeout: 120_000 }); + + const environment = await runPromise( + environmentId(`http://${MOBILE.host}:${MOBILE.serverPort}`), + ); + const openLink = async (url: string) => { + await device.openLink(url, { app: APP_ID }); + }; + const openThreadAndFlush = async (threadId: string, marker: string) => { + await openLink(`${SCHEME}://threads/${environment}/${threadId}`); + await screen.getByText(marker, { exact: false }).first().waitFor({ timeout: 60_000 }); + await screen.getByTestId("BackButton").tap(); + await screen.getByRole("tablist", "Tab Bar").waitFor({ timeout: 30_000 }); + }; + const readClientStorage = async () => { + await openLink(`${SCHEME}://settings/client-storage`); + const clearButton = screen.getByRole("button", /^Clear \d/); + await clearButton.waitFor({ timeout: 30_000 }); + const label = (await clearButton.textContent()) ?? ""; + await screen.getByTestId("BackButton").tap(); + await screen.getByRole("tablist", "Tab Bar").waitFor({ timeout: 30_000 }); + return parseClearSize(label); + }; + + await openThreadAndFlush(bulk.threadId, "first fixture turn complete"); + await openThreadAndFlush(pad.threadId, "first fixture turn complete"); + let { lo, hi } = await readClientStorage(); + + // Narrow the SQLite cache total to ±128 B via half-KB rounding steps, then land inside + // [1,048,064, 1,048,575] where base renders "1024 KB" and head renders "1.0 MB". + for (let step = 1; step <= 6; step += 1) { + const width = hi - lo; + const mid = (lo + hi) / 2; + const done = width <= 320; + const minNext = mid + TURN_OVERHEAD + 100; + const nextHalfKb = Math.ceil((minNext - 512) / 1024) * 1024 + 512; + const target = done ? TARGET_BYTES : Math.max(nextHalfKb, 980.5 * 1024); + const padChars = Math.max(16, Math.round((target - mid - TURN_OVERHEAD) / BYTES_PER_CHAR)); + const expectedGrowth = TURN_OVERHEAD + padChars * BYTES_PER_CHAR; + const tag = `pad-${step}`; + const body = `${tag} ${"a".repeat(Math.max(1, padChars - tag.length - 1))}`; + + await runPromise(cli(["thread", "send", pad.threadId, body, "--wait", "--json"], [0, 2])); + await openThreadAndFlush(pad.threadId, tag); + if (done) break; + + const observed = await readClientStorage(); + const predLo = lo + expectedGrowth - DRIFT_BYTES; + const predHi = hi + expectedGrowth + DRIFT_BYTES; + if (Math.max(predLo, observed.lo) < Math.min(predHi, observed.hi)) { + lo = Math.max(predLo, observed.lo); + hi = Math.min(predHi, observed.hi); + } else { + lo = observed.lo; + hi = observed.hi; + } + } + + await openLink(`${SCHEME}://settings/client-storage`); + await screen.getByRole("button", /^Clear \d/).waitFor({ timeout: 30_000 }); + await app.screenshot("client-storage"); + await expect(screen.getByRole("button", "Clear 1.0 MB")).toBeVisible(); + await expect(screen.getByRole("button", /1024 KB/)).not.toBeVisible(); + }, +); From 49dff77c342213b3e90326bb26919a3d8609c3b9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 18:56:50 +0000 Subject: [PATCH 2/2] fix(mobile): keep the formatter to the rounding fix Drop the TB unit and keep GB labels at one decimal so speech-model sizes read as before; drop the calibration-heavy iOS scenario, the unit cases cover the regression. Co-Authored-By: Claude Opus 5.5 --- apps/mobile/src/lib/formatBytes.test.ts | 7 ++----- apps/mobile/src/lib/formatBytes.ts | 14 ++++++-------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/apps/mobile/src/lib/formatBytes.test.ts b/apps/mobile/src/lib/formatBytes.test.ts index 4a091eea29b7..2d710f563816 100644 --- a/apps/mobile/src/lib/formatBytes.test.ts +++ b/apps/mobile/src/lib/formatBytes.test.ts @@ -4,15 +4,12 @@ import { formatBytes } from "./formatBytes"; describe("formatBytes", () => { it.each([ - [0, "0 B"], [1_023, "1023 B"], - [1_024, "1.0 KB"], [1_047_552, "1023 KB"], [1_048_575, "1.0 MB"], [1_073_741_823, "1.0 GB"], - [10_230, "10 KB"], - [1024 ** 4, "1.0 TB"], - ])("formats %d bytes as %s from the rounded value", (bytes, label) => { + [24 * 1024 ** 3, "24.0 GB"], + ])("formats %d bytes as %s", (bytes, label) => { expect(formatBytes(bytes)).toBe(label); }); }); diff --git a/apps/mobile/src/lib/formatBytes.ts b/apps/mobile/src/lib/formatBytes.ts index 500816841f51..46ff2d0e12ee 100644 --- a/apps/mobile/src/lib/formatBytes.ts +++ b/apps/mobile/src/lib/formatBytes.ts @@ -1,17 +1,15 @@ -const UNITS = ["KB", "MB", "GB", "TB"] as const; - -function formatScaled(value: number): string { - return Number(value.toFixed(1)) < 10 ? value.toFixed(1) : value.toFixed(0); -} +const UNITS = ["KB", "MB", "GB"] as const; export function formatBytes(bytes: number): string { if (bytes < 1024) return `${bytes} B`; let value = bytes / 1024; let unitIndex = 0; - // Pick the unit after rounding, so 1023.9 KB reads "1.0 MB", not "1024 KB". - while (Number(formatScaled(value)) >= 1024 && unitIndex < UNITS.length - 1) { + while (true) { + const isLast = unitIndex === UNITS.length - 1; + const label = value.toFixed(isLast || value < 10 ? 1 : 0); + // Pick the unit after rounding, so 1023.9 KB reads "1.0 MB", not "1024 KB". + if (isLast || Number(label) < 1024) return `${label} ${UNITS[unitIndex]}`; value /= 1024; unitIndex += 1; } - return `${formatScaled(value)} ${UNITS[unitIndex]}`; }