Skip to content

Track terminal-to-session association by session ID instead of cwd#321506

Draft
meganrogge wants to merge 4 commits into
mainfrom
meganrogge/terminal-session-association
Draft

Track terminal-to-session association by session ID instead of cwd#321506
meganrogge wants to merge 4 commits into
mainfrom
meganrogge/terminal-session-association

Conversation

@meganrogge

@meganrogge meganrogge commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Replace the cwd-based terminal matching with a session ID-based map (Map<sessionId, Set<instanceId>>) for reliable 1:1 association. This avoids bugs where multiple sessions sharing the same cwd would interfere with each other's terminal lifecycle.

Key changes

  • Add _sessionTerminals map to track which terminals belong to which session
  • ensureTerminal now records associations and prefers tracked terminals
  • Visibility, archive, and removal use session ID lookups instead of scanning all terminals by cwd
  • Fall back to cwd matching for restored/untracked terminals (backward compat)
  • Transfer tracking on onDidReplaceSession so terminals survive untitled→committed graduation
  • Clean up stale map entries via onDidDisposeInstance when terminals are externally closed
  • Prune disposed terminals in _isTerminalTracked to avoid incorrect cwd fallback exclusion

Tests

  • All 52 tests pass (3 new)
  • transfers tracked terminals when a session is replaced (graduation)
  • cleans up tracked terminal ids when terminals are externally disposed
  • untracked restored terminals are visible alongside tracked terminals for the same session

Fixes #312909

Replace the cwd-based terminal matching with a session ID-based map
(Map<sessionId, Set<instanceId>>) for reliable 1:1 association. This
avoids bugs where multiple sessions sharing the same cwd would
interfere with each other's terminal lifecycle.

Key changes:
- Add _sessionTerminals map to track which terminals belong to which session
- ensureTerminal now records associations and prefers tracked terminals
- Visibility, archive, and removal use session ID lookups instead of
  scanning all terminals by cwd
- Fall back to cwd matching for restored terminals not yet in the map

Fixes #312909

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 15, 2026 20:14
@meganrogge meganrogge self-assigned this Jun 15, 2026
@meganrogge meganrogge added this to the 1.126.0 milestone Jun 15, 2026
@meganrogge meganrogge requested a review from anthonykim1 June 15, 2026 20:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the Agents window terminal management to track terminal ownership by session ID (instead of matching by initial cwd), preventing cross-session interference when multiple sessions share the same working directory. It also adds/updates tests to validate correct behavior for shared-cwd scenarios and session-based visibility decisions.

Changes:

  • Introduce a session→terminal association map (Map<sessionId, Set<instanceId>>) and prefer tracked terminals when ensuring/activating terminals.
  • Update visibility, archive, and removal cleanup to operate using tracked session terminals, with cwd fallback for previously-restored/untracked terminals.
  • Expand unit tests to cover shared-cwd cases and ensure untracked cwd matches aren’t incorrectly reused across sessions.
Show a summary per file
File Description
src/vs/sessions/contrib/terminal/browser/sessionsTerminalContribution.ts Implement session-id based terminal tracking and update ensure/visibility/archive/remove logic with cwd fallback.
src/vs/sessions/contrib/terminal/test/browser/sessionsTerminalContribution.test.ts Update/add tests to validate session-based terminal association and visibility when sessions share a cwd.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

Comment thread src/vs/sessions/contrib/terminal/browser/sessionsTerminalContribution.ts Outdated
- Add _activeSessionId check to the post-await guard in
  _onActiveSessionChanged so stale calls are caught even when two
  sessions share the same cwd/targetKey.
- Update _findTerminalsForKey JSDoc from 'first' to 'all' to match
  the actual behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meganrogge meganrogge marked this pull request as draft June 15, 2026 20:24
- Transfer terminal tracking on onDidReplaceSession (untitled→committed
  graduation) so terminals survive session replacement instead of being
  orphaned and killed.
- Clean up stale map entries when terminals are externally disposed via
  onDidDisposeInstance listener.
- Prune stale entries in _isTerminalTracked to avoid incorrectly
  excluding disposed terminals from cwd fallback matching.
- Use cwd fallback for untracked terminals even when tracked terminals
  exist, so restored terminals from previous windows are shown alongside
  the session's tracked terminals.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meganrogge meganrogge requested a review from connor4312 June 15, 2026 20:29
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
this._register(this._terminalService.onDidDisposeInstance(instance => {
this._removeTerminalFromTrackedSessions(instance.instanceId);
}));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Very nice. I look forward to testing this!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks like a solid fix! With CWD backwards compatible. If there's nothing funny with session lifecycle and _sessionsManagementService.onDidChangeSessions doesn't emit a e.removed for "whatever reason" upstream my problems will hopefully be over!!!

Thank you!

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.

Better associate terminal with sessions

4 participants