Skip to content

fix(muya): skip identity operations during deferred flush - #4815

Merged
Jocs merged 1 commit into
marktext:developfrom
tech-hoon:fix/json-change-null-op
Jul 4, 2026
Merged

Jocs merged 1 commit into
marktext:developfrom
tech-hoon:fix/json-change-null-op

Conversation

@tech-hoon

@tech-hoon tech-hoon commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4806

Summary

Fix a crash caused by forwarding identity JSON operations (null) emitted by ot-json1.compose().

During deferred operation flushing, queued operations may cancel each other out (for example during IME composition), causing compose() to legitimately return null.

JSONState._flushOperationCache() forwarded this identity operation through the json-change event, while downstream consumers such as History expected a JSONOpList, resulting in:

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

This change skips emitting json-change events for identity operations.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added (not needed)
  • Manually tested on: macOS

Manual verification

  • Reproduced the crash using the Korean IME.
  • Verified that deleting a composing syllable no longer crashes the editor.
  • Verified that normal text editing continues to work after the change.

Demo

The attached recording reproduces the crash.

2026-07-02.9.25.40.mov

Note: The op >> log is a temporary debug log added in
packages/muya/src/state/index.ts:282 (JSONState._flushOperationCache())
to inspect the result of ot-json1.type.compose().

Notes for reviewers

ot-json1.type.compose() returns JSONOp (null | JSONOpList), where
null represents the identity operation.

This change preserves the ot-json1 contract by preventing identity
operations from being forwarded through the json-change event.

ot-json1.compose() may legitimately return the identity operation (null) when queued operations cancel each other out, such as during IME composition.

JSONState._flushOperationCache() assumed that any non-empty operation batch would compose to a non-null operation and emitted it through the json-change event. Downstream consumers such as History expected a JSONOpList and crashed when receiving null.

Skip emitting json-change for identity operations to preserve the ot-json1 contract and avoid forwarding no-op operations.
@tech-hoon
tech-hoon marked this pull request as ready for review July 2, 2026 03:38
@Jocs
Jocs merged commit 54a65e7 into marktext:develop Jul 4, 2026
6 checks passed
Jocs added a commit that referenced this pull request Jul 4, 2026
)

* fix(muya): tolerate identity (null) json-change ops in History

`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]>

* test(muya): cover identity-op json-change crash (#4806)

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]>

---------

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.

Unexpected error with message Cannot read properties of null (reading 'length')

2 participants