Skip to content

The agent that grows with you

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

Brooklyn Nicholson6eddeca6ef54
diff --git a/apps/desktop/src/app/chat/composer/focus.test.ts b/apps/desktop/src/app/chat/composer/focus.test.ts
index 4fd21087336..2b3cb782e14 100644
--- a/apps/desktop/src/app/chat/composer/focus.test.ts
+++ b/apps/desktop/src/app/chat/composer/focus.test.ts
@@ -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)
diff --git a/apps/desktop/src/app/chat/composer/focus.ts b/apps/desktop/src/app/chat/composer/focus.ts
index 023afe92dd6..008ce4e1282 100644
--- a/apps/desktop/src/app/chat/composer/focus.ts
+++ b/apps/desktop/src/app/chat/composer/focus.ts
@@ -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()
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
index fd26b57cebb..a42edda424b 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
@@ -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')
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
index 037071a6f59..67e9b47aac9 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
@@ -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