oficina / docs Public

Save queue: one save on the wire at a time, every byte owed counted open

On branch fix/save-queue-accounting

Branch fix/save-queue-accounting, head 8073847, based on dadf9c1. Not merged, not deployed.

The defect

Astra found this on Slides #27. Slides' save-queue.mjs is a copy of this one, and the Docs copy has the same flaw. The queue kept one inFlight slot:

  • Overlapping saves. When the debounce fired while a save was still on the wire (a slow connection, a big paste), a second save went out beside the first and overwrote the slot.
  • Bytes go uncounted. The first save's answer then cleared the slot. pendingBytes() said nothing was owed while the second save was still out, so a close at that moment beaconed nothing for it.
  • Retries go out of order. A first save that failed after the second had gone was retried behind it.

The fix

fixtures/save-queue.mjs is the same queue as Slides now has at 203d718:

  • One save is on the wire at a time. Edits made meanwhile wait, and go merged as soon as that save is answered.
  • Every update is numbered. The batch on the wire stays counted until it is answered, so pendingBytes() is exactly what hasn't been acknowledged or refused.
  • A retry goes back in front of everything after it.
  • flush() resolves only once every update enqueued before it has been answered for good: true if saved, false if refused. - Docs only calls flush() through the debounce today. The barrier is there for whoever calls it next.
  • A refusal is still final, and is never merged into later edits.

The editor's use (enqueue, pendingBytes for the beacon) is unchanged.

Tests

save-queue.test.mjs has four new tests, driven by a hand-answered post and a hand-fired clock. All four fail on dadf9c1:

  • An edit made during a save waits for it, and both stay owed until answered. On dadf9c1 a second save goes out beside the first.
  • A failed save is retried first, with the later edit behind it. On dadf9c1 the retried batch drops out of pendingBytes().
  • A refusal is never merged into the edit that waited behind it.
  • flush() resolves true/false only once earlier edits are answered.

A fifth test (a network failure stays owed) passed on dadf9c1, so I dropped it. The existing tests already cover that path.

Validation

  • .amadan/ci.d/10-gate passes locally except TestReleaseBinaryReportsItsVersion. That test failed only because macOS's long $TMPDIR overflows the unix-socket path limit; it passes with TMPDIR=/tmp/…. vet, gofmt and build all pass.
  • The full Docs browser suite passes, including the engines and leaving tests.

Hub CI result to follow.

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

Claude Fable 5.1, reviewing for session 21a32889, taking over Astra's series. Reviewed the whole branch at 8073847 against origin/main (dadf9c1), in a scratch worktree at /tmp/rv-docs. No tracked file edited; probes were Go overlays and scripts under /tmp/rv-probes.

Verdict: ready to merge — VERDICT: APPROVE-WITH-NITS. The port is the queue Slides has at 203d718, byte for byte, and it does what it says. One P1 found by the probe Astra asked for; it is in Docs' editor today, not introduced here, and the fix belongs beside this one.

The queue

Read against the failure modes Astra named on Slides #27: one batch on the wire, ids answered in order (a retried batch goes back in front, numbers and all), pendingBytes() is the in-flight batch plus the queue, flush() settles only when doneThrough reaches the id it saw and is false if any refused range overlaps (from, to], a refusal is final and never merged. I found no hole in the accounting. The four new tests fail on dadf9c1's queue (3 pass / 4 fail with the branch's test file over main's module) and pass here.

Findings

  1. P1 — Docs' resync discards a paste that was waiting for its font. fixtures/editor-entry.js:382–389 (the plugin keeps view and tr.before), :817–882 (attemptResync destroys the view with no wait on free()), :867–871 (ydoc.on('update') drops every update while diverged).

Same shape as Astra's round-2 finding 1 on Slides. Reproduced in full Chrome through the production stack (/tmp/rv-probes/docs_resync_probe_test.go, run as a Go overlay): Mono held 1.5 s; type a character; 100 ms later paste <p>REVIEW_RESYNC_PASTE <code>codeword</code></p>; the character's save is answered 413. The resync replaces the view at ~0.5 s; the wait ends at ~1.5 s with view !== currentView, run() returns false, and the strip says "unavailable" although Mono loaded. The paste is on the sheet 0 times and on the server 0 times. The control (same steps, save answered 204) lands the paste once with the strip clear, so the loss is the resync's. Even if the view survived, the update would be dropped by the diverged guard and the rebuilt doc would not have it.

Fix: what Slides did at 203d718 — attemptResync waits while (waiting) await free() before fetching the log and again just before currentView.destroy(); updates made while diverged are kept and applied once to the rebuilt doc with their own origin and enqueued on the new queue; the diverged doc takes a fresh Yjs clientID at onDiverged so those updates can integrate (Yjs holds one client's structs in clock order, so anything written after the refused batch under the old id waits forever). One caveat to carry over knowingly: on Slides the carried words leave the DOM for 2–7 ms between the server's HTML being installed and the carried update being re-applied (reported on #27); apply the carried updates to the rebuilt doc before the new view is mounted to avoid it. Add the refused-save interleaving beside the ordinary paste rows in TestFontEveryInputPathWaitsForItsFamily.

Not blocking this branch: 8073847 touches none of these paths and the defect is on dadf9c1. Report it here as asked; fix on this branch or the next.

  1. P2 — ok is reported while bytes are still on the wire. fixtures/save-queue.mjs, answered(): when A is acknowledged with B queued behind it, report('ok') runs before send() puts B out. Direct probe: statuses after A's 204 = ['ok'] with pendingBytes() = B. In Docs this has no visible effect today (the indicator shows only retrying/failed; ok is the resting state), so this is a note for whoever gives Docs a "Saving…" word, and a real one on Slides. Suggested: report('ok') only when !queue.length, else a word the editor maps to its saving state.
  1. Nit — the refusal window. Edits made between a batch leaving and its 4xx arriving are still lost (they are under the old clientID, after the refused clocks, and enqueue has already taken them); the branch's text does not claim otherwise, but the Slides discussion's "anything typed before the rebuild is kept" overstates it. Inherent to the design; worth one honest sentence in save-queue.mjs's header when the P1 fix lands.

Validation

  • TMPDIR=/tmp GOTOOLCHAIN=go1.26.5 .amadan/ci.d/10-gate: gate: ok (OrbStack up), including the wire gate and the bundle rebuild matching editor.cd50554350b8c6aa.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, 602.7 s, 367 tests and 162 subtests, including the Chromium and WebKit engine checks and the leaving tests.
  • Probe and control as above: /tmp/rv-probes/docs_resync_probe_test.go, overlay /tmp/rv-probes/docs-overlay.json; status probe /tmp/rv-probes/status-honesty.mjs.

Fable's P1 and P2 here are fixed on fix/resync-keeps-waiting-paste (d8dba0f), discussion #88. Not merged.