Skip to content

The agent that grows with you

fix(desktop): bank edit-composer undo snapshots for cut and text drag

Konstantin Khlopkov083e685195cf
diff --git a/apps/desktop/src/components/assistant-ui/thread/user-edit-composer.tsx b/apps/desktop/src/components/assistant-ui/thread/user-edit-composer.tsx
index d0824a282c2..6cd1b9149a4 100644
--- a/apps/desktop/src/components/assistant-ui/thread/user-edit-composer.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/user-edit-composer.tsx
@@ -529,6 +529,11 @@ export const UserEditComposer: FC<UserEditComposerProps> = ({ cwd, gateway, sess
const handleDrop = (event: ReactDragEvent<HTMLElement>) => {
if (!dragHasAttachments(event.dataTransfer, HERMES_PATHS_MIME)) {
+ // A plain text drag within the editor mutates the DOM without a
+ // React-visible beforeinput (insertFromDrop), so the undo snapshot has
+ // to be banked here — before Chromium applies the move.
+ recordUndoPoint()
+
return
}
@@ -599,6 +604,15 @@ export const UserEditComposer: FC<UserEditComposerProps> = ({ cwd, gateway, sess
recordUndoPoint({ coalesce: inputType === 'insertText' || inputType === 'deleteContentBackward' })
}
+ // Cut never reaches the handler above: React's onBeforeInput is a
+ // keypress/textInput polyfill and does not observe the native
+ // `beforeinput` event, so Chromium's deleteByCut input type is invisible to
+ // it. The native `cut` clipboard event still fires before the DOM mutation,
+ // which is where the pre-edit snapshot has to be banked or ⌘Z skips the cut.
+ const handleCut = () => {
+ recordUndoPoint()
+ }
+
const handlePaste = (event: ClipboardEvent<HTMLDivElement>) => {
const pastedText = sanitizeComposerInput(event.clipboardData.getData('text'))
@@ -865,6 +879,7 @@ export const UserEditComposer: FC<UserEditComposerProps> = ({ cwd, gateway, sess
composingRef.current = true
beginComposerComposition(event.currentTarget)
}}
+ onCut={handleCut}
onDragOver={handleDragOver}
onDrop={handleDrop}
onFocus={() => markActiveComposer('edit')}
diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message-edit-undo-cut-drag.test.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message-edit-undo-cut-drag.test.tsx
new file mode 100644
index 00000000000..b37677818db
--- /dev/null
+++ b/apps/desktop/src/components/assistant-ui/thread/user-message-edit-undo-cut-drag.test.tsx
@@ -0,0 +1,171 @@
+// The edit composer's undo stack misses the two mutation paths Chromium runs
+// without a React-visible beforeinput: cutting selected text and dragging a
+// selection within the editor. Chromium fires the native `cut` / `drop`
+// clipboard events before mutating the DOM, and React 19's onBeforeInput
+// polyfill (keypress/textInput/paste) never sees the deleteByCut /
+// insertFromDrop input types at all — so handleBeforeInput cannot bank the
+// pre-edit snapshot for them. Undo then skips the cut entirely or steps a
+// paste back only to the post-cut state, the two gaps reported in #115462.
+import { type AppendMessage, AssistantRuntimeProvider, ExportedMessageRepository, type ThreadMessage } from '@assistant-ui/react'
+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
+
+import { useIncrementalExternalStoreRuntime } from '@/lib/incremental-external-store-runtime'
+
+import { assistantMessage, stubThreadEnvironment, stubThreadViewportSize, userMessage } from '../test-utils'
+
+import { Thread } from '.'
+
+stubThreadEnvironment()
+stubThreadViewportSize()
+
+// jsdom does not make contenteditable focusable like Chromium does; give the
+// production editor a tab stop the moment production code tries to focus it.
+beforeAll(() => {
+ const nativeFocus = HTMLElement.prototype.focus
+
+ HTMLElement.prototype.focus = function focus(options?: FocusOptions) {
+ if (this.getAttribute('contenteditable') === 'true' && !this.hasAttribute('tabindex')) {
+ this.tabIndex = 0
+ }
+
+ nativeFocus.call(this, options)
+ }
+})
+
+afterEach(() => {
+ cleanup()
+})
+
+function Harness({ onEdit }: { onEdit: (message: AppendMessage) => Promise<void> }) {
+ const repository = ExportedMessageRepository.fromArray([userMessage(), assistantMessage()])
+
+ const runtime = useIncrementalExternalStoreRuntime<ThreadMessage>({
+ messageRepository: repository,
+ isRunning: false,
+ setMessages: () => {},
+ onNew: async () => {},
+ onEdit,
+ onCancel: async () => {},
+ onReload: async () => {}
+ })
+
+ return (
+ <AssistantRuntimeProvider runtime={runtime}>
+ <Thread cwd={null} gateway={null} sessionId="session-1" />
+ </AssistantRuntimeProvider>
+ )
+}
+
+async function openEditor() {
+ render(<Harness onEdit={vi.fn(async () => {})} />)
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Edit message' }))
+ const editor = await screen.findByRole('textbox', { name: 'Edit message' })
+
+ await act(async () => {
+ editor.focus()
+ })
+
+ return editor
+}
+
+function select(editor: HTMLElement, start: number, end: number) {
+ const text = editor.firstChild!
+
+ if (text.nodeType !== Node.TEXT_NODE) {
+ throw new Error('expected a text node editor')
+ }
+
+ const range = window.document.createRange()
+ range.setStart(text, start)
+ range.setEnd(text, end)
+ const selection = window.getSelection()
+ selection?.removeAllRanges()
+ selection?.addRange(range)
+}
+
+async function undo(editor: HTMLElement) {
+ await act(async () => {
+ editor.focus()
+ fireEvent.keyDown(editor, { key: 'z', metaKey: true })
+ })
+}
+
+// Chromium's cut: a cancelable `cut` event, then the DOM mutation, then
+// `input` deleteByCut (probe-verified on real Chromium; jsdom has no editing
+// pipeline, so the mutation is applied where the pipeline would).
+async function cutSelection(editor: HTMLElement) {
+ await act(async () => {
+ fireEvent.cut(editor)
+ editor.textContent = editor.textContent!.replace(' three', '')
+ fireEvent.input(editor)
+ })
+}
+
+// Chromium's intra-editor drag-move: a cancelable `drop` event carrying the
+// dragged text, then the mutation, then `input` insertFromDrop.
+async function dropText(editor: HTMLElement, dragged: string) {
+ await act(async () => {
+ fireEvent.drop(editor)
+ editor.textContent = editor.textContent!.replace(` ${dragged}`, '') + ` ${dragged}`
+ fireEvent.input(editor)
+ })
+}
+
+describe('edit composer undo — cut and drag coverage', () => {
+ it('undo restores text removed by a cut', async () => {
+ const editor = await openEditor()
+
+ await act(async () => {
+ editor.textContent = 'one two three four'
+ fireEvent.input(editor)
+ })
+
+ select(editor, 8, 14) // " three"
+ await cutSelection(editor)
+ expect(editor.textContent).toBe('one two four')
+
+ await undo(editor)
+ expect(editor.textContent).toBe('one two three four')
+ })
+
+ it('undo restores text after a drag-move within the editor', async () => {
+ const editor = await openEditor()
+
+ await act(async () => {
+ editor.textContent = 'one two three four'
+ fireEvent.input(editor)
+ })
+
+ select(editor, 9, 12) // "two"
+ await dropText(editor, 'two')
+ expect(editor.textContent).toBe('one three four two')
+
+ await undo(editor)
+ expect(editor.textContent).toBe('one two three four')
+ })
+
+ it('undoing a paste after a cut steps back past the cut as well', async () => {
+ const editor = await openEditor()
+
+ await act(async () => {
+ editor.textContent = 'one two three four'
+ fireEvent.input(editor)
+ })
+
+ select(editor, 8, 14) // " three"
+ await cutSelection(editor)
+
+ // Paste a replacement (the paste path already banks its own undo point).
+ await act(async () => {
+ fireEvent.paste(editor, { clipboardData: { getData: () => ' five' } })
+ })
+
+ await undo(editor) // undo the paste
+ expect(editor.textContent).toBe('one two four')
+
+ await undo(editor) // undo the cut — the gap reported in #115462
+ expect(editor.textContent).toBe('one two three four')
+ })
+})