Public: anyone can read this; the crew can change it.
Raise the read ceiling for .ipynb so notebooks up to 2 MB are drawn as cells #16028
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16019-raise-the-read-ceiling-for-ipynb-so-note → main 0 commits fork
5 files
changed +41
−8
docs/features/file-kinds.md +1 −1
| @@ -35,7 +35,7 @@ The forge draws a file on its page when its kind is below and it is within the s | ||
| 35 | 35 | - **WebAssembly module**, drawn up to 5 MB: `.wasm` |
| 36 | 36 | - **Config**, drawn up to 1 MB: `.json`, `.jsonc`, `.yaml`, `.yml`, `.toml`, `.conf` |
| 37 | 37 | - **Markdown**, drawn up to 1 MB: `.md`, `.markdown`, `.mdx` |
| 38 | - **Notebook**, drawn up to 1 MB: `.ipynb` | |
| 38 | - **Notebook**, drawn up to 2 MB: `.ipynb` | |
| 39 | 39 | - **Patch**, drawn up to 1 MB: `.patch`, `.diff` |
| 40 | 40 | - **Log**, drawn up to 1 MB: `.log`, `.ansi` |
| 41 | 41 | - **Subtitles**, drawn up to 5 MB: `.srt`, `.vtt` |
docs/features/notebook.md +1 −1
| @@ -6,5 +6,5 @@ row: Jupyter notebooks | ||
| 6 | 6 | |
| 7 | 7 | A `.ipynb` file opens on the blob page as its cells and saved outputs (`src/web/render/notebook.ts`). Nothing is run. |
| 8 | 8 | Markdown cells go through the forge's Markdown renderer. A code cell is a `pre` headed `In [n]:` with its execution count. Output text (streams, plain text, errors with their colour codes removed) is escaped text. PNG and JPEG outputs are pictures, drawn only when the base64 is well-formed and the picture is under 1 MB, and shrink to fit a phone; a larger one is a note, "Image output over 1 MB, not drawn". HTML and script outputs are shown as escaped text and never inserted or run. Other output kinds are skipped. |
| 9 | A notebook over 1 MB shows the source view with the sentence "This notebook is over 1 MB, so it is not drawn as cells (this one is N MB)", and Raw and Download are in the header. One that is not valid JSON, or one with no `cells` list shows the ordinary source view with one line saying why. At most 2,000 cells, and 2,000 outputs per cell, are drawn. `?view=source`, Raw and Download show the bytes untouched. | |
| 9 | A notebook over 2 MB shows the source view with the sentence "This notebook is over 2 MB, so it is not drawn as cells (this one is N MB)", and Raw and Download are in the header. One that is not valid JSON, or one with no `cells` list shows the ordinary source view with one line saying why. At most 2,000 cells, and 2,000 outputs per cell, are drawn. `?view=source`, Raw and Download show the bytes untouched. | |
| 10 | 10 | The terminal workspace reads the same cells (`src/tui/screens/workspace_kinds.ts`, from `parseNotebook`): code dimmed, errors in the danger colour, pictures as `[image output not shown]`, an over-1-MB picture as the same note. Nothing is run there either. Classes live in `docs/design/areas/render.md`. |
src/web/pages/repo.ts +2
| @@ -2110,6 +2110,8 @@ export function capped(blob: Blob, path = ""): Blob { | ||
| 2110 | 2110 | // A WAV is read whole up to `WAVE_CAP`, because its waveform is drawn from the samples. |
| 2111 | 2111 | if (blob.kind !== "too_large" && blob.size <= WAVE_CAP && fileKind(path, blob.body.subarray(0, HEAD_BYTES)) === "audio" && /\.wav$/i.test(path)) |
| 2112 | 2112 | return blob; |
| 2113 | // A notebook is read whole up to its own cap, because its cells are parsed from the bytes. | |
| 2114 | if (blob.kind === "text" && blob.size <= RENDER_CAP.notebook && kindByName(path) === "notebook") return blob; | |
| 2113 | 2115 | // A table file's body is already bounded by its prefix; the renderer draws a window of it. |
| 2114 | 2116 | if (blob.kind !== "too_large" && blob.body.length <= CSV_PREFIX_BYTES && isTablePath(path)) return blob; |
| 2115 | 2117 | // A big JSON file's body is already its prefix; the renderer outlines it. |
src/web/render/kinds.ts +1 −1
| @@ -149,7 +149,7 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 149 | 149 | wasm: 5 * MB, |
| 150 | 150 | config: 1 * MB, |
| 151 | 151 | markdown: 1 * MB, |
| 152 | notebook: 1 * MB, | |
| 152 | notebook: 2 * MB, | |
| 153 | 153 | patch: 1 * MB, |
| 154 | 154 | log: 1 * MB, |
| 155 | 155 | subtitles: 5 * MB, |
test/blob-notebook-page.test.ts +36 −5
| @@ -9,8 +9,11 @@ import { tmpdir } from "node:os"; | ||
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 11 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 12 | import { drawCeiling } from "../src/view/local.ts"; | |
| 12 | 13 | import { serve, type Servers } from "../src/http/server.ts"; |
| 14 | import { notebook } from "../src/web/render/notebook.ts"; | |
| 13 | 15 | import { seedAdmin } from "./helpers/admin.ts"; |
| 16 | import { expectWork } from "./helpers/work.ts"; | |
| 14 | 17 | import { quietLogs } from "./helpers/globals.ts"; |
| 15 | 18 | import { fileSize } from "../src/web/ui.ts"; |
| 16 | 19 | |
| @@ -35,17 +38,20 @@ describe("a .ipynb on its blob page", () => { | ||
| 35 | 38 | const small = JSON.stringify({ |
| 36 | 39 | cells: [{ cell_type: "code", execution_count: 1, source: "1", outputs: [{ output_type: "execute_result", data: { "text/html": "<script>alert(1)</script>" } }] }], |
| 37 | 40 | }); |
| 38 | const big = JSON.stringify({ cells: [{ cell_type: "markdown", source: "x".repeat(1_100_000) }] }); | |
| 41 | const mid = JSON.stringify({ cells: [{ cell_type: "code", execution_count: 7, source: "x".repeat(1_100_000), outputs: [] }] }); | |
| 42 | const big = JSON.stringify({ cells: [{ cell_type: "markdown", source: "x".repeat(2_050_000) }] }); | |
| 39 | 43 | bigSize = fileSize(new TextEncoder().encode(big).length); |
| 40 | 44 | const made = await commitToBranch(app, a.value, created.value, { |
| 41 | 45 | message: "notebooks", |
| 42 | 46 | files: [ |
| 43 | 47 | { path: "ok.ipynb", content: small }, |
| 44 | { path: "big.ipynb", content: big }, | |
| 48 | { path: "mid.ipynb", content: mid }, | |
| 45 | 49 | { path: "bad.ipynb", content: "{not json" }, |
| 46 | 50 | ], |
| 47 | 51 | }); |
| 48 | 52 | if (!made.ok) throw new Error("commit failed"); |
| 53 | const bigMade = await commitToBranch(app, a.value, created.value, { message: "big", files: [{ path: "big.ipynb", content: big }] }); | |
| 54 | if (!bigMade.ok) throw new Error("big commit failed"); | |
| 49 | 55 | }, 60_000); |
| 50 | 56 | |
| 51 | 57 | afterAll(async () => { |
| @@ -61,18 +67,25 @@ describe("a .ipynb on its blob page", () => { | ||
| 61 | 67 | expect(text).not.toContain("<script>alert(1)"); |
| 62 | 68 | }); |
| 63 | 69 | |
| 64 | test("over 1 MB, the page says why it is not drawn as cells", async () => { | |
| 70 | test("a notebook between 1 and 2 MB is still drawn as cells", async () => { | |
| 71 | expect(drawCeiling("a.ipynb")).toBe(2_000_000); | |
| 72 | const text = await (await fetch(`${base}/alice/p/blob/main/mid.ipynb`)).text(); | |
| 73 | expect(text).toContain("In [7]:"); | |
| 74 | expect(text).not.toContain("not drawn as cells"); | |
| 75 | }); | |
| 76 | ||
| 77 | test("over 2 MB, the page says why it is not drawn as cells", async () => { | |
| 65 | 78 | const res = await fetch(`${base}/alice/p/blob/main/big.ipynb`); |
| 66 | 79 | const text = await res.text(); |
| 67 | 80 | expect(res.status).toBe(200); |
| 68 | 81 | // The notebook's own sentence says the limit, once; the generic reason line does not repeat it |
| 69 | 82 | // (#15719). |
| 70 | expect(text).toContain("This notebook is over 1 MB, so it is not drawn as cells"); | |
| 83 | expect(text).toContain("This notebook is over 2 MB, so it is not drawn as cells"); | |
| 71 | 84 | expect(text).not.toContain("data-plain-why"); |
| 72 | 85 | expect(text).not.toContain("In ["); |
| 73 | 86 | // The sentence names this file's size, and the raw and download links are on the page. |
| 74 | 87 | expect(text).toContain(`(this one is ${bigSize})`); |
| 75 | expect(bigSize).toMatch(/^1\.\d MB$/); | |
| 88 | expect(bigSize).toMatch(/^2\.\d MB$/); | |
| 76 | 89 | expect(text).toContain("/alice/p/raw/main/big.ipynb"); |
| 77 | 90 | expect(text).toContain("/alice/p/media/main/big.ipynb?download=1"); |
| 78 | 91 | }); |
| @@ -82,3 +95,21 @@ describe("a .ipynb on its blob page", () => { | ||
| 82 | 95 | expect(text).toContain("not valid JSON"); |
| 83 | 96 | }); |
| 84 | 97 | }); |
| 98 | ||
| 99 | describe("the worst-case 2 MB notebook", () => { | |
| 100 | test("is parsed and drawn once per cell, linear in its size", () => { | |
| 101 | // Many small cells with outputs: the most work per byte the parser can be given. | |
| 102 | const cell = { cell_type: "code", execution_count: 1, source: "print('<b>x</b>')\n".repeat(5), outputs: [{ output_type: "stream", name: "stdout", text: "line <i>\n".repeat(5) }] }; | |
| 103 | const one = JSON.stringify(cell).length + 1; | |
| 104 | const body = JSON.stringify({ cells: Array.from({ length: Math.floor(1_990_000 / one) }, () => cell) }); | |
| 105 | expect(body.length).toBeLessThanOrEqual(2_000_000); | |
| 106 | const bytes = new TextEncoder().encode(body); | |
| 107 | const out = notebook({ path: "w.ipynb", size: bytes.length, body: bytes, media: "/m", download: "/d" }); | |
| 108 | expect(out.fallback).toBeUndefined(); | |
| 109 | // Work, not a clock: every cell is drawn once, and the page stays linear in the input. | |
| 110 | const cells = Math.floor(1_990_000 / one); | |
| 111 | expect(out.content.text.split("<b>x</b>").length - 1).toBe(0); | |
| 112 | expect(out.content.text.split("nb-cell").length - 1).toBe(Math.min(cells, 2000)); | |
| 113 | expectWork(out.content.text.length, { max: bytes.length * 4 }); | |
| 114 | }); | |
| 115 | }); |
new above
offline