Repository navigation
chat: open Copilot CLI sessions from product protocol links - #342120
Merged
Karthik Nadig (karthiknadig) merged 8 commits intoNov 3, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Cold-start destination selection can drop requested sessions or send them to an unrelated restored window.
1 open finding
What changed in this PR
Adds product protocol links for opening existing Copilot CLI conversations in VS Code’s Chat view.
Changes:
- Parses and validates
copilotCliSessionlink parameters. - Carries chat-session requests through startup and window messaging.
- Opens sessions through the CLI provider, with unavailable-provider errors and unit tests.
| File | Description |
|---|---|
| src/vs/workbench/contrib/chat/test/browser/copilotCliSessionLink.test.ts | Tests session opening and unavailable-provider handling. |
| src/vs/workbench/contrib/chat/electron-browser/chat.contribution.ts | Handles CLI session messages. |
| src/vs/workbench/contrib/chat/browser/copilotCliSessionLink.ts | Validates and opens CLI sessions. |
| src/vs/platform/window/test/common/window.test.ts | Tests link parsing and ID validation. |
| src/vs/platform/window/common/window.ts | Defines session parameters and parsing helpers. |
| src/vs/platform/url/electron-main/url.ts | Carries startup session requests. |
| src/vs/code/electron-main/app.ts | Routes protocol sessions to windows. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Contributor
|
Base:
|
Karthik Nadig (karthiknadig)
force-pushed
the
karthiknadig/copilot-cli-code-deeplink
branch
from
October 17, 2026 05:20
42425ab to
e677679
Compare
3 tasks done
Karthik Nadig (karthiknadig)
force-pushed
the
karthiknadig/copilot-cli-code-deeplink
branch
4 times, most recently
from
November 2, 2026 16:54
9ebd573 to
32cc260
Compare
The Copilot CLI's `/code` and `/code-insiders` commands hand the current CLI session to VS Code with a link such as `vscode://file/<folder>?windowId=_blank&copilotCliSession=<id>`. Insiders needs no separate handling because it registers its own `vscode-insiders` protocol. - Add a `copilotCliSession` link parameter carrying only the CLI session id. The window resolves it to the Copilot CLI chat session provider, so the CLI does not depend on a provider-specific URI. - Open the requested chat session when the link starts VS Code (cold start). The `session` parameter was previously dropped on that path. - When Copilot CLI sessions are unavailable, show an error instead of a new, empty chat. Co-authored-by: Copilot <[email protected]>
When new chats default to the Copilot harness (or the Copilot CLI provider of the Copilot extension is unavailable), a copilotCliSession link imports the CLI session into the agent host and opens it in the Chat view, so the chat continues in the same runtime with the model, effort and modes the CLI left it in. Other cases keep opening the session through the extension provider. - importSession registers a session that discovery has not registered yet, for example one hidden by showExternalSessions or created moments ago, by asking its provider to classify that one session (IAgent.discoverChat). - Before adopting, the provider takes over the default chat (IAgent.importChat). The Copilot provider refuses with a Conflict error when another process holds the session, so two clients never write the same history, then resumes the chat and adopts the permission and agent modes its runtime restored, limited by managed policy, instead of replacing them with the host's defaults on the next turn. Co-authored-by: Copilot <[email protected]>
… over Follow-ups from the Copilot CLI review of the /code handoff: - Same model, effort, context tier and custom agent. Each turn carries the model and agent the client picked, seeded from the chat's draft, and the host applies them before the turn. An imported chat's picker was seeded from the rebuilt history, which records the model without its effort or tier and no custom agent, so the first turn reset the effort and deselected the agent the runtime had restored. IAgent.importChat now returns the selection the runtime continues with (Copilot: rpc.model.getCurrent and rpc.agent.getCurrent, with the agent matched by name to one this host offers), and the host makes it the chat's draft. Values equal to a managed default are still dropped when the turn applies them, so policy decides those. - Taking over a held session. The runtime's in-use record is best effort, and the CLI lets users take over a session when it reports one. Import now takes a takeOver option (vscode/importSession `takeOver`), and the /code link opener asks before retrying with it when the import reports Conflict. Co-authored-by: Copilot <[email protected]>
The Copilot CLI's /code and /code-insiders hand a session over to Copilot in the agent host. Drop the fallback to the Copilot extension's Copilot CLI chat session provider, and with it the check for whether new chats default to the agent host: a copilotCliSession link always imports the session into the agent host, and reports an error when the agent host can't open it. Co-authored-by: Copilot <[email protected]>
A protocol link's chat session (`session` or `copilotCliSession`) was sent to a window picked from the windows `open` returned. On startup that list also holds windows restored from the previous session (`window.restoreWindows: preserve`): a link whose path doesn't exist sent the session to a single restored window, a folder link with a trailing slash didn't match its normalized window, and a file link never matched. `IOpenConfiguration` now carries the chat session, and `WindowsMainService` sends it to the window that opened the requested path, matched on the resolved path (or the window the files opened in). Nothing is sent when the path didn't open. Co-authored-by: Copilot <[email protected]>
…orts The agent host admits a Copilot CLI session's backing (copilotcli:/<id>) under its own address, ahp-session:/<id> for a session it hasn't seen, and lists it under that address. The link opener registered the Chat resource for the backing instead, which conflicts with the listed session's identity, so the session never opened. vscode/importSession now resolves to the session's exact resource, as vscode/moveSession does, and the opener opens that resource. A host that doesn't report it keeps the session at the address it was imported from. Co-authored-by: Copilot <[email protected]>
When the agent host can't find or adopt a /code session (for example, it was deleted, or another app created it), the notification showed the host's internal reason, such as "not an adoptable legacy chat (reason=...)". Show a plain, localized message instead. The raw error is still logged. Co-authored-by: Copilot <[email protected]>
The Copilot CLI launches the /code link before it exits, and releases the session's in-use lock only during its teardown, after it has finished writing to the session. If VS Code checked the lock first, importing refused the session and the user was asked to take over a session the CLI was about to release. When the session is in use, importing now checks again every 250ms for up to 5s before refusing. Taking over still doesn't wait. Co-authored-by: Copilot <[email protected]>
Karthik Nadig (karthiknadig)
force-pushed
the
karthiknadig/copilot-cli-code-deeplink
branch
from
November 3, 2026 17:39
32cc260 to
8e5015d
Compare
Karthik Nadig (karthiknadig)
marked this pull request as ready for review
November 3, 2026 18:32
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: Robo (@deepak1556)Matched files:
|
Bhavya U (bhavyaus)
approved these changes
Nov 3, 2026
Karthik Nadig (karthiknadig)
deleted the
karthiknadig/copilot-cli-code-deeplink
branch
November 3, 2026 23:54
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.

