oficina / docs Public

Resync keeps a paste that was waiting for its font open

On branch fix/resync-keeps-waiting-paste

Branch fix/resync-keeps-waiting-paste, head d8dba0f, based on 88b60fa (#87 merged). This fixes Claude Fable's P1 and P2 on #87. Not merged, not deployed.

P1: a resync discarded a paste that was waiting for its font

What happened: a save was refused (413), so attemptResync destroyed the view while a paste with a code mark was waiting for Mono. When the wait ended its view was gone, so the paste was never made, and the strip said the font was unavailable although it had loaded. Even if the paste had been made, ydoc.on('update') dropped every update while diverged.

The fix ports Slides' approach (oficina/slides #27, 203d718 and 40c3652), with the no-blink ordering from the start:

  • Wait. attemptResync waits for a pending font action twice: before it fetches the log, and again with no await between that check and the view's replacement. The action runs on the view it was made in.
  • Carry. Updates written while diverged are kept in unsaved, not dropped. They are applied to the rebuilt doc before its view is mounted, so the words are never off the sheet for a frame. Then they are saved through the new queue. - yUndoPlugin now gets an UndoManager built first, tracking carried, so a carried paste is one undo step. Its options are the plugin's own defaults plus carried.
  • Fresh client ID. From divergence, the doc writes under a fresh Yjs client ID (onDiverged). Yjs integrates one client's updates only in clock order, so anything written after the refusal under the old ID would wait forever for the refused clocks.
  • Only what integrates is posted. A carried update is posted only if the rebuilt doc's state vector covers it. One that can't integrate would be answered 204 and sit pending in the log forever, the silent 204 the resync comment warns about.
  • Beacon. The unload beacon also sends unsaved.

A limit Paul should know about

Fable's probe pastes straight after the refused character. Yjs anchors the paste to that character, which the server never took, so the paste cannot integrate anywhere: not on the rebuilt doc, and not on the server.

So in that exact case the paste is still lost. The change from main is how it's lost:

  • the strip no longer says "unavailable";
  • nothing is posted;
  • the save line says failed (sticky, as for any refusal).

Keeping it would mean rewriting the paste's Yjs anchors, or replaying it at the ProseMirror level against a doc that lacks what it was pasted next to. I didn't attempt either; it's a decision for Paul. The same limit applies on Slides (#27). Edits typed between a batch leaving and its refusal arriving were already lost with it; save-queue.mjs's header now says so.

P2: the queue said ok with a save still owed

answered() now reports saving when an answered save leaves more queued. Docs has no word for saving, so the editor maps saving to the resting indicator, as before. Only the queue's word changed.

Tests

internal/docs/resync_browser_test.go has three cases. Both refused cases, and the new save-queue unit test, fail on 88b60fa:

  • On the refused text (Fable's probe as written). The strip must not say unavailable, nothing must be posted after the rebuild, the save line must say failed, and there must be no errors. On 88b60fa the strip says "unavailable".
  • Elsewhere. The refused character is in the first paragraph and the paste is at the end of the last. The paste must be on the sheet once and on the server once, and a mutation-and-frame log must show it appearing once and staying. On 88b60fa it is lost: 0 on the sheet, 0 on the server, "unavailable".
  • Control. Fable's probe with the save accepted.

The test waits 30 ms between moving the caret and dispatching the synthetic paste. ProseMirror reads a moved DOM caret on selectionchange, so without the wait the paste landed after the refused character in every case.

Validation

  • The gate passes locally (TMPDIR=/tmp/…, as on #87).
  • The full Docs browser suite passes (619 s).
  • The new test passes 3 times out of 3.

Hub CI result to follow.

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

Claude Fable 5.1, reviewing for session 21a32889. Whole branch at d8dba0f against origin/main (88b60fa, the merge base), scratch worktree /tmp/rv-docs; the fail-without-the-fix checks ran in a second worktree at 88b60fa with the new test overlaid. No tracked file edited; probes under /tmp/rv-probes.

Verdict: ready to merge — VERDICT: APPROVE-WITH-NITS. My P1 on #87 is fixed at the root and the P2 with it. One P2 comes over from Slides' carry() byte for byte; one nit; and my view on the open decision, with a prototype, below.

The port, checked

  • attemptResync waits twice (fixtures/editor-entry.js:871, :935) with no await between the second check and currentView.destroy(); wait() sets waiting = false, runs the action and releases free() in one synchronous stretch, so the paste lands on its own view and the strip no longer says "unavailable". carry() runs before mountView. The UndoManager is y-prosemirror 1.3.7's own shape with carried added; yUndoPlugin({ undoManager }) is a supported option there, and the plugin's view() still attaches its stack-item handlers and destroys the manager with the view, so nothing leaks across resyncs. The fresh id touches nothing else in Docs: the only clientID use is the assignment, and there is no awareness or cursor plugin.
  • Fail-without-the-fix: TestFontAResyncNeverDiscardsAWaitingPaste on 88b60fa — elsewhere fails (0 on the sheet, 0 on the server, strip "unavailable"), on the refused text fails (strip "unavailable"), control passes. The new queue unit test fails over main's save-queue.mjs (7 pass / 1 fail) and passes here (8/8).
  • Undo (probe TestReviewDocsCarriedPasteIsOneUndoStep, /tmp/rv-probes/docs_blink_probe_test.go): one Cmd-Z after the resync removes exactly the carried paste; the sheet equals the server's text; the server drops it on the next save (posts [413 204 204]). failed stays through the carried paste's 204 — updateIndicator ranks sessionFailed first.
  • Frames: in the elsewhere case the words appear once (a mutation at 1794 ms) and stay. In the refused-text case they are in the old view's DOM for about 7 ms between two mutations with no frame between them ({1799 true mut} {1806 false mut}), so they are not painted before they go.
  • Collaborators: the live onUpdates path is unchanged, and the server's structure holds the carried paste once, which is what a second tab reads.

Findings

  1. P2 — carry() catches only the first change made on top of refused text; later ones, and a deletion of the refused text, are still posted. fixtures/editor-entry.js:131–147, the same function as Slides' (reported there with the browser demonstration). Y.encodeStateVectorFromUpdate(u) reports a client only when its structs in the update start at clock 0 (yjs's encodeStateVectorFromUpdateV2: "must start at 0"), and a delete-only update has no structs, so every update under the fresh id after the first decodes to an empty state vector and reads as "took". Node with the real yjs (/tmp/rv-probes/carry-probe.mjs): two keystrokes after the refused x → took 1 of 2 (the second), neither integrated; Backspace over x → took, and a server applying it holds a pending delete set. On Slides, in the browser, the second paste after a refusal and a Backspace over the refused words were each posted and answered 204. - Harm: the dangling row this exists to stop, for everything typed on top of refused text after the first update. Readers unaffected; the console.warn accounting is off with it. - Fix: decide from the update's own contents. const { structs, ds } = Y.decodeUpdate(u); after applyUpdate, a struct took iff the doc's state for its client reaches id.clock + length, a deleted range iff it reaches clock + len. Skip an update only when none of its structs or ranges took; post one that partly took, since the server integrates exactly the same part and skipping it would leave the sheet showing what the server lacks. Add a second paste after the refusal, and a Backspace over it, to the refused case.
  1. Nit — fixtures/resync-controller.mjs:8–32 still says every update produced after a drop is relative to content the server never received and none should be sent. The second half holds (nothing is sent while diverged); the first no longer does under the fresh id and the carry. One sentence pointing at unsaved and carry(). Docs' AGENTS.md has no line on the carry where Slides' now does (its fonts section, "The carried updates go into the rebuilt doc before any view of it is mounted…"); the same sentence belongs here when this lands.

The open decision

My view: the loss is acceptable for this merge, and not as the end state. The replay is more tractable than the branch text suggests, and I prototyped it.

  • Why acceptable now. The case needs a 4xx refusal, a font wait, and a paste anchored exactly to the refused text. The refused text is lost regardless; the paste's words are never painted; nothing is posted; the save line says failed and stays; and the clipboard still holds the paste, so recovery is one Cmd-V. That is honest loss of a small, rare, recoverable kind.
  • The prototype (/tmp/rv-probes/pm-replay-probe.mjs, Docs' own schema and ProseMirror). Map the held transaction's steps from tr.before onto the rebuilt doc through the one span where the two differ (Fragment.findDiffStart/findDiffEnd → a StepMap), apply each with maybeStep, give up if any step maps to null or fails. Kept the paste in 10 of 11 cases: the exact probe; the refused x before, after and mid-paragraph; two refused spans; a paste over a selection ending in the refused x; a refused deletion; two pasted paragraphs; a colleague's words arriving before the spot and at it. Refused cleanly ("Invalid content for node doc") when the paste was inside a refused new paragraph. It never garbled: ProseMirror validates every step, and the failure mode is today's.
  • Why not required for this merge: the wiring, not the mapping. The wait plugin must keep the tr it dispatched; the replay must happen before mount to keep the no-frame rule (build the PM doc from the fragment with yXmlFragmentToProseMirrorRootNode, apply the mapped steps, write back with prosemirrorToYXmlFragment(pmDoc, fragment) inside a doc.transact(…, CARRIED) so the undo manager tracks it as one step, and capture that update for the queue, since doc.on('update') drops it before currentDoc = doc); and only when that also fails say failed. A day with tests in each app. Under Paul's rule I would make it the next branch for both apps, folding finding 1 in, rather than hold these two.

Validation

  • TMPDIR=/tmp GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gate with OrbStack up: gate: ok, including the bundle rebuild matching editor.455b88221c9de6ec.js.
  • Full browser suite in full Chrome (RASTRILLO_CHROME = Playwright's chromium-1234 Google Chrome for Testing), go test -tags browser -count=1 -v ./internal/docs/: PASS, 624 s, 368 tests and 165 subtests, 3 standing harness skips (catalog keys, native scripts), including all three cases of resync_browser_test.go, the font rows with paste/undo/redo, the Chromium and WebKit engine checks and the leaving tests. (A first attempt was cut off by go test's default 10-minute timeout with no failure; these numbers are from the re-run with -timeout 45m.)
  • Probes: /tmp/rv-probes/docs_blink_probe_test.go (overlay docs-blink-overlay.json), /tmp/rv-probes/carry-probe.mjs, /tmp/rv-probes/pm-replay-probe.mjs, /tmp/rv-probes/docs-main-overlay.json for the run on 88b60fa.