Repository navigation
Conversation
…xtensions ## Description Add experimental `@lexical/y`, a framework-independent, extension-based Yjs 14 (`@y/y`) binding. Existing `@lexical/yjs` and React plugins retain their Yjs 13 dependencies and APIs. - Require node JSON serialization schemas and store structured NodeState without JSON stringification. - Preserve BindingV2's grouped text runs while separating shared node identity from placement. Concurrent moves preserve content, resolve duplicate placements, and project reparenting cycles deterministically. - Support live shared `Y.Node` properties and regular/flat NodeState, including aliases, external same-document values, observation, and collaborative undo. - Accept application-owned integrated roots, nested roots, and subdocuments. Keep transport, document lifecycle, loading, awareness, history, and DOM cursors separate. Replace observed providers at runtime without recreating the binding. - Add schema/format checks that suspend outgoing writes after failed reads, relative range selections with ancestor recovery, bootstrap excluded from undo, incremental reconciliation, and configurable cursor rendering. - Add opt-in synchronous Lexical update listeners and preserve commit notification ordering when mutation listeners enqueue updates. The document format is new and intentionally does not read or migrate legacy BindingV1/BindingV2 data. The integration boundary accommodates externally managed transports such as Electric's binding model; no Electric-specific adapter or integration test is included. ### Remaining work and explicit limits This PR does **not** establish that every previously reported collaboration issue is fixed. - Browser regressions are written but could not execute in this environment. Native IME behavior and Firefox/WebKit compatibility remain unverified. - Selection synchronization supports RangeSelection, not NodeSelection or TableSelection. Concurrent table structural edits and normalization need a dedicated convergence audit. - Deleted shared nodes, losing placements, and binding-owned live values are retained; automatic storage reclamation is not implemented. - Applications must coordinate concurrent initialization, schema migrations, and offline-client upgrades. - Disabling/re-enabling the binding discards local undo history. - The default cursor overlay's mounting container must not establish a transformed containing block. - Suggestions, attribution, and snapshot-diff extensions are not implemented. - Most improvements target the new package rather than patching the legacy Yjs 13 binding. ## Test plan ### Before Targeted regressions reproduced a lost pending local edit (`helloB` instead of `AhelloB`) and loss of selection after remote container deletion. Restoring the original core mutation notification behavior reproduced reversed update notification order (`second, first` instead of `first, second`). ### After - Combined core, legacy Yjs, new Yjs, and source tree-shaking run: **89 files, 2,079 tests passed**. - After the final bootstrap regression/change, the new package suite: **3 files, 49 tests passed**. - Production build, declaration build, test TypeScript checking, Flow (0 errors), ESLint, Prettier, and diff checks passed during implementation. - Added browser regressions for cursor geometry/rendering/cleanup, update ordering, selection format/style, local autocomplete decorations, and synthetic composition with a concurrent remote edit. **Browser validation remains blocked:** the installed Chromium fails at startup with `socket() failed: Operation not permitted (1)`; attempts to install Playwright's expected Chromium returned a truncated/invalid archive. These tests have not passed in this environment. Run in a browser-capable environment before treating this as release-ready: ```sh pnpm exec playwright install --with-deps chromium pnpm run test-browser packages/lexical-y/src/__tests__/browser ```
Deploying lexical with
|
| Latest commit: |
c8cb70e
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://efea7a49.lexical-a1o.pages.dev |
| Branch Preview URL: | https://feat-yjs14-extension.lexical-a1o.pages.dev |
## Description
Remove the unused disabled config and output signal. Register the binding, history, and awareness once for the editor lifetime and clean them up on disposal. Keep transport disconnection independent from binding lifecycle. Update TypeScript/Flow APIs and the subdocument lifecycle regression.
Document compatible application schema evolution: stable property types and semantics, defaults, new property names for new representations, mixed-version preservation checks, and application coordination for intentionally incompatible changes. No migration engine or Yjs 13 changes.
## Test plan
### After
pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit
Test Files 3 passed (3)
Tests 49 passed (49)
pnpm exec tsc -p tsconfig.test.json --noEmit: exit 0
pnpm exec eslint packages/lexical-y/src: exit 0
pnpm run flow:
Found 0 errors
Browser tests remain unexecuted because Chromium startup is blocked in this environment.
etrepum
force-pushed
the
feat/yjs14-extension
branch
from
October 4, 2026 04:38
7a5ad48 to
38cf478
Compare
…e dev example
## Description
Deleted document nodes previously remained live in the root store, preventing Yjs garbage collection. Delete locally removed nodes in the same undoable transaction while preserving identities for moves. Only compare the local edit's previous projection, rather than sweeping remote nodes whose placements may still be arriving. Explicit deletion wins over a concurrent move of the deleted node.
Add releaseSharedType for application-directed release of binding-owned values after references are removed. Check references across loaded roots and text formats, reject application-owned values, and use ordinary Yjs deletion so native undo/GC/filter policies apply. Do not override document retention settings.
Add dev-examples/yjs14, independent of React and the playground: two documents with disconnect/reconnect, shared NodeState, move/delete and undo/redo controls, automatic/filtered/manual collection, and separate historical checkpoint/snapshot viewing. Document retention semantics and remaining limitations. Yjs 13 code is unchanged.
## Test plan
### Before
pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/YExtension.test.ts
Test Files 1 failed (1)
Tests 3 failed | 35 passed (38)
The three GC-mode regressions failed because the deleted paragraph's live storage entry was still present.
### After
pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit
Test Files 3 passed (3)
Tests 59 passed (59)
pnpm exec tsc -p tsconfig.test.json --noEmit: exit 0
pnpm exec eslint packages/lexical-y/src dev-examples/yjs14/src dev-examples/yjs14/vite.config.ts: exit 0
pnpm run flow:
Found 0 errors
pnpm --dir dev-examples/yjs14 build:
✓ 119 modules transformed.
✓ built in 409ms
Browser interaction tests remain unexecuted because Chromium startup is blocked in this environment. This example does not establish completed suggestions/attribution UI or native IME correctness.
etrepum
force-pushed
the
feat/yjs14-extension
branch
from
October 4, 2026 05:02
108af36 to
bec7da7
Compare
… table integration
## Description
Complete the initial Yjs 14 architecture with proposal accept/reject, persistent native attribution, isolated checkpoint comparison, extensible node/table selection bookmarks, deterministic table projection repair, and cursor overlays that compensate for 2D transformed containers. Resume offline proposals against current accepted content and keep proposal retention independent of live GC.
Expand the standalone dev example with review, attribution, comparison, tables and presence. Add the optional table package entry point, matching public types, and allow declared peer dependencies in the package build. Keep Yjs 13 code and the playground unchanged.
## Test plan
### After
```text
pnpm exec vitest run packages/lexical-y/src/__tests__/unit
Test Files 5 passed (5)
Tests 76 passed (76)
pnpm run ci-check
Found 0 errors
(exit 0)
pnpm run build
(exit 0)
pnpm run build --prod
(exit 0)
pnpm run lint-flow
(exit 0)
pnpm --dir dev-examples/yjs14 build
✓ 122 modules transformed.
✓ built in 433ms
```
New Chromium regressions cover node/table highlights and transformed overlay geometry; execution is delegated to PR CI because local Chromium cannot create its required sockets. The preceding PR head passed its Chromium suite in CI. Native IME and Firefox/WebKit remain manual validation items.
etrepum
force-pushed
the
feat/yjs14-extension
branch
from
October 4, 2026 16:37
704c1ac to
b7a4546
Compare
…nventions
## Description
CI rejected the multi-entry package's legacy main field and monorepo peer dependency. Remove main/module, declare @lexical/table as a matching workspace dependency, and move its existing lockfile entry accordingly without changing any third-party resolution. Remove the now-unneeded peer-dependency build exception and correct the README.
## Test plan
### Before
CI scripts/__tests__/unit/build.test.ts:
AssertionError: expected [ '@lexical/y', '@lexical/y/table' ] to deeply equal [ '@lexical/y' ]
AssertionError: expected [ '@lexical/table' ] to deeply equal []
### After
node node_modules/vitest/vitest.mjs run --project scripts-unit scripts/__tests__/unit/build.test.ts
Test Files 1 passed (1)
Tests 2577 passed (2577)
The frozen install confirms "Lockfile is up to date, resolution step is skipped" but the offline supply-chain check lacks cached metadata for @docusaurus/faster. The policy was not disabled; CI will verify the online install.
Also enable contentEditable in the standalone demo and include its browser lifecycle smoke test.
…visible ## Description Show an editable shared-value text area for each peer, synchronize minimal text edits, and expose release/undo states. Style local table selections in the standalone example and render peer names on remote node/table highlights. Extend browser coverage for edits, offline merge, undo/release, local cell styles and remote labels. ## Test plan ### After pnpm exec tsc -p tsconfig.test.json --noEmit: exit 0 pnpm exec eslint dev-examples/yjs14/src packages/lexical-y/src/YCursorsExtension.ts packages/lexical-y/src/__tests__/browser/YCursorsExtension.test.ts: exit 0 pnpm --dir dev-examples/yjs14 build: ✓ built in 727ms Browser regressions are included in Chromium CI; local Chromium remains blocked by socket permissions.
…acked changes ## Description Group pending Yjs operations with their text, storage, placement, formatting, shared-reference and table dependencies. Expose individual accept/reject and annotated review deltas, and preserve accepted proposal authorship. Add a standalone inline review surface with per-change controls. ## Test plan ### After - Yjs 14 unit suite: 5 files, 89 tests passed. - TypeScript source/test/demo checks and declaration build passed. - Flow: Found 0 errors; export audit completed. - Standalone example production build: 123 modules transformed. - Added Chromium coverage for inline insertion/deletion and selective review; browser execution delegated to CI because local sockets are unavailable.
## Description Dependent proposal edits may still reference canceled insertions. Transfer those origins together with their deletion records so acceptance cannot resurrect the canceled characters. Hide canceled insertions from review deltas and omit groups with no visible pending operations. Update the example review walkthrough. ## Test plan ### After Yjs 14 unit suite: 5 files, 90 tests passed, including canceled-origin acceptance. TypeScript source check and ESLint/formatting passed.
…tions ## Description Track base/proposed formatting runs instead of pairing unrelated text-node boundary markers. Keep table grid geometry together without sweeping existing cell text or presentation properties into the same suggestion. Allow independent complete-row insertions while retaining their descendant content dependencies. ## Test plan ### After - Yjs 14 unit suite: 5 files, 98 tests passed. - TypeScript test/demo checks, ESLint and formatting passed. - Regression coverage includes disjoint formatting over default and non-default styles, row review out of order with undo/redo, new-row content dependencies, column/merge changes, and insertion through a proposed rowspan. - Extended the demo Chromium test with independent formatting review controls.
## Description Limit the internal boundary-marker exception to Lexical grouped text storage. Application-owned shared text can use a boundary format and needs its complete formatting run reviewed together. ## Test plan ### After Yjs 14 unit suite: 5 files, 99 tests passed. Added a live shared NodeState regression with a custom boundary formatting key. ESLint and formatting passed.
etrepum
pushed a commit
that referenced
this pull request
Oct 8, 2026
…oes and pad code spans only for spaces ## Description - `unescapeText` read backslash escapes and character references in two passes, so an escaped `\A` came back as `A` instead of the text `A`. It now reads both in one pass. Export writes `\&` before text that would read as a reference, so text like ` ` round-trips. - A decimal reference with more than 7 digits (`�`) is not a reference in CommonMark and now stays text instead of becoming U+FFFD. - A code span was padded when it started and ended with any whitespace, but a reader strips only U+0020, so spans like a tab-wrapped `x` or a lone no-break space are now written without padding. ## Test plan ### Before ``` $ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts × round-trips "\tx\t" bare × round-trips " " bare × reads "�" as the text "�" × reads "\\A" as the text "A" × round-trips text that reads as one ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 5 ⎯⎯⎯⎯⎯⎯⎯ Tests 5 failed | 13 passed (18) ``` The second line is the no-break space case, which the terminal shows as a space. ### After ``` $ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts Tests 18 passed (18) ``` Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum
pushed a commit
that referenced
this pull request
Oct 18, 2026
… before an escaped reference, and line-ending padding ## Description - When a text-match transformer that returns nothing edits the matched node in place, its text's escapes are now read only if the transformer left that text unchanged. Text the transformer wrote itself (`A`, `\*`) stays as it wrote it. - With `shouldPreserveNewLines`, backslashes are kept as written, so `\A` was exported as `\\A` and read back as `\A`. The backslashes right before an escaped `&` are now escaped too. - A code span whose ends are line endings (`\nx\n`) was written without padding, but a reader turns line endings into spaces and strips one per end, which loses them. Line endings now count as the spaces that need padding. ## Test plan ### Before ``` $ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts × pads a span whose ends are line endings × leaves the text a transformer writes as it wrote it × round-trips one after a backslash when backslashes are kept ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯ AssertionError: expected '`\nx\n`' to be '` \nx\n `' // Object.is equality AssertionError: expected [ [ 'x ', false ], [ 'A*', true ] ] to deeply equal [ Array(2) ] AssertionError: expected '\A \A' to be '\A \\A' // Object.is equality Tests 3 failed | 18 passed (21) ``` ### After ``` $ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts Tests 21 passed (21) ``` Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum
pushed a commit
that referenced
this pull request
Oct 18, 2026
…erences on export ## Description Export escaped the `&` of text that reads as a decimal character reference (`\A`) but not of one that reads as a hex or named reference (`A`, `&`), which GitHub and other CommonMark readers turn into a character, nor of one split across adjacent text nodes (`` + `5;`), which is written as one reference. It now escapes those too. ## Test plan ### Before ``` $ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts -t "other readers" × escapes the & of text ["A &"] that other readers take as a reference × escapes the & of text ["","5;"] that other readers take as a reference × escapes the & of text ["&am","p;"] that other readers take as a reference AssertionError: expected 'A &' to be '\A \&' // Object.is equality AssertionError: expected 'A' to be '\A' // Object.is equality AssertionError: expected '&' to be '\&' // Object.is equality ``` ### After ``` $ vitest run --project unit packages/lexical-markdown ``` All pass. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum
pushed a commit
that referenced
this pull request
Oct 18, 2026
… any following text nodes ## Description Export escaped the `&` of a reference split across text nodes only when the next node had the same format, and only across two nodes. A format Markdown doesn't write (underline) or a third node (`&#` + `6` + `5;`) still produced `A`, which reads back as `A`. It now joins the text of the following text nodes, whatever their format, and escapes the `&` when they complete a reference. ## Test plan ### Before ``` $ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts × escapes the & of text ["&#","6","5;"] that other readers take as a reference 8ms × escapes a reference completed by text with a format Markdown omits 4ms AssertionError: expected 'A' to be '\A' // Object.is equality AssertionError: expected 'A' to be '\A' // Object.is equality Tests 2 failed | 26 passed (28) ``` ### After ``` $ vitest run --project unit packages/lexical-markdown Tests 664 passed (664) ``` Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…arted by mutation listeners instead of deferring commits in editor.read() This replaces the meta#9335 mechanism with a fix at the source. `triggerMutationListeners` now sets `editor._updating` for the duration of the dispatch, like `triggerListeners`, so an update started by a mutation listener is queued and begun by `$triggerEnqueuedUpdates` after every listener of the commit has run, as updates started by update listeners already are. With no pending state during the dispatch, nothing can commit it early, so the `isDispatchingCommitListeners` tracking, the read-time exception and its DEV warning, and the `EditorReadMode` JSDoc and concept-doc notes are removed: `'force-commit'` again always means what its documentation says. The meta#9335 tests are kept (without the warning assertions) and the LexicalEditor test also covers a discrete update in the second listener. Behaviour changes for mutation listeners: an `editor.update()` started in one begins at the end of the commit rather than immediately, and a discrete update started in one commits when the outer commit finishes rather than inside the listener, both matching update listeners today. Refs meta#7709 ``` $ vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx packages/lexical-react/src/__tests__/unit/Collaboration.test.ts -t 7709 ✓ ... with a read in another listener (meta#7709) × ... with a discrete update in another listener (meta#7709) ✓ ... useCollabV2: false > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) ✓ ... useCollabV2: true > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) AssertionError: expected 'first\n\nsecond\n\nthird' to be 'first\n\nsecond' // Object.is equality Tests 1 failed | 3 passed | 113 skipped (117) ``` ``` $ vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx packages/lexical-react/src/__tests__/unit/Collaboration.test.ts -t 7709 ✓ ... with a read in another listener (meta#7709) ✓ ... with a discrete update in another listener (meta#7709) ✓ ... useCollabV2: false > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) ✓ ... useCollabV2: true > Should sync an update started by a mutation listener when another listener calls editor.read() (meta#7709) Tests 4 passed | 113 skipped (117) $ vitest run --project unit packages/lexical/ packages/lexical-react/ packages/lexical-table/ packages/lexical-history/ packages/lexical-yjs/ packages/lexical-list/ packages/lexical-extension/ packages/lexical-code/ Test Files 192 passed (192) Tests 3072 passed | 1 skipped (3073) ``` E2E tests were not run. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
## Description
`$writeElement` re-read the parent's whole Yjs sequence with `toArray()` and resolved every placement again for each child it placed, and checked each skipped entry against the rest of `next` with `slice().includes()`. The root is rewritten on every local update, so a keystroke in a document with N top-level blocks cost O(N²) or worse: about 2.2 s at 2,000 paragraphs and 11 s at 4,000.
The placements are now resolved once and kept in step with the deletes and inserts made to the sequence, and the skipped-entry check uses an index map of `next`. The writes made to Yjs are unchanged.
## Test plan
Probe (not committed): build an editor on `YExtension` with N one-line paragraphs, then time five single-character `editor.update(..., {discrete: true})` calls on the first paragraph and one paragraph insertion.
### Before
```
{"n":500,"load":156,"keystroke":[90,109,168,74,71],"enter":76}
{"n":1000,"load":234,"keystroke":[337,376,353,326,323],"enter":413}
{"n":2000,"load":1091,"keystroke":[2130,2253,2402,2374,2066],"enter":2359}
```
### After
```
{"n":500,"load":135,"keystroke":[7,4,9,4,4],"enter":13}
{"n":1000,"load":188,"keystroke":[7,7,6,6,7],"enter":30}
{"n":2000,"load":200,"keystroke":[8,8,8,7,9],"enter":42}
```
`vitest run --project unit packages/lexical-y` passes unchanged.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…tegrate main ## Description Merge the reviewed performance branch and main, preserve concurrent inline annotations and unknown content, repair malformed table projections, and fix shared serialization regressions. Keep three structural text-movement failures explicit; paragraph splits/joins and inline decorators still require fragment identities. Yjs 13 implementation is unchanged. ## Test plan ### Before Text-format regression suite: 3 failed | 26 passed. ### After Lexical editor, lexical-y, and Collaboration unit tests: 414 passed | 3 expected fail. TypeScript, Flow, package and dev-example builds, ESLint and formatting passed for the audit changes. Browser results are pending CI.
…hema serialization
## Description
Avoid root sequence and unrelated attribute scans for local and remote text edits. Maintain the projection's parent index for touched containers, rebuilding for cycle repair. Store nodes in unordered sequences with compact Yjs item identities and independent initializer keys, preserving concurrent first edits, remote undo, and independently reviewable content without permanent per-node map keys.
Expose one experimental schema codec contract, warn about custom JSON overrides, and reject live external values in node, EditorState and clipboard JSON export while permitting explicit snapshot codecs. Document the editor-wide experimental synchronous listener option. Move table suggestion grouping into its integration, support its optional peer in packaging, make lib0 a peer, and pin the Yjs RC exactly. The unreleased document format is now version 3.
## Test plan
### Before
The 2,000-block keystroke regression reported:
expected "toArray" to not be called at all
Number of calls: 2
### After
Targeted unit, schema, generated JSON, ordering, Collaboration, packaging and source tree-shaking checks:
Test Files 20 passed (20)
Tests 3597 passed | 3 expected fail (3600)
TypeScript, Flow (Found 0 errors), ESLint, formatting and the full package build pass. A cycle reload additionally preserves the encoded document without repair writes. The three expected failures remain documented release blockers for concurrent paragraph split/join and inline decorator splits. Browser validation of this batch is pending CI.
…on 3 reviews
## Description
Undoing the first content edit could remove its storage container along with nodes subsequently inserted by another peer. Preserve node-store allocation items in collaborative undo and individual suggestion rejection; content items remain undoable. Document the equivalent filter for custom native undo managers.
Update the standalone example's storage counter and review renderer for version 3 item identities and independent text-format bits. Its review surface previously remained empty after the format change.
## Test plan
### Before
FAIL undoing first content preserves concurrently inserted peer nodes in the shared store
AssertionError: expected '' to be 'peer'
Chromium CI also reported:
FAIL dev-examples/yjs14/src/__tests__/browser/Lifecycle.test.ts
TypeError: Cannot read properties of null (reading 'textContent')
### After
Test Files 14 passed (14)
Tests 309 passed | 3 expected fail (312)
Includes real proposal-delta rendering tests for insert/delete markup and independent bold/italic bits, plus the concurrent undo regression. The standalone example TypeScript check and targeted ESLint pass. Browser CI for this follow-up is pending. The three documented split/join ownership failures remain release blockers.
## Description
Registering table collaboration previously forced a full projection read on each remote transaction, including documents with no tables. Allow projection transforms to identify canonical subtree scopes, and refresh table subtrees before geometry repair without re-exporting unrelated paragraphs. Preserve the conservative full read for integrations that do not provide scopes. Keep non-structural root shared-reference updates observable on the incremental read path.
## Test plan
### Before
Scale.test.ts with the previous binding and the table integration enabled:
Test Files 1 failed (1)
Tests 2 failed | 2 passed (4)
The failures cover root scanning on remote keystrokes and exporting unrelated paragraph attributes on remote Enter with an actual table present.
### After
Test Files 1 passed (1)
Tests 4 passed (4)
Full Yjs 14 suite, including a new root shared-reference regression:
Test Files 13 passed (13)
Tests 308 passed | 3 expected fail (311)
TypeScript, Flow (Found 0 errors), ESLint and formatting pass. The three documented split/join ownership failures remain release blockers. Browser CI for this increment is pending; all jobs, including Chromium, Firefox and WebKit, pass on its parent e1e02b0.
etrepum
pushed a commit
that referenced
this pull request
Oct 21, 2026
…nes and mixed rules in table cells ## Description - mdast: an MDX expression in a cell keeps its string and template literals as written (two spaces stay two, tabs stay), writes a template's line ends as `\n` and drops line comments; only the whitespace around a line end becomes a space. An equation likewise only loses its line ends. - mdast: a rule anywhere in a cell's phrasing (`a<hr>b`, `<hr><hr>`, inside `<u>`) is written as `<hr>` again, and import reads each top-level `<hr>` in a cell line as a rule, with the phrasing around it as paragraphs. - `@lexical/markdown`: restores the code check before reading a line's escapes when no transformer matched, since an element transformer can make the line code (`CODE: \*` stays `\*`, `CODE: A` stays `A`). Round 5 had dropped it. - mdast-editor example: a browser test that select-all and Backspace clears the footnotes when the document starts with a heading. ## Test plan ### Before ``` × keeps "\\*" in a line an element transformer makes code 9ms × keeps "A" in a line an element transformer makes code 5ms × writes a rule inside a paragraph in a cell as <hr> and reads each back 13ms × keeps {"type":"mdxFlowExpression","value":"\"first last\" +\t`a\nb` // c\n+ d"} a block exports to in a cell on its row 6ms × keeps the horizontal rules of "a<hr>b" in a table cell across round trips 54ms × keeps the horizontal rules of "<hr><hr>" in a table cell across round trips 27ms × keeps the horizontal rules of "<u>a<hr>b</u>" in a table cell across round trips 23ms ``` ### After `vitest run packages/lexical-mdast packages/lexical-markdown packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts` and `vitest run --project browser dev-examples/mdast-editor` pass. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum
pushed a commit
that referenced
this pull request
Oct 22, 2026
…thin its own text node ## Description Export escaped the `&` of a character reference that following text nodes complete (`` + `5;`), by joining the text of the next sibling text nodes, which missed siblings that aren't text nodes anyway. That lookahead is dropped: a reference written within one text node is still escaped (`\A`), and one split across nodes is an edge case left as it is. The markdown entry is 87 B smaller (min+gzip 10554 to 10467). ## Test plan ### Before ``` $ vitest run --project unit packages/lexical-markdown × escapes the & of text ["","5;"] that other readers take as a reference × escapes a reference completed by text with a format Markdown omits Tests 4 failed | 719 passed (723) ``` (with the lookahead removed, before removing its tests) ### After ``` $ vitest run --project unit packages/lexical-markdown Tests 717 passed (717) ``` Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum
pushed a commit
that referenced
this pull request
Oct 22, 2026
## Description Export escaped the `&` of text that reads as a hex or named character reference (`A` became `\A`), but import decodes only decimal references, so such text came back with a stray backslash. Export now escapes only decimal references, matching what `unescapeText` decodes, and leaves hex and named ones as written. ## Test plan ### Before ``` npx vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts Expected: "\A A &" Received: "\A \A \&" Expected: "A &" Received: "\A \&" Tests 2 failed | 21 passed (23) ``` ### After ``` npx vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts Tests 23 passed (23) ``` Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum
pushed a commit
that referenced
this pull request
Oct 22, 2026
…s CommonMark does
## Description
Import decoded only decimal character references, so `A` and `&` stayed as written where a CommonMark reader shows `A` and `&`. Import now decodes hex references and named ones (HTML entity names only, through `decode-named-character-reference`, whose browser build reads them through the DOM rather than a table; it is kept external in the npm build as for `@lexical/mdast`). Export again escapes the `&` of text that reads as any of them (`\&`), so other readers show the text as written and it reads back as it was.
## Test plan
### Before
```
npx vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t reference
× reads "a#b�" 15ms
× reads "a&b©&nosuch;&" 10ms
× escapes the & of text ["A A &"] that reads as a reference 13ms
Expected: "\A \A \&"
Received: "\A A &"
Expected: "a&b©&nosuch;&"
Received: "a&b©&nosuch;&"
```
### After
```
npx vitest run --project unit packages/lexical-markdown packages/lexical-playground/__tests__/unit packages/lexical-mdast
Test Files 46 passed (46)
```
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 23, 2026
## Description Stacked on #9400 (`@lexical/y`). Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Repairs lift blocks in place.** A block a peer wrote inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. Peers that lift it at once write the same attribute and markers under a content-derived client id (one copy), and peers holding different surroundings keep the one block. Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document is layout 1. The first move record, join record or lifted block upgrades it in place to layout 2: one root attribute, in a transaction undo never reverts, and nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the number never goes back down. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - **Repairs** skip list merges in containers with records, and skip ragged-table repair when moved rows have row spans. - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to #65 for exploring real moves in Lexical; its v3 independently converged on one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page has a new "Wire format" section with diagrams of layout 1 (a container's sequence and the root's layout number and storage kinds), layout 2 (a move record next to the order the editor shows, and a join record) and the upgrade from 1 to 2, checked against a dumped document; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice). The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ## Test plan ### Before The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test IsInline.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × an instance whose isInline() flips is written again in its new form (2) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 60 failed | 847 passed (907) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). ### After ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 334 passed (334) $ npx vitest --project unit --no-watch packages/lexical-y Test Files 52 passed (52) Tests 2628 passed | 2 skipped (2630) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4972 passed (4972) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack ``` - `Moves.test.ts` (242 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - `tsc`, `tsc-test`, `flow`, `eslint`, `prettier`, tree-shaking and `build` pass.
This was referenced Oct 23, 2026
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 26, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 26, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 26, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 26, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 26, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 26, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 26, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 26, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
zurfyx
added a commit
to meta/lexical
that referenced
this pull request
Oct 27, 2026
Stacked on #9439 (the `@lexical/y` stack: #9400, #9437, #9438, #9439). This PR is now last in the stack. > **Status: rebased, not yet reworked.** This change was moved to the end of > the stack and onto #9400's v3 design (no shared repairs, a monotone layout > record, follow-ups that only set attributes or delete). It compiles and its > own tests pass, but parts of this description still describe the earlier > design and are marked below; it gets its own fix round (performance, > remaining review findings, docs) before review. Today a block move in `@lexical/y` is a delete plus a re-insert: the binding re-creates the cheaper side, so text a collaborator types into the moved block at the same time lands in its old neighbour, and two collaborators moving the same block can duplicate it. The normalization repairs every peer runs (a block lifted out of the paragraph it was written in) are written the same way, so two peers that lift one block from different surroundings both re-create it, and text typed into it meanwhile is lost. (On #9400 v3 the author normalizes its own write instead of every peer repairing it; see "Lifted blocks" below.) A move now keeps the moved items where they are and writes a small move record where the block shows. A move changes only position, never the moved content's characters. - **Records.** A record `↷` is a child `Y.Node` of the container. It names a range of the container's own sequence by Yjs item id (`s` is the item before the range, or none; `e` is the range's last item) and has a priority `p`. Items a peer inserts later inside the range (typing, Enter) belong to it too. This ports the semantics of Yjs' unmerged range move (ContentMove: highest priority wins, loops are broken) to binding-level records. Yjs 14 has no native move. - **Ownership is computed when reading, from the document alone.** An item belongs to the active record of highest priority (then client, then clock) whose range holds it. A record placed inside its own range, directly or through other records, shows nothing. Nothing is repaired or written back. A record written again keeps its rank (`o`). A write that splits an anchor item (typing inside the last word of a range) keeps the range: anchors resolve to the item holding their id. - **Records are not boundaries.** A record shows its units before the line its item is in, so text typed concurrently at a line start stays in its line. Formats stay positional, as in Yjs. - **Joins keep their items.** Joining a line into a moved line (Backspace, Delete, a deletion across lines) writes join records (`j: 1`) at the end of the first line's text. They show the second line's text in their place, so the joined line can span ranges and the second line's characters are never copied. A second line that is not one range (a block moved away from between its parts, or it starts inside a joined range) gets one join record per piece. Two peers that make the same join write records of the same ranges; one wins and the other shows nothing, so the text appears once. - **Lines that span ranges move with their items.** A line split off a joined line, or joined across a moved range, is moved with one join record per piece but its last (written at the start of the last piece) and a block record over the last piece, instead of being re-created. - **Lifted blocks keep their node.** A block written inside a line (a table appended to a paragraph without the normalization, an older client) keeps its shared node when the normalization lifts it out (on #9400 v3, the normalization of its author) (a line written inside a line, such as a heading in a list item, is stored as lines by #9400 and needs no lift): the node is marked a unit of its container (`u: 1` on the `i:<type>` node), which splits the line around it, and the line parts get markers. (The earlier design, where every peer lifted it under a content-derived client id, is gone with #9400's shared repairs.) Lexical replacing such a block by a new one of its type (Enter at its start) keeps the shared node too. - **Records left inside a line.** A record can end up between a line's characters when the line around it changed at the same time: a peer joined the lines around it (the record is inside the joined range), or removed the line before it while another typed in that line. New units then go right after the block the record shows only when that is a line boundary, otherwise right before the next line, and when the block shows from inside that next line it is written again with a record where it shows. A moved line that such a record interrupts moves with its pieces (its text before the record too). A record is written again at its line start only when the same records stay active (moving it out of another record's range can make a record that showed nothing show its range). - **Zero cost without moves.** Every new path sits behind "this container holds a record" or "this write lifts a block". The per-keystroke cost, size, 10k-edit growth and load time of documents that never move are unchanged, and Enter and Backspace there are within 3% of #9400. - **Incremental read.** A peer that receives a move, its undo or redo, or a structural edit in a container with records re-parses only windows around the records whose ownership changed. Each window is bounded by end items its zone owns before and after the change, so a unit keeps its node only when its content provably did not change. Format edits, join records and unresolved records still read the whole container. - **Cost with many records.** Outside a transaction, a container's cached ownership is checked against the document (records deleted, anchors split or merged) once after each transaction starts and once after Yjs cleans one up, instead of on every use; inside a transaction it is still checked on every use. Which records are active, and the ownership before a change in an incremental read, come from a painted index instead of a scan of the records per position. An incremental read takes formats from one walk of the container, and the check for records left inside a line stops where the previous record's check stopped. Nothing written changes: the updates of a scripted session and of every fuzz session are byte-identical before and after. At 10,000 random moves on 1,000 blocks (every block moved, 1,000 live records), a local move goes from 347 to 34 ms, a remote read from 1,049 to 27 ms and a load from 647 to 46 ms (#9400: 5.1, 3.6 and 27 ms). What is left grows with the container's items, deleted records included. - **Records over time.** Each move leaves a tombstone of about 18 B once Yjs collects garbage (a delete and a re-insert on #9400 leaves 16.5 B). Live records stay at most one per moved range, apart from rare cases (a block moved back to where it was, a record that lost a loop, a record whose range a peer deleted, until the next local write of that container), tracked in #9455. - **Detection.** No core hook. The writer's existing key matching (same NodeKey under the same container) recognizes the moved units. The records go to the units the user moved, and a whole range that moves again reuses its record. - **Scope: moves inside one container.** This covers the root, a list (items and whole lists), a table (rows), a row (cells, so column moves), a cell's or collapsible's content, and block decorators and containers moved as units. A move into another container is still a delete plus an insert (documented). - **Undo** removes the record, so the block returns with a peer's concurrent typing. Redo writes it again. A local write that deletes a record can let another record show. The writer then reads the container again, as a peer would. - **Layout.** A new document records layout 1 (`lexical-y:1: true`). The first move record, join record or lifted block adds `lexical-y:2: true` to #9400's monotone layout record, in the same transaction as the record (so no client ever receives a record without the version), and undo never deletes it; nothing else is rewritten. Clients without moves then open it read only instead of misreading it, and the record never shrinks. `LAYOUT_VERSION` is now 2, and documents without moves stay editable by older `@lexical/y` builds. The README's wire format section documents layout 2 (records, join records, lifted blocks, the upgrade) next to the frozen layout 1. - (Removed with #9400's shared repairs: the list-merge and ragged-table repair exceptions for containers with records.) - Warnings go to the editor's `onWarn` as `LexicalWarning`s, and the code uses the binding's read-only getters. On main, the update that flips a node's `isInline()` lifts the node itself (the normalization runs on what an update writes), so the flip is written as a block of its container, not as an inline node a later repair lifts in place; this PR therefore leaves `IsInline.test.ts` as #9400 has it. **Prior art.** We studied Bob Ippolito's etrepum#65, which gives every node its own Yjs node and moves a node by writing a `{id, token}` placement into its new parent. We chose instead to port Yjs's own unmerged range-move design (`ContentMove` on the `move` branch: a record at the destination points at the moved range, the higher priority wins, loops are broken) as binding-level records. Our reasons: splits and joins stay cheap marker edits, documents pay nothing until something moves, and #65's per-node layout has known cases where a split or join racing a collaborator's typing loses or misplaces that typing (marked as expected failures in its tests). Credit to one format key per mark id, the same as the follow-up #9439. No change to `lexical`, `@lexical/yjs` or any other package. Docs: the `y.md` page's "Wire format" section, which #9400 starts with a diagram of layout 1, gains diagrams of layout 2 (a move record next to the order the editor shows, and a join record) and of the upgrade from 1 to 2, checked against a dumped document, and links the README's layout 2 specification; its limits describe moves within and across containers, joins, and layout version 2. Its limits also cover text typed into a block moved out of a container that a peer removes or moves (lost), the same block moved into two containers (shown twice), and an undo of a removal racing a move out of the removed range (shown twice), and how records and tombstones grow over time, with the Yjs advice to reload long-lived rooms on the server. The README's wire format documents layout 2, and its fuzz note lists the moves fuzz variables. `@lexical/y` is unreleased and `@experimental`. ``` $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 62 passed (62) Tests 2981 passed | 4 expected fail | 2 skipped (2987) $ FUZZ_MOVE_SEEDS=1000 FUZZ_MOVE_GC_SEEDS=200 NORMALIZE_FUZZ_SEEDS=1000 \ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/{Fuzz,NormalizationConvergence,Moves,Races}.test.ts normalization fuzz: {"duplicated":[],"healed":[… 60 seeds],"seeds":1005} held paragraphs fuzz: {"duplicated":[],"healed":[228,295],"seeds":1000} Tests 3483 passed (3483) ``` Adapted to #9400 v3 in this rebase: the layout test now checks the record (`lexical-y:1` and `lexical-y:2` = `true`), that the version is written in the transaction of the first move record, and that undoing the move keeps it; the two "lifts in place" tests deliver the author's normalization with its write (receivers no longer lift); the "move a block" byte budget is 108 (was 104: the record's attribute is now part of the move's update). Also `pnpm run test-unit` compared test by test with #9439 (0 failed, 0 changed; the replay of a fixture this PR deletes no longer runs), `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `tsc-website`, `tsc-extension`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the tree-shaking tests (369 source, 286 built), `pnpm run build-prod` and `pnpm run build-docs` pass. The rest of this test plan is from the previous base (#9400 before its v3 rework) and is re-measured in the fix round. The new tests against the previous revision of this change (joins of lines that are not one range copied, lines that span ranges re-created, repairs written as a delete and a re-insert): ``` $ npx vitest --project unit --no-watch Moves.test NormalizationConvergence.test × move of a line split after a join ‖ typing in it (4 of 4 orders) Expected: "paragraph("zeroth") / paragraph("one") / paragraph("two") / paragraph("rXee")" Received: "paragraph("zeroth") / paragraph("Xone") / paragraph("two") / paragraph("ree")" × move of a line another zone interrupts ‖ typing in both parts (4 of 4) Expected: "paragraph("three") / paragraph("one") / paragraph("zeXrotwYo")" Received: "paragraph("XrotwYthree") / paragraph("one") / paragraph("zerotwo")" × join ‖ join of a line another zone interrupts (4 of 4) Expected: "paragraph("threezerotwo") / paragraph("one")" Received: "paragraph("threezerotwozerotwo") / paragraph("one")" × join into a moved line of a line another zone interrupts ‖ typing in it (4 of 4) × two blocks written at the end of one line by two peers, lifted by a third (12 of 12) AssertionError: expected [ 'cell', 2 ] to deeply equal [ 'cell', 1 ] × a block lifted out of the middle of a line ‖ typing next to it and in it (12 of 12) AssertionError: expected [ '¶', 'b:test_box', '¶' ] to not include 'b:test_box' × a heading lifted out of a list item ‖ typing in the heading (12 of 12) × fuzz: normalization repairs by 3 peers with partial delivery, seed 44, 47, 51, 64, 79, 93 Tests 58 failed | 839 passed (897) ``` - Randomized normalization fuzz, 2,000 seeds (3 peers, 40% of edits written without the normalization, peers typing into the same blocks, partial delivery): 490 sessions copied or lost text: 102 sessions without a nested line (101 with a copied block, 1 with a lost token) and 388 with one (a heading a peer wrote inside a list item: 387 copied, 2 lost). - `Fuzz.test.ts` / the fuzz utilities: a token a peer moves into a table, box or column counts as lost by delete wins when another peer made a removal, exactly like a token typed there. Duplicates are not excused. - Block id integration (BlockIdExtension on this change), moves fuzz, 2,000 + 500 gc seeds: 15 + 3 seeds with duplicated text (joins written as a copy, lines that span ranges moved as a delete and a re-insert). - This revision's fixes, against the previous revision of this change (a record left inside a line by concurrent edits: a unit written after the block it shows split the line, and a moved line left its text before the record behind; a heading written inside a list item stored as its own node): ``` $ npx vitest --project unit --no-watch Moves.test -t 'a record left inside a line' × a block shown from inside a joined line ‖ a new line right after it (4 of 4) + "@lexical/y self-check: line 81: text \"M0 abcNEW\" != \"NEW\"; ..." × a line typed into after a removal, shown around a record, moved (4 of 4) + "@lexical/y self-check: line 133: text \"M5 pqr\" != \"XM5 pqr\"; ..." Tests 8 failed | 234 skipped (242) $ npx vitest --project unit --no-watch NormalizationConvergence.test × a line written inside a line is stored as lines (48 of 60) × a heading written inside a list item ‖ typing in the heading (12 of 12) × fuzz: seeds 405, 435, 446, 899, 937, 1078, 1388, 1507, 1683, 1820 Tests 70 failed | 665 passed (735) ``` The normalization fuzz at 2,000 seeds copied text in 8 sessions and lost it in 2, all with a heading written inside a list item. The block id moves fuzz with undo and redo off failed 2 of 2,000 seeds (162: a self-check mismatch after a move; 741: a self-check mismatch and lost text after a new line was written next to a moved block). Enter and Backspace in a 10,000 paragraph document without moves took 71.2 and 71.5 ms against 67.0 and 66.8 ms on #9400 (3 alternating runs, +6%). - A move at 10k blocks with 100 earlier moves on #9400 without this change: #9400 writes a move as a delete and a re-insert, 78 ms end to end with 100 earlier moves at 10,000 blocks (its undo writes 131 B). - Many moves, before this revision's caching (1,000 blocks, gc on): at 10,000 random moves (1,000 live records) a local move took 347 ms, a remote read of it 1,049 ms and a load 647 ms, against 5.1, 3.6 and 27 ms on #9400; at 1,000 moves (634 live records), 24.5, 16.1 and 400 ms. ``` $ npx vitest --project unit --no-watch Moves.test Races.test Tests 341 passed (341) $ npx vitest --project unit --no-watch packages/lexical-y packages/lexical-yjs Test Files 55 passed (55) Tests 2726 passed | 4 expected fail | 2 skipped (2732) $ NORMALIZE_FUZZ_SEEDS=2000 npx vitest --project unit --no-watch NormalizationConvergence.test Tests 4973 passed (4973) # 2,000 + 2,000 fuzz seeds, matrix 864 $ NORMALIZE_FUZZ_SEEDS=10000 npx vitest --project unit --no-watch NormalizationConvergence.test -t fuzz Tests 20000 passed # 10,000 + 10,000 fuzz seeds, on the top of the stack: # 0 copies, 0 losses; 434 + 26 sessions healed a remote # self-check mismatch by a re-read $ FUZZ_SEEDS=2000 FUZZ_GC_SEEDS=500 FUZZ_MOVE_SEEDS=2000 FUZZ_MOVE_GC_SEEDS=500 \ npx vitest --project unit --no-watch Fuzz.test Tests 203 passed (203) ``` Re-run on this revision (main with the final node structure, #9452, and the updated #9400): the commands above; the moves fuzz at 2,000 + 500 gc seeds through the same counting harness as before: 26 and 7 seeds with lost or duplicated text, the same seeds as on the previous base, and 1,997 of 2,000 and 498 of 500 sessions write byte-identical updates (the other 3 + 2 differ where main's normalization now runs in a local update); the block id integration (moves fuzz, 2,000 + 500 gc: duplicated text on 882 and 1993 only, as before; the BlockIdExtension fuzz, 2,000 seeds, passes); and the collab Y e2e suite in Chromium (26/26) and Firefox (23 passed, 3 Chromium-only skips). From the previous base and not re-run: the 25,000 + 5,000 gc moves fuzz, the differential, the scripted byte-identity session, the block id run with undo and redo off, the playground drag spec, and the perf and many-moves tables below. - `Moves.test.ts` (249 tests, most in 4 orders) covers: - move ‖ type (inside, start and end), move ‖ move of the same block, neighbour moves, move ‖ delete, move ‖ retype, move ‖ bold, move ‖ Enter; - every join and split ‖ move combination: join ‖ join (Backspace, Delete, both, a range across a moved line), join ‖ split, join ‖ typing, join ‖ move, split ‖ move, undo of a join, a join ‖ an undo that restores a line inside the joined range; - new: lines that span ranges moved (a line split after a join, alone, ‖ typing in it and ‖ another move), a line another zone interrupts moved ‖ typing in both parts and joined ‖ typing and ‖ the same join, and a line that ends inside a joined range moved and then typed in, a block shown from inside a joined line ‖ a new line right after it, and a line typed into after a removal, shown around a record, then moved (each checks that the result is exactly the writer's intent); - list item moves (up, between lists, ‖ indent), whole-list moves ‖ add item, table row ‖ type and column ‖ type, container ‖ type, block decorator; - undo, redo, a second move, delete of a moved block, caret mapping, the layout bump, no record for documents that never move, and reads that stay incremental. - new: cached ownership, compared with ownership built fresh from the document after every step, across undo and redo with a peer typing, records that arrive out of order, a record whose anchors arrive late, text anchors split by typing (with and without gc), anchors merged by gc, and a moved block deleted with its old neighbour under gc. Taking out the anchor check fails them. - `NormalizationConvergence.test.ts`: the 3-peer matrix (18 violations × 8 delivery schedules × 6 id orders) passes 864/864; new lift tests (a block lifted out of the middle of a line ‖ typing next to it and in it, two blocks written at the end of one line by two peers and lifted by a third, a heading written inside a list item ‖ typing in it, a block put inside such a heading and lifted from other surroundings; 6 id orders × 2 deliveries each) check the text, the stored node (`u`, no copy, no `i:heading`) and the layout bump. The randomized fuzz fails on any copied or lost token, in every session, and always runs the seeds that failed during this work. A second randomized fuzz adds edits that write plain paragraphs into quotes and list items (joined into them as lines by the normalization) and new quotes, starting from a document with a quote and a numbered list. Both pass at 2,000 and 10,000 seeds each (0 copies, 0 losses). - Fuzz with block moves (the whole stack): 2,000 + 500 gc and 25,000 + 5,000 gc seeds, 0 absolute findings (divergence, exceptions, fragments, self-check, fresh readers). The fuzz now counts a block moved into a container a peer removed as delete wins, as it already did for text typed into one. Seeds with lost or duplicated text: 26 of 2,000 (1.3%; 69 before that accounting change), 7 of 500 gc (20 before), 278 of 25,000 (1.1%; 767 before) and 52 of 5,000 gc (157 before). All of them are documented limits: two peers moving the same block when one moves it into another container (shown twice), a move into another container racing a move of its container or an undo of a removal (shown twice), text typed into a block a list conversion lifts, and joins written as a copy. `Fuzz.test.ts` at 2,000 + 500 seeds with and without moves passes, including a regression seed (a comment over a line joined with a line of two pieces, then an undo). Differential against v2, 2,000 + 500 gc: 0 absolute findings, 5 + 1 seeds worse than v2. - This revision writes exactly what the previous one wrote. Every update of a scripted two-peer session (900 steps of moves, typing, Enter, joins, removals, undo and redo, with and without gc), of the moves fuzz (2,000 + 2,000 gc and 25,000 + 5,000 gc seeds, every peer's updates hashed per seed) and of 10,000 random moves is byte-identical before and after, and the fuzz findings, the differential (the same 5 + 1 seeds) and the block id runs below are unchanged. - Block id integration (BlockIdExtension on this stack): the moves fuzz, 2,000 + 500 gc seeds, has 0 divergence, 0 self-check warnings and 0 lost text; 2 + 0 seeds show duplicated text (882, 1993): in both, two peers remove the same text and both undo the removal, and each undo restores its own copy, which Yjs does with plain `Y.Text` too (no move is involved). With undo and redo off, 2,000 + 500 seeds have no finding (162 and 741 failed before). The BlockIdExtension fuzz, 2,000 seeds, passes. - Many moves (1,000 blocks; ms: local move, remote read of it, load; mean of the last 50 moves, median of 3 loads; #9400 / before / after): | Moves, gc | Live records | Local move | Remote read | Load | | --- | --- | --- | --- | --- | | 100 random, on | 97 | 4.8 / 8.1 / 8.3 | 2.8 / 3.7 / 3.7 | 20 / 69 / 24 | | 1,000 random, on | 634 | 4.5 / 24.5 / 17.2 | 3.1 / 16.1 / 8.0 | 20 / 400 / 29 | | 10,000 random, on | 1,000 | 5.1 / 347 / 33.5 | 3.6 / 1,049 / 26.9 | 27 / 647 / 46 | | 10,000 random, off | 1,000 | 6.7 / 357 / 35.0 | 5.5 / 1,086 / 28.8 | 49 / 783 / 135 | | 10,000 back and forth, on | 1 | 5.3 / 10.0 / 10.2 | 3.0 / 8.6 / 8.7 | 22 / 28 / 26 | | 10,000 back and forth, off | 1 | 7.6 / 11.2 / 11.1 | 4.3 / 10.1 / 10.2 | 52 / 128 / 122 | A keystroke in a moved block stays at 2.2 to 4.6 ms (#9400: 2.2 to 3.0). Sizes are unchanged (235 KB after 10,000 random moves with gc, 157 KB on #9400). The remaining cost grows with the container's items, deleted records included, which Yjs keeps in the sequence. - Perf at 10,000 blocks (end to end, shared machine, #9400 → the top of the stack): Enter and Backspace in a document without moves, median of the per-round ratio over 6 alternating rounds, +0.0 to +0.5% at the top of the stack and -1.8 to +1.3% with this change alone (+6% for the previous revision); keystroke 38.7 → 38.6 ms (list item 47.9 → 46.3), load 195 → 193 ms, size, 10k-edit growth and bytes per edit identical; a move 97 → 124 ms (131 → 106 B), its undo 69 → 73 ms (131 → 11 B). - Playground (`?isCollab=true&useCollabY=true`, the stack's playground mode, a scratch spec): drag a block while the other tab types in it, a list, a list item, a table row, undo, and the same block dragged twice: Chromium 21/21 (3 repeats), Firefox 7/7, WebKit 7/7; the collab Y e2e suite passes in all three (26/26, 23/23 + 3 Chromium-only IME skips). - On this commit: `pnpm run tsc`, `tsc-test`, `tsc-scripts`, `pnpm run flow` (0 errors), `pnpm run lint`, `pnpm run prettier`, the source tree-shaking test (368 passed), `pnpm run build-prod` and the built tree-shaking test (284 passed).
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.
Description
Add experimental
@lexical/y, a framework-independent Yjs 14 binding for schema-based Lexical nodes, plus a separatedev-examples/yjs14demo. Node content and placements have independent identities so concurrent edits survive moves. Yjs 13 production bindings are unchanged.Not ready to land: concurrent paragraph splitting, paragraph joining, and inline decorator splitting still have three documented text-range ownership failures. Their regression tests use
test.fails; passing CI does not mean these limitations are resolved.Review fixes
claude/project-thread-t75lym(e22899a) and main, including the complete mutation-listener ordering fix for [lexical][lexical-react][lexical-website] Bug Fix: Keep commit listeners in order when editor.read() is called during listener dispatch meta/lexical#9335.getNodeSchemaCodec(editor)contract. Warn in DEV about custom JSON overrides.@lexical/y/table. Make@lexical/tablean optional peer and support optional integrations in the package build checks.lib0a peer and pin@y/yexactly to14.0.0-rc.26.The unreleased wire format is now version 3. Earlier experimental versions are rejected and require migration with their original binding.
Collaboration audit
The README maps the attached HTML report and upstream drafts meta#9374, meta#9375, meta#9376, meta#9377, meta#9378, meta#9379, meta#9380, meta#9381, meta#9383 to regression coverage. Changes cover concurrent inline annotations and independent format bits, opaque unknown nodes and slots, stale text attributes, selections, listener ordering, table projection, whitespace import, table header state, list JSON import and HTML export.
The package also includes application-owned documents/transports, loading and provider extensions, awareness/cursors, collaborative history, native attribution, independent suggestion review, checkpoints and GC/retention controls. See
packages/lexical-y/README.mdfor the contracts and remaining limitations.Test plan
Before
The 2,000-paragraph keystroke regression reported:
After
Targeted core/editor, schema, generated JSON, Yjs 14, Collaboration, packaging and source tree-shaking checks:
TypeScript, Flow (
Found 0 errors), ESLint, formatting and the full package build pass. Additional tests cover root/storage scan avoidance on 2,000 blocks, incremental remote Enter, bounded deletion storage, concurrent first initialization and undo, concurrent deletion undo, and cycle reload without writing projection repairs.On this runner, a manual 8,000-paragraph audit measured approximately 11 ms median local edits and 60–65 ms median remote Enter; 2,000 paragraphs encoded to approximately 0.71–0.75 MB. These are local measurements, not a cross-machine comparison or latency guarantee.
On final head
c8cb70e, Node 22/24 unit CI, Chromium browser tests, integrity checks and package/www/website/playground builds pass. Firefox and WebKit Yjs 14 regressions also pass. The playground canary is still running; all CI jobs passed on the precedinge1e02b0commit. Native OS IME remains unverified.