Skip to content

fix(collab): harden persistence lifecycle against silent write loss (#345) - #41

Merged
PMQ9 merged 2 commits into
wiki-v2from
fix/345-collab-persistence-robustness
Sep 19, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
fix/345-collab-persistence-robustness

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 19, 2026

Copy link
Copy Markdown

TL;DR

Fork half of super-repo cccvu/wiki-v2#345 — grouped robustness defects in the collaborative
persistence lifecycle that can silently lose writes or return misleading status. Fast-follow to docmost#282/docmost#390
(ADR 0019). This PR must merge FIRST; the super-repo PR carries the gitlink re-pin.

The linchpin

Hocuspocus 3.4.4's debouncer (useDebounce.run) deletes its runningExecutions entry only on success,
and storeDocumentHooks re-throws any hook error with a .message — with no finally. So if
onStoreDocument ever rejects, the rejected promise stays resident and every later store for that document
re-throws it before running
→ persistence is permanently dead until restart (silent data loss), and the
flushPageContent settle path returns a false 503 for a write that committed. persistence.extension is the
only hook that does work, so making it never reject fixes every store path (debounce / flush / conditional /
updatePageContent / comment marks) at once.

What changed

  • Post-store side effects never reject the hook (persistence.extension.ts). The effects that run AFTER
    the row commits (broadcast, contributors, mention/AI/history queue enqueues) are each wrapped in
    runPostStoreSideEffect (try/catch + log), mirroring the isolation syncTransclusion already has. One
    failing effect no longer skips the rest, and none can wedge the debouncer. (Defect 2 — highest severity;
    subsumes the residual of Defect 1, whose in-txn half Revert "fix: Add alignment styles for task list items" docmost/docmost#390 already handles.)
  • unloadImmediately: true (collaboration.gateway.ts). Docmost had overridden the upstream default to
    false, under which the last client disconnecting with a pending debounced store neither flushes nor
    unloads (it waits out the 10s/45s timer) — so a rolling deploy / scale-in / crash in that window loses the
    edits, and graceful destroy() can't drain. Unload still fires only after the store completes, so the
    settle's "not resident ⇒ row authoritative" invariant holds and RedisSync releases the lock as designed.
    destroy() is deliberately left unbounded — a mid-store timeout would abandon an in-flight commit
    (Postgres rolls back on connection drop); the only infinite-hang was debouncer poisoning, which the change
    above removes. (Defect 3)
  • Change-origin guards on the three reactive appendTransaction mutators — TrailingNode + Indent
    (editor-ext, via an injected filterTransaction option) and AutoJoiner (client, direct isChangeOrigin),
    mirroring UniqueID. An open browser no longer rewrites content that arrived over the collaboration sync
    (API/MCP/another client): trailing-paragraph insert, list auto-join, and indent normalization now act only
    on local edits. (Defect 5 + two same-class bugs the adjacent-path audit found.)
  • Vitest resolves @docmost/editor-ext to source, not its gitignored (possibly stale) dist/ — which
    had masked the two editor-ext guards in tests while production Vite uses source.

Every upstream-owned file touched is documented in UPSTREAM_MODIFICATIONS.md (docmost#122/#3/#59 updated;
docmost#155–docmost#159 new); scripts/check-upstream-boundary.sh is green.

Tests

pnpm exec jest src/authz src/editor-compat (414) + client pnpm test (315). New: a post-commit throw does
not reject onStoreDocument and one failing effect doesn't skip the rest; the gateway pins
unloadImmediately: true; a change-origin transaction no-ops each of the three mutators while a local one
still acts.

Out of scope (tracked separately)

updatePageContent has no try/catch (30s cross-node reply-hang on a genuine pre-commit throw; needs a
page.service change + the docmost#344 false-200 tradeoff); RedisSync customEventStart reply-on-throw;
movePageToSpace enqueue inside executeTx; upstream Hocuspocus debouncer no-finally.

🤖 Generated with Claude Code

PMQ9 and others added 2 commits September 19, 2026 09:21
…ocmost#345)

Grouped robustness defects in the collaborative persistence lifecycle:

- Post-store side effects (broadcast / contributors / mention+AI+history
  queue enqueues) now run in per-effect try/catch (`runPostStoreSideEffect`)
  so `onStoreDocument` can NEVER reject. Hocuspocus's debouncer leaves a
  rejected store resident in `runningExecutions` and re-throws it on every
  later store, so one post-commit blip (a Redis/DB hiccup) would otherwise
  permanently wedge persistence for that document (silent data loss) and
  return a false 503 on the settle/flush path. This de-poisons every store
  path (debounce, flush, conditional write, updatePageContent, comment marks).

- Flip `unloadImmediately: true` (Docmost had overridden the upstream
  default to false). With false, the last client disconnecting with a
  pending debounced store neither flushes nor unloads and waits out the
  10s/45s timer, so a restart in that window loses unsaved edits and
  graceful shutdown can't drain. Unload still fires only after the store
  completes, so the settle invariant holds; destroy() stays unbounded on
  purpose (a mid-store timeout would abandon an in-flight commit).

- Change-origin guards on the three reactive appendTransaction mutators
  (TrailingNode, Indent via an injected filterTransaction option; AutoJoiner
  via a direct isChangeOrigin check), mirroring UniqueID. An open browser no
  longer rewrites content that arrived over the collaboration sync
  (API/MCP/another client): trailing paragraph, list auto-join, indent
  normalization now act only on LOCAL edits.

- Resolve @docmost/editor-ext to source in Vitest so tests exercise the
  code that ships (production Vite already uses the source/module field),
  not the gitignored stale dist that masked the guards above.

Regression tests: post-commit throw does not reject the hook and one failing
side effect doesn't skip the rest; gateway pins unloadImmediately:true; a
change-origin transaction no-ops each of the three mutators while a local one
still acts.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…eject; pin guard wiring (docmost#345 review)

Round-1 review findings on PR #41 (post-store lifecycle hardening):

- Emit COLLAB_POST_STORE_FAILED (from the fork's authz/ collab-drift-log) when a best-effort
  post-commit side effect fails, so a systematic failure (wedged Redis/BullMQ, broken queue) pages
  via a CloudWatch alarm instead of only landing in the logs. Content is already durable, so it is a
  distinct token from COLLAB_STORE_FAILED (data-durability). [devops L3]
- Add an outer try/catch backstop around the whole post-commit block so "onStoreDocument never
  rejects post-commit" is STRUCTURAL, not merely per-callsite — it also covers syncTransclusion's own
  guard and any FUTURE unwrapped await on the linchpin path. [architecture/correctness/testing]
- Add a source-level regression test pinning that extensions.ts actually wires
  filterTransaction: (t) => !isChangeOrigin(t) into TrailingNode + Indent, so an upstream merge that
  drops the wrapper reds CI instead of silently reintroducing the docmost#345 content-rewrite bug (the
  behavioral tests configure their own guard and cannot catch this). [testing L3]
- Strengthen the post-store isolation specs to assert the alarm token fires, named per side effect.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9
PMQ9 merged commit d6f27fd into wiki-v2 Sep 19, 2026
@PMQ9
PMQ9 deleted the fix/345-collab-persistence-robustness branch September 19, 2026 17:53
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