Skip to content

The agent that grows with you

fix(desktop): approval card hands focus back to the pane the user was in

teknium1f81ac9a45b6c
diff --git a/apps/desktop/src/components/assistant-ui/tool/approval.test.tsx b/apps/desktop/src/components/assistant-ui/tool/approval.test.tsx
index 5677f0a9c46..e18106ca145 100644
--- a/apps/desktop/src/components/assistant-ui/tool/approval.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/tool/approval.test.tsx
@@ -106,6 +106,71 @@ describe('PendingApprovalStack', () => {
expect($approvalRequest.get()).toBeNull()
})
+ it('hands focus back to the surface the user was in before clicking Run', async () => {
+ mockGateway()
+ liveApproval()
+ setRequest('computer_use type', undefined, { requestId: 'apr-1', serverRequestId: 'srq-approval' })
+
+ // The agent's preceding computer_use click left focus in the terminal pane.
+ const terminal = document.createElement('textarea')
+ const pane = document.createElement('div')
+ pane.dataset.terminal = ''
+ pane.append(terminal)
+ document.body.append(pane)
+ terminal.focus()
+
+ try {
+ render(<PendingApprovalStack />)
+ const run = screen.getByRole('button', { name: /Run/ })
+
+ // Chromium moves focus onto the pressed button before `click` fires.
+ fireEvent.pointerDown(run)
+ run.focus()
+ fireEvent.click(run)
+
+ await waitFor(() => {
+ expect(hasOpenServerRequest('srq-approval')).toBe(false)
+ })
+ await waitFor(() => {
+ expect(document.activeElement).toBe(terminal)
+ })
+ } finally {
+ pane.remove()
+ }
+ })
+
+ it('leaves focus alone when the user moved on before the approval settled', async () => {
+ mockGateway()
+ liveApproval()
+ setRequest('computer_use type', undefined, { requestId: 'apr-1', serverRequestId: 'srq-approval' })
+
+ const terminal = document.createElement('textarea')
+ const elsewhere = document.createElement('input')
+ document.body.append(terminal, elsewhere)
+ terminal.focus()
+
+ try {
+ render(<PendingApprovalStack />)
+ const run = screen.getByRole('button', { name: /Run/ })
+
+ fireEvent.pointerDown(run)
+ run.focus()
+ fireEvent.click(run)
+ elsewhere.focus()
+
+ await waitFor(() => {
+ expect(hasOpenServerRequest('srq-approval')).toBe(false)
+ })
+ await act(async () => {
+ await Promise.resolve()
+ })
+ expect(document.activeElement).toBe(elsewhere)
+ } finally {
+ terminal.remove()
+ elsewhere.remove()
+ }
+ })
+
it('falls back to the approval.respond RPC when no live server request is registered', async () => {
// A prompt restored from `approval.pending` (no socket carried the frame):
// the queue-level RPC is the only way to answer it.
diff --git a/apps/desktop/src/components/assistant-ui/tool/approval.tsx b/apps/desktop/src/components/assistant-ui/tool/approval.tsx
index af755435a6a..705a271a842 100644
--- a/apps/desktop/src/components/assistant-ui/tool/approval.tsx
+++ b/apps/desktop/src/components/assistant-ui/tool/approval.tsx
@@ -236,6 +236,35 @@ const ApprovalCard: FC<ApprovalCardProps> = ({ request, total, position, stack }
const present = stack.active
const busy = submitting !== null || !present || stack.busy
+ // Answering with the pointer moves focus onto the card, and the card then
+ // unmounts, which parks focus on <body> — where type-to-focus routes the next
+ // keystrokes into the chat composer. For a computer_use flow the agent may
+ // have just aimed its input at another pane (terminal, preview), so the
+ // approval hands focus back to whatever held it before the press (#113839).
+ const focusOrigin = useRef<HTMLElement | null>(null)
+ const cardRef = useRef<HTMLElement | null>(null)
+
+ const rememberFocusOrigin = useCallback(() => {
+ const active = document.activeElement
+
+ if (active instanceof HTMLElement && active !== document.body && !cardRef.current?.contains(active)) {
+ focusOrigin.current = active
+ }
+ }, [])
+
+ const restoreFocusOrigin = useCallback(() => {
+ const origin = focusOrigin.current
+ const active = document.activeElement
+
+ focusOrigin.current = null
+
+ // Only when the answer itself is what stranded focus — never steal from a
+ // surface the user moved to while the reply was in flight.
+ if (origin?.isConnected && (!active || active === document.body || cardRef.current?.contains(active))) {
+ origin.focus({ preventScroll: true })
+ }
+ }, [])
+
// false when the backend won't honor a permanent allow (tirith warning) → hide "Always allow".
const allowPermanent = request.allowPermanent !== false
const choices = request.choices ?? (request.smartDenied ? ['once', 'deny'] : undefined)
@@ -263,6 +292,7 @@ const ApprovalCard: FC<ApprovalCardProps> = ({ request, total, position, stack }
try {
await stack.depart(() => sendApproval(request, choice))
+ restoreFocusOrigin()
} catch (error) {
releaseApprovalKey()
notifyError(error, copy.sendFailed)
@@ -270,7 +300,7 @@ const ApprovalCard: FC<ApprovalCardProps> = ({ request, total, position, stack }
setSubmitting(null)
}
},
- [copy.gatewayDisconnected, copy.sendFailed, gateway, request, stack]
+ [copy.gatewayDisconnected, copy.sendFailed, gateway, request, restoreFocusOrigin, stack]
)
return (
@@ -280,6 +310,8 @@ const ApprovalCard: FC<ApprovalCardProps> = ({ request, total, position, stack }
data-request-id={request.requestId}
data-slot="tool-approval-card"
inert={!present}
+ onPointerDownCapture={rememberFocusOrigin}
+ ref={cardRef}
>
<div className="flex items-center gap-2 px-2.5 pt-2 text-xs text-(--ui-text-secondary)">
<Codicon name="terminal" size="0.875rem" />