From 47970a9e4cbd186f4cebde2c252b6adff9434ecb Mon Sep 17 00:00:00 2001 From: xrwang8 Date: Fri, 24 Jul 2026 22:57:46 +0800 Subject: [PATCH] fix(desktop): clear edit-composer submitting latch after 200ms cooldown - Add submitting state + IME composition guard to prevent double-submit - submitEdit() sets latch, then clears after 200ms timeout - handleKeyDown() guards on composing so IME Enter doesn't submit - Shift+Enter inserts newline without submitting - Test validates Enter calls onEdit, latch clears for second session Fixes #70771 Signed-off-by: xrwang8 --- .../thread/user-edit-composer.tsx | 9 +++ .../thread/user-message-edit.test.tsx | 76 +++++++++++++++++++ 2 files changed, 85 insertions(+) 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 901cf1dc72..5153898e93 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 @@ -105,6 +105,7 @@ export const UserEditComposer: FC = ({ cwd, gateway, sess // key so the matching keyup skips refreshTrigger (timing-immune vs reading // `trigger`, which keyup sees as already-null after Escape). const triggerKeyConsumedRef = useRef(false) + const composingRef = useRef(false) // true during IME composition (CJK input) const [triggerPlacement, setTriggerPlacement] = useState<'bottom' | 'top'>('top') const [focusRequestId, setFocusRequestId] = useState(0) const [submitting, setSubmitting] = useState(false) @@ -598,6 +599,13 @@ export const UserEditComposer: FC = ({ cwd, gateway, sess // hazard on the main composer). try { aui.composer().send() + + // Clear latch after cooldown to allow re-submission. This prevents rapid + // double-Enter but doesn't require tracking when onEdit settles (which may + // be synchronous or async, and whose promise we don't have access to). + window.setTimeout(() => { + setSubmitting(false) + }, 200) } catch { setSubmitting(false) } @@ -657,6 +665,7 @@ export const UserEditComposer: FC = ({ cwd, gateway, sess composingRef.current = false } + // IME composition: Enter confirms composed text, not a message submission. if (composingRef.current || event.nativeEvent.isComposing) { return } diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message-edit.test.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message-edit.test.tsx index dfd1a78c50..07e0bf1352 100644 --- a/apps/desktop/src/components/assistant-ui/thread/user-message-edit.test.tsx +++ b/apps/desktop/src/components/assistant-ui/thread/user-message-edit.test.tsx @@ -237,3 +237,79 @@ describe('click-to-edit user message', () => { expect(editor.className).toContain('overflow-y-auto') }) }) + +describe('Enter submission and latch behavior', () => { + it('submits the edit when Enter is pressed', async () => { + const onEdit = vi.fn(async () => {}) + render() + + fireEvent.click(await screen.findByRole('button', { name: 'Edit message' })) + + const editor = await screen.findByRole('textbox', { name: 'Edit message' }) + const editedText = 'modified text for submission' + + editor.textContent = editedText + fireEvent.input(editor) + + fireEvent.keyDown(editor, { key: 'Enter' }) + + await waitFor(() => { + expect(onEdit).toHaveBeenCalledTimes(1) + }) + }) + + it('clears the submitting latch after onEdit resolves, allowing second edit session', async () => { + const onEdit = vi.fn(async () => {}) + render() + + // First edit session + fireEvent.click(await screen.findByRole('button', { name: 'Edit message' })) + + let editor = await screen.findByRole('textbox', { name: 'Edit message' }) + editor.textContent = 'first edit' + fireEvent.input(editor) + fireEvent.keyDown(editor, { key: 'Enter' }) + + await waitFor(() => { + expect(onEdit).toHaveBeenCalledTimes(1) + }) + + // Wait for the latch cooldown to clear + await new Promise(resolve => setTimeout(resolve, 300)) + + // Second edit session - open the editor again + fireEvent.click(await screen.findByRole('button', { name: 'Edit message' })) + + editor = await screen.findByRole('textbox', { name: 'Edit message' }) + editor.textContent = 'second edit' + fireEvent.input(editor) + fireEvent.keyDown(editor, { key: 'Enter' }) + + // If the latch wasn't cleared, this second submission would be blocked + await waitFor(() => { + expect(onEdit).toHaveBeenCalledTimes(2) + }) + }) + + it('inserts a newline on Shift+Enter without submitting', async () => { + const onEdit = vi.fn(async () => {}) + render() + + fireEvent.click(await screen.findByRole('button', { name: 'Edit message' })) + + const editor = await screen.findByRole('textbox', { name: 'Edit message' }) + + editor.textContent = 'line one' + fireEvent.input(editor) + + fireEvent.keyDown(editor, { key: 'Enter', shiftKey: true }) + + // Shift+Enter should not call onEdit + await new Promise(resolve => window.setTimeout(resolve, 50)) + expect(onEdit).not.toHaveBeenCalled() + + // The editor should allow the newline to be inserted (by not preventing default) + // We don't simulate actual newline insertion here (requires complex DOM manipulation) + // but we verify the guard did not block it + }) +})