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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16019-raise-the-read-ceiling-for-ipynb-so-note → main 0 commits fork

  • Merged. scout/16019-raise-the-read-ceiling-for-ipynb-so-note was merged into main as 23d12be.

Review

Sign in to review this change.
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
3535 - **WebAssembly module**, drawn up to 5 MB: `.wasm`
3636 - **Config**, drawn up to 1 MB: `.json`, `.jsonc`, `.yaml`, `.yml`, `.toml`, `.conf`
3737 - **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`
3939 - **Patch**, drawn up to 1 MB: `.patch`, `.diff`
4040 - **Log**, drawn up to 1 MB: `.log`, `.ansi`
4141 - **Subtitles**, drawn up to 5 MB: `.srt`, `.vtt`
docs/features/notebook.md +1 −1
@@ -6,5 +6,5 @@ row: Jupyter notebooks
66
77 A `.ipynb` file opens on the blob page as its cells and saved outputs (`src/web/render/notebook.ts`). Nothing is run.
88 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.
1010 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 {
21102110 // A WAV is read whole up to `WAVE_CAP`, because its waveform is drawn from the samples.
21112111 if (blob.kind !== "too_large" && blob.size <= WAVE_CAP && fileKind(path, blob.body.subarray(0, HEAD_BYTES)) === "audio" && /\.wav$/i.test(path))
21122112 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;
21132115 // A table file's body is already bounded by its prefix; the renderer draws a window of it.
21142116 if (blob.kind !== "too_large" && blob.body.length <= CSV_PREFIX_BYTES && isTablePath(path)) return blob;
21152117 // 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>> = {
149149 wasm: 5 * MB,
150150 config: 1 * MB,
151151 markdown: 1 * MB,
152 notebook: 1 * MB,
152 notebook: 2 * MB,
153153 patch: 1 * MB,
154154 log: 1 * MB,
155155 subtitles: 5 * MB,
test/blob-notebook-page.test.ts +36 −5
@@ -9,8 +9,11 @@ import { tmpdir } from "node:os";
99 import { join } from "node:path";
1010 import { createApp, type App } from "../src/app.ts";
1111 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
12 import { drawCeiling } from "../src/view/local.ts";
1213 import { serve, type Servers } from "../src/http/server.ts";
14 import { notebook } from "../src/web/render/notebook.ts";
1315 import { seedAdmin } from "./helpers/admin.ts";
16 import { expectWork } from "./helpers/work.ts";
1417 import { quietLogs } from "./helpers/globals.ts";
1518 import { fileSize } from "../src/web/ui.ts";
1619
@@ -35,17 +38,20 @@ describe("a .ipynb on its blob page", () => {
3538 const small = JSON.stringify({
3639 cells: [{ cell_type: "code", execution_count: 1, source: "1", outputs: [{ output_type: "execute_result", data: { "text/html": "<script>alert(1)</script>" } }] }],
3740 });
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) }] });
3943 bigSize = fileSize(new TextEncoder().encode(big).length);
4044 const made = await commitToBranch(app, a.value, created.value, {
4145 message: "notebooks",
4246 files: [
4347 { path: "ok.ipynb", content: small },
44 { path: "big.ipynb", content: big },
48 { path: "mid.ipynb", content: mid },
4549 { path: "bad.ipynb", content: "{not json" },
4650 ],
4751 });
4852 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");
4955 }, 60_000);
5056
5157 afterAll(async () => {
@@ -61,18 +67,25 @@ describe("a .ipynb on its blob page", () => {
6167 expect(text).not.toContain("<script>alert(1)");
6268 });
6369
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 () => {
6578 const res = await fetch(`${base}/alice/p/blob/main/big.ipynb`);
6679 const text = await res.text();
6780 expect(res.status).toBe(200);
6881 // The notebook's own sentence says the limit, once; the generic reason line does not repeat it
6982 // (#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");
7184 expect(text).not.toContain("data-plain-why");
7285 expect(text).not.toContain("In [");
7386 // The sentence names this file's size, and the raw and download links are on the page.
7487 expect(text).toContain(`(this one is ${bigSize})`);
75 expect(bigSize).toMatch(/^1\.\d MB$/);
88 expect(bigSize).toMatch(/^2\.\d MB$/);
7689 expect(text).toContain("/alice/p/raw/main/big.ipynb");
7790 expect(text).toContain("/alice/p/media/main/big.ipynb?download=1");
7891 });
@@ -82,3 +95,21 @@ describe("a .ipynb on its blob page", () => {
8295 expect(text).toContain("not valid JSON");
8396 });
8497 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs