Skip to content

The agent that grows with you

fix(desktop): resolve the clarify card by zone, not document order

briandevanscebe2168ab06
diff --git a/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx b/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx
index 42f062852a4..a877933679e 100644
--- a/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx
@@ -7,6 +7,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import { onComposerInsertRequest } from '@/app/chat/composer/focus'
import { type SessionView, SessionViewProvider } from '@/app/chat/session-view'
import { hiddenPaneProps } from '@/components/pane-shell/pane-visibility'
+import { $activeTreeGroup, $hoveredTreeGroup } from '@/components/pane-shell/tree/store'
import { I18nProvider } from '@/i18n'
import { clearClarifyRequest, setClarifyRequest } from '@/store/clarify'
import { $gateway } from '@/store/gateway'
@@ -851,8 +852,17 @@ describe('ClarifyTool visible-card scoping', () => {
const FOREGROUND_SESSION = 'session-foreground'
const BACKGROUND_REQUEST = 'request-background'
const FOREGROUND_REQUEST = 'request-foreground'
+ const ZONE_A_SESSION = 'session-zone-a'
+ const ZONE_B_SESSION = 'session-zone-b'
+ const ZONE_A_REQUEST = 'request-zone-a'
+ const ZONE_B_REQUEST = 'request-zone-b'
const QUESTION = 'Which deployment target?'
+ afterEach(() => {
+ $activeTreeGroup.set(null)
+ $hoveredTreeGroup.set(null)
+ })
+
/** Minimal per-session view — the pending card only reads `$runtimeId`. */
function tileView(sessionId: string): SessionView {
return { ...({} as SessionView), $runtimeId: atom<null | string>(sessionId), kind: 'tile' }
@@ -931,4 +941,71 @@ describe('ClarifyTool visible-card scoping', () => {
expect(fireEvent.keyDown(window, { key: 'Enter' })).toBe(true)
expect(request).not.toHaveBeenCalled()
})
+
+ /** A card in its own split zone — unlike `backgroundCard` this one IS on
+ * screen, so a split renders two cards that both clear the hidden-pane
+ * filter and only the zone ladder can tell apart. */
+ function zoneCard(zone: string, sessionId: string) {
+ return (
+ <div data-tree-group={zone}>
+ <SessionViewProvider value={tileView(sessionId)}>
+ <ClarifyTool {...pendingCardProps(`clarify-${zone}`)} />
+ </SessionViewProvider>
+ </div>
+ )
+ }
+
+ /** Both zones visible, zone-a first in document order. */
+ function renderSplit() {
+ const request = vi.fn().mockResolvedValue({ ok: true })
+
+ $gateway.set({ request } as never)
+ parkClarify(ZONE_A_REQUEST, ZONE_A_SESSION)
+ parkClarify(ZONE_B_REQUEST, ZONE_B_SESSION)
+
+ renderClarify(
+ <>
+ {zoneCard('zone-a', ZONE_A_SESSION)}
+ {zoneCard('zone-b', ZONE_B_SESSION)}
+ </>
+ )
+
+ return request
+ }
+
+ it('answers the later-in-document card when its zone is the focused one', async () => {
+ const request = renderSplit()
+
+ $activeTreeGroup.set('zone-b')
+ fireEvent.keyDown(window, { key: 'Enter' })
+
+ await waitFor(() => {
+ expect(request).toHaveBeenCalledTimes(1)
+ })
+
+ // Both cards are visible and both hold a live window listener, so this is
+ // the case document order gets wrong: it would answer zone-a's question.
+ expect(request).toHaveBeenCalledWith('clarify.respond', {
+ answer: 'staging',
+ request_id: ZONE_B_REQUEST
+ })
+ })
+
+ it('answers the other visible card once the focus moves to its zone', async () => {
+ const request = renderSplit()
+
+ $activeTreeGroup.set('zone-a')
+ fireEvent.keyDown(window, { key: 'Enter' })
+
+ await waitFor(() => {
+ expect(request).toHaveBeenCalledTimes(1)
+ })
+
+ // The direct pin for "the other visible card then cannot receive its
+ // shortcut": neither zone may be permanently starved of its own keys.
+ expect(request).toHaveBeenCalledWith('clarify.respond', {
+ answer: 'staging',
+ request_id: ZONE_A_REQUEST
+ })
+ })
})
diff --git a/apps/desktop/src/lib/keybinds/composer-focus-keys.test.ts b/apps/desktop/src/lib/keybinds/composer-focus-keys.test.ts
index a75f649c001..c1fe51484c1 100644
--- a/apps/desktop/src/lib/keybinds/composer-focus-keys.test.ts
+++ b/apps/desktop/src/lib/keybinds/composer-focus-keys.test.ts
@@ -1,13 +1,15 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { $workspaceIsPage } from '@/app/routes'
+import { $activeTreeGroup, $hoveredTreeGroup } from '@/components/pane-shell/tree/store'
import { $switcherOpen, closeSwitcher } from '@/store/session-switcher'
import {
composerFocusBlockedBySurface,
composerFocusKeysAllowed,
isActivateOnEnterTarget,
- typeToFocusChar
+ typeToFocusChar,
+ visibleClarifyCard
} from './composer-focus-keys'
function keydown(init: KeyboardEventInit & { target?: EventTarget }): KeyboardEvent {
@@ -121,9 +123,28 @@ describe('typeToFocusChar', () => {
})
describe('composerFocusKeysAllowed', () => {
+ // The clarify resolver reads the zone ladder, so these have to start neutral.
+ const resetZones = () => {
+ $activeTreeGroup.set(null)
+ $hoveredTreeGroup.set(null)
+ }
+
+ /** A live clarify card inside its own split zone, both on screen. */
+ function cardInZone(zone: string): HTMLElement {
+ const group = document.createElement('div')
+ group.setAttribute('data-tree-group', zone)
+ const card = document.createElement('div')
+ card.setAttribute('data-clarify-choices', '2')
+ group.append(card)
+ document.body.append(group)
+
+ return card
+ }
+
beforeEach(() => {
$workspaceIsPage.set(false)
closeSwitcher()
+ resetZones()
document.body.replaceChildren()
vi.spyOn(document, 'activeElement', 'get').mockReturnValue(document.body)
})
@@ -131,6 +152,7 @@ describe('composerFocusKeysAllowed', () => {
afterEach(() => {
$workspaceIsPage.set(false)
closeSwitcher()
+ resetZones()
document.body.replaceChildren()
vi.restoreAllMocks()
})
@@ -200,4 +222,47 @@ describe('composerFocusKeysAllowed', () => {
expect(composerFocusKeysAllowed(keydown({ key: 'a', target: document.body }), 'type')).toBe(true)
expect(composerFocusKeysAllowed(keydown({ key: 'Enter', target: document.body }), 'enter')).toBe(true)
})
+
+ it('picks the focused zone when a split shows two visible cards', () => {
+ const cardA = cardInZone('zone-a')
+ const cardB = cardInZone('zone-b')
+
+ // Document order would pin this to zone-a forever, so zone-b's card could
+ // never take its own shortcut. Both directions are asserted for that reason.
+ $activeTreeGroup.set('zone-b')
+ expect(visibleClarifyCard()).toBe(cardB)
+
+ $activeTreeGroup.set('zone-a')
+ expect(visibleClarifyCard()).toBe(cardA)
+ })
+
+ it('lets the hovered zone override the focused one', () => {
+ const cardA = cardInZone('zone-a')
+ const cardB = cardInZone('zone-b')
+
+ $activeTreeGroup.set('zone-a')
+ expect(visibleClarifyCard()).toBe(cardA)
+
+ // Hover-first, like every tab verb: pointing at the other pane arms its card
+ // without clicking into it.
+ $hoveredTreeGroup.set('zone-b')
+ expect(visibleClarifyCard()).toBe(cardB)
+ })
+
+ it('steps down the ladder instead of answering nothing', () => {
+ const cardA = cardInZone('zone-a')
+ const cardB = cardInZone('zone-b')
+
+ // Pointer parked on a zone with no pending question — fall through to the
+ // focused zone rather than returning null.
+ $hoveredTreeGroup.set('zone-empty')
+ $activeTreeGroup.set('zone-b')
+ expect(visibleClarifyCard()).toBe(cardB)
+
+ // Neither rung resolves ⇒ document order, deliberately. Returning null here
+ // would leave Enter doing nothing at all.
+ resetZones()
+ expect(visibleClarifyCard()).toBe(cardA)
+ expect(composerFocusKeysAllowed(keydown({ key: 'Enter', target: document.body }), 'enter')).toBe(false)
+ })
})
diff --git a/apps/desktop/src/lib/keybinds/composer-focus-keys.ts b/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
index 799fc56af98..8cc7d7bdcd5 100644
--- a/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
+++ b/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
@@ -7,7 +7,8 @@
*/
import { $workspaceIsPage } from '@/app/routes'
-import { queryVisible } from '@/components/pane-shell/pane-visibility'
+import { queryAllVisible } from '@/components/pane-shell/pane-visibility'
+import { $activeTreeGroup, $hoveredTreeGroup } from '@/components/pane-shell/tree/store'
import { switcherActive } from '@/store/session-switcher'
import { isEditableTarget, isFocusWithin } from './combo'
@@ -48,6 +49,9 @@ const BLOCKING_OVERLAY =
// must not take the foreground composer's letter keys.
const BLOCKING_IN_SURFACE = '[data-clarify-choices]'
+/** The layout-tree zone a pane is rendered in — see `tree/renderer/tree-group`. */
+const TREE_GROUP = '[data-tree-group]'
+
/**
* THE live clarify card — the single card whose shortcuts are armed right now.
*
@@ -56,8 +60,39 @@ const BLOCKING_IN_SURFACE = '[data-clarify-choices]'
* disagree about which card is live they act for different sessions: this
* resolver yields to the VISIBLE card, while a handler keyed on anything else
* (mount order, say) answers one the user cannot see.
+ *
+ * Visible is not by itself an identity, though. A split layout puts two chat
+ * surfaces on screen at once, so both cards clear the hidden-pane filter and
+ * taking the first remaining DOM match would pick document ORDER — the earlier
+ * zone would own the keys permanently and the other visible card could never
+ * receive its shortcut. Break that tie on the SAME hovered → focused zone
+ * ladder every tab verb already runs (`tabTargetGroup` in `tree/store`,
+ * mirrored for the model hotkey by `composerTargetInHoveredZone`, #74447)
+ * rather than inventing a second notion of which surface is "the" one.
+ *
+ * The last resort stays document order rather than null on purpose: when
+ * neither rung names a zone that holds a card (pointer off every zone, nothing
+ * interacted with yet) returning null would leave Enter doing nothing at all,
+ * which is a worse regression than the single-card behaviour it replaces.
*/
-export const visibleClarifyCard = (): HTMLElement | null => queryVisible(BLOCKING_IN_SURFACE)
+export const visibleClarifyCard = (): HTMLElement | null => {
+ const cards = queryAllVisible<HTMLElement>(BLOCKING_IN_SURFACE)
+
+ // Overwhelmingly the common case — nothing to disambiguate, no store read.
+ if (cards.length < 2) {
+ return cards[0] ?? null
+ }
+
+ for (const zone of [$hoveredTreeGroup.get(), $activeTreeGroup.get()]) {
+ const card = zone ? cards.find(el => el.closest<HTMLElement>(TREE_GROUP)?.dataset.treeGroup === zone) : undefined
+
+ if (card) {
+ return card
+ }
+ }
+
+ return cards[0]
+}
/** True when the focused control would normally handle Enter itself. */
export function isActivateOnEnterTarget(target: EventTarget | null): boolean {