diff --git a/src/components/AiPanel.test.tsx b/src/components/AiPanel.test.tsx
index 1b62c223..95e5ff5a 100644
--- a/src/components/AiPanel.test.tsx
+++ b/src/components/AiPanel.test.tsx
@@ -349,6 +349,28 @@ describe('AiPanel', () => {
vi.useRealTimers()
})
+ it('does not steal composer focus after a response when the send button becomes enabled', async () => {
+ vi.useFakeTimers()
+ mockMessages = [{
+ userMessage: 'First question',
+ actions: [],
+ response: 'First answer.',
+ id: 'msg-3',
+ }]
+ render()
+
+ const input = screen.getByTestId('agent-input')
+ input.focus()
+ input.textContent = 'f'
+ fireEvent.input(input)
+
+ await act(() => { vi.advanceTimersByTime(1) })
+
+ expect(screen.getByTestId('agent-send')).toBeEnabled()
+ expect(document.activeElement).toBe(screen.getByTestId('agent-input'))
+ vi.useRealTimers()
+ })
+
it('calls onClose when Escape is pressed while panel has focus', async () => {
vi.useFakeTimers()
const onClose = vi.fn()
diff --git a/src/components/useAiPanelFocus.ts b/src/components/useAiPanelFocus.ts
index bd38919f..a86a0a43 100644
--- a/src/components/useAiPanelFocus.ts
+++ b/src/components/useAiPanelFocus.ts
@@ -13,6 +13,8 @@ function focusPreferredElement(
inputRef: React.RefObject,
shouldFocusPanel: boolean,
) {
+ if (panelRef.current?.contains(document.activeElement)) return
+
if (shouldFocusPanel) {
panelRef.current?.focus()
return