Repository navigation
feat(authz): bounded append/prepend idempotency at the commit boundary (#429) - #44
Merged
Merged
Conversation
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]>
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 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
/v1content write withoperation: append|prependis 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
conditionalUpdatePageContentnow accepts an optionalidempotencyKey. Inside the sameconnection.transact(fn)that applies the content, it checks the key against a top-levelY.Mapon the live document and records it with the content:{ applied:false, reason:'duplicate' }WITHOUT mutating, so the connection's closingdisconnect()store hits the existingisDeepStrictEqualno-op skip (no row write, no reattribution, no broadcast).pages.ydocor are lost together. There is no "content committed without a key" window.'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 mapsreason:'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 viaPromise.allSettledinstead of serial awaits (independent, each self-isolating;syncTransclusionwrapped uniformly). ShrinksonStoreDocumenttail 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-exttrailing-node.ts/indent.ts— corrected the imprecise "default preserves prior behavior" option comment (the default path now also requires adocChangedtransaction).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.tsc --noEmitclean; 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-ownedauthz/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