Skip to content

The agent that grows with you

fix(desktop): a 4033 confirm answered after a session switch is dropped

kshitijk4poor4f5be98c2965
diff --git a/apps/desktop/src/app/chat/session-tile-actions.ts b/apps/desktop/src/app/chat/session-tile-actions.ts
index 41384b8a595..b54c6682191 100644
--- a/apps/desktop/src/app/chat/session-tile-actions.ts
+++ b/apps/desktop/src/app/chat/session-tile-actions.ts
@@ -553,6 +553,7 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
const reloadFromMessage = useCallback(
async (parentId: string | null, answeredFor?: ChatMessage[]): Promise<void> => {
const state = readState()
+ const runtimeId = runtimeIdRef.current
if (!state || state.busy) {
return
@@ -605,8 +606,9 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
if (!isDeepTruncateRefusal(err) || plan.confirmDeepTruncate) {
notifyError(err, copy.regenerateFailed)
- } else if (await confirmDeep()) {
- // Re-run from the top: the session is re-read and re-validated after this wait.
+ } else if ((await confirmDeep()) && runtimeIdRef.current === runtimeId) {
+ // Re-run from the top: the session is re-read and re-validated after this wait. The answer
+ // belongs to this runtime: a rebind during the dialog drops it.
await reloadFromMessageRef.current(parentId, messages)
}
}
@@ -767,8 +769,9 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
if (!isDeepTruncateRefusal(err) || plan.confirmDeepTruncate) {
notifyError(err, copy.editFailed)
- } else if (await confirmDeep()) {
- // Re-run from the top: the session is re-read and re-validated after this wait.
+ } else if ((await confirmDeep()) && runtimeIdRef.current === sessionId) {
+ // Re-run from the top: the session is re-read and re-validated after this wait. The answer
+ // belongs to this runtime: a rebind during the dialog drops it.
await editMessageRef.current(edited, planning)
}
}
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index 554de300288..5cb190adb6f 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -5958,6 +5958,54 @@ describe('usePromptActions stale-closure session routing', () => {
)
})
+ it('drops a 4033 confirm answered after the user switched sessions (#133716)', async () => {
+ const submits: Record<string, unknown>[] = []
+
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'prompt.submit') {
+ submits.push(params ?? {})
+
+ if (!params?.confirm_deep_truncate) {
+ throw new JsonRpcGatewayError('truncation would archive later user turns', { code: 4033 })
+ }
+ }
+
+ return {} as never
+ }) as unknown as GatewayMock
+
+ const activeSessionIdRef: MutableRefObject<string | null> = { current: RUNTIME_SESSION_ID }
+ setMessages([
+ { id: 'u1', parts: [textPart('prompt A')], role: 'user', timestamp: 0 },
+ { id: 'a1', parts: [textPart('reply')], role: 'assistant', timestamp: 1 }
+ ] as never)
+
+ let handle: HarnessHandle | null = null
+ await actRender(
+ <Harness
+ activeSessionId={RUNTIME_SESSION_ID}
+ activeSessionIdRef={activeSessionIdRef}
+ onReady={h => (handle = h)}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ selectedStoredSessionIdRef={{ current: null }}
+ />
+ )
+
+ // Session A's tail regenerate is refused; the user switches to B, then accepts.
+ const stopConfirming = $confirmRequest.listen(request => {
+ if (request) {
+ activeSessionIdRef.current = RUNTIME_SESSION_B
+ settleConfirm(true)
+ }
+ })
+
+ await handle!.reloadFromMessage(null)
+ stopConfirming()
+
+ expect(submits).toHaveLength(1)
+ expect(submits[0]).toMatchObject({ session_id: RUNTIME_SESSION_ID })
+ })
+
it('regenerates against the CURRENT session, not the stale closure session', async () => {
const requestGateway = vi.fn(async () => ({}) as never) as unknown as GatewayMock
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 e59cb96e4a2..1cadc71ad97 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
@@ -1059,8 +1059,9 @@ export function usePromptActions({
if (!isDeepTruncateRefusal(err) || plan.confirmDeepTruncate) {
notifyError(err, copy.regenerateFailed)
- } else if (await confirmDeep()) {
- // Re-run from the top: the session is re-read and re-validated after this wait.
+ } else if ((await confirmDeep()) && activeSessionIdRef.current === sessionId) {
+ // Re-run from the top: the session is re-read and re-validated after this wait. The answer
+ // belongs to this session: a switch during the dialog drops it.
await reloadFromMessageRef.current(parentId, messages)
}
}
@@ -1353,8 +1354,9 @@ export function usePromptActions({
if (!isDeepTruncateRefusal(surfaced) || plan.confirmDeepTruncate) {
notifyError(surfaced, unavailable ? copy.editTurnUnavailable : copy.editFailed)
- } else if (await confirmDeep()) {
- // Re-run from the top: the session is re-read and re-validated after this wait.
+ } else if ((await confirmDeep()) && activeSessionIdRef.current === sessionId) {
+ // Re-run from the top: the session is re-read and re-validated after this wait. The answer
+ // belongs to this session: a switch during the dialog drops it.
await editMessageRef.current(edited, planning)
}
}