The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): the tile restore retries a stale durable target too (#107593)
Brooklyn Nicholson2fd423740f77
diff --git a/apps/desktop/src/app/chat/session-tile-actions.test.ts b/apps/desktop/src/app/chat/session-tile-actions.test.tsindex 306977d0a99..f9f2991d0fa 100644--- a/apps/desktop/src/app/chat/session-tile-actions.test.ts+++ b/apps/desktop/src/app/chat/session-tile-actions.test.ts@@ -273,6 +273,83 @@ describe('useSessionTileActions reloadFromMessage failed-submit rollback (#95745vi.restoreAllMocks()})+ it('re-hydrates the tile transcript and retries once when restore hits a stale durable target', async () => {+ // #107593 (tile surface): the tile's cached rowIds go stale after resume+ // drift or a session.branch remap; Restore must recover like Edit does —+ // refresh the stored transcript, recompute the plan, retry once — instead+ // of rolling back and rethrowing as a 4018 dead-end.+ let submitAttempts = 0+ const submitCalls: { params?: Record<string, unknown> }[] = []++ requestGatewayMock.mockImplementation(async (method: string, params?: Record<string, unknown>) => {+ if (method === 'prompt.submit') {+ submitAttempts += 1+ submitCalls.push({ params })++ if (submitAttempts === 1) {+ throw new Error('target user message is no longer in session history')+ }++ return {}+ }++ return {}+ })++ const resumeTile = vi.fn(async () => {+ // The refreshed transcript: same shapes, fresh row ids after the remap.+ publishSessionState(+ RUNTIME_SESSION_ID,+ createClientSessionState(+ STORED_SESSION_ID,+ [+ { id: 'u1-fresh', parts: [textPart('first')], role: 'user' as const, rowId: 501, timestamp: 0 },+ { id: 'a1-fresh', parts: [textPart('reply')], role: 'assistant' as const, rowId: 502, timestamp: 1 },+ { id: 'u2-fresh', parts: [textPart('later')], role: 'user' as const, rowId: 503, timestamp: 2 }+ ] as never+ )+ )++ return RUNTIME_SESSION_ID+ })++ // Re-wire the delegate so resumeTile is the transcript refresher.+ setSessionTileDelegate({+ archiveSession: vi.fn(async () => undefined),+ branchSession: vi.fn(async () => undefined),+ deleteSession: vi.fn(async () => undefined),+ executeSlash: vi.fn(async () => undefined),+ interruptSession: vi.fn(async () => undefined),+ resumeTile,+ submitToSession: vi.fn(async () => undefined),+ updateSession: vi.fn((_runtimeId, updater) => {+ const current = $sessionStates.get()[RUNTIME_SESSION_ID]++ if (!current) {+ return undefined+ }++ const next = updater(current)++ publishSessionState(RUNTIME_SESSION_ID, next)++ return next+ })+ })++ const { result } = renderTileActions()++ await act(async () => {+ await result.current.restoreToMessage('u1')+ })++ expect(resumeTile).toHaveBeenCalledWith(STORED_SESSION_ID, { refreshTranscript: true })+ expect(submitAttempts).toBe(2)+ expect(submitCalls[1]?.params).toMatchObject({+ truncate_before_row_id: 501+ })+ })+it('restores the full tile transcript when regenerate is rejected', async () => {requestGatewayMock.mockImplementation(async (method: string) => {if (method === 'prompt.submit') {diff --git a/apps/desktop/src/app/chat/session-tile-actions.ts b/apps/desktop/src/app/chat/session-tile-actions.tsindex 3582ea13878..3def34d542a 100644--- a/apps/desktop/src/app/chat/session-tile-actions.ts+++ b/apps/desktop/src/app/chat/session-tile-actions.ts@@ -45,6 +45,7 @@ import type { SessionInfo } from '@/types/hermes'import type { GatewayRequester } from '../contrib/types'import { uploadComposerAttachment } from '../session/hooks/use-prompt-actions'+import { isStaleTargetError } from '../session/hooks/use-prompt-actions'import {appendMidTurnUserMessage,applyBranchVisibility,@@ -613,6 +614,47 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored))} catch (err) {+ // Same stale-target hazard as the primary chat's restore (#107593): a+ // tile's cached durable row ids go stale after optimistic sends, resume+ // drift, or session.branch remapping, and the gateway refuses with 4018.+ // Mirror edit's recovery: refresh the transcript from the stored+ // session, recompute the restore plan against fresh history, retry once.+ if ((plan.truncateMessageId || plan.truncateRowId !== undefined) && isStaleTargetError(err)) {+ try {+ const refreshed = await sessionTileDelegate()!.resumeTile(storedIdRef.current, {+ refreshTranscript: true+ })+ if (typeof refreshed === 'string' && refreshed && refreshed !== sessionId) {+ runtimeIdRef.current = refreshed+ }++ const freshMessages = readMessages()+ const retryPlan = planRestore(freshMessages, messageId, {+ text: target?.text ?? plan.sourceText,+ userOrdinal: target?.userOrdinal ?? plan.truncateOrdinal+ })++ if (retryPlan.truncateMessageId || retryPlan.truncateRowId !== undefined) {+ applySurvivorRowIds(+ await submitRewind(+ retryPlan.text,+ retryPlan.truncateOrdinal,+ interruptFirst,+ retryPlan.truncateMessageId,+ retryPlan.truncateRowId,+ retryPlan.sourceText,+ durableRowIdsForRebind(freshMessages)+ )+ )++ return+ }+ } catch {+ // Fall through to the rollback below: the optimistic truncation+ // must never survive a failed retry.+ }+ }+update(state => ({...state,busy: false,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.tsxindex d7de421c38f..d9c26649db5 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@@ -3054,7 +3054,7 @@ describe('usePromptActions restoreToMessage', () => {$messages.set(initialMessages as never)let submitAttempts = 0- const requestGateway = vi.fn(async (method: string) => {+ const requestGateway = vi.fn(async (method: string, _params?: Record<string, unknown>) => {if (method === 'prompt.submit') {submitAttempts += 1diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.tsindex 5e9ea6cad64..5e8f5f5acb2 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@@ -249,6 +249,8 @@ interface RestoreMessageTarget {const isStaleTargetError = (err: unknown) =>/no longer in session history|not in session history/i.test(err instanceof Error ? err.message : String(err))+export { isStaleTargetError }+export function usePromptActions({activeSessionId,activeSessionIdRef,