Part of microsoft/vscode-internalbacklog#10107 (related: #310621). Companion to github/copilot-agent-runtime#25941 (merged).
What
The Copilot CLI has
/codeand/code-insiders(github/copilot-agent-runtime#25941, merged and experimental). They hand the current CLI session to VS Code, so the session opens in the editor window's Chat view with its history. The CLI saves the session, releases its in-use lock, and opens a link of this form:/code-insidersneeds nothing extra on the VS Code side, because Insiders registers its ownvscode-insidersprotocol.Versions. This PR doesn't need a newer
@github/copilot-sdk:/coderuns in the user's CLI, and the agent host uses SDK calls the pinned version already has (sessions.checkInUse,model.getCurrent,agent.getCurrent)./codeitself first ships in the CLI prerelease after1.0.106-6; no published CLI has it yet. Until then, testing needs a CLI built from the runtime'smain.Link handling (commits 1 and 5)
copilotCliSession=<id>link parameter, accepted onfileandvscode-remotelinks. It carries only the CLI session id, so the CLI doesn't depend on a provider-specific URI. The id is checked against the CLI runtime's rule (ASCII letters, digits,_,-) and capped at 256 characters. The main process sends the id to the target window (vscode:openCopilotCliSession).sessionand the newcopilotCliSession) was dropped:resolveInitialProtocolUrls/openFirstWindowopened the folder but never sent the session to the window. It is now carried inIInitialProtocolUrls.chatSessionToOpen. This also fixes the existingsession=parameter on cold start.IOpenConfiguration.chatSessionToOpen, andWindowsMainService.opensends it to the window that opened the requested path, for links on startup and while VS Code runs. It matches on the resolved (normalized) path, or on the window the files opened in for a file link. Windows restored next to it (window.restoreWindows: preserve) never get the session, and nothing is sent when the path doesn't exist.windowId=_blank, a window that already has the folder open is focused, and other windows are never retargeted.Opening the session in the Copilot agent host (commits 2–4)
CopilotCliSessionLinkOpeneralways opens the session in Copilot in the agent host (the Copilot harness), whatever new chats default to. The Copilot extension's Copilot CLI chat session provider isn't used.canResolveChatSessionwaits for the agent host to register Copilot), callsimportSession(copilotcli:/<id>), and opens the session the import reports in the Chat view. The chat continues in the same runtime, so the runtime restores the session's model, reasoning effort, context tier, custom agent, agent mode and permission mode.Agent host changes that make the import work for a session handed over by the CLI:
Sessions discovery hasn't registered.
importSessionused to require a registered session, but discovery hides external sessions whenchat.agentSessions.showExternalisnone, and on a cold start it may not have scanned yet. A new optionalIAgent.discoverChat(session)classifies just that session with the same eligibility rules as discovery (clientgithub/cliorgithub/autopilot, recently modified, has a working directory). The Copilot provider runs an explicit single-session scan that skips the startup discovery telemetry.One writer per session. A new optional
IAgent.importChat(chat, context, options)runs after restore and before adoption. The Copilot provider calls the SDK'ssessions.checkInUseand refuses withConflictwhen another process holds the session, before anything resumes it. A CLI that runs/codelaunches the link first and releases its lock while it exits, after its last write to the session, so before refusing, the provider checks again every 250 ms for up to 5 s. The session then stays external rather than being adopted. The in-use record is best effort, so, like the CLI, the link opener asks whether to take the session over and retries withtakeOver(a new optionalvscode/importSessionparameter) if the user agrees. Without the check, both processes would append to the sameevents.jsonl.Same permissions and mode. Before this change, the first turn pushed the host's configured approval level and agent mode into the runtime (
syncPermissionMode('turn-start'),applyMode), replacing what the CLI had (for exampleallow-allandplan).importChatnow resumes the chat right away andCopilotAgentSession.adoptRuntimeModes()writes the runtime's restored permission mode and agent mode into the chat's configuration. The Chat input shows them before anything is sent, and the next turn keeps them. A permission mode that managed policy disallows falls back exactly as a configured one would, so policy can lower a level but never raise it. If resuming fails, the import still succeeds and the chat keeps the host's modes, with a warning in the log.Same model, effort, context tier and custom agent. Each turn carries the model and agent the user picked, seeded from the chat's draft, and the host applies them before the turn. History rebuilt from the CLI's events records the model without its effort or tier, and no custom agent, so the first turn reset the effort and deselected the agent the runtime had restored.
importChatnow returns the selection the runtime continues with (rpc.model.getCurrent, plusrpc.agent.getCurrentmatched by name to an agent this host offers), and the host makes it the chat's draft. Values that equal a managed default are still dropped when the turn applies them, so VS Code's policy decides those. A custom agent VS Code doesn't offer, or a model it doesn't list (for example a BYOK model), falls back to VS Code's default.The session's own address. The agent host admits the CLI session's backing (
copilotcli:/<id>) under its own address,ahp-session:/<id>for a session it hasn't seen, and lists it there.vscode/importSessionnow resolves to the session's exact resource ({ session }, asvscode/moveSessiondoes), and the opener opens that resource instead of deriving one from the backing, which would conflict with the listed session. A host that doesn't report it keeps the session at the address it was imported from.These also apply to the Agents window's existing Import action, which goes through the same
importSession.Not covered: a session the Copilot extension's Copilot CLI provider created. The agent host imports those only when
chat.agentSessions.migrateLegacyCopilotCliis on (read at startup); otherwise opening one reports that it couldn't be found.Related
/codedoesn't depend on it.lastClientNameon the SDK'sSessionMetadata). The persistedclientNameis only the client that created it. With that, the agent host can adopt the runtime's modes whenever another client ran a session last, not only on an explicit import. That's a follow-up: no published@github/copilot-sdkincludes it yet (the newest,1.0.29-preview.5, was built before it merged)./code#342466: test plan item.How to test
copilotin a folder in an external terminal, using a CLI that includes github/copilot-agent-runtime#25941, and turn on experimental mode (/experimental). Pick a model and a reasoning effort other than the defaults, select a custom agent that VS Code also offers (for example one in.github/agents), switch to plan mode (Shift+Tab) and allow all tools (/allow-all), then send a message./codein the CLI (or openvscode://file/<folder>?windowId=_blank&copilotCliSession=<session-id>after exiting the CLI session). The folder opens and the session appears in Chat with its history, in Plan mode with Allow all approvals, with the CLI's model, effort and agent selected. Send a message: the CLI's model, effort and agent are still in effect.chat.defaultToCopilotHarness.copilot --resume <session-id>, then open the link. VS Code asks whether to open the session anyway. Cancel leaves it untouched; Open Anyway opens it."chat.disableAIFeatures": trueand open the link. An error notification appears and no chat opens."window.restoreWindows": "preserve", open another folder, and quit. Open the link: the session opens only in the link's folder window. Then quit and open a link to a folder that doesn't exist: VS Code reports the missing path, and no restored window opens a chat.The full scenarios are in the test plan item, #342466.
Validation
Rebased onto
mainat 73db368 (which moved the pinned@github/copilot-sdkto1.0.29-preview.1and Electron to 43.8.6), and run with Node 24.22.1 afterpnpm run install-fastandpnpm run build-fast --client-only. The rebase conflicts were only imports and a neighbouring method thatmainadded next to this PR's changes (refreshSandboxCertificate,isAgentChatDefaultChat). An earlier rebase picked up a_resolveAgentSelectionhelper thatmainadded tocopilotAgent.ts, which has the same name as this PR's lookup by agent name; this PR's helper is now_findAgentSelectionByName.pnpm run typecheck-client: passed, against@github/copilot-sdk1.0.29-preview.1.scripts\test.bat --run src/vs/platform/windows/test/electron-main/windowsFinder.test.ts --run src/vs/platform/window/test/common/window.test.ts --run src/vs/workbench/contrib/chat/test/browser/copilotCliSessionLink.test.ts --run src/vs/platform/agentHost/test/electron-browser/agentHostProtocolClient.test.ts: passed (421 passing).scripts\test.bat --run src/vs/platform/agentHost/test/node/agentService.test.ts --run src/vs/platform/agentHost/test/node/copilotAgent.test.ts --run src/vs/platform/agentHost/test/node/copilotAgentSession.test.ts --run src/vs/platform/agentHost/test/node/copilotChatDiscovery.test.ts --run src/vs/platform/agentHost/test/node/protocolServerHandler.test.ts --run src/vs/platform/agentHost/test/node/copilotSessionLauncher.test.ts: passed (4598 passing, 17 pending).pnpm run eslint -- <changed files>(26 files): passed (no problems).pnpm run valid-layers-check: passed.vscode://file/<folder>?windowId=_blank&copilotCliSession=<id>(the OS protocol handler pointed at the dev build), imports and opens in the Chat view with its history./model, Plan mode, a planning turn, then/code. The dev build opened the same session in the Chat view with the conversation, model, and Interactive mode with manual permissions.