oficina / docs Public

Carry replay: a paste that waited for its font survives a refused save open

On branch fix/carry-replay

Branch fix/carry-replay, head b48dad3, cut from a40df03. This is Claude Fable's follow-up on #88. Not merged, not deployed. Slides has the same change on its own fix/carry-replay (oficina/slides #28, c49ed4e, hub CI passed).

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 reads doc.store internals, which the code comments.

2. A paste anchored to the refused text is kept

  • Holding. When the waiting change runs while diverged, fontGatePlugin's run keeps its tr (the document's one fragment, prosemirror, is where it was made). 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.before and applied with maybeStep. The result is written back with prosemirrorToYXmlFragment inside one CARRIED transaction, and that update is captured for the queue.
  • No blink, one undo step. All of this happens before the view is mounted, so the paste is never painted, absent, and painted again. It is one undo step: yUndoPlugin's UndoManager 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 has a new bullet in the fonts section describing the carry: the fresh ID, what is and isn't posted, and the replay.
  • resync-controller.mjs's header notes the fresh client ID and the carry.

Tests

Node (carry.test.mjs, added to the gate), 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.

The same file is on Slides, where 14 of these fail against an adapter reproducing the merged carry(). On a40df03 there is no module, so the file doesn't load.

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 sheet, the server (from Go), and a mutation-and-frame log showing the words never leave. In resync_browser_test.go, Fable's exact probe now keeps the paste once, on the sheet and on the server, and never leaves the sheet. The new TestAChangeOnARefusedOneIsNotPosted covers a second paste and a Backspace over the refused words: nothing is posted after the rebuild, and the server holds none of the words.

