Repository navigation
[lexical-y][lexical-website] Bug Fix: Two clients initializing a nested or emptied root write the initial content once - #9559
Conversation
…ed or emptied root write the initial content once ## Description `initialize()` writes the initial content of a pristine top-level root once: under a client id derived from the bytes it writes (`seedUpdate`), so two clients that initialize the same document with the same content at once write the same items. Any other root was written under each client's own id, so two clients opening such a document at the same moment showed the content twice (#9515): - a root inside another shared type (the binding's `root` is a `Y.Node` in an app map, as apps with several editors per document do); - a root that held content or an attribute, deleted since. The scratch document the seed is written into was empty, and the items of such a root name what they are written next to (the deleted items, the parent type's item), which an empty document does not hold. Now any other root is written into a copy of the document, and the update is what that write added. The derived id hashes the copy's state vector with the bytes: two clients share an id only when they wrote the same bytes on the same document, where the second write under that id gives the same items too, so one id never holds two payloads. Clients that loaded different states of the document get different ids and the content shows twice, as before. The pristine top-level route is unchanged, byte for byte (same scratch document, same hash input), so a client of this version and an older one still write one copy of a new document. A throwing initial write on these roots now sends nothing (it fails in the copy) instead of the part written. A nested root whose entry was deleted (the app still holds the Y.Node) is written in place as before (nothing is stored there), without throwing. The copy costs the whole document at the initialization of such a root; `y.md` says so and keeps seeding in one place as the advice for large documents. Closes #9515 ## Changeset `.changeset/lexical-y-initialize-any-root.md` (`[fix]`). `y.md` (initialize, and Limits: "Initial content with random values") says when the content is written once. ## Test plan `Init.test.ts`: two clients initializing four rooms (a root inside another shared type, one that held text, one that held an attribute, a nested one that held text) with the same content write one copy, equal documents, and a fresh reader shows one copy; after seeing different changes they get different ids and converge; a pristine root keeps the id main derives; initializing a nested root whose entry was deleted does not throw (gc on and off). `DerivedWrites.test.ts` and `SetState.test.ts` updated for the new route. ### Before ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/Init.test.ts -t 'two clients initializing a root|pristine root is still' × two clients initializing a root inside another shared type with the same content write one copy 59ms ✓ two clients initializing a root inside another shared type after seeing different changes: one payload per id, both converge 7ms × two clients initializing a root that held text, deleted since with the same content write one copy 20ms ✓ two clients initializing a root that held text, deleted since after seeing different changes: one payload per id, both converge 86ms × two clients initializing a root that held an attribute, deleted since with the same content write one copy 7ms ✓ two clients initializing a root that held an attribute, deleted since after seeing different changes: one payload per id, both converge 3ms × two clients initializing a root inside another shared type that held text, deleted since with the same content write one copy 6ms ✓ two clients initializing a root inside another shared type that held text, deleted since after seeing different changes: one payload per id, both converge 5ms ✓ a pristine root is still written under the same id (older clients share it) 3ms ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 4 ⎯⎯⎯⎯⎯⎯⎯ Tests 4 failed | 5 passed | 41 skipped (50) ``` ### After ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/Init.test.ts -t 'two clients initializing a root|pristine root is still' ✓ |unit| packages/lexical-y/src/__tests__/unit/Init.test.ts (50 tests | 41 skipped) 167ms Tests 9 passed | 41 skipped (50) ``` - `@lexical/y` unit suite: all pass (153 files, 7248 passed, 42 expected fail). - `pnpm run ci-check`: passes.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
## Description This commit changes no file. The repository's review bot reviews a pull request when its branch is pushed; this branch was last pushed before the bot started reviewing the repository, so it had no review yet.
There was a problem hiding this comment.
This looks good. The copy-based seed fixes the duplicate initial content from #9515 for nested and emptied roots: such a root's content is written on a copy of the document, under an id derived from the copy's state vector and the bytes written. The pristine route keeps its id byte for byte, and the new Init.test.ts cases reproduce the issue. The suggestions below are optional and non-blocking.
- Client ids add up (docs). Each initialization of a nested or emptied root now writes under its own derived client id rather than the tab's. An app with an editor per item in one
Y.Doctherefore adds one permanent state-vector entry for every editor it ever initializes: 100 nested editors from one tab leave 101 client ids instead of 1. The Client ids add up bullet inpackages/lexical-website/docs/collaboration/y.md(line 942) still says a tab writes under one id. A sentence there would set expectations. - Copy cost (docs).
seedUpdateencodes the whole document once and decodes it twice (packages/lexical-y/src/internal/Binding.ts:1438-1441). That happens on every initialization of such a root, even with no other client racing: about 170 ms next to a 1.4 MB document, versus about 1 ms for the in-place write before. y.md (lines 129-131) names the cost; a rough figure would help. Its "seed large documents in one place" advice does not cover nested roots, becauseseedYStateseeds only a top-level root of a new document. - Tests. The four "write one copy" cases (
Init.test.ts:164) create both clients with{gc: false}, but client documents are garbage collected by default (createYDoc). Afor (const gc of [false, true])loop like the one inDerivedWrites.test.ts:198would cover that setup too. Garbage-collected and mixed clients also write one copy with this change. - Changeset. CONTRIBUTING.md describes the changeset entry as what people upgrading read.
.changeset/lexical-y-initialize-any-root.mdline 5 promises one copy unconditionally, but that holds only when both clients loaded the same state of the document, as y.md now says. Consider adding that condition to the summary or to theVerify:paragraph. - Nit.
byteStringandstoredTypeOfwere inserted betweeninitClientID's JSDoc (Binding.ts:6351-6361) and the function, so that comment now sits onbyteString. Moving the two helpers above it restores it.
| root that held content before, or a root inside another shared type, after | ||
| the clients loaded different states of the document. Seed new documents in one place (on the server with `seedYState`, for | ||
| example), or give the initial blocks fixed ids. | ||
| - **Client ids add up.** A document keeps every client id that ever wrote |
There was a problem hiding this comment.
Optional: with this change, every initialize() of a nested or emptied root writes under its own derived client id, so a document gains one permanent state-vector entry per editor it ever initializes (100 nested editors from one tab: 101 client ids instead of 1). Worth a sentence in this bullet, which still says a tab writes under one id.
| (the initial content of such a root is written on a copy of the whole | ||
| document, a cost that grows with the other content the document holds; | ||
| seed large documents in one place instead). The editor then shows the content as stored, as |
There was a problem hiding this comment.
Optional: a rough figure would help here. The copy is taken on every initialization of such a root, even with no race, and comes to about 170 ms next to a 1.4 MB document versus about 1 ms for the in-place write before. "Seed large documents in one place" does not apply to nested roots, since seedYState seeds only a top-level root of a new document.
| createYDoc(null, {gc: false}), | ||
| createYDoc(null, {gc: false}), |
There was a problem hiding this comment.
Optional: client documents are garbage collected by default (createYDoc), but these cases only run {gc: false}. A for (const gc of [false, true]) loop like the one in DerivedWrites.test.ts would cover the common setup; it passes with this change.
| '@lexical/y': patch | ||
| --- | ||
|
|
||
| [fix] Two clients that initialize a root inside another shared type, or a root that held content before, at the same time write the initial content once, as for a new document |
There was a problem hiding this comment.
Optional: this holds only when both clients loaded the same state of the document (as y.md now says); consider adding that condition here or in the Verify: paragraph, since this line is what people upgrading read.
| * as two random client ids; so every client of a document must use the same | ||
| * range. | ||
| */ | ||
| /** `bytes` as a string of one char per byte (see initClientID). */ |
There was a problem hiding this comment.
Nit: these two helpers now sit between initClientID's JSDoc (the block just above) and initClientID itself, so that comment documents byteString. Moving byteString and storedTypeOf above the comment restores it.
## Description Merges `main` at be5afea88816 (including #9550 and #9559). One conflict, in the link definitions of `packages/lexical-y/DECISIONS.md`, resolved by keeping both sides; the guard's accepted-limit row is renumbered D79, as `main` now uses D74 to D78. ## Test plan ### After See the next commit, which fixes the two tests the merge breaks, and the PR's test plan.
…n; say plainly that the peer dependency enforces the exact version ## Description After merging `main` (#9550, #9559): - **#9550's Seen guard** (`Source.test.ts`, "internal writes of node attributes go through Seen") flagged `YjsContract.ts`: its probe writes to scratch documents of its own, never a document a binding reads, so there is no Seen record to keep. The file is excluded there, with that reason. - **#9559 uses `encodeStateVector`**, which the contract test listed as used but not in the contract: added to `YJS_EXPORTS` (checked as a function). And, on how tight the coupling is: `@y/y` exposes no version at run time (no export or constant; its duplicate-import flag `globalThis['__ $YJS14$ __']` is only `true`; its `package.json` subpath needs a JSON import that not every bundler or runtime supports, and none synchronously), so the exact version is the peer dependency's to enforce and the run-time check is a shape check. Measured: `14.0.0-rc.24` and earlier fail at build or load (no `Node` export: esbuild `No matching export ... for import "Node"`, native ES modules `SyntaxError`), and `rc.25`, `rc.26` and `rc.28` pass the probe. The changeset summary (it read as an exact-version check), `y.md` Install and Limits, the README and D79 now say so. ## Test plan ### Before On the merged tree, before this commit: ``` × internal writes of node attributes go through Seen + "YjsContract.ts probeMembers: root.setAttr('probe')", ... (6 entries) × lists exactly what the sources use of @y/y + "export encodeStateVector (internal/Binding.ts)", ``` ### After ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/YjsContract.test.ts packages/lexical-y/src/__tests__/unit/Source.test.ts Tests 45 passed (45) ``` Per release candidate (the contract bundled with esbuild, `@y/y` external, run in Node against each install): ``` rc.24 load error: The requested module '@y/y' does not provide an export named 'Node' rc.25 [] rc.26 [] rc.28 [] rc.29 [] ```
Description
initialize()writes the initial content of a pristine top-level root once: under a client id derived from the bytes it writes (seedUpdate), so two clients that initialize the same document with the same content at once write the same items. Any other root was written under each client's own id, so two clients opening such a document at the same moment showed the content twice (#9515):rootis aY.Nodein an app map, as apps with several editors per document do);The scratch document the seed is written into was empty, and the items of such a root name what they are written next to (the deleted items, the parent type's item), which an empty document does not hold. Now any other root is written into a copy of the document, and the update is what that write added. The derived id hashes the copy's state vector with the bytes: two clients share an id only when they wrote the same bytes on the same document, where the second write under that id gives the same items too, so one id never holds two payloads. Clients that loaded different states of the document get different ids and the content shows twice, as before.
The pristine top-level route is unchanged, byte for byte (same scratch document, same hash input), so a client of this version and an older one still write one copy of a new document. A throwing initial write on these roots now sends nothing (it fails in the copy) instead of the part written. A nested root whose entry was deleted (the app still holds the Y.Node) is written in place as before (nothing is stored there), without throwing. The copy costs the whole document at the initialization of such a root;
y.mdsays so and keeps seeding in one place as the advice for large documents.Closes #9515
Changeset
.changeset/lexical-y-initialize-any-root.md([fix]).y.md(initialize, and Limits: "Initial content with random values") says when the content is written once.Test plan
Init.test.ts: two clients initializing four rooms (a root inside another shared type, one that held text, one that held an attribute, a nested one that held text) with the same content write one copy, equal documents, and a fresh reader shows one copy; after seeing different changes they get different ids and converge; a pristine root keeps the id main derives; initializing a nested root whose entry was deleted does not throw (gc on and off).DerivedWrites.test.tsandSetState.test.tsupdated for the new route.Before
After
@lexical/yunit suite: all pass (7492 passed, 46 expected fail).pnpm run ci-check: passes.