oficina / docs Public

Replay while diverged: every change made while a refusal is pending is kept open

On branch feat/replay-while-diverged

Branch feat/replay-while-diverged, head 89d4b5c. Stacked on fix/carry-replay (b8dbbf7, #89), whose span fix it needs; I'll rebase it onto main once #89 lands. Not merged, not deployed. Slides has the same change (oficina/slides #29, 72f74b0, hub CI passed).

Why

Paul ranks a lost edit as worse than a flash. On #89 only the paste that waited for its font kept its ProseMirror transaction. Anything else written on the refused text was lost (typing, a plain paste, a delete), and so was anything written on a change that couldn't be carried.

What

  • The view dispatches through one hook. Docs' one EditorView gets a dispatchTransaction. It applies the transaction together with every transaction the plugins append, and hands the state update to aroundDispatch.
  • What gets held. While diverged, aroundDispatch holds the change as tr.before plus every step, so changes made by appended transactions are replayed too. It skips transactions that ySync makes to mirror Yjs, and ones that change nothing. The font gate's run now dispatches through the same path, instead of setting held itself.
  • Replay and fallback. carry.mjs replays any entry whose own updates don't all integrate, recomputing the span against the rebuilt document each time. If a replay can't be made, it falls back to whatever of the entry's own updates integrates. An entry of which nothing would integrate is never applied, so nothing is left pending on the live doc.
  • Undo. The carried changes undo in the steps they were made in. The keeper stamps each entry with its time, and yUndoPlugin's UndoManager, now with an explicit 500 ms captureTimeout, stops capturing wherever two entries were made more than that window apart. A word typed quickly is still one undo step; a change made a second later is its own.

Tests

  • Node: carry.test.mjs gains 4 tests: - Fable's general probe: five mixed changes, nothing lost, page and server the same, nothing pending; - a change that can't be kept, in the middle of the sequence; - the fallback; - the undo grouping.

The last two fail on b8dbbf7's module. The first two already pass there, because the module copes once every change has a transaction; the wiring is what changes, and the browser tests show that.

  • Browser: TestEnginesEveryChangeWhileARefusalIsPendingIsKept runs fixtures/engines/diverged.mjs in full Chromium and WebKit. The first save is refused and the rebuild held while the edits are made with real keys. Each case checks the sheet, one Cmd/Ctrl-Z, and the server (from Go). The three cases: - Mixed: type on the refused x, paste at the top, type again, Backspace across the server's words, the x and the new letters, then a second later type elsewhere. One undo removes only that last change. - Typing on the refused text survives: "KEPT" after the x, a paste at the top, then " more" after it. - A change that can't be kept, in the middle: typing into the paragraph a refused Enter made, then elsewhere, then into it again.

On b8dbbf7: in both engines "KEPT more" is lost, and one undo takes back every carried change.

  • Rewritten: TestAChangeOnARefusedOneIsNotPosted becomes TestChangesOnRefusedTextAreKeptWithoutIt. The two pastes after the refused words are now kept, once, on sheet and server; on b8dbbf7 they're lost. The refused words are on neither.

Validation

  • .amadan/ci.d/10-gate passes (TMPDIR=/tmp/…).
  • The full Docs browser suite passes (857 s).

Limits

  • A non-ProseMirror change on refused text still can't be replayed. That covers an undo or redo, which arrives as Yjs updates. Such a change goes through the updates path: carried if it integrates, held back otherwise. With a fresh ID per change it no longer takes anything else down with it.
  • Cost. probe() copies the doc once per held entry. That's fine for a divergence of a second or two.

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

Claude Fable 5.1, reviewing for session 21a32889. Branch feat/replay-while-diverged at 89d4b5c, reviewed as its own diff against its base fix/carry-replay (b8dbbf7), scratch worktree /tmp/rv5-docs-rwd. No tracked file edited; probes under /tmp/rv-probes.

Verdict: needs changes — VERDICT: CHANGES-REQUIRED. carry.mjs and engines/diverged.mjs are Slides' (the selectors and the hold on the whole-log fetch differ), and the wiring is the one-view form of the same thing, so the full account — what holds, the three findings and the fixes — is on oficina/slides #29. Here is what is Docs' own.

Verified here

  • The one dispatch path. The EditorView gets a dispatchTransaction that runs applyTransaction and hands the state update to aroundDispatch; that is what the default dispatch did, plus the hold. The gate's deferred view.dispatch(tr) goes through it, and the old held = in the gate is gone. No plugin here dispatches a doc change from inside a view update (pages.mjs dispatches meta-only transactions, which docChanged filters), so held never swallows a nested change.
  • ySync-originated transactions are skipped by the meta check, which covers collaborators, resyncs and yUndoPlugin's undo/redo.
  • Undo grouping: yUndoPlugin's UndoManager now has an explicit captureTimeout: UNDO_CAPTURE_MS, and steps stops capturing at gaps of 500 ms or more; sound for the reason given on #29.
  • The rewritten test TestChangesOnRefusedTextAreKeptWithoutIt is a faithful inversion: both pastes once on sheet and server, the refused words on neither, sameSheetAndServer at the end. It passes here.

Findings

  1. P1 — an undo made while diverged, after a change the resync replays, brings the undone text back and posts it. Mechanism and fixes on #29. Real editor, this head, rv_docs_undo_probe_test.go (/tmp/rv5-docs-rwd2): "x" refused at the end of "First paragraph.", "a" typed after it, Cmd-Z — the sheet reads First paragraph.x; after the rebuild the sheet and the server read First paragraph.a (posts [413 204]). With a "b" typed after the undo: First paragraph.ab where the person wrote First paragraph.b. The smaller fix (mark entries undone from the undo manager's stack-item-popped, one client per entry) fits Docs as it is; the one undo manager is yUndoPlugin's, replaced at each resync, so the listener goes on currentView's.
  2. P2 — multi-step transactions mapped through one map. Node reproduction and the drop-in replaySteps on #29. It matters a little more here: Docs' autoformat rules (wrappingInputRule, textblockTypeInputRule) delete the marker and then wrap or retype the block in one transaction, and the heading/list commands do the same shape; the later step is mapped as if it were the first, and lands off by the marker's length or is refused when the refused text sits at that block's edge.
  3. P2 — cost over a long divergence (numbers on #28; the branch's own additions are cheap, the per-change id is the quadratic part).

Validation

  • TMPDIR=/tmp GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gate with OrbStack up: gate: ok (carry.test.mjs 28/28 in it, bundle matching editor.279133e860986229.js).
  • Full browser suite in full Chrome (RASTRILLO_CHROME = Playwright's chromium-1234), go test -tags browser -count=1 -timeout 60m -v ./internal/docs/: 791 s, 373 tests and 167 subtests pass, 3 standing harness skips, including TestFontEnginesAWaitingPasteSurvivesARefusedSave ("every case kept its paste, or refused it cleanly, in both engines", 133 s), the three carry_browser_test.go tests, TestChangesOnRefusedTextAreKeptWithoutIt, all of resync_browser_test.go. The one red line, TestEnginesEveryChangeWhileARefusalIsPendingIsKept, failed at 0.00 s on its own install check (fixtures/engines was not yet installed in the fresh worktree); rerun after installing: both engines tests pass — TestEnginesEveryChangeWhileARefusalIsPendingIsKept (49.8 s, "every change made while the resync was pending was kept, in both engines", six RESULT lines held against the server) and TestFontEnginesAWaitingPasteSurvivesARefusedSave (133.8 s, all eleven cases in Chromium 1234 and WebKit 2336).
  • Probes: rv_docs_undo_probe_test.go; the rest on #29.

Fable's review of 89d4b5c: fixed at 71178d3

Rebased first onto main 29b6dea: 98037c7 is the old 89d4b5c, and this round is 71178d3. The squash merge equals the old base b8dbbf7.

The fix is the same module change as Slides #29 (c0990c8); the full account is there. In short:

  1. P1, undo. The keeper's watchUndo follows yUndoPlugin's undo manager, which is replaced at each resync, so it's attached per resync. An undo marks the entries it took back by the very items it inserted, plus its own delete of them, and leaves them out. A redo brings them back and leaves out its own re-insert. An undo item mixing in the refused x or saved text marks only what was kept, and its redo goes as its own updates.
  2. P2, multi-step mapping. It now rebases the prosemirror-collab way. That covers the autoformat shape, where a rule deletes its marker and then wraps or retypes the block in one transaction.
  3. P2, cost. IDs come from the doc's beforeTransaction: one per 500 ms window for ProseMirror changes (finding 3 still holds, tested), and one each for undo and redo. carry is linear: clocks via Y.getState, one running probe copy, and no entry of which nothing takes is applied. freshClientID(doc) never returns an ID the doc holds.

Tests.

  • Node: carry.test.mjs, in the gate, is the same 45 cases as Slides. 4000 changes carry in about 0.5 s.
  • Real editor: TestAnUndoWhileARefusalIsPendingStaysUndone is your probe plus redo. On 98037c7, "Cmd-Z" ends at First paragraph.a and "Cmd-Z then b" at First paragraph.ab; the redo case reaches First paragraph.a there too and is a guard.
  • Engines: diverged.mjs's new undo-in-the-middle case fails on 98037c7 in both engines.

Validation.

  • .amadan/ci.d/10-gate passes.
  • The full Docs browser suite passed except TestFontEnginesAWaitingPasteSurvivesARefusedSave. At that point the machine was at a load average of 100–150 from other sessions, and in the affected runs the paste never ran (its font wait timed out). The same case also fails on 98037c7 under that load.
  • Rerun at a lower load, that test passes all 22 runs (11 cases in each engine).

Not merged.

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

Claude Fable 5.1, reviewing for session 21a32889. Second review of feat/replay-while-diverged, now at 71178d3, read as one diff against its merge base 29b6dea (origin/main has since taken feat/contract-workflow, which touches internal/contract and internal/export only; git merge-tree is clean), scratch worktree /tmp/rv-docs. No tracked file edited; probes under /tmp/rv-probes (rv-undo-delete-probe.mjs, rv-linear-probe.mjs, the r1 ones re-run).

Verdict: ready to merge — VERDICT: APPROVE-WITH-NITS. The three fixes do what they say: an undo made while diverged no longer brings the undone text back (the r1 P1 on #90, real editor, both engines), a multi-step change rebases prosemirror-collab's way, and carry is linear. One P1 remains in the undo area — a sequence the branch still loses, not one it makes worse — with the fix sketched and prototyped below; I am not blocking on it because main loses the same edit and more, and holding this branch keeps the wider loss live. Two P2s.

Findings

  1. *P1 — an undo or redo that puts text back is lost when that text belongs to a change the resync replays. The item matching covers what an undo deletes: the kept entry whose items it removed is marked undone and the undo's own delete is dropped. It does not cover what an undo re-inserts*. An undo of a Backspace, of a type-over, or a redo after an undo that mixed in the refused text re-inserts the items as new structs whose rightOrigin is the original, deleted item (yjs 13.6.27 redoItem: left = item.left; right = item); when that original was typed on the refused text, the rebuilt document never holds it, the re-insert reads none, and — the transaction being ySync's mirror, which aroundDispatch rightly skips — there is no replay. The entry is left out. On the branch's own module, with watchUndo and before() wired as the editor wires them (rv-undo-delete-probe.mjs, run from fixtures/): - "abc" typed on the x, Backspace, Cmd-Z: tab …abc, page and server …ab, lost 1. - "a" on the x, Backspace, Cmd-Z: tab …a, page … (the a is gone); then "b" typed after: tab …ab, page …b. - a two-word paste on the x, one word selected and deleted, Cmd-Z: tab … one two, page … two. - x and "a" in one undo step, Cmd-Z, Cmd-Shift-Z: tab …xa, page … — the "falls back to plain Yjs" path cannot integrate either, for the same rightOrigin reason, so the redo is lost, not carried. - The branch's own cases (undo, undo then type, undo/redo/undo/redo, two undos and two redos, undo elsewhere) all pass; so does a type-over's undo (by luck: the type-over entry is skipped, which already restores the a).

It is the gesture "type, Backspace a typo, undo", made while a save is refused and after typing on the refused text. Main loses all of it (and the typing besides), so this is not a regression; but the branch's AGENTS.md says every local change made while diverged is kept, and this one is not.

Fix. Give undo/redo entries a replay too. y-prosemirror's mirror transaction carries isUndoRedoOperation in its ySync meta and has tr.before and tr.doc; its steps replace the whole document, so derive one ReplaceStep from findDiffStart/findDiffEnd (the same overlap rule as replaySteps) and hold it as { before: tr.before, steps: [step] }. Ordering works out: the mirror dispatch runs in Yjs's observer phase, before the doc's 'update' event, so the derived replay can be set before keep() runs for that update. One caution: with the derived replay in place, watchUndo's marking becomes redundant and conflicts — a type-over's undo then doubles (…aa), because skipping the type-over entry already restores the a and the derived step inserts it again. The prototype with the derived replay alone, watchUndo off (PROTO=2), gets every one of the seventeen cases right, including the branch's undo/redo cases and the r1 P1. The trade-off to put to Paul: that way an undo of a Backspace over xa, or a redo of an undo step that held the x, brings the refused x back onto the page and posts it — which is what the person saw, and what the item-matching version loses whole instead. In Docs there is one view, so one mirror per undo; yUndoPlugin's selection restore after a pop changes no document and is already passed through.

Until it is fixed, AGENTS.md should name the limit next to "every local change … is kept".

  1. P2 — TestFontEnginesAWaitingPasteSurvivesARefusedSave is flaky under load, on main as on this branch. In the full suite it failed on Chromium case (k) ("the pasted words appeared 0 times": the paste never reached the page); alone, on WebKit case (a) once ("appeared 1 times and went": on the page at 2327 ms, gone at 2353 ms when the rebuild landed), then passed. So I sampled it: six serial full runs alternating this branch and origin/main (a fresh worktree, /tmp/rv-docs-main) — branch 2 pass / 1 fail, main 0 pass / 3 fail, main's failures the same (j)/(k) "appeared 0 times" shape; and case (a) alone, eight runs (DOCS_REPLAY_CASE="(a)", four per tree, one with the page console captured): 8 of 8 kept the paste, nothing in the console. The load average was 25–157 throughout. It is the fixture's timing (a paste that must still be waiting for its font when the 413 lands, a colleague's tab, a 1.5 s font hold), not this branch's code; the shape where the paste "went" I saw once, under two concurrent browser suites, and could not reproduce in nine further tries. Not a reason to hold the branch, but the hub's CI will show it red under load, and TestFontEnginesFirstRevealIsTheFinalLayout already takes 200–260 s here.
  1. P2 — the 4000-change timing test will flake on a loaded runner. The bound is 1 s; here it took 0.84 s and 0.77 s with the load average above 60, and the AGENTS.md sentence repeats the bound. The linearity is real (below); a looser bound, or asserting the ratio between 2000 and 4000, would hold the claim without the flake.
  1. Nits. An undo whose delete matched nothing kept here (Cmd-Z on the x alone, within its capture window) counts as lost and warns; harmless. The r1 rv-undo-probe.mjs predates before()/watchUndo and no longer exercises the fix; rv-undo-delete-probe.mjs supersedes it.

What I checked, and what holds

  • Undo/redo matching. Yjs adds the new stack item (afterTransaction) before 'update' fires and reports the popped one after the transaction, so made.redo/made.undo are the right items at stack-item-popped, and the undo's own delete-only entry exists when it is dropped. The undo manager's own origin is tracked (trackedOrigins.add(this)), so every undo makes a redo item. Redo of a whole undo restores the original entries and skips its own re-insert; a second undo follows redid; an undo that mixed in refused or saved text leaves the undo's delete live, which reads none (nothing applied) or some (the saved part deleted, posted). Nothing doubles, and refused text is not resurrected by an undo. The real-editor test and the fourth engines case pass in both engines.
  • The 500 ms window. Within one id, a held entry does strand the later ones (integrateStructs queues same-client structs out of clock order), and every stranded ProseMirror entry then replays — the test and my probes confirm it. What the window gives up: a change made elsewhere in the same window as one on the refused text, whose replay then cannot be made (its block gone, a step that cannot apply), is lost where a per-change id would have carried it. Rare; worth a line in AGENTS.md.
  • Collab rebase. replaySteps is prosemirror-collab's rebaseSteps with the span in place of the remote steps, mirrors and slice(mapFrom) included; a failed step refuses the whole change rather than skipping it, which is the right choice here. rv-multistep-probe.mjs: 4 of 4 scenarios right on the branch.
  • Cost. rv-linear-probe.mjs, 60 paragraphs: 1000/2000/4000 changes typed elsewhere carry in 89/226/342 ms (0.09 ms each); on the refused text, every one replayed, 1392/2617/3567 ms (1.4 → 0.9 ms each). Linear on both paths. freshClientID(doc) retries on a collision.
  • Never applied on a failing path. Every entry is tried on the running copy first; one that does not carry whole is replayed or, failing that, applied only if something of it takes. pendingStructs is null after every Node case.
  • No flash. Both carry calls sit where they were: the first before currentView.destroy() and mountView, with currentDoc swapped after it, the second on the live doc through y-prosemirror. Unchanged from main.
  • Dispatch wiring. the view's dispatchTransaction is applyTransaction + updateState, the default in two lines; aroundDispatch returns apply() unless diverged, holds tr.before and every appended step, and skips ySync mirrors. The font gate's deferred run goes through the view's dispatch, so a paste that waited is held by the same code as a keystroke — and an autoformat rule's delete-then-wrap is now kept as all its steps, which is what the collab rebase is for here. beforeTransaction returns early unless diverged; watchUndo is on yUndoPlugin's own manager, whose undo command runs from the keymap, not inside a dispatch.
  • Safari (fix/safari-save-stall, not reviewed). Nothing here can plausibly cause it: outside divergence the branch adds a dispatch wrapper that is the default and a beforeTransaction hook that returns at once; the save queue and fetch are untouched. If the stall flips a tab into "diverged", this branch only adds client-side work there. The one thing to keep in mind is on main already: a carried update whose post is refused again is gone at the next rebuild.

Validation

  • TMPDIR=/tmp GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gate with OrbStack stopped at save-queue.test.mjs's 100 ms retry timing under load (alone: 8/8). By hand after it, every step: the remaining Node suites all green (carry.test.mjs 45/45), the bundle byte-identical, cargo fmt --check, the wasm build and cmp against the embedded artifact (same), cargo test, wire-gate.sh ok, go vet (with and without -tags browser), go test ./... -count=1, gofmt, go build ./.... Load average was 50–135 for most of this.
  • Full browser suite in full Chrome (RASTRILLO_CHROME = Playwright's chromium-1234), go test -tags browser -count=1 -timeout 120m -v ./internal/docs/: 2024 s, 374 pass, 1 fail — the engines paste test, finding 2. The branch's tests pass: TestAnUndoWhileARefusalIsPendingStaysUndone (three subtests), TestEnginesEveryChangeWhileARefusalIsPendingIsKept (120 s, both engines), TestChangesOnRefusedTextAreKeptWithoutIt.
  • Probes: rv-undo-delete-probe.mjs (17 cases, with PROTO=1/PROTO=2 variants; the module is byte-identical to Slides' bar two comment lines, so the Slides run stands for both), rv-linear-probe.mjs, rv-multistep-probe.mjs.

Fable's second-review findings here (undo/redo that puts text back, the flaky paste-replay fixture, the timing bound, the lost-count nit) are on fix/undo-redo-while-diverged, discussion #97.