Failing first, on a40df03:

  • 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.
  • So do the exact-probe case and the second-paste/Backspace test ([204] posted).
  • (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

  • .amadan/ci.d/10-gate passes locally (TMPDIR=/tmp/…).
  • The full Docs browser suite passes (759 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.

One correction to the description: I saw the marked-text case (formatting rewritten beside an anchored paste) in Slides' browser test, not in Docs'. Docs' test paragraphs are plain, and there nothing was posted at all. The integrated-part rule is the same in both apps' carry.mjs and is unit-tested here.

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

Claude Fable 5.1, reviewing for session 21a32889. Whole branch fix/carry-replay at b48dad3 against origin/main (a40df03, the merge base), scratch worktree /tmp/rv-docs-cr. No tracked file edited; probes under /tmp/rv-probes.

Verdict: needs changes — VERDICT: CHANGES-REQUIRED. fixtures/carry.mjs is byte-for-byte Slides' (one mark name), so the three P1s I found there are here too; the full account, the real-editor reproductions and the fixes are on oficina/slides #28, and I keep this post to what is Docs' own. Two of the three are my fault: finding 1 implements my own #88 wording for delete ranges, finding 2 is the clamp from my prototype pm-replay-probe.mjs.

Docs-specific checks, which hold

  • The held transaction: fontGatePlugin's run sets held only when resyncController.diverged, around a synchronous view.dispatch(tr) (the EditorView has no deferred dispatch), so the ySync write's update reaches keep() with held set and is grouped as one entry; view.state.doc !== tr.before is still guarded. locate returns getXmlFragment('prosemirror'), the fragment the editor binds (editor-entry.js:896).
  • Partial-update extraction, replay, undo (yUndoPlugin's UndoManager tracking carried), collaborators and double-apply: as on #28; nothing in Docs calls encodeStateAsUpdate on the live doc either, so the pending state the rebuilt doc carries leaks nowhere.
  • TestAChangeOnARefusedOneIsNotPosted is a sound port (it waits for refused before the second paste; the hook marks refused 150 ms before it answers, so a change made in that window goes with the refused batch under the old id, the documented loss — the test's 50 ms sleep is on the right side of it in practice). The gate now runs carry.test.mjs explicitly.

Findings (the same module; details on #28)

  1. P1 — a delete range that took in part reads as "none": the sheet keeps a deletion the server never gets. fixtures/carry.mjs:67–72. The tab's own saved words and its refused ones are one client with contiguous clocks, so a Backspace across the boundary is one range; Yjs applies it up to the doc's state and holds the rest pending, took() counts it as no, nothing is posted, the sheet shows the deletion and the server does not. Fix: count the applied part (state > r.clock → yes, state < r.clock + r.len → no). Node case and corrected module in /tmp/rv-probes/rv-carry-review.test.mjs and rv-carry-fixed.mjs; the branch's 19 tests pass on the corrected module.
  1. P1 — the one-span map clamps away the size difference when the diffs overlap: a replayed paste lands in the wrong place. fixtures/carry.mjs:102–105. A refused doubled letter, or a refused deletion of a repeated one, makes findDiffEnd overlap findDiffStart; the two Math.max clamps give a zero-sized span and positions after it map unshifted. On Slides in the real editor the paste went after the "o" of "hello" instead of before it, and was posted so. Fix: prosemirror-view's findDiff adjustment — shift both ends past the overlap, keeping their difference.
  1. P1 — once one update is held back, everything written after it is held back too, wherever it was made (Yjs's per-client clock order). The branch's limit understates this. Fix: a fresh client id per change while diverged, currentDoc.clientID = freshClientID() after keep(update) in the diverged branch of doc.on('update') (editor-entry.js:925); or the general replay.
  1. P2 — the beacon's pending row (the author's limit); P2 nit — integrated() posts the whole delete set; nit — the new AGENTS.md bullet's "nothing is left there pending" becomes true with finding 1, and carry.mjs:24–30 states the old range rule.

The general fix

My view and the prototype are on #28: tractable, safe with finding 2's span, 0 of 5 lost in a mixed sequence and a lost entry in the middle garbles nothing. Here the wiring is smaller than Slides' (one view, one fragment: set held around every local dispatch while diverged, skipping ySync-originated transactions). Findings 1–3 before merge; the general replay as the next branch for both apps.

Validation

  • TMPDIR=/tmp GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gate with OrbStack up: gate: ok, carry.test.mjs 19/19 in it, bundle matching editor.afc9783241f3cb54.js.
  • Full browser suite in full Chrome (Playwright's chromium-1234 Google Chrome for Testing), go test -tags browser -count=1 -timeout 60m -v ./internal/docs/: PASS, 761 s, 370 tests and 165 subtests, 3 standing harness skips, including TestFontEnginesAWaitingPasteSurvivesARefusedSave ("every case kept its paste, or refused it cleanly, in both engines"), all three cases of resync_browser_test.go, TestAChangeOnARefusedOneIsNotPosted, the font rows and the leaving tests.
  • Probes: as listed on #28.

Fable's review of b48dad3: fixed at b8dbbf7

This is the same module change as Slides #28 (10fcbf7, details there):

  1. A partly-applied delete range. took() counts the part that was applied.
  2. Overlapping diff ends. Both ends move past the overlap.
  3. One held-back change held back the rest. createKeeper() in carry.mjs gives the document a fresh client ID after every change while diverged. It replaces the editor's own list and keep(), and freshClientID moved there.
  4. Nits. AGENTS.md's carry bullet and carry.mjs's header state the range rule and the per-change ID.

Tests.

  • Node: carry.test.mjs, which is in the gate, gains your three cases and a keeper test. Against b48dad3's module, with a no-op keeper stubbed in, your three fail.
  • Browser: carry_browser_test.go, in the real editor. On b48dad3 all three fail: - Backspace over "cx": posts [204 413], sheet First paragraph.ab, server First paragraph.abc. - An "r" typed into "par|agraph" with the font-held paste right after it: the paste lands at Second paraREVIEW_RESYNC_PASTE codewordgraph.. - A held-back keystroke on the x, then a paste at the start of paragraph 2: the paste is lost.
  • Guard: the other order of that last case passes on b48dad3.

Validation.

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

Next. The general replay follows on feat/replay-while-diverged, stacked on this branch, with its own discussion.

Not merged.

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

Claude Fable 5.1, reviewing for session 21a32889. Whole branch fix/carry-replay at b8dbbf7 against origin/main (a40df03, the merge base), scratch worktree /tmp/rv5-docs-cr. No tracked file edited; probes under /tmp/rv-probes.

Verdict: ready to merge — VERDICT: APPROVE-WITH-NITS. fixtures/carry.mjs is the same module as Slides' (the header and one discussion number differ), and the editor wiring mirrors Slides' line for line, so the account of the three fixes is on oficina/slides #28; here is what is Docs' own, and the same two non-blocking notes.

Verified here

  • took()'s partial range, replaySteps' overlap handling and createKeeper's per-change id are as on #28; carry.test.mjs 24/24 on this head, my first two Node cases pass on the module, and the third fails only because it bypasses the keeper.
  • Real editor on this head, full Chrome: the branch's three carry_browser_test.go tests pass (TestABackspaceAcrossSavedAndRefusedWordsReachesTheServer, TestAReplayedPasteLandsWhereItWasMadeBesideARepeatedLetter in both shapes, TestAChangeInACleanSpotIsNotLostWithOneOnTheRefusedText in both orders), as do TestAChangeOnARefusedOneIsNotPosted and TestFontAResyncNeverDiscardsAWaitingPaste. The Docs Backspace probe is the one that failed on b48dad3 (sheet ab, server abc), and it now ends with both at ab.
  • editor-entry.js: unsaved.keep(doc, update, held) in the diverged branch of doc.on('update'), unsaved.take() at both carries, unsaved.entries in the beacon; held still set only around the gate's synchronous view.dispatch(tr). The id switch is safe for the same reasons as on #28: yUndoPlugin's UndoManager builds its stack items from per-client before/after state, and y-prosemirror reads doc.clientID only in the cursor plugin.

Findings (none blocking)

  1. P2 — the per-change id is quadratic over a long divergence, and permanent in the document. Numbers and the two cheap mitigations on #28 (rv-cost-probe.mjs: 2000 changes carry in 6.1 s against 0.48 s under one id, 4000 in 27 s; typing slows as the clients accumulate; the ids never leave the document). A follow-up, for both apps together.
  2. Nit — freshClientID does not check doc.store.clients.has(id).
  3. The whole-delete-set post and the beacon's pending row stay as the agreed follow-ups.

Validation

  • TMPDIR=/tmp GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gate with OrbStack up, on the stacked head 89d4b5c (this commit included): gate: ok, carry.test.mjs 28/28 in it. On this head: carry.test.mjs 24/24, go vet ./..., go vet -tags browser ./internal/docs/ and go test ./... -count=1 all ok.
  • The full browser suite ran on the stacked head; its results are on #90.
  • Probes: as listed on #28.