Skip to content

The agent that grows with you

fix(desktop): a stale-target retry warns when the turn moved and reports a rebound runtime

kshitijk4poor3380ab0ddfe4
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 63407ebea15..7fcae754ec1 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
@@ -6,7 +6,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import { textPart } from '@/lib/chat-messages'
import { createClientSessionState } from '@/lib/chat-runtime'
import { $confirmRequest, settleConfirm } from '@/store/confirm'
-import { clearNotifications } from '@/store/notifications'
+import { $notifications, clearNotifications } from '@/store/notifications'
import { $busy, setActiveSessionId, setMessages } from '@/store/session'
import { dropSessionState, publishSessionState } from '@/store/session-states'
@@ -323,7 +323,8 @@ describe('usePromptActions deep-edit consent scope (#133716)', () => {
it.each([
['accepting', 'accept'],
['declining', 'decline'],
- ['accepting after a session switch', 'switch']
+ ['accepting after a session switch', 'switch'],
+ ['accepting after the edited turn moved', 'moved']
])('%s the second deep confirm on a stale-target retry acts on the refreshed history', async (_label, answer) => {
const seed = [
{ id: 'u1', parts: [textPart('first')], role: 'user' as const, rowId: 11, timestamp: 0 },
@@ -388,6 +389,13 @@ describe('usePromptActions deep-edit consent scope (#133716)', () => {
handle!.activeSessionIdRef.current = 'other-runtime'
}
+ if (confirms === 2 && answer === 'moved') {
+ // A newer user turn lands while the dialog is open: the plan no longer covers it.
+ const grown = [...fresh, { id: 'u4', parts: [textPart('newer')], role: 'user', rowId: 27, timestamp: 4 }]
+ publishSessionState(RUNTIME_SESSION_ID, createClientSessionState('stored', grown as never))
+ setMessages(grown as never)
+ }
+
settleConfirm(confirms === 1 || answer !== 'decline')
}
})
@@ -413,6 +421,57 @@ describe('usePromptActions deep-edit consent scope (#133716)', () => {
expect(lastState).toMatchObject({ busy: false })
expect(lastState?.messages).not.toBe(seed)
}
+
+ // Only a turn that moved under an accepted confirm is worth a notice; a decline is the user's choice.
+ expect($notifications.get().some(n => n.kind === 'warning')).toBe(answer === 'moved')
+ })
+
+ it('a stale-target retry whose resume rebinds the runtime reports the turn unavailable', async () => {
+ 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)
+ const submits: Record<string, unknown>[] = []
+
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'prompt.submit') {
+ submits.push(params ?? {})
+ throw new JsonRpcGatewayError('target user message is no longer in session history', { code: 4018 })
+ }
+
+ return {} as never
+ })
+
+ let handle: HarnessHandle | undefined
+
+ await actRender(
+ <Harness
+ onReady={h => {
+ handle = h
+ }}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ resumeStoredSession={async () => {
+ handle!.activeSessionIdRef.current = 'rebound-runtime'
+ }}
+ seedMessages={seed}
+ storedSessionId="stored"
+ />
+ )
+
+ await handle!.editMessage({
+ content: [{ text: 'edited first', type: 'text' }],
+ parentId: null,
+ role: 'user',
+ sourceId: 'u1'
+ } as never)
+
+ expect(submits).toHaveLength(1)
+ 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 () => {
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 ca704d10603..70edd4ad53a 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
@@ -1343,6 +1343,12 @@ export function usePromptActions({
await resumeStoredSession(storedId)
}
+ // The resume can rebind the runtime (or the user switched away meanwhile): surface that as
+ // unavailable rather than drop the edit.
+ if (activeSessionIdRef.current !== sessionId) {
+ return { unavailable: true }
+ }
+
// A refresh that keeps the same later turns keeps the user's answer; one that reveals more
// asks again. After the dialog the plan is re-aimed by content (revalidateEditPlan), since the
// session slice and the $messages mirror are distinct arrays.
@@ -1363,6 +1369,11 @@ export function usePromptActions({
})
if (planned === null || planned === 'moved') {
+ if (planned === 'moved') {
+ // The edited turn moved while the confirm was open: nothing was sent.
+ notify({ kind: 'warning', message: copy.editFailed })
+ }
+
// 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)
@@ -1399,6 +1410,7 @@ export function usePromptActions({
[
activeSessionIdRef,
applySurvivorRowIds,
+ copy.editFailed,
resumeStoredSession,
rollBackEdit,
selectedStoredSessionIdRef,