Skip to content

The agent that grows with you

feat(desktop): single confirm for the batch clarify card

ethernet25c6516607a3
diff --git a/apps/desktop/e2e/batch-clarify.spec.ts b/apps/desktop/e2e/batch-clarify.spec.ts
index 452ff9cc870..8658bd3b1b5 100644
--- a/apps/desktop/e2e/batch-clarify.spec.ts
+++ b/apps/desktop/e2e/batch-clarify.spec.ts
@@ -58,19 +58,19 @@ test.describe('batch clarify card', () => {
await expect(page.getByText(entry.question)).toHaveCount(1)
}
- // Answer question 1: stage a choice, lock it with Continue.
- await batchCard.getByRole('button', { name: /Coffee/ }).click()
- const continueButton = batchCard.locator('button[type="submit"]')
- await expect(continueButton).toContainText('Continue')
- await continueButton.click()
+ // Answer both questions: stage picks locally (no server traffic yet).
+ const confirmButton = batchCard.locator('button[type="submit"]')
+ await expect(confirmButton).toContainText('Confirm and continue')
+ await expect(confirmButton).toBeDisabled()
- // One locked, one left: the button relabels to Confirm and continue.
- await expect(batchCard.locator('[data-clarify-batch-question][data-locked]')).toHaveCount(1)
- await expect(continueButton).toContainText('Confirm and continue')
+ await batchCard.getByRole('button', { name: /Coffee/ }).click()
+ await expect(confirmButton).toBeDisabled()
- // Answer question 2 and confirm — the batch resolves and settles.
await batchCard.getByRole('button', { name: /Morning/ }).click()
- await continueButton.click()
+ await expect(confirmButton).toBeEnabled()
+
+ // ONE confirm submits the whole batch.
+ await confirmButton.click()
// The settled card lists both questions with their locked answers.
const settled = page.locator('[data-clarify-settled]')
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 e78158a2eb5..0da12eba131 100644
--- a/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx
@@ -533,67 +533,72 @@ describe('ClarifyTool batch card', () => {
expect(screen.getByText('0 of 2 answered')).toBeTruthy()
})
- it('locks a picked choice via Continue, keyed by qid', async () => {
+ it('stages locally and keeps the single confirm disabled until all answered', async () => {
const request = renderLiveBatch()
+ const confirm = screen.getByRole('button', { name: /Confirm and continue/ })
- fireEvent.click(screen.getByRole('button', { name: /red/ }))
- fireEvent.submit(document.querySelector('form') as HTMLFormElement)
+ expect((confirm as HTMLButtonElement).disabled).toBe(true)
- await waitFor(() => {
- expect(request).toHaveBeenCalledWith('clarify.respond', {
- answer: 'red',
- question_id: 'q0',
- request_id: 'request-batch'
- })
- })
+ // Staging a pick sends NOTHING to the server.
+ fireEvent.click(screen.getByRole('button', { name: /red/ }))
+ expect(screen.getByText('1 of 2 answered')).toBeTruthy()
+ expect(request).not.toHaveBeenCalled()
+ expect((confirm as HTMLButtonElement).disabled).toBe(true)
- await waitFor(() => {
- expect(screen.getByText('1 of 2 answered')).toBeTruthy()
- })
+ fireEvent.change(screen.getByPlaceholderText('Type your answer…'), { target: { value: 'packet' } })
+ expect(screen.getByText('2 of 2 answered')).toBeTruthy()
+ expect(request).not.toHaveBeenCalled()
+ expect((confirm as HTMLButtonElement).disabled).toBe(false)
})
- it('answers in any order: free-text question first', async () => {
+ it('confirm sends every per-question lock in order and completes the batch', async () => {
const request = renderLiveBatch()
- const nameBox = screen.getByPlaceholderText('Type your answer…')
- fireEvent.change(nameBox, { target: { value: 'packet' } })
+ fireEvent.click(screen.getByRole('button', { name: /red/ }))
+ fireEvent.change(screen.getByPlaceholderText('Type your answer…'), { target: { value: 'packet' } })
fireEvent.submit(document.querySelector('form') as HTMLFormElement)
await waitFor(() => {
- expect(request).toHaveBeenCalledWith('clarify.respond', {
- answer: 'packet',
- question_id: 'q1',
- request_id: 'request-batch'
- })
+ expect(request).toHaveBeenCalledTimes(2)
+ })
+ expect(request).toHaveBeenNthCalledWith(1, 'clarify.respond', {
+ answer: 'red',
+ question_id: 'q0',
+ request_id: 'request-batch'
+ })
+ expect(request).toHaveBeenNthCalledWith(2, 'clarify.respond', {
+ answer: 'packet',
+ question_id: 'q1',
+ request_id: 'request-batch'
})
})
- it('relabels the button to Confirm and continue when one question remains', async () => {
- renderLiveBatch({ q1: 'already locked' })
-
- expect(screen.getByText('1 of 2 answered')).toBeTruthy()
- expect(screen.getByRole('button', { name: /Confirm and continue/ })).toBeTruthy()
- })
-
- it('re-staging a locked answer un-locks it and a re-lock overwrites', async () => {
- const request = renderLiveBatch({ q0: 'red' })
+ it('a staged answer stays editable before confirm', async () => {
+ const request = renderLiveBatch()
- // q0 arrived locked from replay. Picking blue un-locks it locally…
+ fireEvent.click(screen.getByRole('button', { name: /red/ }))
fireEvent.click(screen.getByRole('button', { name: /blue/ }))
- expect(screen.getByText('0 of 2 answered')).toBeTruthy()
-
- // …and Continue re-locks with the new answer.
+ fireEvent.change(screen.getByPlaceholderText('Type your answer…'), { target: { value: 'packet' } })
fireEvent.submit(document.querySelector('form') as HTMLFormElement)
await waitFor(() => {
- expect(request).toHaveBeenCalledWith('clarify.respond', {
- answer: 'blue',
- question_id: 'q0',
- request_id: 'request-batch'
- })
+ expect(request).toHaveBeenCalledTimes(2)
+ })
+ // The re-pick won: blue, not red.
+ expect(request).toHaveBeenNthCalledWith(1, 'clarify.respond', {
+ answer: 'blue',
+ question_id: 'q0',
+ request_id: 'request-batch'
})
})
+ it('pre-stages replayed locked answers from a reconnect', () => {
+ renderLiveBatch({ q0: 'red' })
+
+ // The replayed answer counts as staged: one question left to answer.
+ expect(screen.getByText('1 of 2 answered')).toBeTruthy()
+ })
+
it('Skip cancels the whole batch without a question_id', async () => {
const request = renderLiveBatch()
diff --git a/apps/desktop/src/components/assistant-ui/clarify-tool.tsx b/apps/desktop/src/components/assistant-ui/clarify-tool.tsx
index b3584ccb4d5..b7f0fabd97c 100644
--- a/apps/desktop/src/components/assistant-ui/clarify-tool.tsx
+++ b/apps/desktop/src/components/assistant-ui/clarify-tool.tsx
@@ -906,11 +906,13 @@ function BatchQuestionBlock({
const emptyStage = { choices: [] as string[], draft: '' }
-/** Live batch card (B lock-in): all questions at once, staging per question,
- * one footer button that locks the focused answer. Locked answers stay
- * editable (a re-lock overwrites server-side) until the final confirm — the
- * last unanswered question's lock IS the confirm, so the button relabels to
- * "Confirm and continue" when exactly one remains. */
+/** Live batch card: all questions at once, staged locally, ONE confirm.
+ * Picks and drafts stay in component state — nothing reaches the server
+ * until every question has a staged answer and the user presses the single
+ * "Confirm and continue" button, which sends the per-question locks
+ * back-to-back and completes the batch. Staged answers stay editable up to
+ * that moment. The per-question wire protocol is unchanged (the TUI/CLI
+ * still lock incrementally); this card just batches its locks at the end. */
function ClarifyToolBatchPending({ request }: { request: ClarifyRequest | null }) {
const { t } = useI18n()
const copy = t.assistant.clarify
@@ -922,17 +924,35 @@ function ClarifyToolBatchPending({ request }: { request: ClarifyRequest | null }
const ready = Boolean(request?.requestId) && questions.length > 0
const [staged, setStaged] = useState<Record<string, { choices: string[]; draft: string }>>({})
- // Locked = accepted by the server (clarify.respond succeeded). Seeded from
- // the reconnect replay so a reattached window shows its earlier locks.
- const [locked, setLocked] = useState<Record<string, string>>(() => request?.lockedAnswers ?? {})
- // The question the footer button acts on: last interacted-with, unlocked.
- const [focusedQid, setFocusedQid] = useState<string | null>(null)
const [submitting, setSubmitting] = useState(false)
+ // Reconnect replay: answers the server already locked (an earlier window's
+ // partial progress) pre-stage their questions so the restored card shows
+ // them selected instead of blank.
useEffect(() => {
- if (request?.lockedAnswers) {
- setLocked(current => ({ ...request.lockedAnswers, ...current }))
+ const lockedAnswers = request?.lockedAnswers
+
+ if (!lockedAnswers) {
+ return
}
+
+ setStaged(current => {
+ const next = { ...current }
+
+ for (const question of questions) {
+ const answer = lockedAnswers[question.qid]
+
+ if (answer === undefined || next[question.qid]) {
+ continue
+ }
+
+ const asChoice = (question.choices ?? []).find(choice => bareChoice(choice) === answer)
+ next[question.qid] = asChoice ? { choices: [asChoice], draft: '' } : { choices: [], draft: answer }
+ }
+
+ return next
+ })
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- keyed by the replay map only
}, [request?.lockedAnswers])
const stageFor = (qid: string) => staged[qid] ?? emptyStage
@@ -952,73 +972,43 @@ function ClarifyToolBatchPending({ request }: { request: ClarifyRequest | null }
[staged]
)
- const unansweredCount = questions.filter(q => locked[q.qid] === undefined).length
+ const answeredCount = questions.filter(q => stagedAnswer(q) !== null).length
+ const allStaged = answeredCount === questions.length
- // The button targets the focused question when it has something staged;
- // otherwise the first unlocked question with a staged answer.
- const target = useMemo(() => {
- const focused = questions.find(q => q.qid === focusedQid)
+ const confirmAll = useCallback(async () => {
+ if (!request || !gateway) {
+ notifyError(new Error(request ? copy.gatewayDisconnected : copy.notReady), copy.sendFailed)
- if (focused && stagedAnswer(focused) !== null) {
- return focused
+ return
}
- return questions.find(q => stagedAnswer(q) !== null) ?? null
- }, [focusedQid, questions, stagedAnswer])
-
- const lockAnswer = useCallback(
- async (question: ClarifyQuestion, answer: string) => {
- if (!request || !gateway) {
- notifyError(new Error(request ? copy.gatewayDisconnected : copy.notReady), copy.sendFailed)
-
- return
- }
+ setSubmitting(true)
- setSubmitting(true)
+ try {
+ // Sequential, not Promise.all: the LAST lock resolves the blocked tool
+ // server-side, so every earlier lock must already be accepted when it
+ // lands — a reordered burst could complete the batch with a missing
+ // answer.
+ for (const question of questions) {
+ const answer = stagedAnswer(question)
- try {
await gateway.request<{ ok?: boolean }>('clarify.respond', {
- answer,
+ answer: answer ?? '',
question_id: question.qid,
request_id: request.requestId
})
- triggerHaptic('submit')
- setLocked(current => ({ ...current, [question.qid]: answer }))
- setStaged(current => ({ ...current, [question.qid]: emptyStage }))
- setFocusedQid(null)
-
- // Locking the last unanswered question completes the batch: the
- // server resolves the blocked tool and tool.complete lands next
- // (→ ClarifyToolBatchSettled). Drop the store entry now, mirroring
- // the single-question card.
- const remaining = questions.filter(q => q.qid !== question.qid && locked[q.qid] === undefined)
-
- if (remaining.length === 0) {
- clearClarifyRequest(request.requestId, request.sessionId)
- }
- } catch (error) {
- notifyError(error, copy.sendFailed)
- } finally {
- setSubmitting(false)
}
- },
- [copy, gateway, locked, questions, request]
- )
-
- const submitTarget = useCallback(() => {
- if (!target) {
- return
- }
-
- const answer = stagedAnswer(target)
- if (answer !== null) {
- void lockAnswer(target, answer)
+ triggerHaptic('submit')
+ // tool.complete lands next → ClarifyToolBatchSettled.
+ clearClarifyRequest(request.requestId, request.sessionId)
+ } catch (error) {
+ notifyError(error, copy.sendFailed)
+ setSubmitting(false)
}
- }, [lockAnswer, stagedAnswer, target])
+ }, [copy, gateway, questions, request, stagedAnswer])
const toggleChoice = useCallback((question: ClarifyQuestion, choice: string) => {
- setFocusedQid(question.qid)
setStaged(current => {
const stage = current[question.qid] ?? emptyStage
const next = question.multiSelect
@@ -1029,33 +1019,10 @@ function ClarifyToolBatchPending({ request }: { request: ClarifyRequest | null }
return { ...current, [question.qid]: { choices: next, draft: '' } }
})
- // A fresh pick un-locks the question for editing: the old answer stays on
- // the server until the re-lock overwrites it.
- setLocked(current => {
- if (current[question.qid] === undefined) {
- return current
- }
-
- const next = { ...current }
- delete next[question.qid]
-
- return next
- })
}, [])
const draftFor = useCallback((question: ClarifyQuestion, value: string) => {
- setFocusedQid(question.qid)
setStaged(current => ({ ...current, [question.qid]: { choices: [], draft: value } }))
- setLocked(current => {
- if (current[question.qid] === undefined || !value.trim()) {
- return current
- }
-
- const next = { ...current }
- delete next[question.qid]
-
- return next
- })
}, [])
const cancelAll = useCallback(async () => {
@@ -1075,9 +1042,12 @@ function ClarifyToolBatchPending({ request }: { request: ClarifyRequest | null }
const handleSubmit = useCallback(
(event: FormEvent<HTMLFormElement>) => {
event.preventDefault()
- submitTarget()
+
+ if (allStaged) {
+ void confirmAll()
+ }
},
- [submitTarget]
+ [allStaged, confirmAll]
)
if (!ready) {
@@ -1088,9 +1058,6 @@ function ClarifyToolBatchPending({ request }: { request: ClarifyRequest | null }
)
}
- const answeredCount = questions.length - unansweredCount
- const lastOne = unansweredCount === 1
-
return (
<form className="my-1.5 grid gap-4" data-clarify-batch={questions.length} onSubmit={handleSubmit}>
<ClarifyShell className="grid gap-3">
@@ -1104,7 +1071,7 @@ function ClarifyToolBatchPending({ request }: { request: ClarifyRequest | null }
<BatchQuestionBlock
disabled={submitting}
key={question.qid}
- locked={locked[question.qid] !== undefined}
+ locked={false}
onDraft={value => draftFor(question, value)}
onToggle={choice => toggleChoice(question, choice)}
question={question}
@@ -1117,12 +1084,12 @@ function ClarifyToolBatchPending({ request }: { request: ClarifyRequest | null }
<Button disabled={submitting} onClick={() => void cancelAll()} size="xs" type="button" variant="text">
{copy.skip}
</Button>
- <Button disabled={submitting || !target} size="xs" type="submit">
+ <Button disabled={submitting || !allStaged} size="xs" type="submit">
{submitting ? (
<Loader2 className="size-3 animate-spin" />
) : (
<>
- {lastOne ? copy.confirmAndContinueLabel : copy.continueLabel}
+ {copy.confirmAndContinueLabel}
<span aria-hidden className="ml-0.5 text-[0.625rem] opacity-70">
⏎
</span>
diff --git a/website/docs/reference/tools-reference.md b/website/docs/reference/tools-reference.md
index 4f4939128ea..4851c2edada 100644
--- a/website/docs/reference/tools-reference.md
+++ b/website/docs/reference/tools-reference.md
@@ -50,7 +50,7 @@ The `clarify` tool also accepts a `questions` array (2–5 independent questions
Per-surface behavior:
-- **Desktop** shows every question on one card. Picking or typing stages an answer; **Continue** locks the focused question, and locked answers stay editable until the last one — the button becomes **Confirm and continue** when exactly one question remains, and that final lock submits the batch. Skip cancels the whole batch.
+- **Desktop** shows every question on one card. Picks and typed answers stage locally, and one **Confirm and continue** button (enabled once every question has an answer) submits the whole batch. Staged answers stay editable until that confirm. Skip cancels the whole batch.
- **TUI and CLI** show a compact status list (`✓` answered / `▸` active / `·` pending) with only the active question's choices expanded. Enter locks the active answer and jumps to the next unanswered question; Tab moves between questions to answer in any order; Esc cancels the batch.
- **Messaging platforms** (Telegram, Discord, …) fall back to asking the questions one at a time through the existing single-question prompt. If the user stops responding, the remaining questions are not sent.