Skip to content

The agent that grows with you

fix(desktop): let the composer answer past a clarify card

Brooklyn Nicholson251b668f4d83
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 7594a12b06d..a75f649c001 100644
--- a/apps/desktop/src/lib/keybinds/composer-focus-keys.test.ts
+++ b/apps/desktop/src/lib/keybinds/composer-focus-keys.test.ts
@@ -80,22 +80,11 @@ describe('composerFocusBlockedBySurface', () => {
expect(composerFocusBlockedBySurface()).toBe(true)
})
- it('blocks while a live clarify choices card owns its letter keys', () => {
+ it('ignores a clarify card — it yields only its own keys, per-key', () => {
const card = document.createElement('div')
- card.setAttribute('data-clarify-choices', '')
+ card.setAttribute('data-clarify-choices', '2')
document.body.append(card)
- expect(composerFocusBlockedBySurface()).toBe(true)
- })
-
- it('ignores a clarify card waiting in a kept-alive background tab', () => {
- const tab = document.createElement('div')
- tab.setAttribute('data-pane-hidden', '')
- const card = document.createElement('div')
- card.setAttribute('data-clarify-choices', '')
- tab.append(card)
- document.body.append(tab)
-
expect(composerFocusBlockedBySurface()).toBe(false)
})
@@ -174,15 +163,41 @@ describe('composerFocusKeysAllowed', () => {
expect(composerFocusKeysAllowed(keydown({ key: 'a', code: 'KeyA', target: document.body }), 'type')).toBe(false)
})
- it('yields letter + Enter keys to a live clarify choices card', () => {
+ it('yields only the keys a live clarify card actually binds', () => {
const card = document.createElement('div')
- card.setAttribute('data-clarify-choices', '')
+ // Two choices → rows A/B plus the "Other" row C; 1/2/3 are the digit twins.
+ card.setAttribute('data-clarify-choices', '2')
document.body.append(card)
- // The clarify card's own A/B/C… + Enter shortcuts must win over type-to-focus.
- expect(composerFocusKeysAllowed(keydown({ key: 'a', code: 'KeyA', target: document.body }), 'type')).toBe(false)
- expect(composerFocusKeysAllowed(keydown({ key: 'Enter', code: 'Enter', target: document.body }), 'enter')).toBe(
- false
- )
+ const allowed = (key: string, combo: string) =>
+ composerFocusKeysAllowed(keydown({ key, target: document.body }), combo)
+
+ // The card's own shortcuts win over type-to-focus.
+ expect(allowed('a', 'type')).toBe(false)
+ expect(allowed('B', 'type')).toBe(false)
+ expect(allowed('c', 'type')).toBe(false)
+ expect(allowed('1', 'type')).toBe(false)
+ expect(allowed('3', 'type')).toBe(false)
+ expect(allowed('Enter', 'enter')).toBe(false)
+
+ // Everything past the last row is not the card's — typing a real message
+ // instead of picking an option must still reach the composer.
+ expect(allowed('d', 'type')).toBe(true)
+ expect(allowed('z', 'type')).toBe(true)
+ expect(allowed('4', 'type')).toBe(true)
+ expect(allowed('?', 'type')).toBe(true)
+ expect(allowed(' ', 'type')).toBe(true)
+ })
+
+ it('leaves every key alone for a clarify card in a background tab', () => {
+ const tab = document.createElement('div')
+ tab.setAttribute('data-pane-hidden', '')
+ const card = document.createElement('div')
+ card.setAttribute('data-clarify-choices', '2')
+ tab.append(card)
+ document.body.append(tab)
+
+ expect(composerFocusKeysAllowed(keydown({ key: 'a', target: document.body }), 'type')).toBe(true)
+ expect(composerFocusKeysAllowed(keydown({ key: 'Enter', target: document.body }), 'enter')).toBe(true)
})
})
diff --git a/apps/desktop/src/lib/keybinds/composer-focus-keys.ts b/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
index 29da02cda58..4660ed0ff51 100644
--- a/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
+++ b/apps/desktop/src/lib/keybinds/composer-focus-keys.ts
@@ -56,19 +56,61 @@ export function isActivateOnEnterTarget(target: EventTarget | null): boolean {
}
/**
- * Dialogs, menus, terminal, full pages, session switcher, any open overlay
- * (settings / command-center / star map / …), and a live clarify choices card —
+ * True when a live clarify card binds THIS key, so type-to-focus must yield it.
+ *
+ * The card owns Enter plus the shortcuts it actually renders — `1..N+1` and
+ * `A..` for its N choices and the trailing "Other" row. It does NOT own the
+ * rest of the alphabet: typing a real message instead of picking an option is a
+ * legitimate answer ("none of these"), and blanket-blocking every printable
+ * left the user unable to start that message at all — the first letter vanished
+ * and the composer never focused. Out-of-range keys fall through to the
+ * composer, which skips the question on send.
+ *
+ * The choice count rides in the attribute's value, so this stays a DOM read
+ * with no store coupling.
+ */
+export function clarifyCardOwnsKey(event: KeyboardEvent): boolean {
+ const card = queryVisible(BLOCKING_IN_SURFACE)
+
+ if (!card) {
+ return false
+ }
+
+ if (event.key === 'Enter') {
+ return true
+ }
+
+ // "Other" is the row past the last choice, hence the +1.
+ const rows = Number(card.getAttribute('data-clarify-choices')) + 1
+
+ if (!Number.isFinite(rows)) {
+ return false
+ }
+
+ const key = event.key.toLowerCase()
+
+ if (key.length !== 1) {
+ return false
+ }
+
+ const index = /^[1-9]$/.test(key) ? Number(key) - 1 : key >= 'a' && key <= 'z' ? key.charCodeAt(0) - 97 : -1
+
+ return index >= 0 && index < rows
+}
+
+/**
+ * Dialogs, menus, terminal, full pages, session switcher, and any open overlay —
* they keep their keys, so type-to-focus / soft `/` / Enter stand down rather
* than stealing keystrokes those surfaces own (or leaking them into the composer
- * mounted behind an overlay).
+ * mounted behind an overlay). A live clarify card is handled per-key by
+ * `clarifyCardOwnsKey`, not here — it only owns its own shortcuts.
*/
export function composerFocusBlockedBySurface(): boolean {
return (
switcherActive() ||
$workspaceIsPage.get() ||
isFocusWithin('[data-terminal]') ||
- Boolean(document.querySelector(BLOCKING_OVERLAY)) ||
- Boolean(queryVisible(BLOCKING_IN_SURFACE))
+ Boolean(document.querySelector(BLOCKING_OVERLAY))
)
}
@@ -95,7 +137,8 @@ export function composerFocusKeysAllowed(event: KeyboardEvent, combo: string): b
event.defaultPrevented ||
event.isComposing ||
isEditableTarget(event.target) ||
- composerFocusBlockedBySurface()
+ composerFocusBlockedBySurface() ||
+ clarifyCardOwnsKey(event)
) {
return false
}
diff --git a/apps/desktop/src/store/clarify.test.ts b/apps/desktop/src/store/clarify.test.ts
index ed995a7a52a..aec413b5b36 100644
--- a/apps/desktop/src/store/clarify.test.ts
+++ b/apps/desktop/src/store/clarify.test.ts
@@ -1,13 +1,16 @@
-import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
$clarifyRequest,
$clarifyRequests,
type ClarifyRequest,
clearClarifyRequest,
+ hasClarifyRequest,
normalizeChoices,
- setClarifyRequest
+ setClarifyRequest,
+ skipClarifyRequest
} from './clarify'
+import { $gateway } from './gateway'
import { $activeSessionId } from './session'
function clarify(sessionId: string | null, requestId: string): ClarifyRequest {
@@ -81,6 +84,47 @@ describe('clarify store', () => {
})
})
+describe('skipClarifyRequest', () => {
+ const request = vi.fn(async () => ({ ok: true }))
+
+ beforeEach(() => {
+ $clarifyRequests.set({})
+ request.mockClear()
+ $gateway.set({ request } as unknown as ReturnType<typeof $gateway.get>)
+ })
+
+ afterEach(() => {
+ $clarifyRequests.set({})
+ $gateway.set(null)
+ })
+
+ it('answers the session\u2019s clarify with an empty answer and drops it', async () => {
+ setClarifyRequest(clarify('session-a', 'req-a'))
+ setClarifyRequest(clarify('session-b', 'req-b'))
+
+ await expect(skipClarifyRequest('session-a')).resolves.toBe(true)
+
+ expect(request).toHaveBeenCalledWith('clarify.respond', { request_id: 'req-a', answer: '' })
+ expect(hasClarifyRequest('session-a')).toBe(false)
+ // A background session's question is untouched — only the one being typed
+ // over is skipped.
+ expect(hasClarifyRequest('session-b')).toBe(true)
+ })
+
+ it('is a no-op when the session has no clarify parked', async () => {
+ await expect(skipClarifyRequest('session-a')).resolves.toBe(false)
+ expect(request).not.toHaveBeenCalled()
+ })
+
+ it('still reports the skip when the respond RPC fails', async () => {
+ setClarifyRequest(clarify('session-a', 'req-a'))
+ request.mockRejectedValueOnce(new Error('socket closed'))
+
+ await expect(skipClarifyRequest('session-a')).resolves.toBe(true)
+ expect(hasClarifyRequest('session-a')).toBe(false)
+ })
+})
+
describe('normalizeChoices', () => {
it('returns empty array for null/undefined', () => {
expect(normalizeChoices(null)).toEqual([])
diff --git a/apps/desktop/src/store/clarify.ts b/apps/desktop/src/store/clarify.ts
index f90dcd13174..6cdc41eb44a 100644
--- a/apps/desktop/src/store/clarify.ts
+++ b/apps/desktop/src/store/clarify.ts
@@ -1,5 +1,6 @@
import { atom, computed } from 'nanostores'
+import { $gateway } from './gateway'
import { $activeSessionId } from './session'
export interface ClarifyRequest {
@@ -102,3 +103,42 @@ export function clearClarifyRequest(requestId?: string, sessionId?: string | nul
$clarifyRequests.set(next)
}
}
+
+/** Whether `sessionId` has a clarify parked on it right now (imperative read —
+ * the composer checks this on Enter, not on every render). */
+export const hasClarifyRequest = (sessionId: string | null | undefined): boolean =>
+ Boolean($clarifyRequests.get()[keyFor(sessionId)])
+
+/**
+ * Answer `sessionId`'s pending clarify with an empty answer (a skip) and drop it
+ * locally, resolving to whether there was one to skip.
+ *
+ * The composer uses this when the user types a real message instead of picking
+ * an option: a clarify blocks the agent inside its tool batch, so leaving it
+ * unanswered would park the follow-up until the server-side clarify timeout
+ * (default 5 min) — the message looks sent and nothing happens. Skipping lets
+ * the tool return and the turn carry on with the user's actual words.
+ *
+ * An empty answer is the same thing the card's own Skip button sends, and
+ * `clarify.respond` is `allow_expired`, so racing the timeout is harmless.
+ */
+export async function skipClarifyRequest(sessionId: string | null | undefined): Promise<boolean> {
+ const request = $clarifyRequests.get()[keyFor(sessionId)]
+
+ if (!request) {
+ return false
+ }
+
+ // Clear first: the answer is already decided, and an in-flight RPC must not
+ // leave a live card the user can answer a second time.
+ clearClarifyRequest(request.requestId, request.sessionId)
+
+ try {
+ await $gateway.get()?.request('clarify.respond', { request_id: request.requestId, answer: '' })
+ } catch {
+ // The tool times out on its own; a failed skip must never swallow the
+ // message the user is actually sending.
+ }
+
+ return true
+}