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
This commit is contained in:
程序员阿江(Relakkes) 2026-06-22 22:24:39 +08:00
parent e944db1e2f
commit 79b2f4b3b8
2 changed files with 44 additions and 1 deletions

View File

@ -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[] = [
{

View File

@ -892,6 +892,11 @@ async function fetchAndMapSessionHistory(sessionId: string) {
const historyLoadsInFlight = new Map<string, Promise<void>>()
function shouldPrewarmSession(sessionId: string): boolean {
const knownSession = useSessionStore.getState().sessions.find((session) => session.id === sessionId)
return !knownSession || knownSession.messageCount === 0
}
export const useChatStore = create<ChatStore>((set, get) => ({
sessions: {},
@ -938,7 +943,11 @@ export const useChatStore = create<ChatStore>((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' })
}