From 79b2f4b3b8c258c6fbf97397343fdce84a278ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E9=98=BF=E6=B1=9F=28Relakkes?= =?UTF-8?q?=29?= Date: Mon, 22 Jun 2026 22:24:39 +0800 Subject: [PATCH] fix(desktop): avoid prewarming viewed history sessions (#880) Viewing an existing transcript should only load history, not start the CLI prewarm path. Empty placeholder sessions still prewarm so new chats stay fast. Fixes #880 Tested: cd desktop && bunx vitest run src/stores/chatStore.test.ts Tested: bun run check:desktop Confidence: high Scope-risk: narrow --- desktop/src/stores/chatStore.test.ts | 34 ++++++++++++++++++++++++++++ desktop/src/stores/chatStore.ts | 11 ++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/desktop/src/stores/chatStore.test.ts b/desktop/src/stores/chatStore.test.ts index 17f1ba71..ff780feb 100644 --- a/desktop/src/stores/chatStore.test.ts +++ b/desktop/src/stores/chatStore.test.ts @@ -215,6 +215,40 @@ describe('chatStore history mapping', () => { }) }) + it('does not prewarm an existing transcript when opening it for history review', () => { + sessionStoreSnapshot.sessions = [{ + id: TEST_SESSION_ID, + title: 'Existing transcript', + createdAt: '2026-06-20T10:00:00.000Z', + modifiedAt: '2026-06-20T10:30:00.000Z', + messageCount: 4, + projectPath: '/workspace/project', + workDir: '/workspace/project', + workDirExists: true, + }] + + useChatStore.getState().connectToSession(TEST_SESSION_ID) + + expect(sendMock).not.toHaveBeenCalledWith(TEST_SESSION_ID, { type: 'prewarm_session' }) + }) + + it('still prewarms empty placeholder sessions so new chats start quickly', () => { + sessionStoreSnapshot.sessions = [{ + id: TEST_SESSION_ID, + title: 'New Session', + createdAt: '2026-06-20T10:00:00.000Z', + modifiedAt: '2026-06-20T10:00:00.000Z', + messageCount: 0, + projectPath: '/workspace/project', + workDir: '/workspace/project', + workDirExists: true, + }] + + useChatStore.getState().connectToSession(TEST_SESSION_ID) + + expect(sendMock).toHaveBeenCalledWith(TEST_SESSION_ID, { type: 'prewarm_session' }) + }) + it('preserves thinking blocks when restoring transcript history', () => { const messages: MessageEntry[] = [ { diff --git a/desktop/src/stores/chatStore.ts b/desktop/src/stores/chatStore.ts index 07c28a5c..a6247145 100644 --- a/desktop/src/stores/chatStore.ts +++ b/desktop/src/stores/chatStore.ts @@ -892,6 +892,11 @@ async function fetchAndMapSessionHistory(sessionId: string) { const historyLoadsInFlight = new Map>() +function shouldPrewarmSession(sessionId: string): boolean { + const knownSession = useSessionStore.getState().sessions.find((session) => session.id === sessionId) + return !knownSession || knownSession.messageCount === 0 +} + export const useChatStore = create((set, get) => ({ sessions: {}, @@ -938,7 +943,11 @@ export const useChatStore = create((set, get) => ({ if (runtimeSelection) { wsManager.send(sessionId, { type: 'set_runtime_config', ...runtimeSelection }) } - if (!sessionId.startsWith('__') && !useTeamStore.getState().getMemberBySessionId(sessionId)) { + if ( + !sessionId.startsWith('__') && + !useTeamStore.getState().getMemberBySessionId(sessionId) && + shouldPrewarmSession(sessionId) + ) { wsManager.send(sessionId, { type: 'prewarm_session' }) }