A diff is plain text until the script has drawn it, so it is never an empty box #3
merged git-mobile-diff was squashed into main as 7f307d7 by jonas, 2026-11-26 22:25 UTC. jonas opened it 2026-11-26 22:25 UTC.
Jonas, 26 Nov 2026, in Slack, with a picture of a commit's page on his phone: "Is it meant to look like this on mobile?", then "Fix it". Each changed file showed its name and an empty box. The mind's own record of that page load says why: `exception/seen` on /exceptions, offset 2873, 22:08:13 UTC, kind module-load, "script did not load: /page.js". The page's script did not load on the phone, and a diff was drawn by the script alone. Why the load failed there is not known: the same page loads in Chromium and in WebKitGTK 2.54 at 390 px, every module of the script answers 200 to an iPhone's user agent, and the mind's Workers recorded no failure at that time. So a diff no longer needs the script: - pages.ts writes each diff as plain text in its place, as `git diff` shows it (plain-diff.ts: Myers, three lines around a change; a change of more than 1500 steps is the old lines, then the new ones). page.js draws its own diff beside it and takes the text away once that has a height, as it does for a file. - When the script does not load, the page says so and says to reload it. - A commit's diffs no longer wait for the markdown of a pull's threads: only a pull has threads. What a reader gets is the same text as before, in one more form: nothing new is reachable. Checked: node --test "tests/*.test.mjs" (37 pass; plain-diff.test.mjs holds the text against git's own diff, 20 random texts), tsc; WebKitGTK 2.54 at 390 px and 1280 px on tests/serve.mjs (drawn diffs, no plain text left), and at 390 px with /page.js answered 503 (plain diffs, the note); a candidate of the config, chat, git and stdlib tips with these files answers a public commit's page with its plain diffs. Not checked on a real iPhone. Via: /agents/claude/git-mobile-diff-mwy3y1ok-633f1a Session: claude 311deb66-082e-43f2-a88f-cad5e0e1e0c7 on beelink Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NCuxgXGuEBDvCTy9DmTVMT
Conversation
Nobody has said anything yet.
Sign in to comment or to merge.
Files
6 files changed
- M pages.ts
- A plain-diff.ts
- M public/page.css
- M public/page.js
- M tests/pages.test.mjs
- A tests/plain-diff.test.mjs
M pages.ts
@@ -15,6 +15,7 @@ import type { RepoLogEntry } from "iterate/api"; import type { IterateConfigEntrypoint } from "iterate/sdk"; import { z } from "zod"; +import { plainDiff } from "./plain-diff.ts"; import { type LinePlace, type Pull, pullsIn, type Standing, standingOf } from "./pulls.ts"; type Host = Pick<IterateConfigEntrypoint, "getItx" | "auth">; @@ -530,8 +531,20 @@ return changes; } -/** The files that changed: how many, their names, then each one under a name that folds it. The - * script draws each diff in its `data-diff` from the two texts (`diffsOf`). */ +/** A diff as plain text, which needs no script: the lines that went and came, as `git diff` + * shows them. It is what a reader sees until the script has drawn the diff, and all that a + * reader sees when the script did not load. */ +function plainHtml(sides: { before: string; after: string }) { + const look = { " ": "", "-": ` class="went"`, "+": ` class="came"`, "@": ` class="part"` }; + const lines = plainDiff(sides.before, sides.after).map( + ({ kind, text }) => `<span${look[kind]}>${esc(kind === "@" ? text : kind + text)}</span>`, + ); + return `<pre class="plain">${lines.join("\n")}</pre>`; +} + +/** The files that changed: how many, their names, then each one under a name that folds it. Each + * diff is there as plain text; the script draws it again in its `data-diff`, from the two texts + * (`diffsOf`), and then takes the plain text away. */ function filesHtml(changes: Change[]) { const letter = { added: "A", deleted: "D", modified: "M" }; const names = changes.map( @@ -540,7 +553,7 @@ ); const diffs = changes.map((change, index) => { const shown = change.sides - ? `<div class="diff" data-diff="${index}"></div>` + ? `<div class="diff" data-diff="${index}">${plainHtml(change.sides)}</div>` : note(change.note || ""); // each file folds: the reader closes the ones they have read return `<details class="changed" id="diff-${index}" open><summary><span class="status ${change.status}">${letter[change.status]}</span> ${esc(change.path)}</summary>${shown}</details>`; @@ -603,7 +616,7 @@ <header class="where">${where}</header> <main>${main}</main> <script type="application/json" id="data">${forScript}</script> - <script type="module" src="/page.js"></script> + <script type="module" src="/page.js" onerror="document.documentElement.classList.add('unloaded')"></script> </body> </html> `;
A plain-diff.ts
@@ -0,0 +1,115 @@ +// plain-diff.ts — A CHANGE AS LINES OF PLAIN TEXT, worked out where the page is written. +// +// The page's script draws each diff with colours and comments, from the two texts. A script that +// has not loaded yet, or that did not load at all (a phone on a weak network), draws nothing: so +// pages.ts writes each diff as text first, and the script puts its own in that place when it has +// drawn. This file works that text out: the lines that went and came, with three lines around +// them, as `git diff` shows them. + +/** One line of a plain diff: a line that stayed (" "), went ("-") or came ("+"), or the line + * that says where a part is ("@"). */ +export type PlainLine = { kind: " " | "-" | "+" | "@"; text: string }; + +/** A change of more steps than this is shown as the old lines, then the new ones. */ +const MOST_STEPS = 1500; +const AROUND = 3; + +const linesOf = (text: string) => { + const lines = text.split("\n"); + if (lines.at(-1) === "") lines.pop(); + return lines; +}; + +type Step = { kind: " " | "-" | "+"; text: string }; + +/** The fewest lines to take out and put in that turn `a` into `b` (Myers), in order. */ +function stepsOf(a: string[], b: string[]): Step[] { + const whole = (): Step[] => [ + ...a.map((text) => ({ kind: "-" as const, text })), + ...b.map((text) => ({ kind: "+" as const, text })), + ]; + if (a.length === 0 || b.length === 0) return whole(); + const most = Math.min(MOST_STEPS, a.length + b.length); + const at = most + 1; + const reach = new Int32Array(2 * most + 3); + // what `reach` held before each step, from diagonal -(step + 1) to step + 1 + const before: Int32Array[] = []; + let found = -1; + for (let step = 0; step <= most && found < 0; step++) { + before.push(reach.slice(at - step - 1, at + step + 2)); + for (let k = -step; k <= step; k += 2) { + const down = k === -step || (k !== step && reach[at + k - 1]! < reach[at + k + 1]!); + let x = down ? reach[at + k + 1]! : reach[at + k - 1]! + 1; + let y = x - k; + while (x < a.length && y < b.length && a[x] === b[y]) x++, y++; + reach[at + k] = x; + if (x >= a.length && y >= b.length) { + found = step; + break; + } + } + } + if (found < 0) return whole(); + const steps: Step[] = []; + let x = a.length; + let y = b.length; + for (let step = found; step > 0; step--) { + const was = before[step]!; + const read = (k: number) => was[k + step + 1]!; + const k = x - y; + const down = k === -step || (k !== step && read(k - 1) < read(k + 1)); + const fromK = down ? k + 1 : k - 1; + const fromX = read(fromK); + const fromY = fromX - fromK; + while (x > fromX && y > fromY) steps.push({ kind: " ", text: a[--x]! }), y--; + if (down) steps.push({ kind: "+", text: b[--y]! }); + else steps.push({ kind: "-", text: a[--x]! }); + } + while (x > 0 && y > 0) steps.push({ kind: " ", text: a[--x]! }), y--; + return steps.reverse(); +} + +/** The change from `before` to `after` as the lines of a unified diff: each part that changed, + * with three lines around it, under a line that says where it is. No line when the two texts + * have the same lines. */ +export function plainDiff(before: string, after: string): PlainLine[] { + const a = linesOf(before); + const b = linesOf(after); + let head = 0; + while (head < a.length && head < b.length && a[head] === b[head]) head++; + let tail = 0; + while (tail < a.length - head && tail < b.length - head && a[a.length - 1 - tail] === b[b.length - 1 - tail]) tail++; + const same = (lines: string[]) => lines.map((text) => ({ kind: " " as const, text })); + const steps: Step[] = [ + ...same(a.slice(Math.max(0, head - AROUND), head)), + ...stepsOf(a.slice(head, a.length - tail), b.slice(head, b.length - tail)), + ...same(a.slice(a.length - tail, a.length - tail + AROUND)), + ]; + // the line of each side that the first step is on + let oldLine = Math.max(0, head - AROUND) + 1; + let newLine = oldLine; + const changed = steps.map((step) => step.kind !== " "); + const shown = steps.map((_, index) => { + for (let near = Math.max(0, index - AROUND); near <= Math.min(steps.length - 1, index + AROUND); near++) + if (changed[near]) return true; + return false; + }); + const lines: PlainLine[] = []; + for (let index = 0; index < steps.length; ) { + if (!shown[index]) { + oldLine++, newLine++, index++; + continue; + } + let end = index; + while (end < steps.length && shown[end]) end++; + const part = steps.slice(index, end); + const olds = part.filter((step) => step.kind !== "+").length; + const news = part.filter((step) => step.kind !== "-").length; + lines.push({ kind: "@", text: `@@ -${olds ? oldLine : oldLine - 1},${olds} +${news ? newLine : newLine - 1},${news} @@` }); + lines.push(...part); + oldLine += olds; + newLine += news; + index = end; + } + return lines; +}
M public/page.css
@@ -57,6 +57,15 @@ border-radius: 0.5rem 0.5rem 0 0; } .changed > .note { padding: 0.5rem 0.75rem; } .diff { min-height: 4rem; overflow: hidden; border-radius: 0 0 0.5rem 0.5rem; } +/* a diff as plain text: what is there before the script draws it, and when no script loads */ +.diff > .plain { padding: 0.5rem 0.75rem; } +.plain .came { color: light-dark(oklch(0.45 0.15 150), oklch(0.78 0.15 150)); } +.plain .went { color: var(--destructive); } +.plain .part { color: var(--muted-foreground); } +/* the script did not load (pages.ts sets the class): say so, and what helps */ +.unloaded main::before { content: "This page's script did not load, so the page shows plain text. Reload it to try again."; + display: block; margin: 0.75rem 1rem 0; padding: 0.4rem 0.75rem; border: 1px solid var(--border); + border-radius: 0.5rem; color: var(--muted-foreground); font-size: 0.8125rem; } .status { font-weight: 600; } .status.added { color: light-dark(oklch(0.5 0.15 150), oklch(0.75 0.15 150)); } .status.deleted { color: var(--destructive); }
M public/page.js
@@ -2,7 +2,7 @@ // and hands this script `data` (a JSON element of the page). The script draws, in their places: // // - the file tree, and each file and README in the place of its <pre> -// - each diff, when the reader scrolls near it +// - each diff, when the reader scrolls near it, in the place of its plain text // - on a pull, for a member: the conversation with its reply boxes, a comment box on any line // of a diff, and the buttons that merge or close; on a compare page, the box that opens a pull // @@ -349,14 +349,18 @@ if (file) file.open = true; }); MultiFileDiff = (await import("@pierre/diffs/react")).MultiFileDiff; - // a thread on a line is drawn with markdown - if (threads.length || data.me) await markdown(); + // a thread on a line is drawn with markdown, and only a pull has threads: a commit's diffs + // do not wait for it + if (data.pull && (threads.length || data.me)) await markdown(); const near = new IntersectionObserver( (entries) => { for (const entry of entries.filter((entry) => entry.isIntersecting)) { near.unobserve(entry.target); const index = Number(entry.target.dataset.diff); - diffRoots.set(index, createRoot(entry.target)); + // the plain text stays until the diff is drawn, so a diff is never an empty box + const plain = entry.target.querySelector("pre.plain"); + const place = plain ? inPlaceOf(plain, "drawn") : entry.target; + diffRoots.set(index, createRoot(place)); drawDiff(index); } },
M tests/pages.test.mjs
@@ -146,10 +146,11 @@ ["#diff-1", "changed.ts"], ["#diff-2", "gone.ts"], ]); - // each file is open, under its name: a click on the name folds it, with no script + // each file is open, under its name: a click on the name folds it, with no script. Its diff + // is there as plain text, for a reader whose script has not drawn it assert.ok( html.includes( - `<details class="changed" id="diff-1" open><summary><span class="status modified">M</span> changed.ts</summary><div class="diff" data-diff="1"></div></details>`, + `<details class="changed" id="diff-1" open><summary><span class="status modified">M</span> changed.ts</summary><div class="diff" data-diff="1"><pre class="plain"><span class="part">@@ -1,1 +1,1 @@</span>\n<span class="went">-old</span>\n<span class="came">+new</span></pre></div></details>`, ), ); assert.deepEqual(dataOf(html), {
A tests/plain-diff.test.mjs
@@ -0,0 +1,83 @@ +import "@repos/stdlib/testing/mind-loader.mjs"; +import assert from "node:assert/strict"; +import { execFileSync } from "node:child_process"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { test } from "node:test"; + +const { plainDiff } = await import("../plain-diff.ts"); + +const text = (lines) => lines.map((line) => (line.kind === "@" ? line.text : line.kind + line.text)).join("\n"); + +/** What `git diff` says of the same two texts: the parts alone, without its heading. */ +function gitDiff(before, after) { + const folder = mkdtempSync(join(tmpdir(), "plain-diff-")); + try { + writeFileSync(join(folder, "a"), before); + writeFileSync(join(folder, "b"), after); + let out = ""; + try { + execFileSync("git", ["diff", "--no-index", "--no-color", "--diff-algorithm=myers", "a", "b"], { cwd: folder }); + } catch (error) { + out = String(error.stdout); + } + return out + .split("\n") + .filter((line) => /^[ +\-@]/.test(line) && !/^(---|\+\+\+) /.test(line)) + .map((line) => line.replace(/^(@@ [^@]+ @@).*$/, "$1").replace(/(\d+),1 /g, "$1,1 ")) + .join("\n"); + } finally { + rmSync(folder, { recursive: true, force: true }); + } +} +/** git writes `-4` for `-4,1`: the same place. */ +const counted = (diff) => diff.replace(/^@@ -(\d+) /gm, "@@ -$1,1 ").replace(/^(@@ -\d+,\d+) \+(\d+) @@/gm, "$1 +$2,1 @@"); + +test("a changed line is shown as the line that went and the line that came, with three lines around", () => { + const before = "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\n"; + const after = before.replace("five", "FIVE"); + assert.equal(text(plainDiff(before, after)), "@@ -2,7 +2,7 @@\n two\n three\n four\n-five\n+FIVE\n six\n seven\n eight"); +}); + +test("a file that came or went is every line of it, and two texts with the same lines are nothing", () => { + assert.equal(text(plainDiff("", "hello\nthere\n")), "@@ -0,0 +1,2 @@\n+hello\n+there"); + assert.equal(text(plainDiff("bye\n", "")), "@@ -1,1 +0,0 @@\n-bye"); + assert.deepEqual(plainDiff("same\n", "same\n"), []); +}); + +test("changes far apart are parts of their own, each with its place, as git shows them", () => { + const lines = Array.from({ length: 60 }, (_, index) => `line ${index + 1}`); + const next = [...lines]; + next[4] = "changed 5"; + next.splice(30, 2); + next.splice(50, 0, "came a", "came b"); + const before = lines.join("\n") + "\n"; + const after = next.join("\n") + "\n"; + assert.equal(text(plainDiff(before, after)), counted(gitDiff(before, after))); +}); + +test("texts of many changed lines give the same parts as git", () => { + let seed = 7; + const random = () => (seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648; + for (let round = 0; round < 20; round++) { + const lines = Array.from({ length: 5 + Math.floor(random() * 200) }, (_, index) => `row ${index} ${Math.floor(random() * 4)}`); + const next = lines.flatMap((line) => (random() < 0.08 ? [] : random() < 0.08 ? [line, `new ${Math.floor(random() * 9)}`] : [line])); + const before = lines.join("\n") + "\n"; + const after = next.join("\n") + "\n"; + const ours = plainDiff(before, after); + // the lines that went and came turn the one text into the other + assert.equal(ours.filter((line) => line.kind === "-").length - ours.filter((line) => line.kind === "+").length, lines.length - next.length); + assert.equal(text(ours), counted(gitDiff(before, after)), `round ${round}`); + } +}); + +test("a change of more steps than the page works out is the old lines, then the new ones", () => { + const before = Array.from({ length: 2000 }, (_, index) => `old ${index}`).join("\n") + "\n"; + const after = Array.from({ length: 2000 }, (_, index) => `new ${index}`).join("\n") + "\n"; + const lines = plainDiff(before, after); + assert.equal(lines.length, 4001); + assert.equal(lines[0].text, "@@ -1,2000 +1,2000 @@"); + assert.deepEqual(lines[1], { kind: "-", text: "old 0" }); + assert.deepEqual(lines.at(-1), { kind: "+", text: "new 1999" }); +});