Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 3e11767e79e1b1acc34fc7c9acbfe7a2c1a2d25a
parent 639ebb07b989e51987f2856dca4be86f85acad05
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Wed, 14 Oct 2026 19:56:57 -0400

umtool: the board's redraw gap — a key after a write has answered but before its answer is drawn waits for the draw

- Race 3 (R6.1's full run, board.spec:127 "`e` did not focus the claim"): a
  write's answer is applied (setView, the list's key) and the write ends in
  microtasks, but React draws it in a later task. The board counted itself
  settled at the write's end, so a key landing in between acted on the list
  the redraw was about to replace: `e` focused the old claim field, the redraw
  took it away, the claim typed next ran as board commands. Under load the gap
  is wide. The pending count now drops only in a layout effect on the commit
  that carries the answer (`drawn`): until then keys are held, as during the
  write.
- `data-board-keys="ready"` once the board's key listeners are attached.
- board.spec: a test that hits the gap for certain (the page's Response.json
  wrapped, spec-only, to dispatch `e` 50 microtasks after the held move's
  answer is read: after the write, before any task); every board test opens
  the board through `openBoard`, which waits for its keys to be live.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Diffstat:
Mumtool/components/research/BoardView.tsx | 24+++++++++++++++++++++---
Mumtool/e2e/board.spec.ts | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 102 insertions(+), 9 deletions(-)

