Skip to content

fix(muya): keep saving edits after a batch fails to apply (#5373) - #5376

Closed
Jocs wants to merge 1 commit into
developfrom
fix/5373-flush-failure-recovery
Closed

Jocs wants to merge 1 commit into
developfrom
fix/5373-flush-failure-recovery

Conversation

@Jocs

@Jocs Jocs commented Sep 15, 2026

Copy link
Copy Markdown
Member

Closes #5373

Summary

When one batch of queued edits fails to apply to the json state, the editor never recovers. This happens when the block tree and the json state have drifted apart, as in #4903 and #5148.

JSONState._flushOperationCache() cleared _operationCache only after the apply succeeded. So the failed ops stayed queued and were composed into every later flush:

  • every keystroke threw again, and in a prerelease build each throw opens an error dialog;
  • nothing typed after the first error reached the saved document until the file was reopened, while the editor kept showing the new text.

Changes in packages/muya/src/state/index.ts:

  • Batch taken out first. The batch leaves the cache before it is composed and applied, so a failed batch is never retried.
  • Json state rebuilt from the tree on failure. New _syncWithTree() replaces the json state with the block tree's state. The tree already has the failed edits, later ops are built from it, and it is what the user sees.
  • Saved and undoable. The replacement is dispatched as a json-change, which the desktop saves. It is recorded as one rebuild undo step, the same way Muya.replaceContent records the source-mode handoff.
  • Error still reported. The error is rethrown, so it is reported once instead of on every edit.

The drifts known today are already fixed (#5357). This PR limits the cost of the next one to a single error.

Steps to reproduce

On develop, with a simulated drift 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().
muya.getMarkdown() Errors
develop first paragraph 13, one per keystroke
This PR first paragraph edited / second paragraph / third 1

In the desktop app, with a real trigger. The #4903 steps from #5357 were used, with #5357 reverted locally on both builds because develop no longer has that drift. The 0.20.0-rc.2 packaged app behaves like the first row.

  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, click at the end of Outro paragraph and type one.
  5. Save (File → Save), then undo three times and redo three times.

The editor shows Intro paragraph / jsqnst a = 1 / Outro paragraph one / new in both builds.

Build Renderer errors File on disk after Save Undo / redo
develop with #5357 reverted 8, and still growing Intro paragraph / jsqnst a = 1 undo puts Outro paragraph on screen twice while the document goes back to the original
This PR with #5357 reverted 1 Intro paragraph / jsqnst a = 1 / Outro paragraph one / new each step leaves the saved markdown equal to the screen; no new errors

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 unit tests in state/__tests__/flushFailureRecovery5373.spec.ts (3 cases), all failing on develop:
    • edits made after the failure are saved, and the json state equals the block tree;
    • the recovered document is emitted as a json-change;
    • undo and redo across the recovery keep the tree and the json state equal.
  • New e2e spec editing/flush-failure-recovery-5373.spec.ts: real keyboard edits after a simulated drift, checking the markdown and that exactly one error is raised. It fails on develop and passes with the fix (5/5 repeated).
  • Related e2e specs pass (26 tests): undo-redo, undo-redo-wedge-4685, the Unexpected error with message Cannot insert into out of bounds index #4903 cut specs, clipboard and stability.
  • Real desktop app (Playwright _electron with real mouse, keyboard and menu clicks): the table above, on build:unpack builds of develop and of this branch, both with fix(muya): don't drop blocks when a cut ends inside a code block (#4903, #5148) #5357 reverted, plus the packaged 0.20.0-rc.2.
  • muya: vitest run passes 235 files / 1666 tests; lint:types, eslint on the changed files and check-circular pass.
  • Manually tested on: macOS

Notes for reviewers

  • Why the json state follows the tree, not the other way round. Rebuilding the tree from the json state would visibly drop the failed edits, plus whatever the earlier drift had already lost (Outro paragraph in the steps above).
  • Undo. Undoing the recovery goes back to the json state from before the failure. That state still lacks what the earlier drift dropped, so it matches what the file held at that point.
  • flush() callers (save, tab switch, entering source mode) still get the rethrown error, as before. The document is already consistent by then, so trying again works.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop

`_flushOperationCache` cleared the op cache only after the composed batch
applied. When compose or apply threw (the block tree and the json state had
drifted, as in #4903 / #5148), the failed ops stayed queued and every later
flush composed them again and threw: each keystroke raised the error again,
and nothing edited afterwards reached the saved document until the file was
reopened.

Take the batch out of the cache before applying it. When it fails, replace
the json state with the block tree's state, which already has the failed
edits and which later ops are built from, record the replacement as one
undo step (as `Muya.replaceContent` does), and rethrow so the error is still
reported once.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop
@Jocs

Jocs commented Sep 15, 2026

Copy link
Copy Markdown
Member Author

Closing without merging. This recovers after a failed batch instead of removing the cause of the failure, so we are not adding a recovery path to _flushOperationCache. The triggers that make the block tree and the json state drift apart will keep being fixed at their source instead (for example #5357, #5370 and #5372).

@Jocs Jocs closed this Sep 15, 2026
@Jocs
Jocs deleted the fix/5373-flush-failure-recovery branch September 15, 2026 14:44
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.

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

1 participant