Skip to content

fix(collab): reconcile stale resident doc before store so it cannot clobber the row (#390) - #35

Merged
PMQ9 merged 2 commits into
wiki-v2from
fix/390-stale-collab-clobber
Sep 18, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
fix/390-stale-collab-clobber

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 18, 2026

Copy link
Copy Markdown

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 onStoreDocument persists the resident Y.Doc to the pages row 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 existing SELECT … FOR UPDATE transaction:

  1. Read the row ydoc under the same withLock findById (one locked read).
  2. reconcileRowIntoDoc(document, page.ydoc) folds any row ops the resident doc is missing (Y.diffUpdate → non-empty structs) 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.
  3. Serialize the write payload from document after the reconcile, so it is always a superset of the locked row — never a stale subset. The FOR UPDATE lock 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. onStoreDocument cannot re-throw (Hocuspocus re-throws hook errors → unhandled rejection on the setTimeout debounce 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-boundary SEAM_ALLOWLIST + UPSTREAM_MODIFICATIONS.md seam docmost#122 in the super-repo). Emits COLLAB_STALE_RECONCILE / COLLAB_STORE_FAILED for 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 docmost gitlink 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

PMQ9 and others added 2 commits September 18, 2026 00:50
…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]>
@PMQ9
PMQ9 merged commit be7bbef into wiki-v2 Sep 18, 2026
@PMQ9
PMQ9 deleted the fix/390-stale-collab-clobber branch September 18, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant