Skip to content

The agent that grows with you

fix(desktop): an edit that fails after a runtime recovery leaves the composer idle

kshitijk4poorc9c343c37913
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.deep-cut.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.deep-cut.test.tsx
index 9bb86be3a14..25d7b655f74 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.deep-cut.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.deep-cut.test.tsx
@@ -7,7 +7,7 @@ import { textPart } from '@/lib/chat-messages'
import { createClientSessionState } from '@/lib/chat-runtime'
import { $confirmRequest, settleConfirm } from '@/store/confirm'
import { $notifications, clearNotifications } from '@/store/notifications'
-import { $busy, setActiveSessionId, setMessages } from '@/store/session'
+import { $awaitingResponse, $busy, setActiveSessionId, setMessages } from '@/store/session'
import { dropSessionState, publishSessionState } from '@/store/session-states'
import { actRender, Harness, type HarnessHandle, RUNTIME_SESSION_ID } from './prompt-actions-harness'
@@ -479,6 +479,61 @@ describe('usePromptActions deep-edit consent scope (#133716)', () => {
expect($notifications.get().some(n => n.kind === 'error')).toBe(true)
})
+ it.each([
+ ['fails', new JsonRpcGatewayError('generation failed', { code: 5035 })],
+ ['hits a stale target', new JsonRpcGatewayError('target user message is no longer in session history', { code: 4018 })]
+ ])('an edit whose submit recovers the runtime and then %s leaves the composer idle', async (_label, failure) => {
+ const seed = [
+ { id: 'u1', parts: [textPart('first')], role: 'user' as const, rowId: 11, timestamp: 0 },
+ { id: 'a1', parts: [textPart('reply')], role: 'assistant' as const, timestamp: 1 }
+ ]
+
+ setActiveSessionId(RUNTIME_SESSION_ID)
+ publishSessionState(RUNTIME_SESSION_ID, createClientSessionState('stored', seed as never))
+ setMessages(seed as never)
+
+ // The first runtime is gone: the submit resumes the stored session under a new runtime id,
+ // which moves the on-screen session to it, and the resent submit fails there.
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'session.resume') {
+ return { session_id: 'recovered-runtime' } as never
+ }
+
+ if (method === 'prompt.submit') {
+ throw params?.session_id === RUNTIME_SESSION_ID ? new Error('session not found') : failure
+ }
+
+ return {} as never
+ })
+
+ let handle: HarnessHandle | undefined
+
+ await actRender(
+ <Harness
+ onReady={h => {
+ handle = h
+ }}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ resumeStoredSession={async () => undefined}
+ seedMessages={seed}
+ storedSessionId="stored"
+ />
+ )
+
+ await handle!.editMessage({
+ content: [{ text: 'edited first', type: 'text' }],
+ parentId: null,
+ role: 'user',
+ sourceId: 'u1'
+ } as never)
+
+ expect(handle!.activeSessionIdRef.current).toBe('recovered-runtime')
+ expect($busy.get()).toBe(false)
+ expect($awaitingResponse.get()).toBe(false)
+ expect($notifications.get().some(n => n.kind === 'error')).toBe(true)
+ })
+
it('a deep-edit confirm answered after a session switch sends nothing and leaves the new session idle', async () => {
const seed = [
{ id: 'u1', parts: [textPart('first')], role: 'user', rowId: 11, timestamp: 0 },
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index 70edd4ad53a..47639d4b6a3 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -1299,11 +1299,11 @@ export function usePromptActions({
// Roll an optimistic edit/truncation back to *messages* (the original history) so the UI stays in
// sync with what's persisted instead of stranding a partial timeline; without *messages* only the
- // busy state is cleared.
+ // busy state is cleared. *ownsForeground* is false when the foreground flags now belong to another
+ // session (the user switched away during a confirm).
const rollBackEdit = useCallback(
- (sessionId: string, messages?: ChatMessage[]) => {
- // The foreground flags belong to whichever session is on screen now.
- if (activeSessionIdRef.current === sessionId) {
+ (sessionId: string, messages?: ChatMessage[], ownsForeground = true) => {
+ if (ownsForeground) {
setMutableRef(busyRef, false)
setBusy(false)
setAwaitingResponse(false)
@@ -1318,7 +1318,7 @@ export function usePromptActions({
...(messages ? { messages } : {})
}))
},
- [activeSessionIdRef, busyRef, updateSessionState]
+ [busyRef, updateSessionState]
)
// Stale target after compression/resume drift (the cached rowId and ordinal address the
@@ -1375,8 +1375,9 @@ export function usePromptActions({
}
// Nothing was cut and the refresh already replaced the optimistic edit: keep whatever
- // history the session holds now and only drop the busy arm.
- rollBackEdit(sessionId)
+ // history the session holds now and only drop the busy arm. A switch during the confirm
+ // leaves the foreground flags to the session now on screen.
+ rollBackEdit(sessionId, undefined, activeSessionIdRef.current === sessionId)
return 'done'
}