Skip to content

[Bug] After one failed edit, every later edit throws and is not saved until the file is reopened #5373

Closed as not planned

Description

@Jocs

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:

  1. Open a document with:
    Intro paragraph
    
    ```js
    const a = 1
    ```
    
    Outro paragraph
  2. Click in the code line, click right after js in the language box, press Shift+↓ and type q.
  3. Click at the end of Outro paragraph and press Enter: Cannot insert into out of bounds index.
  4. Type new, then click at the end of Outro paragraph and type one.
  5. Save and reopen the file.

develop, where that drift is fixed, with a simulated one in the muya e2e host (packages/muya/e2e) console:

  1. 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()
  2. Click at the end of second paragraph, press Enter and type third.
  3. Click at the end of first paragraph and type edited.
  4. 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

  1. added
    🐛 bug
    🚧 needs triage
    on Sep 15, 2026
  2. Jocs commented on Sep 15, 2026

    @Jocs
    MemberAuthor

    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 of Cannot insert into out of bounds index or Cannot pick up or remove undefined with reproduction steps should get its own issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bug🔭 area/muya🚧 needs triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions