diff --git a/src/vs/sessions/services/sessions/browser/sessionNavigation.ts b/src/vs/sessions/services/sessions/browser/sessionNavigation.ts index a71bba595f8d1..2acb51322e797 100644 --- a/src/vs/sessions/services/sessions/browser/sessionNavigation.ts +++ b/src/vs/sessions/services/sessions/browser/sessionNavigation.ts @@ -78,10 +78,12 @@ export class SessionsNavigation extends Disposable { this._register(autorun(reader => { const activeSession = this._sessionsManagementService.activeSession.read(reader); const activeChat = activeSession?.activeChat.read(reader); + const sessionStatus = activeSession?.status.read(reader); + const chatStatus = activeChat?.status.read(reader); if (this._navigating) { return; } - if (!activeSession || activeSession.status.read(reader) === SessionStatus.Untitled) { + if (!activeSession || sessionStatus === SessionStatus.Untitled) { // User navigated to new-session view: if we have history, remember we're // beyond the stack so Back can return to the last real session. if (this._history.length > 0) { @@ -91,7 +93,7 @@ export class SessionsNavigation extends Disposable { } // Skip untitled chats (new-chat-in-session that hasn't been submitted) - const chatResource = activeChat && activeChat.status.read(reader) !== SessionStatus.Untitled + const chatResource = activeChat && chatStatus !== SessionStatus.Untitled ? activeChat.resource : undefined; @@ -185,7 +187,12 @@ export class SessionsNavigation extends Disposable { const session = this._sessionsManagementService.getSession(entry.sessionResource); if (session) { if (entry.chatResource) { - await this._sessionsManagementService.openChat(session, entry.chatResource); + const chatExists = session.chats.get().some(c => c.resource.toString() === entry.chatResource!.toString()); + if (chatExists) { + await this._sessionsManagementService.openChat(session, entry.chatResource); + } else { + await this._sessionsManagementService.openSession(entry.sessionResource); + } } else { await this._sessionsManagementService.openSession(entry.sessionResource); } diff --git a/src/vs/sessions/services/sessions/test/browser/sessionNavigation.test.ts b/src/vs/sessions/services/sessions/test/browser/sessionNavigation.test.ts index a99ce155659e5..b2f257e784dd9 100644 --- a/src/vs/sessions/services/sessions/test/browser/sessionNavigation.test.ts +++ b/src/vs/sessions/services/sessions/test/browser/sessionNavigation.test.ts @@ -463,4 +463,30 @@ suite('SessionsNavigation', () => { store.setActiveSession(s1, chatUntitled); assert.strictEqual(canGoBack(), false, 'untitled chat produces a session-only entry, no second entry'); }); + + test('goBack falls back to openSession when chat was deleted', async () => { + const chatA = stubChatWithId('a'); + const chatB = stubChatWithId('b'); + const chatsObs = observableValue('test.chats', [chatA, chatB]); + const s1: ISession = { + ...stubSession('s1', SessionStatus.Completed, [chatA, chatB]), + chats: chatsObs, + }; + const s2 = stubSession('s2'); + store.addSession(s1); + store.addSession(s2); + + // Record history: s1/chatA → s1/chatB → s2 + store.setActiveSession(s1, chatA); + store.setActiveChat(chatB); + store.setActiveSession(s2); + + // Remove chatB from the session + chatsObs.set([chatA], undefined); + + // Go back — chatB is stale, should fall back to openSession(s1) + await nav.goBack(); + assert.strictEqual(store.lastOpenedResource?.toString(), s1.resource.toString()); + assert.strictEqual(store.lastOpenedChatResource, undefined, 'should not open a stale chat'); + }); });