oficina / docs Public

Undo and redo while diverged end as the person saw them open

On branch fix/undo-redo-while-diverged

Branch fix/undo-redo-while-diverged, head e2d2b57, on main 317e09b (cut from 25cd32a and rebased onto the Help-menu commit; the bundle is unchanged by it). This fixes Claude Fable's findings on #90 (his second review of feat/replay-while-diverged). Not merged, not deployed. Slides has the same change (oficina/slides #31, 59b4770, hub CI passed).

P1: an undo or redo that puts text back

The bug: item matching (watchUndo) covered what an undo deletes, not what it re-inserts. Yjs's redoItem anchors the re-inserted text to the original item, which the rebuilt doc never holds when that text was typed on the refused x. And ySync's mirror carried no replay.

The fix is Fable's:

  • A replay for every undo and redo. aroundDispatch sees ySync's mirror of an undo or redo (isUndoRedoOperation). While diverged, it derives one ReplaceStep from the mirror's tr.before and final doc (carry.mjs's derivedStep, with the same overlap rule as replaySteps). It leaves that as mirrored. The mirror runs in Yjs's observer phase, before the doc's 'update', so the next keep() in the same Yjs transaction takes it; beforeTransaction clears it.
  • watchUndo is gone. With the derived replay in place it would double a type-over's undo.
  • Trade-off for Paul, as Fable flagged: an undo or redo that put the refused x back on screen now brings it back on the page and posts it, as the person saw.

Tests. Fable's 17 cases (A–Q), each held to the tab's own text, with the refused x gone unless an undo or redo put it back:

  • Node: in carry.test.mjs, through a harness that keeps the derived replay exactly as the editor does.
  • Real editor: in fixtures/engines/undo.mjs (Slides' script, with Docs' selectors) with real keys (Cmd/Ctrl-Z, Cmd/Ctrl-Shift-Z, Backspace, selections), Chromium and WebKit. TestEnginesUndoAndRedoWhileARefusalIsPendingEndAsSeen holds the server to the same text. Case K, the x and "a" in one undo step, answers the x's save at once so the "a" lands after the refusal but within the x's 500 ms undo step.
  • Failing first: on 4c23861 seven fail in both engines, the same seven Fable's probe flagged: - A: ab for abc - C: ` for a - D: b for ab - I and J: for xa - K: for xa - M: two for one two`

The other ten were already right there and stay as guards. Docs fails the same seven as Slides.

P2: the timing test

  • Bound on growth. carry.test.mjs now bounds growth in CPU time (process.cpuUsage), not wall time. 4000 changes typed elsewhere may take at most 8× the CPU of 1000: linear is 4×, and the quadratic version this replaced was about 20×. The refused-text path is checked at 500 against 2000. Each takes the best of several runs, with a generous ceiling.
  • Result. The wall-clock version failed once here at a load average of 60 (24 ms against 301 ms). The CPU-time version passed 3 runs out of 3 at a load average of 134.
  • Client count. It's still bounded to one per 500 ms window.

P2: TestFontEnginesAWaitingPasteSurvivesARefusedSave was flaky under load

It failed on main too. replay.mjs held the code face for a fixed 1.5 s, which raced the refusal:

  • a face that arrived first put the paste into the refused batch ("appeared once and went");
  • a face that arrived late ran past the paste's three-second deadline ("appeared 0 times").

Now the fixture:

  • holds the face until this case's refusal has landed, then lets it go;
  • waits for the colleague's save to be answered, instead of 1.5 s;
  • waits for the sheet to hold the expected text and its saves to settle, instead of 4.5 s.

Since the change it has passed every run, including twice at a load average of about 60, where it had been failing; and as part of the full suite below. It's also faster: about 95 s instead of 144 s.

Nits

  • An entry that only deletes what the rebuilt doc never held (Cmd-Z on the refused x alone) is no longer counted as lost; there's a Node test for that.
  • No reference to rv-undo-probe.mjs remains.

AGENTS.md

It no longer says every change is kept. It describes the derived replay, and names what still isn't kept:

  • a change whose replay can't be made (the structural case);
  • a change made elsewhere in the same 500 ms ID window as one on the refused text, when its own replay can't be made either;
  • (Slides only, not Docs) an undo mirrored in more than one view, and an inspector op written on the refused text.

Validation

  • .amadan/ci.d/10-gate passes on the rebased head (TMPDIR=/tmp/…).
  • The full Docs browser suite passes (1245 s).

Coordination with Docs fix/safari-save-stall (#94)

This branch keeps to carry.mjs, the undo paths of editor-entry.js (aroundDispatch, the update handler, beforeTransaction) and the engines fixtures. It doesn't touch the save queue. #94 is not merged yet. If it lands first, this branch rebases onto it, rebuilds internal/docs/static/editor.*.js and reruns the gate; if this lands first, #94 does the same.

Hub CI passed on e2d2b57 (amadan ci status exit 0). Not merged.

Claude Fable 5.1, reviewing for session 21a32889. Review of fix/undo-redo-while-diverged at e2d2b57, read as one diff against its merge base 317e09b (origin/main has since taken 0d00351, the Colophon follow-ups, which touch AGENTS.md elsewhere, internal/shipped and the native packages; git merge-tree is clean), scratch worktree /tmp/rv7-docs. No tracked file edited; probes under /tmp/rv-probes (pf/, quad*/, the r2 ones re-run).

Verdict: ready to merge — VERDICT: APPROVE-WITH-NITS. The derived-step replay fixes the P1 as sketched: an undo or redo while diverged is replayed on the rebuilt document as the person saw it, and all seventeen cases hold in Node and in the real editor. The flaky paste test is now condition-driven and passed here, and the growth test no longer depends on the wall clock. Three P2s below, none blocking; Docs has one view, so the two-view limit I found on Slides (#31, finding 1) does not arise here.

Findings

  1. P2 — the growth test catches the quadratic it replaced, and little else. Flipping the branch's carry back to a fresh id per change and a state vector per update (the parent's quadratic) fails it at ratio 20.6 (10.7 s against 520 ms CPU). But each of the two halves alone passes: the state vector per update alone at ratio 7.6 (354 against 47 ms; the bound is 8), and a probe copy rebuilt per entry at ratio 5.3 while fifteen times slower in absolute terms (1.9 s for 4000, against 169 ms; the ceiling is 10 s). The bound of 8 on a 4× size leaves a quadratic term that is still small at 4000 room to pass, and the ceiling is loose enough for a 10× constant slowdown. CPU time is stable under load (the branch's own numbers held at load 70–110 here: ratios 4.2 and 3.6), so both can be tightened: a bound of 6 (linear is 4, the min-of-three gives little noise) and a ceiling near ten times the real cost (2 s elsewhere, 8 s on the refused text), or compare 500 against 4000 so the quadratic term shows. Not blocking: the regression it was written for is caught, and the flake is gone.
  1. P2 — lost undercounts for a delete-only undo whose replay cannot be made. The new rule (a delete-only entry that reads none loses nothing) is right for the undo of the x alone, but it also matches an undo whose derived step replaySteps refused (a structural one, say) and which deleted text the resync had replayed under new ids: that undo is dropped and the page keeps the undone text, with the save line saying nothing. carry knows which entries it replayed; a delete-only none entry whose deletes touch their structs' ids is a loss. Small, and the same change would fix Slides' two-view case.
  1. P2 — the paste test is better, not load-proof. In the full suite here, with the Slides suite running alongside and the load average at 130, TestFontEnginesAWaitingPasteSurvivesARefusedSave failed once more on Chromium case (j), the old shape: "the pasted words appeared 0 times" — the page's log holds one frame, without the words, and the fixture confirmed the paste did wait for its font and the refusal came first. Alone, straight after, it passed twice (101 s, 89 s; load 50–90). Everything on the fixture's critical path is now a condition, so what is left is the browser itself: a starved Chromium that takes more than COMMAND_TIMEOUT_MS (4 s) from the refusal to the released face and the paste gives up, which is the editor's own rule — consistent with the log, not proven (no console in the run). Not this branch's to fix; the sentence in AGENTS.md ("under load a fixed hold raced the refusal") is true, and I would add that under heavy enough load the case can still fail on the command deadline, so a red hub run with that shape is read right.
  1. Nits. The engines fixture's pause(700) between steps relies on UNDO_CAPTURE_MS being 500; a constant would be safer than a number. replay.mjs's route hold polls watched every 25 ms for up to 20 s; a code-face request made during the page's own load (the picker prefetch) is held until the case's refusal lands, which is the intent, but worth a comment since it reads as a race with watched still null.

What I checked, and what holds

  • The derived step. derivedStep is replaySteps's span rule as one ReplaceStep: valid wherever the content outside the span is identical (it is, by findDiffStart/findDiffEnd), including a join or a split across paragraphs (the slice is open at both ends) and a mark change (text nodes with different marks differ). Where it cannot fit, replay's try/catch and maybeStep refuse it and the entry falls back to its updates — what every undo did before. The mirror is one dispatch per Yjs transaction, in the observer phase, before afterTransaction and the 'update' emission (yjs 13.6.27 cleanupTransactions), so keep reads mirrored in the same transaction; beforeTransaction clears it; held is never set during it (guarded(undo) runs from the keymap, not inside a dispatch; yUndoPlugin's selection restore is a doc-less transaction). isUndoRedoOperation is y-prosemirror 1.3.7's own meta (transaction.origin instanceof Y.UndoManager).
  • Doubling and loss. My r2 probe (PROTO=2, the prototype of this fix) on the branch's module: every case matches the branch's own expectations, including the three where the x comes back (I, J, K: xa on page and server, lost=0, nothing pending). A type-over's undo (B) no longer doubles. rv-carry-review.test.mjs's third case (two changes under one id with no keeper and no replay) fails identically on main's carry.mjs — the documented window limit, not this branch's. The module is byte-identical to Slides' bar two comment lines, so the multi-step and linear probes there stand for both.
  • What watchUndo handled. Every case it got right the derived step gets right (undo, undo-then-type, undo/redo/undo/redo, two undos and two redos, undo elsewhere, undo of the x alone, the saved-s-then-x two-undo test rewritten). With one view, no mirror is ever 'many'.
  • No flash. carry still runs before currentView.destroy() and mountView; unchanged.
  • The replay fixture. The code face is held until window.refused on the watched page, then released 50 ms later; the colleague's save is awaited (__saved > 0, nothing in flight, 600 ms quiet); the page is awaited on its text and on saves settling. No fixed sleeps on the critical path. In the full suite here it failed once (finding 3); alone, 2 of 2.
  • Fails without the fix. The identical script on Slides' main fails exactly the seven cases #97 names (A, C, D, I, J, K, M, with the same wrong text) and passes the other ten (#31); the Docs script differs only in selectors and the config name.
  • #94 (fix/safari-save-stall). git merge-tree of the two branches conflicts only on the rebuilt bundle (both rename editor.*.js); editor-entry.js and AGENTS.md auto-merge. Semantically disjoint: #94 changes post, the queue's batching and give-up, pageshow and beforeunload; this branch changes aroundDispatch, the 'update' and beforeTransaction handlers and the undo manager's wiring. The one shared name is unsaved.entries in beforeunload, whose getter now returns every entry (nothing is marked undone any more) — the beacon carries an undo's own updates as it did any other entry. Whichever lands second rebases, rebuilds the bundle and reruns the gate, as #97 says.

Validation

  • TMPDIR=/tmp GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gate with OrbStack: gate: ok end to end (every Node suite green, carry.test.mjs 56/56, the bundle byte-identical, wasm built and matching, wire-gate ok, go vet, go test ./..., gofmt, build). Hub CI passed on e2d2b57.
  • Full browser suite in full Chrome (RASTRILLO_CHROME = Playwright's chromium-1234), go test -tags browser -count=1 -timeout 40m -v ./internal/docs/: 1778 s, 381 pass, 1 fail (finding 3; alone 2 of 2), 3 standing harness skips. The branch's tests pass: TestEnginesUndoAndRedoWhileARefusalIsPendingEndAsSeen (216 s, 34 cases, both engines, the server held to each page), TestAnUndoWhileARefusalIsPendingStaysUndone, TestEnginesEveryChangeWhileARefusalIsPendingIsKept (88 s), TestChangesOnRefusedTextAreKeptWithoutIt.
  • Load average 30–120 throughout.