diff --git a/ui/desktop/src/acp/__tests__/chatSessionController.test.ts b/ui/desktop/src/acp/__tests__/chatSessionController.test.ts index 665069a83..6acaaeb7b 100644 --- a/ui/desktop/src/acp/__tests__/chatSessionController.test.ts +++ b/ui/desktop/src/acp/__tests__/chatSessionController.test.ts @@ -220,6 +220,35 @@ describe('acpChatSessionController.stop', () => { ); expect(acpCancelPrompt).toHaveBeenCalledWith(SESSION_ID); }); + + it('leaves the chat state alone when stopping during a session load', () => { + vi.mocked(acpChatSessionStore.getSnapshot).mockReturnValue({ + ...snapshotWithActivePrompt(null), + chatState: ChatState.LoadingConversation, + }); + + acpChatSessionController.stop(SESSION_ID); + + expect(acpChatSessionActions.setChatState).not.toHaveBeenCalled(); + expect(acpChatSessionActions.startPromptCancellation).not.toHaveBeenCalled(); + expect(acpCancelPrompt).not.toHaveBeenCalled(); + }); + + it('cancels a leftover prompt attempt when stopping during a session load', () => { + vi.mocked(acpChatSessionStore.getSnapshot).mockReturnValue({ + ...snapshotWithActivePrompt('attempt-1'), + chatState: ChatState.LoadingConversation, + }); + + acpChatSessionController.stop(SESSION_ID); + + expect(acpChatSessionActions.startPromptCancellation).toHaveBeenCalledWith( + SESSION_ID, + 'attempt-1' + ); + expect(acpCancelPrompt).toHaveBeenCalledWith(SESSION_ID); + expect(acpChatSessionActions.setChatState).not.toHaveBeenCalled(); + }); }); describe('acpChatSessionController.submitMessage', () => { diff --git a/ui/desktop/src/acp/__tests__/chatSessionStore.test.ts b/ui/desktop/src/acp/__tests__/chatSessionStore.test.ts index 464598c76..662e8c74c 100644 --- a/ui/desktop/src/acp/__tests__/chatSessionStore.test.ts +++ b/ui/desktop/src/acp/__tests__/chatSessionStore.test.ts @@ -542,6 +542,28 @@ describe('acpChatSessionStore', () => { expect(finishedSnapshot.messages[0].content).toEqual([{ type: 'text', text: 'Hello' }]); }); + it('keeps only the late notification when one arrives after a failed session load', () => { + const currentSessionId = sessionId('session-1'); + + acpChatSessionActions.startSessionLoad(currentSessionId); + acpChatSessionActions.applyAcpSessionNotification( + agentMessageChunkNotification(currentSessionId, 'message-1', 'Hello') + ); + + const failedSnapshot = acpChatSessionActions.failSessionLoad(currentSessionId, 'boom'); + expect(failedSnapshot.sessionLoadError).toBe('boom'); + expect(failedSnapshot.messages).toEqual([]); + + const lateSnapshot = acpChatSessionActions.applyAcpSessionNotification( + agentMessageChunkNotification(currentSessionId, 'message-2', 'World') + ); + + expect(lateSnapshot.messages.map((item) => item.id)).toEqual(['message-2']); + expect( + acpChatSessionStore.getSnapshot(currentSessionId)?.messages.map((item) => item.id) + ).toEqual(['message-2']); + }); + it('applies permission requests as waiting action-required messages', () => { const currentSessionId = sessionId('session-1'); diff --git a/ui/desktop/src/acp/chatSessionController.ts b/ui/desktop/src/acp/chatSessionController.ts index ac03fd565..9d6bbaf3d 100644 --- a/ui/desktop/src/acp/chatSessionController.ts +++ b/ui/desktop/src/acp/chatSessionController.ts @@ -225,7 +225,8 @@ async function submitMessage( } function stop(sessionId: string): void { - const storedPromptAttemptId = acpChatSessionStore.getSnapshot(sessionId)?.activePromptAttemptId; + const snapshot = acpChatSessionStore.getSnapshot(sessionId); + const storedPromptAttemptId = snapshot?.activePromptAttemptId; const hasStoredAcpPrompt = storedPromptAttemptId !== null && storedPromptAttemptId !== undefined; if (hasStoredAcpPrompt) { @@ -238,6 +239,11 @@ function stop(sessionId: string): void { return; } + // A session load has nothing to cancel; ending it here would leave the replay fast path. + if (snapshot?.chatState === ChatState.LoadingConversation) { + return; + } + acpChatSessionActions.setChatState(sessionId, ChatState.Idle); } diff --git a/ui/desktop/src/acp/chatSessionStore.ts b/ui/desktop/src/acp/chatSessionStore.ts index 2f02aaaa3..3743d3a14 100644 --- a/ui/desktop/src/acp/chatSessionStore.ts +++ b/ui/desktop/src/acp/chatSessionStore.ts @@ -237,6 +237,7 @@ function createAcpChatSessionStoreInternal(): AcpChatSessionStoreInternal { sessionLoadError ) => { const entry = getOrCreateEntry(sessionId); + resetReplayState(entry); entry.sessionLoadError = sessionLoadError; entry.progressMessage = undefined; entry.chatState = ChatState.Idle;