Skip to content

[lexical-yjs] Bug Fix: sync unknown node state when a node is first created - #9007

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:yjs-unknown-state
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:yjs-unknown-state

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

syncNodeStateFromLexical writes a node's state into the shared doc in two
loops — one for unknown keys (state whose StateConfig this build has not
registered) and one for known keys. They disagree about how to treat "there is
no previous state":

const [prevUnknown, prevKnown] =
  prevState && stateMap.doc
    ? prevState.getInternalState()
    : [undefined, new Map()];
if (unknown) {
  for (const [k, v] of Object.entries(unknown)) {
    if (prevUnknown && v !== prevUnknown[k]) {
      stateMap.set(k, v);
    }
  }
}
for (const [stateConfig, v] of known) {
  if (prevKnown.get(stateConfig) !== v) {
    stateMap.set(stateConfig.key, stateConfig.unparse(v));
  }
}

The known loop deliberately falls back to an empty Map, so on the create path
every entry compares unequal and is written. The unknown loop falls back to
undefined and then short-circuits on it, so nothing is written.

NodeState.getInternalState() returns [UnknownStateRecord | undefined, ...],
so prevUnknown is also undefined whenever the previous state carried only
known keys — not just on creation. Either way the guard means "nothing was
synced yet", which is exactly when the entries should be written.

The create path is the common one: $createCollabNodeFromLexicalNode calls
syncPropertiesFromLexical(..., null, node), so prevLexicalNode is null.
The unknown keys of a newly created node therefore never reach the shared
document, and because later updates only write changed keys, a value dropped
at creation stays dropped forever. Peers never receive it.

This matters precisely for the case unknown state exists to serve — as
NodeState.unknownState documents, it is what stops "an old version of your
code" from erasing metadata written by a newer version. Under collab that
metadata is dropped instead of preserved.

Making the unknown loop use the same "no previous state means everything is
new" rule as the known loop fixes it.

Test plan

New unit test packages/lexical-yjs/src/__tests__/unit/NodeStateSyncUnknown.test.ts.
It serializes a paragraph carrying unknown state through a real binding and
asserts the __state Y.Map contains it. A third case sets known state the
same way as a control — that one passes before and after, which is what pins
the asymmetry.

Before

$ npx vitest run packages/lexical-yjs/src/__tests__/unit/NodeStateSyncUnknown.test.ts

     × unknown state on a newly created node is written to the shared doc 10ms
     × several unknown keys all reach the shared doc 1ms

AssertionError: expected undefined to be 42 // Object.is equality
AssertionError: expected undefined to be 1 // Object.is equality

      Tests  2 failed | 1 passed (3)

After

$ npx vitest run packages/lexical-yjs

 Test Files  6 passed (6)
      Tests  81 passed (81)

$ npx vitest run packages/lexical-react

 Test Files  31 passed (31)
      Tests  182 passed (182)

…reated

## Description

`syncNodeStateFromLexical` writes a node's state into the shared doc in two
loops — one for unknown keys (state whose `StateConfig` this build has not
registered) and one for known keys. They disagree about how to treat "there is
no previous state":

```js
const [prevUnknown, prevKnown] =
  prevState && stateMap.doc
    ? prevState.getInternalState()
    : [undefined, new Map()];
if (unknown) {
  for (const [k, v] of Object.entries(unknown)) {
    if (prevUnknown && v !== prevUnknown[k]) {
      stateMap.set(k, v);
    }
  }
}
for (const [stateConfig, v] of known) {
  if (prevKnown.get(stateConfig) !== v) {
    stateMap.set(stateConfig.key, stateConfig.unparse(v));
  }
}
```

The known loop deliberately falls back to an empty `Map`, so on the create path
every entry compares unequal and is written. The unknown loop falls back to
`undefined` and then short-circuits on it, so **nothing** is written.

`NodeState.getInternalState()` returns `[UnknownStateRecord | undefined, ...]`,
so `prevUnknown` is also `undefined` whenever the previous state carried only
known keys — not just on creation. Either way the guard means "nothing was
synced yet", which is exactly when the entries *should* be written.

The create path is the common one: `$createCollabNodeFromLexicalNode` calls
`syncPropertiesFromLexical(..., null, node)`, so `prevLexicalNode` is `null`.
The unknown keys of a newly created node therefore never reach the shared
document, and because later updates only write *changed* keys, a value dropped
at creation stays dropped forever. Peers never receive it.

This matters precisely for the case unknown state exists to serve — as
`NodeState.unknownState` documents, it is what stops "an old version of your
code" from erasing metadata written by a newer version. Under collab that
metadata is dropped instead of preserved.

Making the unknown loop use the same "no previous state means everything is
new" rule as the known loop fixes it.

## Test plan

New unit test `packages/lexical-yjs/src/__tests__/unit/NodeStateSyncUnknown.test.ts`.
It serializes a paragraph carrying unknown state through a real binding and
asserts the `__state` Y.Map contains it. A third case sets *known* state the
same way as a control — that one passes before and after, which is what pins
the asymmetry.

### Before

```
$ npx vitest run packages/lexical-yjs/src/__tests__/unit/NodeStateSyncUnknown.test.ts

     × unknown state on a newly created node is written to the shared doc 10ms
     × several unknown keys all reach the shared doc 1ms

AssertionError: expected undefined to be 42 // Object.is equality
AssertionError: expected undefined to be 1 // Object.is equality

      Tests  2 failed | 1 passed (3)
```

### After

```
$ npx vitest run packages/lexical-yjs

 Test Files  6 passed (6)
      Tests  81 passed (81)

$ npx vitest run packages/lexical-react

 Test Files  31 passed (31)
      Tests  182 passed (182)
```
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 9, 2026
@LeSingh1

LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up note while probing this area: I instrumented the real sync path to check whether this fix is complete for the create path, and it is. On main unknown state cannot reach the shared doc at all — the prevUnknown && guard fixed here is compounded by the stateMap.doc guard, which makes the very sync that creates the __state Y.Map skip its values (stateMap is still detached at that point, so stateMap.doc is null). With the change here, the create path writes into the detached map before sharedTypeSet integrates it, so the values survive — which is what the added test asserts.

One gap this PR does not cover, noted for a separate change: syncNodeStateFromLexical has no deletion pass, so a key removed from a node's unknown state is never removed from the __state Y.Map and peers keep the stale value. That is not observable on main today precisely because of the bug fixed here, so it is better as a follow-up on top of this.

@etrepum

etrepum commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

It would be better to consolidate the fix into this PR, a high velocity of lots of small PRs is not a considerate contribution strategy. Reviewing a 20 line PR does not take twice as long as reviewing a 10 line PR.

@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9057 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants