From 9d74a53f59f9aa7dacf45f68cc101f4244e75700 Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Wed, 29 Apr 2026 13:38:42 +0200 Subject: [PATCH] Fix deleting forked session showing wrong confirmation dialog When clicking X on a sub-session tab, deleteChat checks chatIds.length before finding the specific chat by URI. If the grouping cache doesn't include the forked chat (e.g. due to stale sessionParentId metadata), _getChatIdsInGroup returns only the main chat. The chatIds.length <= 1 check then falls through to deleteSession, which shows 'delete this session?' and deletes the entire session including the worktree. Fix: find the specific chat by URI first. Only fall through to deleteSession if the chat IS found in the group AND it's the last one. Fixes #313148 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../browser/copilotChatSessionsProvider.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) 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