Skip to content

fix(muya): harden History against identity (null) json-change ops - #4838

Merged
Jocs merged 2 commits into
developfrom
fix/history-null-op-4806
Jul 4, 2026
Merged

Jocs merged 2 commits into
developfrom
fix/history-null-op-4806

Conversation

@Jocs

@Jocs Jocs commented Jul 4, 2026

Copy link
Copy Markdown
Member

Refs #4806. Follow-up hardening for #4815.

Summary

ot-json1.compose() legitimately returns the identity operation (null)
when queued operations cancel each other out — for example during IME
composition, where an insert and a delete land in the same animation frame.

#4815 fixed the reported crash by stopping the deferred-flush emitter
(JSONState._flushOperationCache) from forwarding that null through
json-change. This PR closes the same crash at the consumer, covering
the remaining emitter that #4815 did not touch.

Why a second fix

JSONState.dispatch() — used by Editor.updateContents and
Editor.rebuildContents — still forwards the identity op deliberately. The
updateContents contract even documents this ("the ot-json1 no-op is
forwarded to dispatch … so listeners still see a json-change event for the
no-op"). But History's json-change listener handed the op straight to
_record / _transform, both of which read op.length:

TypeError: Cannot read properties of null (reading 'length')

Today that path only fires under History's own _ignoreChange guard (undo/
redo, replaceContent), so it does not crash in production — but it is a
latent trap: any future caller invoking updateContents(null) outside that
guard reproduces the identical crash via a different stack.

Change

Ignore identity ops in History's json-change listener — a no-op has
nothing to record or transform. This makes History crash-proof against a
null op regardless of which emitter produced it, and leaves dispatch's
existing "forward the no-op" contract intact.

Test plan

  • packages/muya unit tests — new identityOpJsonChange.spec.ts:
  • Full history + state suites green (222 tests).
  • lint + lint:types clean.

🤖 Generated with Claude Code

Jocs and others added 2 commits July 4, 2026 10:11
`ot-json1.compose()` legitimately returns the identity operation (`null`)
when queued operations cancel each other out (for example during IME
composition). PR #4815 stopped the deferred-flush emitter from forwarding
that null, but `dispatch()` — used by `Editor.updateContents` and
`rebuildContents` — still forwards it deliberately, and the `updateContents`
contract explicitly routes the no-op through `json-change`.

`History`'s `json-change` listener passed the op straight to `_record`
(and `_transform`), both of which read `op.length` and crash on `null`
with `TypeError: Cannot read properties of null (reading 'length')`.

Ignore identity ops in the listener: a no-op has nothing to record or
transform. This closes the crash at the consumer regardless of which
emitter produced the null.

Refs #4806

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Two regressions for the null-op crash:

- `updateContents(null)` drives the `dispatch` path that forwards the
  identity op through `json-change`; asserts History neither crashes nor
  records an undo entry.
- Flushing a compose-to-null batch (an op and its exact inverse) asserts
  the deferred-flush emitter stays silent, locking the PR #4815 fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant