fix(desktop): stop hidden composers from stealing the caret

Keep-alive tabs remount their composer on transcript/status backstops
and were calling focus() while the user typed in the front tab. Gate
autofocus on pane visibility, and refuse to steal the caret from
another visible composer. A hidden tab that still holds DOM focus
does not block the pane the user just switched to.

Co-authored-by: Dan Bennett <dan@danbennett.me>
Co-authored-by: mor44-AI <andrefmontemor@gmail.com>
Co-authored-by: d4rk pr10r <darkpriorlabs@gmail.com>
This commit is contained in:
Brooklyn Nicholson
2026-08-31 19:47:32 -05:00
committed by brooklyn!
parent 60ba6024b5
commit 6eddeca6ef
4 changed files with 117 additions and 5 deletions
@@ -4,6 +4,7 @@ import { $hoveredTreeGroup } from '@/components/pane-shell/tree/store'
import {
blurComposerInput,
focusComposerInput,
getActiveComposer,
markActiveComposer,
onComposerFocusRequest,
@@ -52,6 +53,39 @@ afterEach(() => {
$hoveredTreeGroup.set(null)
})
describe('focusComposerInput', () => {
it('does not steal the caret from another live composer', () => {
const foreground = mountInput()
const background = mountInput()
foreground.focus()
focusComposerInput(background)
expect(document.activeElement).toBe(foreground)
})
it('still focuses when the caret is not already in a composer', () => {
const input = mountInput()
const outside = document.createElement('button')
document.body.append(outside)
outside.focus()
focusComposerInput(input)
expect(document.activeElement).toBe(input)
})
it('takes the caret from a hidden keep-alive composer on tab switch', () => {
const hidden = mountInput(true)
const visible = mountInput()
hidden.focus()
focusComposerInput(visible)
expect(document.activeElement).toBe(visible)
})
})
describe('blurComposerInput', () => {
it('blurs the foreground composer while a hidden tab matches first', () => {
const background = mountInput(true)
+18 -3
View File
@@ -10,7 +10,7 @@
* steal focus from the composer effect.
*/
import { queryAllVisible, queryVisible } from '@/components/pane-shell/pane-visibility'
import { isElementInHiddenPane, queryAllVisible, queryVisible } from '@/components/pane-shell/pane-visibility'
import { $hoveredTreeGroup } from '@/components/pane-shell/tree/store'
import type { InlineRefInput } from './inline-refs'
@@ -396,10 +396,25 @@ export const focusComposerInput = (el: HTMLElement | null) => {
// Skip when already focused: focus() runs the full focusing steps (forcing
// layout) even on the active element, and during a session switch the DOM is
// large and dirty — the redundant retries were measurably expensive there.
// Also skip when another VISIBLE composer holds the caret — a keep-alive
// remount must not yank typing. A hidden tab that still has DOM focus must
// not block the pane the user just switched to.
const focus = () => {
if (document.activeElement !== el) {
el.focus({ preventScroll: true })
if (document.activeElement === el) {
return
}
const active = document.activeElement
if (
active instanceof HTMLElement &&
active.dataset.slot === RICH_INPUT_SLOT &&
!isElementInHiddenPane(active)
) {
return
}
el.focus({ preventScroll: true })
}
focus()
@@ -2,6 +2,7 @@ import { act, cleanup, render, waitFor } from '@testing-library/react'
import { useLayoutEffect } from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { PaneVisibleContext } from '@/components/pane-shell/pane-visibility'
import { clearSessionDraft, type ComposerAttachment, mainComposerScope, stashSessionDraft } from '@/store/composer'
import { $connection } from '@/store/session'
@@ -294,3 +295,59 @@ describe('useComposerDraft — a closing composer hands the focus-bus key back',
expect(getActiveComposer()).toBe('tile:other')
})
})
describe('useComposerDraft — a hidden keep-alive tab never auto-focuses its composer', () => {
afterEach(() => {
cleanup()
mainComposerScope.clear()
markActiveComposer('main')
})
function renderScopedHidden(target: ComposerTarget, hidden: boolean) {
const scope: ComposerScope = { ...MAIN_COMPOSER_SCOPE, target }
return render(
<PaneVisibleContext.Provider value={!hidden}>
<ComposerScopeProvider value={scope}>
<ProbeHarness
activeQueueSessionKey="session-tile"
onLayoutSnapshot={() => undefined}
sessionId="session-tile"
/>
</ComposerScopeProvider>
</PaneVisibleContext.Provider>
)
}
it('does not claim the focus bus when the composer mounts inside a hidden pane', () => {
renderScopedHidden('tile:bg', true)
expect(getActiveComposer()).toBe('main')
})
it('still claims the bus when the same composer becomes visible', () => {
const { rerender } = renderScopedHidden('tile:fg', true)
expect(getActiveComposer()).toBe('main')
rerender(
<PaneVisibleContext.Provider value={true}>
<ComposerScopeProvider value={{ ...MAIN_COMPOSER_SCOPE, target: 'tile:fg' }}>
<ProbeHarness
activeQueueSessionKey="session-tile"
onLayoutSnapshot={() => undefined}
sessionId="session-tile"
/>
</ComposerScopeProvider>
</PaneVisibleContext.Provider>
)
expect(getActiveComposer()).toBe('tile:fg')
})
it('claims the bus on mount when visible', () => {
renderScopedHidden('tile:vis', false)
expect(getActiveComposer()).toBe('tile:vis')
})
})
@@ -9,6 +9,7 @@ import '@/store/suggestion-providers/skill'
import { useAui, useAuiState, useComposerRuntime } from '@assistant-ui/react'
import { type RefObject, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
import { usePaneVisible } from '@/components/pane-shell/pane-visibility'
import { SLASH_COMMAND_RE } from '@/lib/chat-runtime'
import { sanitizeComposerInput } from '@/lib/composer-input-sanitize'
import {
@@ -75,6 +76,7 @@ export function useComposerDraft({
}: UseComposerDraftArgs) {
const aui = useAui()
const composerRuntime = useComposerRuntime()
const paneVisible = usePaneVisible()
// Which composer this is on the focus bus + which attachment set it owns.
const { attachments: attachmentScope, target } = useComposerScope()
@@ -181,11 +183,15 @@ export function useComposerDraft({
[paintDraft]
)
// Keep-alive tabs keep this composer mounted. A background session whose
// turn finished or recovered from a reconnect would otherwise re-run this
// effect and steal the caret. usePaneVisible defaults true outside a tab
// stack, so tiles, pop-outs, and secondary windows still auto-focus.
useEffect(() => {
if (!inputDisabled) {
if (!inputDisabled && paneVisible) {
focusInput()
}
}, [focusInput, focusKey, focusRequestId, inputDisabled])
}, [focusInput, focusKey, focusRequestId, inputDisabled, paneVisible])
// The mirror of the `markActiveComposer` above: give the key back when this
// composer goes away (a session tile closing, a pane unmounting). Covers both