Skip to content

The agent that grows with you

fix(desktop): merge duplicate batch clarify cards

ethernet838c4692bca5
diff --git a/apps/desktop/e2e/batch-clarify.spec.ts b/apps/desktop/e2e/batch-clarify.spec.ts
new file mode 100644
index 00000000000..452ff9cc870
--- /dev/null
+++ b/apps/desktop/e2e/batch-clarify.spec.ts
@@ -0,0 +1,86 @@
+/**
+ * E2E batch clarify test — the multi-question clarify card must mount ONCE.
+ *
+ * Regression coverage for the duplicated-card bug: `tool.start` carries the
+ * model's tool_call_id while `clarify.request` carries a gateway-generated
+ * request_id. A batch payload has no top-level `question`, so the two rows
+ * only merge when the correlation key comes from the question list
+ * (`batchClarifyMatchValue` in lib/chat-messages.ts). Before that fix this
+ * exact flow rendered two identical interactive cards.
+ *
+ * The flow runs the real chain: composer → gateway → agent → clarify tool →
+ * clarify.request event → renderer, against the mock inference server.
+ */
+
+import { expect, test } from './test'
+
+import { type MockBackendFixture, setupMockBackend, waitForAppReady } from './fixtures'
+import { BATCH_CLARIFY_QUESTIONS, BATCH_CLARIFY_TRIGGER } from './mock-server'
+
+let fixture: MockBackendFixture | null = null
+
+test.beforeAll(async () => {
+ fixture = await setupMockBackend()
+ await waitForAppReady(fixture!, 120_000)
+})
+
+test.afterAll(async () => {
+ await fixture?.cleanup()
+ fixture = null
+})
+
+test.describe('batch clarify card', () => {
+ test('renders exactly one card and completes via per-question locks', async () => {
+ const page = fixture!.page
+ const composer = page.locator('[contenteditable="true"]').first()
+ await composer.waitFor({ state: 'visible', timeout: 10_000 })
+
+ await composer.click()
+ await composer.type(BATCH_CLARIFY_TRIGGER, { delay: 20 })
+ await page.keyboard.press('Enter')
+
+ // The live batch form marks itself with data-clarify-batch=<count>.
+ const batchCard = page.locator('form[data-clarify-batch]')
+ await batchCard.first().waitFor({ state: 'visible', timeout: 60_000 })
+
+ // THE regression assertion: one card, not two.
+ await expect(batchCard).toHaveCount(1)
+ await expect(batchCard).toHaveAttribute('data-clarify-batch', String(BATCH_CLARIFY_QUESTIONS.length))
+
+ // Both questions render inside the single card.
+ for (const entry of BATCH_CLARIFY_QUESTIONS) {
+ await expect(batchCard.getByText(entry.question)).toHaveCount(1)
+ }
+
+ // Each question text also appears exactly once in the whole transcript —
+ // catches a duplicate that mounts outside a form[data-clarify-batch].
+ for (const entry of BATCH_CLARIFY_QUESTIONS) {
+ 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()
+
+ // 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')
+
+ // Answer question 2 and confirm — the batch resolves and settles.
+ await batchCard.getByRole('button', { name: /Morning/ }).click()
+ await continueButton.click()
+
+ // The settled card lists both questions with their locked answers.
+ const settled = page.locator('[data-clarify-settled]')
+ await settled.waitFor({ state: 'visible', timeout: 30_000 })
+ await expect(settled.getByText(BATCH_CLARIFY_QUESTIONS[0].question)).toBeVisible()
+ await expect(settled.getByText('Coffee', { exact: true })).toBeVisible()
+ await expect(settled.getByText(BATCH_CLARIFY_QUESTIONS[1].question)).toBeVisible()
+ await expect(settled.getByText('Morning', { exact: true })).toBeVisible()
+
+ // And still no duplicate live card lingering after settle.
+ await expect(page.locator('form[data-clarify-batch]')).toHaveCount(0)
+ })
+})
diff --git a/apps/desktop/e2e/mock-server.ts b/apps/desktop/e2e/mock-server.ts
index 8de1af8aa4f..1e0fd1f7205 100644
--- a/apps/desktop/e2e/mock-server.ts
+++ b/apps/desktop/e2e/mock-server.ts
@@ -339,6 +339,40 @@ const BLOCKING_CLARIFY_TURN: ScriptedTurn = {
toolCalls: [{ name: 'clarify', args: { question: BLOCKING_CLARIFY_QUESTION, choices: ['Yes', 'No'] } }],
}
+/**
+ * A marker that makes the mock emit a blocking BATCH clarify tool call
+ * (multi-question form). Regression coverage for the duplicated-card bug:
+ * the tool.start row and the clarify.request row carry different ids and a
+ * batch payload has no top-level question, so the correlation key must come
+ * from the question list or the card mounts twice.
+ */
+export const BATCH_CLARIFY_TRIGGER = 'E2E_BATCH_CLARIFY_TRIGGER'
+export const BATCH_CLARIFY_QUESTIONS = [
+ { question: 'Pick a batch drink?', choices: ['Coffee', 'Tea'] },
+ { question: 'Pick a batch time?', choices: ['Morning', 'Night'] },
+]
+
+const BATCH_CLARIFY_TURN: ScriptedTurn = {
+ text: '',
+ toolCalls: [{ name: 'clarify', args: { questions: BATCH_CLARIFY_QUESTIONS } }],
+}
+
+function includesBatchClarifyTrigger(value: unknown): boolean {
+ if (typeof value === 'string') {
+ return value.includes(BATCH_CLARIFY_TRIGGER)
+ }
+
+ if (Array.isArray(value)) {
+ return value.some(includesBatchClarifyTrigger)
+ }
+
+ if (value && typeof value === 'object') {
+ return Object.values(value).some(includesBatchClarifyTrigger)
+ }
+
+ return false
+}
+
function includesBlockingClarifyTrigger(value: unknown): boolean {
if (typeof value === 'string') {
return value.includes(BLOCKING_CLARIFY_TRIGGER)
@@ -484,6 +518,15 @@ export function startMockServer(options: MockServerOptions = {}): Promise<MockSe
return
}
+ if (includesBatchClarifyTrigger(parsed.messages)) {
+ if (stream) {
+ streamScriptedTurn(res, model, BATCH_CLARIFY_TURN)
+ } else {
+ nonStreamingScriptedTurn(res, model, BATCH_CLARIFY_TURN)
+ }
+ return
+ }
+
if (includesBlockingClarifyTrigger(parsed.messages)) {
if (stream) {
streamScriptedTurn(res, model, BLOCKING_CLARIFY_TURN)
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/clarify-hydration.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/clarify-hydration.test.tsx
index 306c460a560..b64171cc7ec 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/clarify-hydration.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/clarify-hydration.test.tsx
@@ -169,4 +169,45 @@ describe('clarify.request stream hydration', () => {
expect(clarifyParts()).toHaveLength(1)
})
+
+ it('merges a BATCH tool.start row with its clarify.request (no top-level question)', async () => {
+ await mountStream()
+
+ // The batch shape: tool args carry `questions`, no top-level `question`.
+ // The correlation key must come from the question list, or the two ids
+ // mount two cards (the duplicate seen in the field).
+ toolStart({
+ args: { questions: [{ question: 'Drink?' }, { question: 'Productive when?' }] },
+ name: 'clarify',
+ tool_id: 'call-batch'
+ })
+ clarifyRequest({
+ questions: [
+ { qid: 'q0', question: 'Drink?' },
+ { qid: 'q1', question: 'Productive when?' }
+ ],
+ request_id: 'req-batch'
+ })
+
+ expect(clarifyParts()).toHaveLength(1)
+ })
+
+ it('does not duplicate when the batch clarify.request arrives before tool.start', async () => {
+ await mountStream()
+
+ clarifyRequest({
+ questions: [
+ { qid: 'q0', question: 'Drink?' },
+ { qid: 'q1', question: 'Productive when?' }
+ ],
+ request_id: 'req-batch-2'
+ })
+ toolStart({
+ args: { questions: [{ question: 'Drink?' }, { question: 'Productive when?' }] },
+ name: 'clarify',
+ tool_id: 'call-batch-2'
+ })
+
+ expect(clarifyParts()).toHaveLength(1)
+ })
})
diff --git a/apps/desktop/src/lib/chat-messages.ts b/apps/desktop/src/lib/chat-messages.ts
index 892a2f1dea3..937a1024344 100644
--- a/apps/desktop/src/lib/chat-messages.ts
+++ b/apps/desktop/src/lib/chat-messages.ts
@@ -624,20 +624,53 @@ function toolPayloadMatchValues(payload: GatewayEventPayload | undefined): strin
// row (the model's tool_call_id) so the two ids don't produce a duplicate
// clarify card — same correlation ClarifyToolPending uses for request↔args.
// `server` is setup_mcp's identifying arg, for the identical reason.
- const query = firstStringField(payloadArgs, ['search_term', 'query', 'question', 'server', 'command', 'code', 'path'])
+ const query =
+ firstStringField(payloadArgs, ['search_term', 'query', 'question', 'server', 'command', 'code', 'path']) ||
+ batchClarifyMatchValue(payloadArgs.questions)
const context = typeof payload?.context === 'string' ? payload.context.trim() : ''
const preview = typeof payload?.preview === 'string' ? payload.preview.trim() : ''
return collectToolMatchValues(query, context, preview)
}
+/**
+ * The batch-clarify counterpart of the `question` correlation key: a batch
+ * payload has no top-level `question`, only `questions[]`, so without this
+ * the request row and the tool.start row never match and the card mounts
+ * twice. The joined per-question texts identify the batch the same way one
+ * question text identifies a single prompt. The `\u0000` separator cannot
+ * appear in real question text, so a batch key can never collide with a
+ * single-question key.
+ */
+function batchClarifyMatchValue(questions: unknown): string {
+ if (!Array.isArray(questions)) {
+ return ''
+ }
+
+ const texts = questions
+ .map(entry => {
+ if (!entry || typeof entry !== 'object') {
+ return ''
+ }
+
+ const question = (entry as Record<string, unknown>).question
+
+ return typeof question === 'string' ? question.trim() : ''
+ })
+ .filter(Boolean)
+
+ return texts.length > 0 ? texts.join('\u0000') : ''
+}
+
function toolPartMatchValues(part: ChatMessagePart): string[] {
if (part.type !== 'tool-call' || !part.args || typeof part.args !== 'object') {
return []
}
const args = part.args as Record<string, unknown>
- const query = firstStringField(args, ['search_term', 'query', 'question', 'server', 'command', 'code', 'path'])
+ const query =
+ firstStringField(args, ['search_term', 'query', 'question', 'server', 'command', 'code', 'path']) ||
+ batchClarifyMatchValue(args.questions)
const context = typeof args.context === 'string' ? args.context.trim() : ''
const preview = typeof args.preview === 'string' ? args.preview.trim() : ''