Repository navigation
[lexical-y] Bug Fix: A read-only client keeps reading remote changes after it refuses a local update - #9552
Conversation
…after it refuses a local update
## Description
Two ways a read-only client (a newer schema or layout recorded in the document, a node type it does not register) ended up showing something else than its own document, with nothing written:
1. **A refused update that removed stored nodes.** `$deleteTableColumnAtSelection` on a read-only client removes the column's cells; the binding refuses it and reads the rows and the cells again. The read of a row makes each removed cell anew (`Reader#$createStored`), which gave the cell's Y.Node a second container state next to the one the refused list still held. The re-read then read that first state into the new cell as well, taking the cell's items from the state the binding reads remote changes with: every later remote change in those cells (typing, comments) was read with the wrong items and never shown, self-check on or off.
- Rule: one container state per Y.Node. `$createStored` keeps the state the Y.Node already has (`mapNode` moves it to the new key, and now drops the old key's entry) instead of making a second one.
2. **An update an app's listener queues while the binding opens a document.** The binding makes the editor not editable until it has read the document; an editable listener that replies with an update queued it, and the binding's read of the whole document (`replacesPending`) ran it inside its own update, tagged as a remote change: neither written nor refused nor read again, and no `UNSUPPORTED_EDIT_WARNING`. The editor kept the extra paragraph through later remote changes.
- Rule: a read of the whole document takes over the pending state, never the updates still queued. Those are held like the replies `notifyApart` holds and commit on their own as local updates. While read only they are refused (with the warning) like any other local update; still opening, before the first read, they are part of what that read replaces, as the pending state already was.
- Behavior change to review: on a document that opens active, the reply to the opening (made before the document was read) is no longer written; the reply to the editor turning editable still is. Before, the first one was written by accident, after running inside the read.
## Changeset
`.changeset/lexical-y-read-only-reread.md` (`[fix]`).
## Test plan
New `regressions/ReadOnlyReread.test.ts`: a read-only client deletes a column or a row, then the newer client types in its cells (self-check on and off); an editable listener's reply while the binding opens a document with a newer schema, a newer layout, or neither.
### Before
```
$ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/ReadOnlyReread.test.ts
× the read-only client deletes a column; the newer client types in its cells (selfCheck false) 116ms
✓ the read-only client deletes a row; the newer client types in its cells (selfCheck false) 22ms
× the read-only client deletes a column; the newer client types in its cells (selfCheck true) 41ms
✓ the read-only client deletes a row; the newer client types in its cells (selfCheck true) 50ms
× an editable listener's reply (a newer schema) 9ms
× an editable listener's reply (a newer layout) 8ms
× an editable listener's reply (none) 9ms
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 5 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected '[[["r0c0","r0c1"],["r1c0","r1c1"]],"a…' to be '[[["r0c0 typed","r0c1"],["r1c0 too","…' // Object.is equality
AssertionError: expected [ Array(1) ] to deeply equal []
AssertionError: expected [ 'hello', 'reply' ] to deeply equal [ 'hello' ]
AssertionError: expected [ 'hello', 'reply', 'reply' ] to deeply equal [ 'hello', 'reply' ]
Tests 5 failed | 2 passed (7)
```
### After
```
$ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/ReadOnlyReread.test.ts
✓ |unit| packages/lexical-y/src/__tests__/unit/regressions/ReadOnlyReread.test.ts (7 tests) 252ms
Tests 7 passed (7)
```
- `@lexical/y` unit suite: all pass (156 files, 7284 passed, 42 expected fail).
- The soak repros this came from pass: a refused table edit on a read-only peer, then a remote change (32/32, read only because of a newer schema and because of an unknown node type; 4/32 failed before), and an editable listener's update while the binding opens (4/4; 3/4 failed before).
|
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: |
…ings a layout record as it is ## Description Follow-up in the same PR (soak finding, a read-only client that had refused nothing). A remote change that brings a layout record (the first member entry of a set records layout 2) makes a binding that is not active evaluate the document again and read it whole (`readAll`), from inside the change's transaction. Unlike every other read, that read did not start reading (`startReading`), so it saw the document as it was before the change (the `Unread` view): content the change made was shown as not there. In the captured session, cells another client's undo of a column delete brought back showed with empty list items on the read-only client, self-check on or off; an active client, which reads the same change through `$readRemote`, showed them right. `readAll` now starts reading before its read, as `recoverRead` does: a read of the whole document reads it as it is, the change being read included. ## Test plan New `regressions/ReadOnlyLayoutRecord.test.ts`: the captured state and remote update (`ReadOnlyLayoutRecord.json`, a randomized session's capture), read by a read-only (schema 1, the document records 2) and an active client, self-check on and off; the editor must show what a fresh read of its document shows. ### Before ``` $ pnpm exec vitest run --project unit --reporter=verbose packages/lexical-y/src/__tests__/unit/regressions/ReadOnlyLayoutRecord.test.ts × a read-only client shows a remote change that brings a layout record (selfCheck false) × a read-only client shows a remote change that brings a layout record (selfCheck true) ✓ an active client shows a remote change that brings a layout record (selfCheck false) ✓ an active client shows a remote change that brings a layout record (selfCheck true) Tests 2 failed | 2 passed (4) ``` ### After ``` $ pnpm exec vitest run --project unit --reporter=verbose packages/lexical-y/src/__tests__/unit/regressions/ReadOnlyLayoutRecord.test.ts ✓ a read-only client shows a remote change that brings a layout record (selfCheck false) ✓ a read-only client shows a remote change that brings a layout record (selfCheck true) ✓ an active client shows a remote change that brings a layout record (selfCheck false) ✓ an active client shows a remote change that brings a layout record (selfCheck true) Tests 4 passed (4) ``` - `@lexical/y` unit suite: all pass (7494 passed, 46 expected fail).
… line another container holds
## Description
Follow-up in the same PR (soak finding S9-8, introduced by the previous commit). The full-read line guard looked up a line it set aside by its old node key in `unitByKey`. A read that makes a line's node anew (a line whose type changed: a heading made a paragraph again) keeps its unit for the new node and leaves the old key's entry behind, naming that live unit. The guard took that stale entry for a line another container still holds and threw. On a read-only client reading a change that brings a layout record (a whole-document read), the throw made the binding fail closed: it kept showing what it showed before the change, for good, and missed text a collaborator typed in the same change (with or without the self-check).
The guard now counts a line as held elsewhere only while its unit is still its own (`unit.key === key`).
## Test plan
`EditorDrift.test.ts`: a change that retypes a line and brings a layout record is read (a client read only for a node type it does not register).
### Before
```
$ pnpm exec vitest run --project unit --reporter=verbose packages/lexical-y/src/__tests__/unit/EditorDrift.test.ts -t 'retypes a line and brings'
a change that retypes a line and brings a layout record is read 33ms
a change that retypes a line and brings a layout record is read 13ms
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯
Expected: "paragraph("one") / quote(paragraph("inner typed"))"
Received: "heading[h2]("one") / quote(paragraph("inner"))"
Tests 2 failed | 32 skipped (34)
```
### After
```
$ pnpm exec vitest run --project unit --reporter=verbose packages/lexical-y/src/__tests__/unit/EditorDrift.test.ts -t 'retypes a line and brings'
a change that retypes a line and brings a layout record is read 33ms
a change that retypes a line and brings a layout record is read 12ms
Tests 2 passed | 32 skipped (34)
```
- The soak capture (a read-only client applying a remote batch with a retype and a layout record) passes, alone and together with #9552.
…eason ## Description Follow-up in the same PR (integration review). The read-only regression tests asserted the exact message of the refused-edit warning, which `LexicalWarning` says may change (another open PR rewords it). They now assert the binding's typed warnings: exactly one `UNSUPPORTED_EDIT_WARNING` with reason `read-only` for a refused update while the binding opens a document, and, for a refused update that removed stored nodes, the full list of warnings (the schema warning and one refused edit: no recovered read, no self-check finding), so the S9-1 tests still fail if that path goes through a whole-document recovery. ## Test plan ``` $ pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/regressions/ReadOnlyReread.test.ts Tests 7 passed (7) ```
## 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.
Thanks, this looks good: each of the three read-only fixes comes with its own regression tests, and 7 of the 11 new cases fail on main and pass here. A few optional, non-blocking suggestions:
1. The reply to the opening is still written when an update is pending (packages/lexical-y/src/internal/Binding.ts:2705-2722)
The held updates commit before the read only when the editor is idle (schedulePending() at line 2721); otherwise they run after it, once the binding is active, and are written. YCollabExtension with the default $initialEditorState is that case, since InitialStateExtension applies the initial state with a non-discrete editor.update before the binding starts. On a document that already has content, an editable listener's reply to the opening then ends up written (hello, reply, reply, as on main), while a bare editor or $initialEditorState: null gives hello, reply. Both converge, so this isn't a regression, but the behavior change in the description only holds in the second setup. Could the held updates commit on one side of the read whether or not an update is pending? If the difference is intended, please say so in the comment and the changeset, and add an S9-3 variant with an update pending (through YCollabExtension with its default initial state, or a non-discrete editor.update() before start()).
2. Changeset (.changeset/lexical-y-read-only-reread.md)
CONTRIBUTING.md asks the entry to describe the user-facing change. It doesn't mention the third fix (a read-only client now shows the content of a remote change that brings a layout record, Binding.ts:2218-2222), and the Verify: paragraph could cover the opening-reply change for documents that open active.
3. Read-only opens that never read (Binding.ts:4155-4161)
The new early return relies on the opening read replacing the change. On a read-only open that never reads the document (for example an empty document created without createYDoc, which keeps Yjs's format cleanup on), a local update now warns UNSUPPORTED_EDIT_WARNING but stays in the editor; main kept it too, without the warning. The read-only paragraph of packages/lexical-website/docs/collaboration/y.md says such an update is read again, so the comment could say that no read follows on those opens.
4. Minor: no test needs the csByKey cleanup in State#mapNode (State.ts:286-288); all the new cases pass without it, so a word in its comment that it is hygiene would help the next reader.
| // the read replaces. | ||
| this.replies.unshift(...editor._updates.splice(0)); | ||
| this.pending.add('replies'); | ||
| this.schedulePending(); |
There was a problem hiding this comment.
Optional: this commits the held updates before the read only when the editor is idle. With an update pending (the default $initialEditorState of YCollabExtension, applied with a non-discrete update before the binding starts) they run after the read, once the binding is active, and the reply to the opening is written (hello, reply, reply); with nothing pending the read replaces it (hello, reply). Both converge. Could the rule not depend on a pending update, or be documented and tested both ways?
| // document, replaces what the update changed. Refused (and warned | ||
| // about) where the document turned out read only; still opening, it | ||
| // is part of the content the read replaces (see replacesPending). | ||
| return this._status === 'readonly'; |
There was a problem hiding this comment.
Nit: on a read-only open that never reads the document (an empty root with format cleanup on, older-layout roots with content, a newer schema record on an empty root), no read follows to replace the change, so it stays shown while this warns. Worth saying in the comment.
| '@lexical/y': patch | ||
| --- | ||
|
|
||
| [fix] A client that is read only (a newer schema or layout, a node type it does not register) no longer stops showing a collaborator's changes after it refuses a local update that removed stored nodes (a table column's cells), and refuses, reads again and warns about an update an app's listener makes while the binding opens the document |
There was a problem hiding this comment.
Nit: this doesn't mention the third fix, that a read-only client now shows the content of a remote change that brings a layout record. Per CONTRIBUTING.md the entry should cover the user-facing change; the Verify: paragraph could also mention the opening-reply change for documents that open active.
Description
Two ways a read-only client (a newer schema or layout recorded in the document, a node type it does not register) ended up showing something else than its own document, with nothing written:
$deleteTableColumnAtSelectionon a read-only client removes the column's cells; the binding refuses it and reads the rows and the cells again. The read of a row makes each removed cell anew (Reader#$createStored), which gave the cell's Y.Node a second container state next to the one the refused list still held. The re-read then read that first state into the new cell as well, taking the cell's items from the state the binding reads remote changes with: every later remote change in those cells (typing, comments) was read with the wrong items and never shown, self-check on or off.$createStoredkeeps the state the Y.Node already has (mapNodemoves it to the new key, and now drops the old key's entry) instead of making a second one.replacesPending) ran it inside its own update, tagged as a remote change: neither written nor refused nor read again, and noUNSUPPORTED_EDIT_WARNING. The editor kept the extra paragraph through later remote changes.notifyApartholds and commit on their own as local updates. While read only they are refused (with the warning) like any other local update; still opening, before the first read, they are part of what that read replaces, as the pending state already was.Changeset
.changeset/lexical-y-read-only-reread.md([fix]).Test plan
New
regressions/ReadOnlyReread.test.ts: a read-only client deletes a column or a row, then the newer client types in its cells (self-check on and off); an editable listener's reply while the binding opens a document with a newer schema, a newer layout, or neither.Before
After
@lexical/yunit suite: all pass (156 files, 7284 passed, 42 expected fail).Follow-up commit: a read-only client reads a remote change that brings a layout record as it is
A remote change that brings a layout record (the first member entry of a set records layout 2) makes a binding that is not active read the whole document again (
readAll) from inside the change's transaction. That read did not start reading (startReading), so it saw the document as it was before the change: content the change made was shown as not there (in the captured session, cells a remote undo of a column delete brought back showed empty list items on the read-only client, self-check on or off).readAllnow starts reading before its read, asrecoverReaddoes.Test:
regressions/ReadOnlyLayoutRecord.test.ts(a captured state and update): the read-only cases fail before (2/4), pass after (4/4).@lexical/ysuite: 7494 passed, 46 expected fail.ci-checkpasses.