Repository navigation
Description
Description
When one edit fails to apply to the editor's document (the "Unexpected error" crashes with Cannot insert into out of bounds index (#4903), Cannot pick up or remove undefined (#5148) or Invalid document snapshot), the editor never recovers:
- every later keystroke throws the error again (in a prerelease build each one opens another error dialog);
- nothing typed after the first error reaches the saved document. The editor keeps showing the new text, so the loss is only noticed after saving and reopening the file.
JSONState._flushOperationCache() (packages/muya/src/state/index.ts) composes the queued ops, applies them, and clears _operationCache only after the apply succeeded. When the apply throws, the failed ops stay queued, so every later flush composes them again and throws again.
The errors come from the block tree and the json state drifting apart. The drifts known so far are fixed on develop (#5357), but any future one ends the same way: one bug costs everything typed until the file is reopened.
Steps to Reproduce
0.20.0-rc.2, with the #4903 steps:
- Open a document with:
Intro paragraph ```js const a = 1 ``` Outro paragraph
- Click in the code line, click right after
jsin the language box, press Shift+↓ and typeq. - Click at the end of
Outro paragraphand press Enter:Cannot insert into out of bounds index. - Type
new, then click at the end ofOutro paragraphand typeone. - Save and reopen the file.
develop, where that drift is fixed, with a simulated one in the muya e2e host (packages/muya/e2e) console:
- Run:
muya.setContent('first paragraph\n\nsecond paragraph\n') // The json state loses a block the editor still shows. muya.editor.jsonState.removeOperation([1]) muya.flush()
- Click at the end of
second paragraph, press Enter and typethird. - Click at the end of
first paragraphand typeedited. - Read
muya.getMarkdown().
Expected Behavior
The failed edit is reported once, and editing goes on normally: later edits are saved, and the saved document is what the editor shows.
Actual Behavior
Measured with real mouse and keyboard input.
MarkText 0.20.0-rc.2 on macOS (packaged app, Playwright _electron):
| After | Saved markdown | Renderer errors |
|---|---|---|
step 2 (q) |
Intro paragraph / jsqnst a = 1 |
0 |
| step 3 (Enter) | unchanged | 1 |
typing new |
unchanged | 4 |
typing one |
unchanged | 8 (now Invalid document snapshot: undefined) |
The editor shows Intro paragraph / jsqnst a = 1 / Outro paragraph one / new.
After File → Save, the file on disk is Intro paragraph / jsqnst a = 1, so Outro paragraph one and new are lost. Undo then puts the code block back while Outro paragraph one and new stay on screen next to a second Outro paragraph.
develop @ 8bcb1e0 (muya e2e host, Chromium): after Enter, third and edited the saved markdown is still first paragraph, while the editor shows first paragraph edited / second paragraph / third. There are 13 errors, one per keystroke.
Screenshots / Logs
Error: Cannot insert into out of bounds index
at JSONState._apply (packages/muya/src/state/index.ts)
at JSONState._flushOperationCache (packages/muya/src/state/index.ts)
Operating System
macOS
MarkText Version
0.20.0-rc.2, develop @ 8bcb1e0
OS Version
macOS 27.0
Reproducibility
- I can reliably reproduce this issue
Activity
Jocs commentedon Sep 15, 2026
Closing as not planned. Recovering after a failed flush (#5376) would only treat the symptom. Instead, each trigger that makes the editor and the saved document drift apart is fixed at its source (for example #5357). The trigger in the steps above is already fixed on
develop. A new report ofCannot insert into out of bounds indexorCannot pick up or remove undefinedwith reproduction steps should get its own issue.