Repository navigation
fix(work): make AI Work application undoable as a single editor edit - #394
Merged
Merged
Conversation
The AI Work result was pushed through onDocumentChange, which updates React
state but leaves the editor's undo stack untouched: the replacement was not
an editor operation, so one Undo could not restore the pre-Work document.
Extract applyWorkEdit() into src/lib/apply-work-edit.mjs and route executeWork
through it. When the mounted Monaco editor is available the full document is
replaced with editor.executeEdits("ai-work", ...) wrapped in pushUndoStop()
calls, so the whole AI result is one undo step (Undo restores the pre-Work
document, Redo reapplies it) and does not merge into the user's previous
typing. The change still flows through the editor change callback, preserving
the existing state/preview synchronization; when no editor/model is mounted
(focus mode, not yet ready) it falls back to onDocumentChange unchanged.
Fixes petertzy#300.
Owner
|
Approved. This is a necessary and well-scoped fix for the AI Work undo behavior. The implementation routes the full-document replacement through Monaco with explicit undo stops, preserving the existing change callback for state and preview synchronization while retaining the no-editor fallback. The focused regression coverage verifies undo/redo, separation from prior typing, consecutive Work applications, and fallback behavior. Frontend tests and lint pass. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Applying an AI Work result went straight through
onDocumentChange, which updates React state but never performs an editor operation — so the AI replacement had no undo boundary of its own in Monaco. As issue #300 describes, a single Undo could not restore the exact pre-Work document.Change
frontend/src/lib/apply-work-edit.mjsexportingapplyWorkEdit(). When the mounted editor/model is available it replaces the full document witheditor.executeEdits("ai-work", …)wrapped ineditor.pushUndoStop()on both sides, so one Undo restores the pre-Work document and Redo reapplies the AI result without merging into the user’s previous typing.useAIActions.executeWork()routes the (still freshness-guarded) result throughapplyWorkEdit, passingeditorRef.current. The edit flows through the editor change callback, so the existing state/preview synchronization path is preserved.onDocumentChangeexactly as before.Acceptance criteria (from #300)
Tests
frontend/tests/ai-work-undo.test.mjsdrives a real Monaco model: editor-vs-fallback routing, single-Undo/Redo, no merge with prior typing, consecutive AI applies each individually undoable, an unsealed prior edit not swallowed, and the no-editor fallback. Mutation-tested (dropping the opening/trailing undo stops, and forcing the fallback) — each mutation is caught.Local:
npm test101 passed,npm run lintclean,npm run buildgreen.Fixes #300.