Skip to content

The agent that grows with you

fix(desktop): don't cancel the running turn on Esc while an overlay is open

Junhao Wang53daff064479
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-esc-cancel.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-esc-cancel.test.tsx
new file mode 100644
index 00000000000..903826d3f38
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-esc-cancel.test.tsx
@@ -0,0 +1,50 @@
+import { cleanup, fireEvent, render } from '@testing-library/react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { useComposerEscCancel } from './use-composer-esc-cancel'
+
+function Harness({ busy, onCancel }: { busy: boolean; onCancel: () => void }) {
+ useComposerEscCancel({ awaitingInput: false, busy, onCancel, target: 'main' })
+
+ return null
+}
+
+afterEach(() => {
+ cleanup()
+ document.body.innerHTML = ''
+})
+
+describe('useComposerEscCancel', () => {
+ it('cancels the turn on Esc while busy with chat focus', () => {
+ const onCancel = vi.fn()
+ render(<Harness busy onCancel={onCancel} />)
+
+ fireEvent.keyDown(window, { key: 'Escape' })
+
+ expect(onCancel).toHaveBeenCalledTimes(1)
+ })
+
+ it('does not cancel on Esc when the turn is not busy', () => {
+ const onCancel = vi.fn()
+ render(<Harness busy={false} onCancel={onCancel} />)
+
+ fireEvent.keyDown(window, { key: 'Escape' })
+
+ expect(onCancel).not.toHaveBeenCalled()
+ })
+
+ it('does not cancel on Esc while an overlay surface covers the chat', () => {
+ const onCancel = vi.fn()
+ render(<Harness busy onCancel={onCancel} />)
+
+ // OverlayView stamps `data-overlay-surface` on its root — the same signal
+ // the type-to-focus path uses to stand down while an overlay is open.
+ const overlay = document.createElement('div')
+ overlay.setAttribute('data-overlay-surface', '')
+ document.body.appendChild(overlay)
+
+ fireEvent.keyDown(window, { key: 'Escape' })
+
+ expect(onCancel).not.toHaveBeenCalled()
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-esc-cancel.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-esc-cancel.ts
index ff017edcf96..6aaaba57cee 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-esc-cancel.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-esc-cancel.ts
@@ -1,6 +1,7 @@
import { useEffect, useRef } from 'react'
import { triggerHaptic } from '@/lib/haptics'
+import { composerFocusBlockedBySurface } from '@/lib/keybinds/composer-focus-keys'
import { type ComposerTarget, getActiveComposer } from '../focus'
@@ -47,7 +48,10 @@ export function useComposerEscCancel({ awaitingInput, busy, onCancel, target }:
return
}
- if (document.querySelector('[role="dialog"],[role="alertdialog"],[data-radix-popper-content-wrapper]')) {
+ // An overlay covering the chat owns Esc (its escape layer closes it) —
+ // the composer stays mounted beneath it, so stand down. Same surface
+ // signal as type-to-focus; also covers Radix dialogs/popovers.
+ if (composerFocusBlockedBySurface()) {
return
}