Remember last active chat per session across restarts#312108
Merged
Conversation
Persist per-session state (ISessionState) in the sessions management service so that when a session is reopened, the last active chat is restored instead of always defaulting to the first chat. - Add ISessionState interface with sessionResource, activeChatResource, and isActive fields, stored as a JSON array and loaded into a ResourceMap keyed by session resource URI - Restore last active chat in setActiveSession() when switching sessions - Track active chat changes via autorun to keep persisted state current - Mark the currently active session with isActive flag for reload - Remove legacy lastSelectedSession/LAST_SELECTED_SESSION_KEY storage (consolidated into the new session state) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds persistence for per-session UI state in the Sessions window so that reopening a session restores the previously active chat (instead of always defaulting to the first chat), and consolidates prior “last selected session” storage into a session-state store.
Changes:
- Introduces
ISessionStateand persists session states as JSON in workspace storage, loaded into aResourceMap. - Restores a session’s last active chat when activating/opening that session.
- Tracks active chat changes via
autorunand persistsactiveChatResource(and anisActivemarker).
Show a summary per file
| File | Description |
|---|---|
| src/vs/sessions/services/sessions/browser/sessionsManagementService.ts | Adds persisted per-session state loading/saving and restores last active chat when reopening sessions. |
Copilot's findings
Comments suppressed due to low confidence (3)
src/vs/sessions/services/sessions/browser/sessionsManagementService.ts:462
- In the persistence
autorun,chat.status.read(undefined)is not tracked by the autorun (and won’t re-run when the chat transitions fromUntitledto a real status). This means a chat that starts untitled may never get persisted as the last active chat even after it becomes titled. Usechat.status.read(reader)(or a separate reaction that watches the status) so the state is updated once the chat becomes non-untitled.
this._activeSessionDisposables.add(autorun(reader => {
const chat = activeChatObs.read(reader);
if (chat && chat.status.read(undefined) !== SessionStatus.Untitled) {
// Mark all sessions as inactive, then set this one as active
src/vs/sessions/services/sessions/browser/sessionsManagementService.ts:504
_saveSessionStatespersists every entry currently in_sessionStates, but entries are never removed when sessions are deleted/removed. Over time this can grow unbounded in workspace storage and keep restoring state for sessions that no longer exist. Consider pruning_sessionStates(e.g., keep only keys that still exist ingetSessions()/ remove one.removed) before writing to storage.
private _saveSessionStates(): void {
const entries: ISessionState[] = [];
for (const [, state] of this._sessionStates) {
entries.push(state);
}
this.storageService.store(ACTIVE_SESSION_STATES_KEY, JSON.stringify(entries), StorageScope.WORKSPACE, StorageTarget.MACHINE);
src/vs/sessions/services/sessions/browser/sessionsManagementService.ts:483
_sessionStatesis created with the defaultResourceMapkeying (uri.toString()), which is case-sensitive and can fail to match the same logical resource on platforms with case-insensitive paths or different URI normalizations. Consider constructing theResourceMapwithuri => this.uriIdentityService.extUri.getComparisonKey(uri)so lookups likethis._sessionStates.get(session.resource)are stable across restarts and OSes.
private _loadSessionStates(): ResourceMap<ISessionState> {
const map = new ResourceMap<ISessionState>();
const raw = this.storageService.get(ACTIVE_SESSION_STATES_KEY, StorageScope.WORKSPACE);
- Files reviewed: 1/1 changed files
- Comments generated: 1
alexr00
previously approved these changes
Apr 23, 2026
…rvice.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
lszomoru
approved these changes
Apr 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Persist per-session state (
ISessionState) in the sessions management service so that when a session is reopened, the last active chat is restored instead of always defaulting to the first chat.Changes
ISessionStateinterface withsessionResource,activeChatResource, andisActivefieldsResourceMapkeyed by session resource URIsetActiveSession()when switching sessionsautorunto keep persisted state currentisActiveflag for use on reloadlastSelectedSession/LAST_SELECTED_SESSION_KEYstorage (consolidated into the new session state)