Hub CI passed on c49ed4e (amadan ci status exit 0). Not merged.
Carry replay: a paste that waited for its font survives a refused save open
On branch fix/carry-replay
Branch fix/carry-replay, head c49ed4e, cut from 52983f8. This is Claude Fable's follow-up on #27 and #88. Not merged, not deployed. Docs has the same change on its own fix/carry-replay, with its own discussion.
1. carry() now judges every update
The fix moves carry() to fixtures/carry.mjs and decides from the update's own contents (Y.decodeUpdate):
- a struct took when the doc's state for its client reaches the struct's end;
- a deleted range took when the doc's state for its client reaches the range's end.
The old check used encodeStateVectorFromUpdate. That names a client only when the update's structs start at clock 0, and a delete-only update has no structs at all. So every update after a client's first, and every Backspace, read as taken and was posted.
Now:
- An update of which nothing took is held back.
- For one that partly took, only the part that integrated is posted. I went slightly beyond "post one that partly took" here, because in the browser that case is common: a paste into marked text on top of refused words also rewrites the formatting around it. That part integrates; the words don't. Posting the whole update left the words pending in the server's log forever, which is the dangling row this fix set out to stop. The part posted is
encodeStateAsUpdate(doc, svBefore)with Yjs's pending structs and delete set set aside for the call. That readsdoc.storeinternals, which the code comments.
2. A paste anchored to the refused text is kept
- Holding.
editor-fonts.mjsgets anaroundRunhook. When the waiting change runs while diverged, the editor keeps itstr, and finds the block's Y text again on the rebuilt doc by its own id (a relative position that encodes the type, resolved withcreateAbsolutePositionFromRelativePosition). That change's updates are grouped as one entry. - Replaying. If the entry's updates don't all integrate (checked on a copy of the rebuilt doc first), the paste is replayed. Its steps are mapped through the one span where the rebuilt text differs from
tr.beforeand applied withmaybeStep. The result is written back withprosemirrorToYXmlFragmentinside oneCARRIEDtransaction, and that update is captured for the queue. - No blink, one undo step. All of this happens before any view is mounted, so the paste is never painted, absent, and painted again. It is one undo step: the undo manager tracks
CARRIED. - When it can't be kept. If ProseMirror refuses a mapped step, the paste is left out as today: nothing posted, the save line says failed, the clipboard is untouched.
- A paste that integrates anyway still carries as its own Yjs updates.
3. Nits
- AGENTS.md's carry sentence now says exactly what is and isn't posted, and describes the replay.
resync-controller.mjs's header notes the fresh client ID and the carry.
Tests
Node (carry.test.mjs, in the gate's glob), using the real Yjs, ProseMirror and y-prosemirror. 19 tests:
- Fable's 11 replay cases, each checked on the page, on a server copy after the posted updates, for no pending state, and that the code mark survives;
- the refused structural case;
- one undo step;
- a paste made elsewhere carries as its own updates;
- his classification probes: a second keystroke, a Backspace over refused text, a partial update, and a format-plus-anchored update.
Against an adapter reproducing 52983f8's carry(), 14 of them fail.
Browser. TestFontEnginesAWaitingPasteSurvivesARefusedSave runs fixtures/engines/replay.mjs in full Chromium and WebKit. Each of the 11 cases is made with real keys and clipboard events:
- the refused change is answered 413;
- the paste starts waiting for its font before the refusal arrives;
- in (j) and (k) a colleague's words come from a second editor while the rebuild's fetch is held.
The test checks the page, the server (from Go), and a mutation-and-frame log showing the words never leave. TestAChangeOnARefusedOneIsNotPosted adds a second paste and a Backspace over the refused words, and checks that no save after the rebuild carries those words.
Failing first, on 52983f8:
- Cases (a), Fable's exact probe, plus (c), (e), (h), (j) and (k) fail in both engines. The words are painted and then vanish 9–14 ms later.
- The second-paste/Backspace test fails ("a save carries ANDAGAIN").
- (b), (f), (g) and (i) were already kept by the Yjs carry, because there the paste isn't anchored to the refused text. (d) is refused both before and after. Those five can't fail first and stay as guards.
Validation
make cipasses.- The gate's JS/Go half passes, with 231/231 Node tests. The full gate still stops at the known Mac wasm byte-compare.
- The full browser suite passes (925 s).
Limits worth knowing
- Only the change that waited for its font is replayed. A later keystroke or paste written on top of the refused text, or on top of the replayed paste's original Yjs items, is still held back, not replayed. Replaying every local transaction made while diverged would be the general form; I left it out to keep to the brief.
- The beacon on close still sends every kept update. A pending one can still land as a dangling row if the tab closes in the second or so before the rebuild.
Claude Fable 5.1, reviewing for session 21a32889. Whole branch fix/carry-replay at c49ed4e against origin/main (52983f8, the merge base), scratch worktree /tmp/rv-slides-cr; the corrected module was tried in a second worktree, /tmp/rv-slides-fix, with the bundle rebuilt. No tracked file edited; probes under /tmp/rv-probes.
Verdict: needs changes — VERDICT: CHANGES-REQUIRED. The design is right and both of my #27 recommendations are implemented at the root; the eleven replay cases hold in the real editor in both engines, undo is one step, and the server never holds anything pending in the tested cases. But two of the new rules are wrong at an edge each, and each one, when it hits, does the thing Paul ranks worst: the page silently shows what the server does not have, or a paste is posted in the wrong place. Both reproduce in the real editor (Chrome, this head), both are three-line fixes, and I owe the author an apology for both: finding 1 implements my own wording from #27 ("a deleted range took iff the doc's state reaches clock + len"), and finding 2 is the clamp from my own prototype, pm-replay-probe.mjs, copied faithfully. Docs' carry.mjs is byte-for-byte the same (one mark name), so both findings are on #89 too.
What I checked, and what holds
- Partial-update extraction (
integrated()):encodeStateAsUpdate(doc, before)withstore.pendingStructs/pendingDsset aside is sound. Yjs holds pending structs per client contiguously (integrateStructsstacks a client's rest the moment one struct is missing a dependency), so "the doc's state reaches the struct's end" classifies structs exactly; the structs written sincebeforeare precisely what integrated; the fields are documented onStructStore, pinned yjs 13.6.27, restored infinally, and nothing else in the editor callsencodeStateAsUpdateon the live doc, so the pending state carried in the rebuilt doc leaks nowhere (the beacon merges raw updates). The whole delete set is posted with each such update: idempotent on yrs and on every reader, far from the 1 MiB limit; a nit below. No double apply: an update applied todocwithCARRIEDis not kept again, the replay's update is captured bygraband not kept, andlaterre-applies the rebuilt doc's own updates as no-ops. - Replay:
aroundRunruns insidewait()'s synchronous dispatch, the stage'sEditorViewhas no deferred dispatch, soheldis set whenkeep()sees the update, andtr.before === v.state.docis guarded. The relative position finds the block's fragment on the rebuilt doc by its item id.maybeStepvalidates every mapped step; a step strictly inside the span maps to null (deletedAcross), so (d) is refused cleanly. Undo: the replay is oneCARRIEDtransaction onnextUndo. Collaborators: the replay posts a fresh update from the rebuilt doc's own client id; asomeupdate posts the fresh id's structs plus the delete set; the live path is unchanged. - Fail-without-the-fix: the author's account matches what I saw on main in the #27 round (the anchored paste was held back and never shown); the Node file has no module to load on main.
Findings
- P1 — a delete range that took in part reads as "none": the page keeps a deletion the server never gets.
fixtures/carry.mjs:67–72. Yjs applies a delete range up to the doc's state for its client and holds the rest pending (readAndApplyDeleteSet,DeleteSet.js:290–293), so a range is not all-or-nothing. The realistic case is this tab's own typing: "abc" saved and "x" refused are the same client, contiguous clocks, so a Backspace over "cx" is one range,[clock(c), clock(x)+1).took()counts it asno, returnsnone, nothing is posted — but the "c" is already deleted on the rebuilt doc. Real editor (/tmp/rv-probes/slides_carry_replay_probe_test.go,TestReviewDeleteAcrossSavedAndRefusedDiverges): posts[204 413], page"hello worldab", server"hello worldabc", the save line saying failed. On reload the "c" is back: a lost edit, silently.carry.test.mjscannot see it becausetextWorldputs the server's words under client 1 and the refused text under client 2, so the Backspace there is two ranges. - Fix: count the applied part.const state = have.get(client) || 0; if (state > r.clock) yes++; if (state < r.clock + r.len) no++. The update then readssome,integrated()posts the delete set, and the probe passes on the rebuilt bundle (page and server both"hello worldab", one 14-byte post). Node:/tmp/rv-probes/rv-carry-review.test.mjs, first case; the module with the fix is/tmp/rv-probes/rv-carry-fixed.mjs, and the branch's 19 tests pass on it. Update the header comment that states the old rule.
- P1 — the one-span map clamps away the size difference when the diffs overlap, so a replayed paste lands in the wrong place.
fixtures/carry.mjs:102–105.findDiffStart/findDiffEndoverlap whenever the change repeats what is beside it: a doubled letter typed, a repeated letter deleted.Math.max(end.a, start)andMath.max(end.b, start)each clamp tostart, which turns, say,"helllo"→"hello"into a zero-sized span, and every position after it maps unshifted. Real editor (TestReviewRepeatedLetterInAWordMisplacesTheReplay): the refused change is an "l" typed into "hell|o", the waiting paste made right after it (anchored to the refused l, so the replay runs): page and server"helloREVIEW_RESYNC_PASTE codeword world"— the paste after the "o", one character from where it was made, posted. Node, a paste at the end after a refused deletion of one "l":"hello worlPASTEd". It is the exact bugprosemirror-view'sfindDiffguards against, and it is mine: the prototype clamps the same way. - Fix: move both ends past the overlap, keeping their difference:let {a: endA, b: endB} = before.content.findDiffEnd(doc.content); const over = start - Math.min(endA, endB); if (over > 0) { endA += over; endB += over }; ranges = [start, endA - start, endB - start]. With it the probe gives"hellREVIEW_RESYNC_PASTE codewordo world"in the real editor, the eleven cases still pass, and (d) is still refused (/tmp/rv-probes/diff-span-fix-probe.mjs, nine cases). Node:rv-carry-review.test.mjs, second case.
- P1 — once one update is held back, everything this tab writes after it is held back too, wherever it was made. Not new to this branch, but the branch's limit ("a later keystroke or paste written on top of the refused text") understates it. Yjs integrates a client's structs in clock order: a held-back update at the fresh id's clock 0 leaves a gap, and the next update, a paste at the top of the first paragraph nowhere near the refused words, is pending for that reason alone. Node (
/tmp/rv-probes/contiguity-probe.mjs): keystroke on the x then a paste elsewhere → posted 0, lost 2, page"hello"; the other order → the paste is kept.carry.test.mjs's "a paste elsewhere is posted and a keystroke after the refused x is not" tests the lucky order only. The natural thing after a refusal is to keep typing where one was, so this is the common case, and the loss is of edits made where nothing was wrong. - Fix, one line in the editor: a fresh client id per change while diverged,doc.clientID = freshClientID()afterkeep(update)in the diverged branch ofdoc.on('update'), so each update's structs start at clock 0 and depend only on what they touch (the premise the header already states). The probe's third run shows it: posted 1, lost 1, page"PASTE hello". Safe to change between transactions (the branch already does it once; nothing in y-prosemirror, the undo manager or presence reads the id), at the cost of one state-vector entry per diverged change. Node:rv-carry-review.test.mjs, third case, which the module alone cannot pass — it needs the editor's id switch, or the general replay below.
- P2 — the beacon can post a held-back update as a pending row (the author's second limit). Acknowledged; a dangling row, not a lost edit; fine as a follow-up.
- P2 (nit) —
integrated()posts the document's whole delete set. Correct and idempotent, but it grows with the document and is re-applied by every reader of the log. Clipping the update's own ranges to the doc's state (a zero-struct update with that delete set, merged with the structs sincebefore) would post exactly what took. Not required.
- Nit — the AGENTS.md sentence "of one that partly integrates, the part that did is posted (the server takes the same part, and nothing is left there pending)" becomes true with finding 1; and
carry.mjs:24–30states the old range rule.
The general fix
Replaying every local ProseMirror transaction made while a resync is pending is tractable, safe, and reuses this mechanism as it stands. I prototyped it in Node with the real modules (/tmp/rv-probes/rv-general-probe.mjs, on the corrected carry.mjs): a sequence of five transactions while diverged — "a" typed on the refused x, "ELSEWHERE " pasted at the top, "b" after the a, a selection over the server's words, the x and the new ones deleted, "c" typed elsewhere — each kept as { updates, replay: { tr, locate, schema } }. As the branch is: 5 of 5 lost (finding 3). With a tr on every entry: 0 lost, page and server ["ELSEWHERE First paragraph.c", "Second "], nothing pending. A refused Enter with a paste inside its paragraph, then " after" elsewhere, then "!" inside again: the two inside are refused cleanly, " after" is kept, nothing garbled. It works because the span is recomputed per entry against the current rebuilt doc, so earlier carried or replayed entries are already on both sides and only the refused and the refused-and-lost remain in the span; anything inside the combined span maps to null, so it never misplaces.
- Wiring: set
heldaround every local dispatch while diverged (the stage'sEditorViewtakes adispatchTransaction;aroundRunalready builds the entry), skipping transactions that mirror Yjs changes (ySyncPluginKeymeta), keep the font gate's path as is. Undo manager entries made while diverged carry no tr and go through the update path, so finding 3's fresh id still matters for them. - Cost:
probe()copies the doc once per entry — fine for a divergence of a second or two; incremental later if ever needed. All replays areCARRIEDwithin onecaptureTimeout, so the carried batch is one undo step, as today. - Order: findings 1–3 before merge; the general replay as the immediate next branch for both apps, with browser tests for "typing on the refused text, then elsewhere" and "a lost entry in the middle". It needs finding 2's span to be safe, and it is more than a day with the Slides stage wiring and both suites, which is why I would not hold this one for it.
Validation
GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gate: rustfmt, clippy, wasm build pass; stops at the known Mac wasm byte-compare. The steps after it by hand: 231/231 Node tests (carry.test.mjs 19/19 in the glob), vectors unchanged, bundle byte-identical (editor.07a0c82a854673ff.js),make cipasses.- Full browser suite in full Chrome (Playwright's chromium-1234 Google Chrome for Testing),
go test -tags browser -count=1 -timeout 60m -v ./internal/slidestest: PASS, 922 s, 185 tests and 68 subtests, no failures, includingTestFontEnginesAWaitingPasteSurvivesARefusedSave("every case kept its paste, or refused it cleanly, in both engines"), both views ofTestFontAResyncNeverDiscardsAWaitingPaste,TestAChangeOnARefusedOneIsNotPosted, the beacon and presenter tests. - The corrected module in the real editor (
/tmp/rv-slides-fix, findings 1 and 2 applied tocarry.mjs, bundle rebuilt): the branch's 19 Node tests pass, my three browser probes give page = server in every case, and the eleven replay cases all keep their paste in Chromium (every case kept its paste, or refused it cleanly; WebKit not re-run there). - Probes:
slides_carry_replay_probe_test.go(three browser cases, run on this head and on the corrected build),partial-ds-probe.mjs,diff-span-probe.mjs,diff-span-fix-probe.mjs,contiguity-probe.mjs,rv-general-probe.mjs,rv-carry-review.test.mjs,rv-carry-fixed.mjs.
Fable's review of c49ed4e: fixed at 10fcbf7
- A partly-applied delete range.
took()now counts the applied part: a range has partly taken if the doc's state for its client is past the range's start, and has not fully taken if that state is short of its end. A Backspace over this tab's own saved and refused words is one range of one client; it now readssome, and the part that took is posted. - Overlapping diff ends.
replayStepsnow moves both ends past the overlap, as prosemirror-view'sfindDiffdoes, instead of clamping to the start. Your 11 cases still pass, and (d) is still refused. - One held-back change held back the rest.
keep()moved intocarry.mjsascreateKeeper(), so it can be tested in Node. It gives the document a fresh client ID after every change while diverged. The editor now uses it in place of its own list andkeep().freshClientIDlives there too. - Nits. AGENTS.md and
carry.mjs's header now state the range rule and the per-change ID. The whole-delete-set post (your P2 nit) and the beacon's pending row are left as follow-ups, as you suggested.
Tests.
- Node:
carry.test.mjsgains your three cases. Case 3 goes through the keeper and runs in both orders. There is also a keeper unit test. Against c49ed4e's module, with a no-op keeper stubbed in so the file loads, your three fail (the lucky order passes). - Browser:
carry_browser_test.goholds your two real-editor probes, plus finding 3 in both orders, each ending with page and server compared. On c49ed4e two of them fail: - the "l" typed into "hell|o" with the paste right after it: page and server readhelloREVIEW_RESYNC_PASTE codeword world; - the held-back-then-clean order: the clean paste is lost. - Guards that pass on c49ed4e on this machine: - Your Backspace probe passes 3 runs out of 3 here. The editor's update for that Backspace also carries a struct, so c49ed4e posted it through the partial path and its whole delete set:
posts [204 413 204], against your[204 413]. The Node case reproduces finding 1 exactly and fails first. The Docs version of this browser probe does fail on b48dad3 (sheetab, serverabc). - The deleted-letter shape with the paste at the end: that paste isn't anchored to refused text, so its own updates carry.
Validation.
make cipasses.- The gate's JS/Go half passes, with 236/236 Node tests.
- The full browser suite passed everything except
TestBrowserEditsTheTitle, while the Docs suite ran alongside; that's the pre-existing flake under load you noted. Alone it passes 4 runs out of 4.
Next. The general replay (every local transaction while diverged) comes on feat/replay-while-diverged, stacked on this branch because it needs the span fix. It gets its own discussion.
Not merged.
Hub CI passed on 10fcbf7 (amadan ci status exit 0). Not merged.
Claude Fable 5.1, reviewing for session 21a32889. Whole branch fix/carry-replay at 10fcbf7 against origin/main (52983f8, the merge base), scratch worktree /tmp/rv5-slides-cr. No tracked file edited; probes under /tmp/rv-probes.
Verdict: ready to merge — VERDICT: APPROVE-WITH-NITS. All three P1s from c49ed4e are fixed at the root, each verified in Node and in the real editor; the nits are done. Two non-blocking notes below, one of them a cost I did not weigh when I asked for the per-change id.
The three fixes, verified
- Partly-applied delete range (
took(),carry.mjs:74–78): a range counts as taken in part when the client's state is past its start, and as not taken in full while it is short of the end. My Node case passes on this module (rv-carry-review.probe.mjs, case 1), the branch's own case passes, and in the real editor both my probeTestReviewDeleteAcrossSavedAndRefusedDivergesand the branch'sTestABackspaceAcrossSavedAndRefusedWordsReachesTheServerend with page and server athello worldab. - Overlapping diff ends (
replaySteps,carry.mjs:117–121): both ends moved past the overlap, keeping their difference, asprosemirror-view'sfindDiffdoes. My Node case 2 passes; the branch'sTestAReplayedPasteLandsWhereItWasMadeBesideARepeatedLetterpasses in both shapes. My own probeTestReviewRepeatedLetterMisplacesTheReplaynow reports page = server =hellREVIEW_RESYNC_PASTE codewordo world, which is the right place; itswantstring was the pre-fix one, so the probe "fails" on a stale assertion only, and I note that so nobody reads the red line as a regression. - One held-back change held back the rest (
createKeeper,carry.mjs:180–197): the keeper gives the document a fresh id after every kept update. I checked that the switch between transactions is safe: Yjs'sUndoManagerderives each stack item from the transaction's before/after state per client (yjs.mjs:3649–3660), so it never depends ondoc.clientID, and nothing in y-prosemirror reads it outside the cursor plugin. The branch's Node case passes in both orders, andTestAChangeInACleanSpotIsNotLostWithOneOnTheRefusedTextpasses in both orders in the real editor. My third Node case still fails, as expected: it feedscarrydirectly and bypasses the keeper, which is where the fix lives.
Findings (none blocking)
- P2 — the per-change id is quadratic over a long divergence, and permanent in the deck. Not this branch's fault alone (I asked for it), but it needs a follow-up.
rv-cost-probe.mjs(60 paragraphs, 32 KB of state, typing elsewhere, kept through the keeper): 500 changes carry in 0.44 s; 2000 in 6.1 s; 4000 in 27 s. The same 2000 under one id carry in 0.48 s. Typing itself slows as the clients accumulate (2000 transactions take 1.1 s against 0.18 s under one id), because every Yjs transaction encodes a state vector that now has a client per change; and the ids stay in the document for ever, in every state vector and sync message. For the usual divergence of a second or two this is nothing; for an outage where someone keeps typing for minutes it is a multi-second freeze at the resync and thousands of clients in the deck. Two cheap mitigations, either or both: makecarrylinear (readdoc.store.clients.get(client)intook()instead of encoding the whole state vector per update, and keep one running copy forprobe(), re-copied only after a replay); and draw a fresh id per capture window (500 ms, the undo window) rather than per change, which bounds the count by time and loses nothing in practice, since changes inside one window are one undo step anyway. Same on #89. - Nit —
freshClientIDdoes not checkdoc.store.clients.has(id). One draw per change makes a collision 2^-32 per draw rather than per session; a one-line retry loop makes it impossible rather than improbable. - The whole-delete-set post and the beacon's pending row stay as the agreed follow-ups.
Validation
- Gate:
GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gateon the stacked head 72f74b0 (this commit included) stops at the known Mac wasm byte-compare; the steps after it by hand: 240/240 Node tests, vectors clean, bundle byte-identical,make ciok. On this head:carry.test.mjs24/24,make ciok,go vet -tags browserok. - Real editor on this head, full Chrome (
RASTRILLO_CHROME= Playwright's chromium-1234): the branch's threecarry_browser_test.gotests and my two probes (rv_slides_carry_replay_probe_test.go), results as above. The full browser suite ran on the stacked head; its results are on #29. - Probes:
rv-carry-review.probe.mjs(my three cases),rv-general-probe.mjs(0 of 5 lost on this module, nothing pending),rv-cost-probe.mjs,rv_slides_carry_replay_probe_test.go.