Track terminal-to-session association by session ID instead of cwd#321506
Track terminal-to-session association by session ID instead of cwd#321506meganrogge wants to merge 4 commits into
Conversation
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>
There was a problem hiding this comment.
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
- 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>
- 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>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| this._register(this._terminalService.onDidDisposeInstance(instance => { | ||
| this._removeTerminalFromTrackedSessions(instance.instanceId); | ||
| })); | ||
|
|
There was a problem hiding this comment.
Very nice. I look forward to testing this!
There was a problem hiding this comment.
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!
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
_sessionTerminalsmap to track which terminals belong to which sessionensureTerminalnow records associations and prefers tracked terminalsonDidReplaceSessionso terminals survive untitled→committed graduationonDidDisposeInstancewhen terminals are externally closed_isTerminalTrackedto avoid incorrect cwd fallback exclusionTests
transfers tracked terminals when a session is replaced (graduation)cleans up tracked terminal ids when terminals are externally disposeduntracked restored terminals are visible alongside tracked terminals for the same sessionFixes #312909