Skip to content

Copilot CLI sessions: restore the session's model and don't resume sessions in use elsewhere - #342230

Closed
Karthik Nadig (karthiknadig) wants to merge 2 commits into
mainfrom
karthiknadig/copilotcli-session-model-and-lock
Closed

Karthik Nadig (karthiknadig) wants to merge 2 commits into
mainfrom
karthiknadig/copilotcli-session-model-and-lock

Conversation

@karthiknadig

Copy link
Copy Markdown
Member

Part of microsoft/vscode-internalbacklog#10107. Split out of #342120, which is about routing /code links.

Two fixes to the Copilot extension's Copilot CLI chat session provider (copilotcli):

  • Restore a session's model when it opens. Chat history reported each request's model by its bare Copilot CLI model id (for example claude-sonnet-4.5). VS Code looks up a session's model by its full language model identifier (copilotcli/claude-sonnet-4.5), so opening a Copilot CLI session never restored its model: the picker fell back to a default, and the next request switched the session to it. History now reports the full identifier. Requests made with Auto stay on Auto.
  • Don't resume a session that another process has open. Resuming a session appends to its event log, so a session that is open in the Copilot CLI in a terminal, or running a request in another VS Code window, must not also be resumed here. The session service checks the session's in-use locks before resuming, and again right after, in case another process opened it in between. The request then fails with "This session is open in Copilot CLI or another VS Code window. Close it there or wait for it to finish, then try again." VS Code holds a session's lock only while a request runs.

How to test

  1. In a terminal, run copilot, pick a model other than the default, and send a message. Exit the CLI.
  2. Open the session from the Chat sessions list in VS Code. The model picker shows the session's model.
  3. Run copilot --resume=<session-id> in a terminal and leave it open. Send a message to the same session in VS Code. The request fails with the message above, and nothing is written to the session.
  4. Exit the CLI and send the message again. It runs.

Validation

  • npx tsc --noEmit --project tsconfig.json in extensions/copilot: passed.
  • npx vitest run src/extension/chatSessions/copilotcli src/extension/chatSessions/vscode-node in extensions/copilot: 1277 passed, 1 failed. The failure ("preserves order of edit toolCallIds and permissions for multiple pending edits") also fails on main without these changes.
  • npx eslint --max-warnings=0 on the changed files: passed.
  • Not tested in a running VS Code yet.

Chat history reported the bare Copilot CLI model id, but VS Code looks up a session's model by its full language model identifier (copilotcli/<id>). Opening a Copilot CLI session therefore never restored its model: the picker fell back to a default, and the first request switched the session to that model. This matters for sessions handed off from the Copilot CLI with /code.

Co-authored-by: Copilot <[email protected]>
Resuming a session appends to its event log, so a Copilot CLI session that is open in a terminal, or running a request in another VS Code window, must not also be resumed here. Check the session's in-use locks before resuming, and again after resuming in case another process opened it in between. When the session is in use, the request fails with a message that says where it is open instead of writing to the session.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 17, 2026 04:40
@github-actions

Copy link
Copy Markdown
Contributor

Screenshot Changes

Base: 1e81fc75 Current: ae13e46e

Changed (1)

sessions/chatCompositeBar/OverflowingTabs/Light
Before After
before after

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.

🟡 Changes recommended

Two history-reconstruction paths still replace Auto selection with a fixed model.

2 open issues
What changed in this PR

Improves Copilot CLI session reopening in VS Code by restoring model identifiers and rejecting resumes when another process holds the session.

Changes:

  • Reports fully qualified model identifiers in chat history.
  • Checks session locks before and after resuming and surfaces a specific error.
  • Adds model-restoration and session-lock regression coverage.
File Description
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​copilotCLIChatSessionsContribution.ts Displays session-in-use errors.
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​copilotCLIChatSessions.ts Handles session-in-use errors.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​node/​test/​testHelpers.ts Adds mock session-lock state.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​node/​test/​copilotCliSessionService.spec.ts Tests rejection of busy sessions.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​node/​copilotcliSessionService.ts Guards session resumption with lock checks.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​node/​copilotCli.ts Reuses the model vendor constant.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​common/​utils.ts Defines qualified model identifiers.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​common/​test/​copilotCLITools.spec.ts Updates model-history assertions and coverage.
extensions/​copilot/​src/​extension/​chatSessions/​copilotcli/​common/​copilotCLITools.ts Emits qualified history model identifiers.

Review effort: Balanced

const turn = turns[currentRequestTurnIndex];
if (turn instanceof ChatRequestTurn2 && turn.modelId !== modelId) {
turns[currentRequestTurnIndex] = new ChatRequestTurn2(turn.prompt, turn.command, turn.references, turn.participant, [...turn.toolReferences], turn.editedFileEvents, turn.id, modelId, turn.modeInstructions2);
const modelIdentifier = getCopilotCLIModelIdentifier(modelId);
currentResponseModelId = resolvedRequestModelId;
currentCreditsUsed = details?.creditsUsed;
turns.push(new ChatRequestTurn2(`${commandPrefix}${prompt}`, undefined, references, '', [], undefined, details?.requestId ?? event.id, details?.isUsingAutoModel ? 'auto' : resolvedRequestModelId, modeInstructions2));
const requestModelId = details?.isUsingAutoModel ? 'auto' : resolvedRequestModelId;
@karthiknadig

Copy link
Copy Markdown
Member Author

Closing: the Copilot CLI's /code handoff now opens sessions only in Copilot through the agent host (#342120), so it no longer depends on the Copilot extension's Copilot CLI provider.

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.

2 participants