Repository navigation
fix(collab): bound the Yjs conflict scan one editor's frames can cost (wiki-v2 #816) - #89
Merged
Merged
Conversation
… (wiki-v2 docmost#816) A crafted collab frame can make Yjs's YATA conflict scan (Item.integrate) walk the whole page once per item: quadratic, paid by the guard's shadow, by live and by every browser, and nothing in Yjs or Hocuspocus bounds it. - conflict-scan.ts: a meter installed once on Y.Item.prototype.integrate. Inside the guard's shadow apply only, it replays Yjs's walks read for read (offset left, entry condition, keyed pre-walk, loop, keyed reconnect) before Yjs makes them and trips past 50k steps per item or 1M per frame. The trip lives on the scope; a replay error is a ConflictScanMeterError (fails closed). Elsewhere the wrapper is one null check and never throws. A yjs of another shape refuses to install, so the guard and the boot fail. - update-guard.ts: conflict-scan refusals (4422, logs steps and cap); meter failures are internal with stage conflict-scan; wire rules delete-order (falling or overlapping delete ranges) and attr-kind (a keyed item with a rightOrigin, checked in the shadow: the wire does not show keyed-ness); a per-frame ceiling of 64 client ids new to the page (client-ids) with the rest charged to the (user, document) id budget awareness uses, paid once; a per-user collab CPU debt (2x the shadow step, clamped to the 2 s burst, refill 100 ms/s; 1013 user_collab_cpu while negative), with a rebuild charged to the user whose refusal dirtied the shadow. - Specs: replay exactness against an instrumented yjs, a source tripwire, inertness, install refusal, fail-closed paths, each attack shape with a quadratic control arm, wire rules with honest arms, the id and CPU budgets, report mode, and a real Hocuspocus 4.7.1 refusal. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Vwm9j5B5LLLzKWohCPRa6q
…wiki-v2 docmost#816) A seeded corpus of 2-5 concurrent editors (typing, marks, attributes, deletes, pastes, y-tiptap edits, undo/redo, offline runs, reconnect Step2s, remounts from a local copy) through a real CollabUpdateGuard in enforce mode: no refusal, no 1013, convergence. Plus the worst honest cases the step-0 bench found (an offline run typed where another editor pasted 2000 paragraphs; one Step2 carrying several offline sessions' ids), and an adversarial peer that trips, as anti-vacuity. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Vwm9j5B5LLLzKWohCPRa6q
…er-item scan cap (docmost#816) Round 1 of the PR 831 review. - CPU debt: charges are no longer clamped to the burst, and the balance no longer refills while the charged work runs, so frames of 10 s and more leave their sender in debt (they ran back to back before). - The clock starts before the pre-pass, so frames refused there (delete-order, client-ids, undecodable, ...) are charged too. - The pre-pass and any rebuild are charged once, the shadow apply twice. - No cap per item: a co-editor's cheap concurrent siblings made an honest keystroke trip the 50k item cap (4422). The 1M frame cap stays. - The refusal log extra is typed; the trip logs `steps` only. - Specs: wall and CPU on one clock (share under 12% for 1.7 s, 10 s and 15.8 s frames); refused pre-pass and shadow frames end in 1013; the victim keystroke applies; the honest fuzz runs on the real CPU clock and asserts its lowest balance. Each guard is mutation-proven. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Vwm9j5B5LLLzKWohCPRa6q
…itor (docmost#816) Round 2 of the PR 831 review (Testing N1, Level 3): with charges no longer clamped, the refused user went into debt after one blamed rebuild, so the spec no longer exercised several rebuilds and dropping `- othersMs` survived. The rebuild cost is lowered so several rounds rebuild, and the co-editor's exact balance (its own applies only) is asserted; that mutant now fails. Also notes the measured real-clock headroom beside the honest worst-case balance assertion (Testing N2). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Vwm9j5B5LLLzKWohCPRa6q
Resolves update-guard.ts/.spec.ts against docmost#810's shared log sink and Hocuspocus MessageType import, and lists docmost#816's user_client_ids and user_collab_cpu in docmost#810's typed SOCKET_LIMITS. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Vwm9j5B5LLLzKWohCPRa6q
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 bounds the Yjs work one editor's collab frames can make the server do. It fixes wiki-v2 issue 816 (https://github.com/cccvu/wiki-v2/issues/816). It is paired with the super-repo PR (gitlink, ADR 0036 amendment, live harness), https://github.com/cccvu/wiki-v2/pull/831. Everything is CCC-owned code under
apps/server/src/authz/collab-ingress/; no upstream file changes.Why. Yjs's conflict scan (
Item.integrate,yjs/src/structs/Item.js:459-482) walks every struct between an item's origin and its rightOrigin. Crafted frames make that walk quadratic, and the issue-781 guard paid for it twice: once on the shadow, once on live. The issue's frames were about 160 KB each, took 13–77 s, and stalled the whole process.The meter (
conflict-scan.ts):Y.Item.prototype.integrate, and is active only while the guard's shadow applies a client frame. Everywhere else the wrapper costs one null check and never throws.getItem/compareIDs/createIDand the_start/_mapfields Yjs's own loop reads:conflict-scanbefore Yjs does the work. There is no cap per item: one item's walk is linear in the concurrent structs it crosses, which another editor places, and a 50k per-item cap (the first version) refused an honest stale keystroke behind a co-editor's cheap concurrent siblings.internalwithstage: conflict-scan. The broken state stays in the shadow, which is discarded, and live never sees the frame. If the install fails (missing or wrongly shaped exports), the guard's constructor throws, so the server does not boot.conflict-scan.spec.tspins yjs 13.6.30 and the SHA-256 ofintegrate's source. A yjs bump turns CI red until someone re-checks the replay.Wire rules and budgets (
update-guard.ts):delete-order: each client's delete ranges must be ascending and non-overlapping. Every Yjs producer emits them sorted and merged, and falling ranges make the split splices quadratic.attr-kind: a keyed item may not carry a rightOrigin. Yjs never sends one, and without this rule the reconnect walk runs outside the loop.client-ids: a frame may introduce at most 64 client ids that are new to the page. They are paid for once, from the existing per-(user, page) ids bucket, which is shared with awareness. When that bucket is empty the socket gets 1013user_client_ids.user_collab_cpubefore any work.dirtiedBy).Tests
At
b23152c. All underauthz/collab-ingress: 19 suites, 357 passed (4 Postgres specs skipped locally). The wholesrc/authzroot: 180 suites, 3221 passed (363 Postgres-backed skipped locally). The two-order typecheck is clean.conflict-scan.spec.ts:update-guard.spec.ts: each attack shape is refused on its first expensive frame. Times:steps: 1,000,001asserted), ~157 msPlus:
dirtiedBy: the co-editor's exact balance is asserted;honest-concurrency.spec.ts: a seeded fuzz of 2–5 peers running y-tiptap edits, undo and redo, pastes, offline runs and remount Step2s. 2,299 frames give zero refusals and zero 1013s, and the documents converge. The CPU budget runs on the real CPU clock: the lowest balance was about 1,875 of 2,000 ms, and about 1,590 in the worst honest case (asserted above 1,000 and above 0). An adversarial peer trips the meter (anti-vacuity).The extension spec against real Hocuspocus 4.7.1: a
conflict-scanrefusal closes the attacker's session with 4422, and an honest peer keeps editing.38 mutants of the first design plus 8 added during review against the final design, all killed. The list is in the super PR.
Not in this PR
Other measured single-frame stalls (formatting cleanup, ascending splits, reload growth) are deferred, by the owner's decision, to wiki-v2 issue 827. Review polish (Level ≤2) is in wiki-v2 issue 832.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Vwm9j5B5LLLzKWohCPRa6q