Skip to content

feat(authz): bounded append/prepend idempotency at the commit boundary (#429) - #44

Merged
PMQ9 merged 2 commits into
wiki-v2from
fix/429-append-idempotency
Sep 20, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
fix/429-append-idempotency

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 20, 2026

Copy link
Copy Markdown

What this does

Adds bounded append/prepend idempotency at the commit boundary (the substance of super-repo issue docmost#429), plus the collab-persistence review polish deferred from docmost#41.

A /v1 content write with operation: append|prepend is non-idempotent: if the fork COMMITS the write but the platform relay then times out (or a client/agent retries), the append double-applies. This fixes it at the component that commits the mutation — Docmost — rather than at a platform layer whose dedup record and the content would live in two independent stores.

The mechanism

conditionalUpdatePageContent now accepts an optional idempotencyKey. Inside the same connection.transact(fn) that applies the content, it checks the key against a top-level Y.Map on the live document and records it with the content:

  • Check-first, pure — a duplicate returns { applied:false, reason:'duplicate' } WITHOUT mutating, so the connection's closing disconnect() store hits the existing isDeepStrictEqual no-op skip (no row write, no reattribution, no broadcast).
  • Record-on-apply, atomic — a fresh key is written to the ledger in the SAME CRDT mutation as the append, so the key and the content persist together in pages.ydoc or are lost together. There is no "content committed without a key" window.
  • Concurrency is intrinsic to the single resident doc — two same-key writes serialize on the doc's synchronous transaction (the same guarantee the add a public switch, which can be viewed without logging in docmost/docmost#282 CAS already relies on), so correctness needs no platform coordination and holds even standalone.
  • The ledger is a top-level type (never the 'default' content fragment), so recording a key does not rotate the version anchor / ETag. It is pruned by age + a hard size cap.

Guarantee: bounded idempotency over a retention window (default ~1h, PAGE_WRITE_IDEMPOTENCY_RETENTION_MS), not unconditional exactly-once. The controller maps reason:'duplicate' to a 2xx no-op returning the current page.

docmost#429 review polish (docmost#41 follow-ups)

  • persistence.extension.ts — the best-effort post-commit side effects now run via Promise.allSettled instead of serial awaits (independent, each self-isolating; syncTransclusion wrapped uniformly). Shrinks onStoreDocument tail latency on the settle→write path, making a false 504 rarer.
  • persistence-reconcile.spec.ts — added per-effect isolation cases (history, mention→notificationQueue, failing transclusion) + a concurrent-all-run case.
  • editor-ext trailing-node.ts / indent.ts — corrected the imprecise "default preserves prior behavior" option comment (the default path now also requires a docChanged transaction).

How it works

Full design, the concurrency proof, and the durability-boundary audit (why no path preserves content while dropping the key map) are in the super-repo issue docmost#429 implementation-plan comment.

Testing

  • write-idem.spec.ts (new): pure ledger unit tests — dedup, no-mutation-on-check, anchor-unaffected, age/size pruning, ydoc round-trip durability.
  • conditional-content-handler.spec.ts: handler dedup wiring (apply+record first sight, no-op duplicate, independent keys, unkeyed unaffected).
  • conditional-page.controller.spec.ts: duplicate → 2xx no-op (no re-apply) + DTO validation.
  • persistence-reconcile.spec.ts: the new isolation + concurrent cases.
  • Full server tsc --noEmit clean; touched suites green; the super-repo upstream-boundary check passes against this commit.

Upstream boundary

Edits are confined to the two already-documented seam files (collaboration.handler.ts, persistence.extension.ts) — the dedup POLICY lives in fork-owned authz/page-write/write-idem.ts. UPSTREAM_MODIFICATIONS.md (super-repo) is updated in the paired PR.

Merge order

Paired with super-repo PR (issue docmost#429). Merge this fork PR first, then the super PR re-pins the gitlink to the merge commit.

🤖 Generated with Claude Code

PMQ9 and others added 2 commits September 20, 2026 00:48
docmost#429)

A `/v1` content write with `operation: append|prepend` is non-idempotent: if the
fork COMMITS the write but the platform relay then times out (or a client/agent
retries), the append double-applies. Fix it at the component that commits the
mutation — record a caller `Idempotency-Key` in a top-level Y.Map on the LIVE
document, INSIDE the same `connection.transact(fn)` that applies the content, so
the key and the content are one atomic CRDT op (persisted together in `pages.ydoc`
or lost together). A repeat within the retention window is a no-op that returns the
current page; concurrency is intrinsic to the single resident doc's synchronous
transact (no dependence on the platform). Bounded idempotency over the window
(default ~1h), not unconditional exactly-once. Builds on ADR 0019 / docmost#282 / docmost#390.

- authz/page-write/write-idem.ts (new): pure, unit-tested ledger primitive
  (isDuplicateWriteKey / recordWriteKey) — top-level Y.Map, invisible to the
  'default' content fragment (version anchor unaffected), pruned by age + size cap.
- collaboration.handler.ts (seam): thread optional `idempotencyKey`; check-first
  (pure, no mutation → benign no-op store), record-on-apply (atomic with content).
- conditional-page.controller.ts + ConditionalUpdatePageDto: accept the key,
  forward it, and map `reason:'duplicate'` to a 2xx no-op returning the current page.
- collab-outcomes.ts: add the `'duplicate'` reason.

docmost#429 review polish (docmost#41 follow-ups):
- persistence.extension.ts: run the best-effort post-commit side effects via
  Promise.allSettled (independent, each self-isolating) — shrinks onStoreDocument
  tail latency on the settle→write path, making a false 504 rarer; wrap
  syncTransclusion uniformly.
- persistence-reconcile.spec.ts: add per-effect isolation cases (history,
  mention→notificationQueue, failing transclusion) + a concurrent-all-run case.
- editor-ext trailing-node.ts / indent.ts: correct the imprecise "default preserves
  prior behavior" option comment (the default path now also requires a docChanged tx).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
… the CAS ordering, no metadata drop

Addresses the 7-lens review of docmost#44 / wiki-v2#437.

- S1 (security, blocking): namespace the idempotency-key ledger entry by the acting user
  (write-idem.scopedWriteKey). The key is caller-chosen and the ledger is per-page, so an
  unscoped key let one user's write suppress another user's genuine write as a false
  "duplicate" — a cross-user lost update returning 200. Same-identity retries still dedup.
- T1 (testing, blocking): pin the two load-bearing in-transaction orderings that were
  untested — dedup runs BEFORE the CAS (a committed keyed write's retry dedups, does not
  412 on the digest its own apply moved) and the key is recorded ONLY on an actual apply
  (a write that fails the precondition must not poison the ledger). New "key × CAS
  ordering" describe; the record-after-CAS invariant is now documented at the call site.
- F1 (architecture, L3): a duplicate content write that ALSO carried metadata now
  (re)applies the metadata instead of silently dropping it; a content-only duplicate stays
  an inert no-op. Metadata is idempotent, so re-applying on retry is safe.
- S2 (security, L3): @maxlength(255) on the DTO idempotencyKey so an over-long key cannot
  bloat pages.ydoc.
- correctness nits: strip idempotencyKey from the metadata-only update; correct the
  duplicate-path no-op comment re: the docmost#282 concurrent-edit residual.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9
PMQ9 merged commit 50a815e into wiki-v2 Sep 20, 2026
@PMQ9
PMQ9 deleted the fix/429-append-idempotency branch September 20, 2026 14:36
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