Skip to content

The agent that grows with you

fix(desktop): keep live clarify cards through turn signals

Ayush Nangiacc931d239c34
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 b4679627655..7eef88f402e 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
@@ -4,6 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { ChatMessage } from '@/lib/chat-messages'
import { createClientSessionState } from '@/lib/chat-runtime'
import { $clarifyRequests, clearClarifyRequest } from '@/store/clarify'
+import { resetServerRequestsForTests } from '@/store/server-requests'
import { onScrollToBottomRequest } from '@/store/thread-scroll'
import { type MessageStreamHarness, renderMessageStream } from './test-harness'
@@ -58,6 +59,7 @@ function seedHydratedMessages(messages: ChatMessage[]) {
describe('clarify request stream hydration', () => {
beforeEach(() => {
clearClarifyRequest()
+ resetServerRequestsForTests()
scrollToBottom.mockClear()
stopScrollListener = onScrollToBottomRequest(scrollToBottom, SID)
})
@@ -65,6 +67,7 @@ describe('clarify request stream hydration', () => {
afterEach(() => {
cleanup()
clearClarifyRequest()
+ resetServerRequestsForTests()
stopScrollListener?.()
stopScrollListener = null
vi.restoreAllMocks()
@@ -274,6 +277,24 @@ describe('clarify request stream hydration', () => {
expect(stream.state().needsInput).toBe(false)
})
+ it.each(['message.complete', 'error'] as const)('keeps a live clarify card through a spurious %s', type => {
+ mountStream()
+ clarifyRequest({ choices: ['a', 'b'], question: 'Pick', request_id: 'req-live' })
+
+ act(() =>
+ stream.handleEvent({
+ payload: type === 'error' ? { message: 'spurious error' } : { text: '' },
+ session_id: SID,
+ type
+ })
+ )
+
+ expect($clarifyRequests.get()[SID]?.requestId).toBe('req-live')
+
+ clarifyExpire('req-live')
+ expect($clarifyRequests.get()[SID]).toBeUndefined()
+ })
+
it('merges a BATCH tool.start row with its clarify.request (no top-level question)', () => {
mountStream()
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
index 202d76f0247..73f24b3af68 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/message-stream.ts
@@ -8,7 +8,7 @@ import { playCompletionSound } from '@/lib/completion-sound'
import { parseErrorSurface } from '@/lib/error-surface'
import { triggerHaptic } from '@/lib/haptics'
import { billingCtaLabel, clearBillingBlock, runBillingRecovery, setBillingBlock } from '@/store/billing-block'
-import { clearClarifyRequest } from '@/store/clarify'
+import { clearSettledClarifyRequest } from '@/store/clarify'
import { setSessionCompacting } from '@/store/compaction'
import { notify } from '@/store/notifications'
import { flashPetActivity, markPetUnread, setPetActivity } from '@/store/pet'
@@ -338,7 +338,7 @@ export function handleMessageStreamEvent(ctx: GatewayEventContext): boolean {
// session so a background turn finishing can't wipe the active chat's
// prompt, and vice versa.
clearAllPrompts(sessionId)
- clearClarifyRequest(undefined, sessionId)
+ clearSettledClarifyRequest(sessionId)
// Turn ended without a final `todo` update — drop a still-unfinished
// list so "Tasks N/M" doesn't stay pinned above the composer with the
// last item stuck pending/in_progress. Finished lists keep their linger.
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/status.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/status.ts
index 5804af6900e..2bf40b9d935 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/status.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/status.ts
@@ -6,7 +6,7 @@ import type { ErrorSurface } from '@/lib/error-surface'
import { errorCardText } from '@/lib/error-surface-copy'
import { isProviderSetupErrorMessage } from '@/lib/provider-setup-errors'
import { type AgentNoticePayload, clearAgentNotice, nativeNoticeInput, showAgentNotice } from '@/store/agent-notices'
-import { clearClarifyRequest } from '@/store/clarify'
+import { clearSettledClarifyRequest } from '@/store/clarify'
import { reconcileSessionCompacting, setSessionCompacting, takeCompressDeferred } from '@/store/compaction'
import { refreshBackgroundProcesses } from '@/store/composer-status'
import { applyGoalStatusText } from '@/store/goals'
@@ -308,7 +308,7 @@ export function handleStatusEvent(ctx: GatewayEventContext): boolean {
// the failed turn (same intent as the message.complete clear).
if (sessionId) {
clearAllPrompts(sessionId)
- clearClarifyRequest(undefined, sessionId)
+ clearSettledClarifyRequest(sessionId)
clearActiveSessionTodos(sessionId)
reconcileSessionCompacting(sessionId, 'terminal')
compactedTurnRef.current.delete(sessionId)
diff --git a/apps/desktop/src/store/clarify.ts b/apps/desktop/src/store/clarify.ts
index 22e74717c2d..bd265a7e13c 100644
--- a/apps/desktop/src/store/clarify.ts
+++ b/apps/desktop/src/store/clarify.ts
@@ -1,6 +1,6 @@
import { atom, computed } from 'nanostores'
-import { respondToServerRequest } from './server-requests'
+import { hasOpenServerRequest, respondToServerRequest } from './server-requests'
import { $activeSessionId } from './session'
export interface ClarifyQuestion {
@@ -159,6 +159,15 @@ export function clearClarifyRequest(requestId?: string, sessionId?: string | nul
export const hasClarifyRequest = (sessionId: string | null | undefined): boolean =>
Boolean($clarifyRequests.get()[keyFor(sessionId)])
+/** Clear a stale card at a turn boundary, but keep it while its backend request is still waiting. */
+export function clearSettledClarifyRequest(sessionId: string | null): void {
+ const request = $clarifyRequests.get()[keyFor(sessionId)]
+
+ if (request && !hasOpenServerRequest(request.requestId)) {
+ clearClarifyRequest(request.requestId, sessionId)
+ }
+}
+
/**
* 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