Skip to content

The agent that grows with you

fix(desktop): send typed clarify answers in order so a failed one cannot strand the card

alt-glitch055e7ccfe2b0
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
index d96c23f0a30..d6656774fc7 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
@@ -640,20 +640,20 @@ describe('useComposerSubmit with a clarify parked on the session', () => {
expect(hasOpenServerRequest('req-two')).toBe(true)
})
- it('rolls back only the answer whose lock failed', async () => {
+ it('brings the card back when an earlier lock fails after the last answer cleared it', async () => {
let failFirst: (error: Error) => void = () => {}
const request = vi
.fn()
.mockImplementationOnce(() => new Promise((_resolve, reject) => (failFirst = reject)))
- .mockResolvedValue({ status: 'ok', remaining: ['q2'] })
+ .mockResolvedValue({ status: 'ok', remaining: [] })
setGateway(request)
- rememberServerRequest({ fail: vi.fn(), id: 'req-three', method: 'clarify', params: {}, respond })
+ rememberServerRequest({ fail: vi.fn(), id: 'req-pair', method: 'clarify', params: {}, respond })
$clarifyRequests.set({
'runtime-session': {
- requestId: 'req-three',
- questions: ['q0', 'q1', 'q2'].map(qid => ({ choices: null, multiSelect: false, qid, question: `${qid}?` })),
+ requestId: 'req-pair',
+ questions: ['q0', 'q1'].map(qid => ({ choices: null, multiSelect: false, qid, question: `${qid}?` })),
sessionId: 'runtime-session'
}
})
@@ -663,9 +663,15 @@ describe('useComposerSubmit with a clarify parked on the session', () => {
act(() => hook.result.current.submitDraft())
}
+ expect($clarifyRequests.get()['runtime-session']).toBeUndefined()
+ await waitFor(() => expect(request).toHaveBeenCalledTimes(1))
+
await act(async () => failFirst(new Error('socket closed')))
- await waitFor(() => expect($clarifyRequests.get()['runtime-session']?.lockedAnswers).toEqual({ q1: 'second' }))
+ await waitFor(() => expect($clarifyRequests.get()['runtime-session']?.lockedAnswers).toEqual({}))
+ // The second answer waits on the first, so it never reaches the server without it.
+ expect(request).toHaveBeenCalledTimes(1)
+ expect(hasOpenServerRequest('req-pair')).toBe(true)
})
it('leaves the question alone for an empty Enter (Stop, not an answer)', () => {
diff --git a/apps/desktop/src/store/clarify.ts b/apps/desktop/src/store/clarify.ts
index c9436520e25..e78b6216361 100644
--- a/apps/desktop/src/store/clarify.ts
+++ b/apps/desktop/src/store/clarify.ts
@@ -428,6 +428,12 @@ function typedAnswer(question: ClarifyQuestion, text: string): string {
return question.multiSelect ? JSON.stringify([answer]) : answer
}
+/**
+ * A request's typed answers are locked one at a time in answer order, as the card's Confirm does: an
+ * answer is not sent after an earlier one failed, so the server never holds a later answer without it.
+ */
+const lockChains = new Map<string, Promise<boolean>>()
+
function lockTypedAnswer(request: ClarifyRequest, text: string): boolean {
const gateway = $gateway.get()
const question = request.questions.find(entry => request.lockedAnswers?.[entry.qid] === undefined)
@@ -457,26 +463,45 @@ function lockTypedAnswer(request: ClarifyRequest, text: string): boolean {
setClarifyRequest({ ...request, lockedAnswers })
}
- void requestForOwnedSession<{ status?: string }>(
- request.sessionId,
- <R>(...args: Parameters<typeof gateway.request>) => gateway.request<R>(...args),
- 'clarify.lock',
- { answer, question_id: question.qid, request_id: request.requestId }
- )
- .then(reply => {
- if (reply.status === 'expired') {
- // The wait already ended; the tool's own result says how.
- forgetSettledClarify(request.requestId)
- } else if (last) {
- // The last lock resolved the batch server-side, so no response frame is owed.
- forgetServerRequest(request.requestId)
+ const previous = lockChains.get(request.requestId) ?? Promise.resolve(true)
+
+ const sent = previous.then(async earlierSaved => {
+ if (earlierSaved) {
+ try {
+ const reply = await requestForOwnedSession<{ status?: string }>(
+ request.sessionId,
+ <R>(...args: Parameters<typeof gateway.request>) => gateway.request<R>(...args),
+ 'clarify.lock',
+ { answer, question_id: question.qid, request_id: request.requestId }
+ )
+
+ if (reply.status === 'expired') {
+ // The wait already ended; the tool's own result says how.
+ forgetSettledClarify(request.requestId)
+ } else if (last) {
+ // The last lock resolved the batch server-side, so no response frame is owed.
+ forgetServerRequest(request.requestId)
+ }
+
+ return true
+ } catch (error) {
+ notifyError(error, translateNow('assistant.clarify.sendFailed'))
}
- })
- .catch(error => {
- forgetSettledClarify(request.requestId)
- rollBackTypedAnswer(request, lockedAnswers, question.qid, answer)
- notifyError(error, translateNow('assistant.clarify.sendFailed'))
- })
+ }
+
+ forgetSettledClarify(request.requestId)
+ rollBackTypedAnswer(request, lockedAnswers, question.qid, answer)
+
+ return false
+ })
+
+ lockChains.set(request.requestId, sent)
+
+ void sent.then(() => {
+ if (lockChains.get(request.requestId) === sent) {
+ lockChains.delete(request.requestId)
+ }
+ })
return true
}