Repository navigation
fix(collab): reconcile stale resident doc before store so it cannot clobber the row (#390) - #35
Merged
Merged
Conversation
…lobber the row (docmost#390) The debounced onStoreDocument persisted the resident Y.Doc to the pages row unconditionally (bar a deep-equal no-op). ADR 0019 guarded the API->Y.Doc write direction (conditionalUpdatePageContent) but left Y.Doc->row unguarded: when a resident doc goes STALE relative to the row (the row advanced out-of-band — e.g. the rolling-deploy two-node window ADR 0019 names under "known degradations"), its next flush silently reverts the row. That is the docmost#390 data loss. Symmetric guard, inside the existing FOR UPDATE transaction: - read the row ydoc under the SAME withLock findById (one locked read), - reconcileRowIntoDoc() folds any row ops the resident lacks back in via a lossless Yjs union (no-op in normal op; both a concurrent human edit and an out-of-band API write survive), then serialize the write payload from the doc AFTER the reconcile, so it is always a superset of the locked row — no stale-subset clobber (TOCTOU). - null-ydoc legacy rows (verified absent from all live content-write paths) keep a defensive blank-clobber refusal. Folds in ADR 0019's other named degradation: onStoreDocument no longer silently swallows a store DB error — it records it (store-failure-registry) so the settle (flushPageContent) fails a guarded write closed instead of reporting a false success over an unwritten row. It does not re-throw (that would risk an unhandled rejection on the debounce timer path). Reconcile + store-failure logic live in fork-owned authz/page-write/; the upstream persistence hook only calls them (recorded in the import-boundary SEAM_ALLOWLIST). Emits COLLAB_STALE_RECONCILE / COLLAB_STORE_FAILED for the platform alarms. Tests: reconcile-store.spec (real yjs: clobber reproduced->fixed, no false positive, concurrent-edit survival, null-ydoc no-op); store-failure-registry.spec; the settle fail-closed case; persistence-reconcile.spec pins the locked-read/serialize-order/ record-clear/null-ydoc orchestration. Refs cccvu/wiki-v2#390 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…n TOCTOU + alarms (docmost#390) - Extract the null-ydoc blank-clobber decision into a pure, unit-tested authz/page-write/blank-clobber-guard.ts (architecture review: its reconcile sibling was already extracted; the upstream seam stays a call-through). The classifier is now STRUCTURAL (isBlankProseMirror) not text-based, so an image-only / table-only / drawio page is correctly non-blank (correctness review). - persistence-reconcile.spec: pin serialize-INSIDE-the-FOR-UPDATE-txn (an inTx flag assertion — moving serialize out but after the reconcile stays green on the order test alone yet reopens the TOCTOU), and assert the two alarm call-sites (logStaleReconcile on merge, logStoreFailure on failure) fire — dropping either keeps data-loss protection working while silently killing the shipped alarms (testing review). Plus a null-ydoc genuine-edit-proceeds case. - reconcile-store: document + test the bounded divergent-lineage DUPLICATION edge (a legacy content-without-ydoc page loaded on two nodes duplicates, not loses — strictly better than the pre-docmost#390 clobber, alarmed; real cure is routing docmost#395) (correctness review). All fork authz + collaboration suites green (37 suites / 357 tests). Refs cccvu/wiki-v2#390 Co-Authored-By: Claude Opus 4.8 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Fixes the fork side of cccvu/wiki-v2#390 — a
/v1/MCP page write is accepted and persisted, then silently reverted ~2–3 min later (repeated restores each eaten; the page renders blank while history holds the content).Root cause. The debounced
onStoreDocumentpersists the resident Y.Doc to thepagesrow unconditionally (bar a deep-equal no-op). ADR 0019 (docmost#282) guarded the API → Y.Doc direction but left Y.Doc → row unguarded. When a resident Y.Doc goes stale relative to the row (the row advanced out-of-band — the rolling-deploy two-node window ADR 0019 names under "known degradations", or a lock-expiry mis-route), its next flush reverts the row. ADR 0019 filed this class separately; this is it.The fix — reconcile-before-store (the symmetric guard)
Inside
onStoreDocument's existingSELECT … FOR UPDATEtransaction:ydocunder the samewithLockfindById(one locked read).reconcileRowIntoDoc(document, page.ydoc)folds any row ops the resident doc is missing (Y.diffUpdate→ non-emptystructs) into the resident doc via a lossless Yjs union — no-op in normal operation; preserves both a concurrent human edit and the out-of-band write.documentafter the reconcile, so it is always a superset of the locked row — never a stale subset. TheFOR UPDATElock is held read-through-write, so no concurrent writer can change the row in between (TOCTOU closed by the lock).A null-ydoc legacy row (verified absent from every live content-write path) keeps a defensive blank-clobber refusal.
Folds in ADR 0019's other named degradation: a failed store no longer reports false success.
onStoreDocumentcannot re-throw (Hocuspocus re-throws hook errors → unhandled rejection on thesetTimeoutdebounce path), so it records the failure; the settle (flushPageContent) reads it and returns{ flushed: false, reason: 'error' }→ the platform fails the guarded write closed (503).Fork-owned logic lives in
authz/page-write/{reconcile-store,store-failure-registry,collab-drift-log}.ts; the upstream persistence hook only calls it (import-boundarySEAM_ALLOWLIST+UPSTREAM_MODIFICATIONS.mdseam docmost#122 in the super-repo). EmitsCOLLAB_STALE_RECONCILE/COLLAB_STORE_FAILEDfor the super-repo alarms.Tests (TDD)
reconcile-store.spec.ts— real yjs: clobber reproduced → fixed; no false positive; concurrent human edit + out-of-band write both survive; null-ydoc no-op.store-failure-registry.spec.ts,collab-drift-log.spec.ts,collab-flush-handler.spec.ts(settle fails closed on a recorded store failure),persistence-reconcile.spec.ts(single locked read incl. includeYdoc, reconcile-before-serialize order, record/clear-failure, null-ydoc refusal).authz+collaboration: 36 suites / 347 tests green; upstream boundary check passes.Merge order
Fork-first: merge this, then the super-repo PR re-pins the
docmostgitlink to this PR's merge SHA. Coordinated with the parallel docmost#368/docmost#392 fork work to sequence the shared gitlink re-pin.Refs cccvu/wiki-v2#390
🤖 Generated with Claude Code