diff --git a/umtool/components/research/BoardView.tsx b/umtool/components/research/BoardView.tsx @@ -2,7 +2,7 @@ import Link from "next/link"; import { useRouter } from "next/navigation"; -import { useCallback, useEffect, useMemo, useRef, useState } from "react"; +import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import AppHeader from "@/components/shell/AppHeader"; import { buttonVariants } from "@/components/ui/button"; import { Dialog } from "@/components/ui/dialog"; @@ -175,12 +175,26 @@ export default function BoardView({ initial }: { initial: CaseView }) { return null; }); chain.current = run.finally(() => { - pendingRef.current -= 1; + // NOT settled yet: the answer is applied (setView, the list's key) but + // not drawn -- React renders it in a later task, and a key can arrive + // in between (under load, often). The ref drops once the render that + // carries this answer has committed (the layout effect below). + answered.current += 1; setPending((n) => n - 1); + setDrawn((n) => n + 1); }); return run; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + // Writes answered but not yet drawn. A state update is never rendered ahead + // of one queued before it in the same lane, so the commit that carries + // `drawn`'s update carries the answer's view and list key too. + const answered = useRef(0); + const [drawn, setDrawn] = useState(0); + useLayoutEffect(() => { + pendingRef.current -= answered.current; + answered.current = 0; + }, [drawn]); const apply = (v: CaseView) => { docRef.current = v.case; setView(v); @@ -339,6 +353,9 @@ export default function BoardView({ initial }: { initial: CaseView }) { const dispatching = useRef(false); const [heldN, setHeldN] = useState(0); const [kick, setKick] = useState(0); + // The keys are live (hydrated, every listener attached): a page's first key + // before this is the browser's, not the board's. + const [keysReady, setKeysReady] = useState(false); useEffect(() => { const onKey = (e: KeyboardEvent) => { if (dispatching.current || e.metaKey || e.ctrlKey) return; @@ -353,6 +370,7 @@ export default function BoardView({ initial }: { initial: CaseView }) { setHeldN(held.current.length); }; window.addEventListener("keydown", onKey, true); + setKeysReady(true); return () => window.removeEventListener("keydown", onKey, true); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); @@ -519,7 +537,7 @@ export default function BoardView({ initial }: { initial: CaseView }) { /> <div className="grid min-h-0 flex-1 grid-cols-[minmax(0,1fr)_300px]"> <div className="flex min-h-0 min-w-0 flex-col"> - <div className="min-h-0 flex-1 overflow-auto px-7 pb-4 pt-2" data-board-busy={busy || heldN > 0 ? "" : undefined}> + <div className="min-h-0 flex-1 overflow-auto px-7 pb-4 pt-2" data-board-busy={busy || heldN > 0 ? "" : undefined} data-board-keys={keysReady ? "ready" : undefined}> {doc.quotes.length === 0 && doc.sections.length === 0 ? ( <EmptyState title="No quotes on this board yet"> <Link prefetch={false} href={researchFromCaseHref(doc)} className="ink-link"> diff --git a/umtool/e2e/board.spec.ts b/umtool/e2e/board.spec.ts @@ -50,6 +50,16 @@ async function holdNext(page: Page, method: string, part: string) { return { seen, release, done: () => page.unroute(pattern, handler) }; } +/** + * Open a board and wait until its keys are live (hydrated, every listener + * attached: `data-board-keys="ready"`). Before that a key is the browser's: + * the slips are server-drawn, but nothing on the page listens yet. + */ +async function openBoard(page: Page, id: string) { + await page.goto(researchCaseHref(id)); + await expect(page.locator('[data-board-keys="ready"]')).toHaveCount(1); +} + async function searched(page: Page) { await page.goto(researchHref({ q: RESEARCH_PHRASE })); await expect(page.locator("[data-honest]")).toBeVisible({ timeout: 30_000 }); @@ -125,7 +135,7 @@ test("pin from the results with p (a new case, named on the way) and from the re }); test("sections and order: a, alt+j/k across sections, the section menu; claim, note and speaker saved", async ({ page }) => { - await page.goto(researchCaseHref("lighthouse-case")); + await openBoard(page, "lighthouse-case"); const b = board(page); await expect(b.locator("[data-quote]")).toHaveCount(3); await page.locator("body").click(); @@ -197,7 +207,7 @@ test("sections and order: a, alt+j/k across sections, the section menu; claim, n // fields the answer replaces. On the board as test 102 left it: Unsorted q3, // q1; The keeper q2; Approval. test("keys pressed while a write is held wait for it, then act on the settled board", async ({ page }) => { - await page.goto(researchCaseHref("lighthouse-case")); + await openBoard(page, "lighthouse-case"); const b = board(page); await expect(b.locator("[data-quote]")).toHaveCount(3); const busy = page.locator("[data-board-busy]"); @@ -311,6 +321,71 @@ test("keys pressed while a write is held wait for it, then act on the settled bo expect(order()).toBe("q3 q1"); }); +// THE REDRAW GAP (race 3, after R6.1): a write's answer is applied before it +// is drawn -- React renders it in a later task -- and a key landing between +// the two acted on the list the redraw was about to replace (`e` focused the +// old claim field, the redraw took it away, the claim typed next ran as board +// commands). Under load that gap is wide; here it is hit for certain: the +// page's own Response.json is wrapped (spec-only) so that, when the held +// move's answer is read, `e` is dispatched 50 microtasks later -- after the +// write has finished, before any task (so before React draws the answer). +test("a key landing after a write has answered but before the board has redrawn waits for the redraw", async ({ page }) => { + await openBoard(page, "lighthouse-case"); + const b = board(page); + await expect(b.locator("[data-quote]")).toHaveCount(3); + const quote = (id: string) => caseJson("lighthouse-case").quotes.find((q: { id: string }) => q.id === id); + const order = () => + caseJson("lighthouse-case") + .quotes.filter((q: { section: string | null }) => q.section === null) + .map((q: { id: string }) => q.id) + .join(" "); + await expect.poll(order).toBe("q3 q1"); + await b.locator('[data-quote="q3"]').click({ position: { x: 8, y: 8 } }); + await expect(litId(page)).toHaveAttribute("data-lit-id", "q:q3"); + + const h = await holdNext(page, "PATCH", "/cases/lighthouse-case"); + await page.keyboard.press("Alt+j"); + await h.seen; + await page.evaluate(() => { + const w = window as unknown as { __gap?: string }; + w.__gap = "armed"; + const json = Response.prototype.json; + Response.prototype.json = function (this: Response) { + const p = json.call(this); + if (w.__gap === "armed" && this.url.endsWith("/api/research/cases/lighthouse-case")) { + w.__gap = "answered"; + void p.then(async () => { + for (let i = 0; i < 50; i++) await null; + const el = (document.activeElement as HTMLElement | null) ?? document.body; + el.dispatchEvent(new KeyboardEvent("keydown", { key: "e", code: "KeyE", bubbles: true, cancelable: true })); + w.__gap = "sent"; + Response.prototype.json = json; + }); + } + return p; + }; + }); + h.release(); + await page.waitForFunction(() => (window as unknown as { __gap?: string }).__gap === "sent"); + await page.keyboard.type("Recorded in the gap"); + await page.keyboard.press("Enter"); + await expect.poll(() => quote("q3").claim).toBe("Recorded in the gap"); + await expect(page.locator("[data-board-busy]")).toHaveCount(0); + await h.done(); + expect(order()).toBe("q1 q3"); + expect(caseJson("lighthouse-case").title).toBe("Lighthouse case"); + await expect(page.locator("dialog[open]")).toHaveCount(0); + + // Back as the tests below expect it. + expect((await page.request.patch("/api/research/cases/lighthouse-case/quotes/q3", { data: { claim: "" } })).ok()).toBe(true); + await page.reload(); + await expect(page.locator('[data-board-keys="ready"]')).toHaveCount(1); + await b.locator('[data-quote="q3"]').click({ position: { x: 8, y: 8 } }); + await page.keyboard.press("Alt+k"); + await expect.poll(order).toBe("q3 q1"); + await expect(page.locator("[data-board-busy]")).toHaveCount(0); +}); + test("the export: cited markdown in the house format, copied and downloadable", async ({ page, context, request }) => { const md = await (await request.get("/api/research/cases/lighthouse-case/export.md")).text(); const heads = md.split("\n").filter((l) => l.startsWith("#")); @@ -325,7 +400,7 @@ test("the export: cited markdown in the house format, copied and downloadable", expect(dl.headers()["content-disposition"]).toBe('attachment; filename="lighthouse-case.md"'); await context.grantPermissions(["clipboard-read", "clipboard-write"]); - await page.goto(researchCaseHref("lighthouse-case")); + await openBoard(page, "lighthouse-case"); await expect(board(page).locator("[data-quote]")).toHaveCount(3); await page.locator("body").click(); await page.keyboard.press("c"); @@ -334,7 +409,7 @@ test("the export: cited markdown in the house format, copied and downloadable", }); test("hand-offs: an article's plan (the model and cap, a seeded sweep) and a video's (the cue spans); local-only refused", async ({ page, request }) => { - await page.goto(researchCaseHref("lighthouse-case")); + await openBoard(page, "lighthouse-case"); await page.getByRole("button", { name: "Draft an article from this board" }).click(); const art = page.locator('dialog[data-handoff="article"]'); await expect(art).toContainText("lighthouse-case"); @@ -369,7 +444,7 @@ test("hand-offs: an article's plan (the model and cap, a seeded sweep) and a vid }); test("re-verify: the drifted quote shows the old and the new words, en vs en-orig is flagged, and nothing changes by itself", async ({ page }) => { - await page.goto(researchCaseHref(BOARD_CASE)); + await openBoard(page, BOARD_CASE); const b = board(page); await expect(b.locator("[data-quote]")).toHaveCount(3); await page.locator("body").click(); @@ -398,7 +473,7 @@ test("re-verify: the drifted quote shows the old and the new words, en vs en-ori test("the board fits 1366×768: the side panel and the first quote are on screen, the page does not scroll sideways", async ({ page }) => { await page.setViewportSize({ width: 1366, height: 768 }); - await page.goto(researchCaseHref("lighthouse-case")); + await openBoard(page, "lighthouse-case"); await expect(page.getByRole("button", { name: "Draft an article from this board" })).toBeInViewport(); await expect(board(page).locator("[data-quote]").first()).toBeInViewport(); expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth)).toBe(true);