Skip to content

fix(collab): bound the Yjs conflict scan one editor's frames can cost (wiki-v2 #816) - #89

Merged
PMQ9 merged 5 commits into
wiki-v2from
fix/816-conflict-scan
Nov 11, 2026
Merged

PMQ9 merged 5 commits into
wiki-v2from
fix/816-conflict-scan

Conversation

@PMQ9

@PMQ9 PMQ9 commented Nov 11, 2026 •

Copy link
Copy Markdown

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):

  • It wraps 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.
  • It replays Yjs's own walks exactly, reading through the public getItem/compareIDs/createID and the _start/_map fields Yjs's own loop reads:
    • the offset left resolution;
    • the entry condition;
    • the map pre-walk;
    • case 1 and case 2 with their breaks;
    • the keyed reconnect walk.
  • It counts the steps. Going over 1,000,000 per frame refuses the frame with 4422 conflict-scan before 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.
  • Fails closed. A meter error refuses the frame as internal with stage: 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.
  • Tripwire. conflict-scan.spec.ts pins yjs 13.6.30 and the SHA-256 of integrate'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 1013 user_client_ids.
  • Per-user collab CPU, debt model. The budget has a burst of 2 s and refills 100 ms per second.
    • Every frame's work is timed from its pre-pass to its last exit, refused or not, and charged in full: the pre-pass decode and any rebuild once, the shadow apply and its inspection twice (live applies the frame again).
    • A charge is never clamped, and the balance does not refill while the charged work runs, so a frame costing T leaves its sender about 20·T of wall clock in debt: one user holds the event loop about a tenth of the time, whatever the frame size.
    • A frame that arrives while the budget is in debt gets 1013 user_collab_cpu before any work.
    • A rebuild is charged, once, to the user whose refusal dirtied the shadow (dirtiedBy).

Tests

At b23152c. All under authz/collab-ingress: 19 suites, 357 passed (4 Postgres specs skipped locally). The whole src/authz root: 180 suites, 3221 passed (363 Postgres-backed skipped locally). The two-order typecheck is clean.

  • conflict-scan.spec.ts:

    • replay exactness against an instrumented copy of yjs's loop, over 211k items with 0 mismatches;
    • the tripwire;
    • inertness, idempotent install, refusal to install, fail-closed.
  • update-guard.spec.ts: each attack shape is refused on its first expensive frame. Times:

    Shape Refused in
    password authentication failed, brand new installation docmost/docmost#816 rising ids 130 ms
    two clients 101 ms
    distinct rightOrigins 154 ms
    rightOrigin left of origin 212 ms
    keyed history 103 ms
    a frame worth about 1e8 steps at the cap (steps: 1,000,001 asserted), ~157 ms

    Plus:

    • control arms whose unguarded step counts grow quadratically;
    • live byte-identical after a refusal;
    • the wire rules with honest arms, and client-id caps;
    • no cap per item: a stale keystroke behind 60,000 concurrent siblings applies;
    • CPU debt on one clock for wall and CPU time: frames of 1.7 s, 10 s and 15.8 s each hold their sender between 5% and 12% of the event loop, with each frame's exact charge asserted;
    • frames refused in the pre-pass or in the shadow end in 1013;
    • dirtiedBy: the co-editor's exact balance is asserted;
    • report mode.
  • 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-scan refusal 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

PMQ9 and others added 5 commits November 10, 2026 23:26
… (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
@PMQ9
PMQ9 merged commit 9885f57 into wiki-v2 Nov 11, 2026
@PMQ9
PMQ9 deleted the fix/816-conflict-scan branch November 11, 2026 16:38
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