diff --git a/src/vs/sessions/contrib/copilotChatSessions/browser/copilotChatSessionsProvider.ts b/src/vs/sessions/contrib/copilotChatSessions/browser/copilotChatSessionsProvider.ts index afca3ffd79788..869f566c6cbf2 100644 --- a/src/vs/sessions/contrib/copilotChatSessions/browser/copilotChatSessionsProvider.ts +++ b/src/vs/sessions/contrib/copilotChatSessions/browser/copilotChatSessionsProvider.ts @@ -1464,12 +1464,11 @@ export class CopilotChatSessionsProvider extends Disposable implements ISessions } const chatIds = this._getChatIdsInGroup(sessionId); - if (chatIds.length <= 1) { - // Only one chat — delete the entire session - return this.deleteSession(sessionId); - } - // Find the chat matching the URI + // Find the chat matching the URI first, before deciding whether to + // delete the entire session. This prevents accidentally deleting the + // whole session when the grouping cache is stale and chatIds doesn't + // include the chat being closed. const chatId = chatIds.find(id => { const chat = this._sessionCache.get(this._localIdFromchatId(id)); return chat && chat.resource.toString() === chatUri.toString(); @@ -1478,6 +1477,11 @@ export class CopilotChatSessionsProvider extends Disposable implements ISessions return; } + if (chatIds.length <= 1) { + // This is the only chat in the session — delete the entire session + return this.deleteSession(sessionId); + } + // Delete the underlying agent session first. // _refreshSessionCacheMultiChat handles the removed chat gracefully: // it detects the chat belongs to a group with remaining siblings and