Skip to content

Remember last active chat per session across restarts#312108

Merged
sandy081 merged 2 commits intomainfrom
sandy081/persist-session-state
Apr 23, 2026
Merged

Remember last active chat per session across restarts#312108
sandy081 merged 2 commits intomainfrom
sandy081/persist-session-state

Conversation

@sandy081
Copy link
Copy Markdown
Member

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

  • Add ISessionState interface with sessionResource, activeChatResource, and isActive fields
  • Store session states as a JSON array, load 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 use on reload
  • Remove legacy lastSelectedSession / LAST_SELECTED_SESSION_KEY storage (consolidated into the new session state)

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>
Copilot AI review requested due to automatic review settings April 23, 2026 09:33
@github-actions
Copy link
Copy Markdown
Contributor

Screenshot Changes

Base: f8d07504 Current: 0d80cc67

Changed (2)

chat/aiCustomizations/aiCustomizationManagementEditor/McpBrowseMode/Light
Before After
before after
editor/inlineCompletions/other/JumpToHint/Dark
Before After
before after

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ISessionState and persists session states as JSON in workspace storage, loaded into a ResourceMap.
  • Restores a session’s last active chat when activating/opening that session.
  • Tracks active chat changes via autorun and persists activeChatResource (and an isActive marker).
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 from Untitled to a real status). This means a chat that starts untitled may never get persisted as the last active chat even after it becomes titled. Use chat.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

  • _saveSessionStates persists 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 in getSessions() / remove on e.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

  • _sessionStates is created with the default ResourceMap keying (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 the ResourceMap with uri => this.uriIdentityService.extUri.getComparisonKey(uri) so lookups like this._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

Comment thread src/vs/sessions/services/sessions/browser/sessionsManagementService.ts Outdated
alexr00
alexr00 previously approved these changes Apr 23, 2026
…rvice.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@sandy081 sandy081 merged commit c809950 into main Apr 23, 2026
26 checks passed
@sandy081 sandy081 deleted the sandy081/persist-session-state branch April 23, 2026 11:05
@vs-code-engineering vs-code-engineering Bot added this to the 1.118.0 milestone Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants