Skip to content

chat: open Copilot CLI sessions from product protocol links - #342120

Merged
Karthik Nadig (karthiknadig) merged 8 commits into
mainfrom
karthiknadig/copilot-cli-code-deeplink
Nov 3, 2026
Merged

Karthik Nadig (karthiknadig) merged 8 commits into
mainfrom
karthiknadig/copilot-cli-code-deeplink

Conversation

@karthiknadig

@karthiknadig Karthik Nadig (karthiknadig) commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Part of microsoft/vscode-internalbacklog#10107 (related: #310621). Companion to github/copilot-agent-runtime#25941 (merged).

What

The Copilot CLI has /code and /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:

vscode://file/<absolute-folder>?windowId=_blank&copilotCliSession=<cli-session-id>
vscode-insiders://file/<absolute-folder>?windowId=_blank&copilotCliSession=<cli-session-id>

/code-insiders needs nothing extra on the VS Code side, because Insiders registers its own vscode-insiders protocol.

Versions. This PR doesn't need a newer @github/copilot-sdk: /code runs in the user's CLI, and the agent host uses SDK calls the pinned version already has (sessions.checkInUse, model.getCurrent, agent.getCurrent). /code itself first ships in the CLI prerelease after 1.0.106-6; no published CLI has it yet. Until then, testing needs a CLI built from the runtime's main.

Link handling (commits 1 and 5)

  • New copilotCliSession=<id> link parameter, accepted on file and vscode-remote links. 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).
  • Cold start. When the link is what starts VS Code, the chat session parameter (session and the new copilotCliSession) was dropped: resolveInitialProtocolUrls/openFirstWindow opened the folder but never sent the session to the window. It is now carried in IInitialProtocolUrls.chatSessionToOpen. This also fixes the existing session= parameter on cold start.
  • The window that opened the link's path gets the session. The chat session goes into IOpenConfiguration.chatSessionToOpen, and WindowsMainService.open sends 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.
  • Unchanged. The "An external application wants to open…" prompt, workspace trust, and window selection all stay as they are. With 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)

CopilotCliSessionLinkOpener always 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.

  • The window waits for the ambient agent host's handshake (on a cold start, canResolveChatSession waits for the agent host to register Copilot), calls importSession(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.
  • If another process holds the session, the user is asked whether to open it anyway.
  • An error notification when AI features are disabled, the agent host isn't available or doesn't offer Copilot, the host can't import sessions, or the import fails. When the host can't find the session (for example, it was deleted, or another app created it), the notification says so in plain terms instead of showing the host's internal reason; the raw error is logged. A new, empty chat is never shown.

Agent host changes that make the import work for a session handed over by the CLI:

  • Sessions discovery hasn't registered. importSession used to require a registered session, but discovery hides external sessions when chat.agentSessions.showExternal is none, and on a cold start it may not have scanned yet. A new optional IAgent.discoverChat(session) classifies just that session with the same eligibility rules as discovery (client github/cli or github/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's sessions.checkInUse and refuses with Conflict when another process holds the session, before anything resumes it. A CLI that runs /code launches 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 with takeOver (a new optional vscode/importSession parameter) if the user agrees. Without the check, both processes would append to the same events.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 example allow-all and plan). importChat now resumes the chat right away and CopilotAgentSession.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. importChat now returns the selection the runtime continues with (rpc.model.getCurrent, plus rpc.agent.getCurrent matched 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/importSession now resolves to the session's exact resource ({ session }, as vscode/moveSession does), 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.migrateLegacyCopilotCli is on (read at startup); otherwise opening one reports that it couldn't be found.

Related

How to test

  1. Run copilot in 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.
  2. Quit VS Code, then run /code in the CLI (or open vscode://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.
  3. Repeat with VS Code already running, both with that folder open (the window is focused) and without it (a new window opens and existing windows are untouched). The result doesn't depend on chat.defaultToCopilotHarness.
  4. Resume the session in another terminal with 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.
  5. Set "chat.disableAIFeatures": true and open the link. An error notification appears and no chat opens.
  6. Set "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 main at 73db368 (which moved the pinned @github/copilot-sdk to 1.0.29-preview.1 and Electron to 43.8.6), and run with Node 24.22.1 after pnpm run install-fast and pnpm run build-fast --client-only. The rebase conflicts were only imports and a neighbouring method that main added next to this PR's changes (refreshSandboxCertificate, isAgentChatDefaultChat). An earlier rebase picked up a _resolveAgentSelection helper that main added to copilotAgent.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-sdk 1.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.
  • Manual, dev build (earlier rebase): a session created by a Copilot CLI built from github/copilot-agent-runtime#25941, opened through 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.
  • End to end, recorded (earlier rebase): Copilot CLI built from github/copilot-agent-runtime#25941 in Windows Terminal: /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.
  • Not run: the manual and recorded end-to-end steps on this rebase, with the merged View is flickering when scrolling #25941, or with the wait for the CLI's lock (that run didn't hit the race: VS Code checked about 2.4 s after the CLI began exiting).

Copilot AI balanced review requested due to automatic review settings October 16, 2026 17:26

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

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 copilotCliSession link 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.

Comment thread src/vs/code/electron-main/app.ts Resolved
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Base: ae11fb02 Current: 3b48871a

No screenshot changes.

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]>
@karthiknadig
Karthik Nadig (karthiknadig) force-pushed the karthiknadig/copilot-cli-code-deeplink branch from 32cc260 to 8e5015d Compare November 3, 2026 17:39
@karthiknadig
Karthik Nadig (karthiknadig) marked this pull request as ready for review November 3, 2026 18:32
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Robo (@deepak1556)

Matched files:

  • src/vs/code/electron-main/app.ts

@karthiknadig
Karthik Nadig (karthiknadig) merged commit be79b94 into main Nov 3, 2026
55 checks passed
@karthiknadig
Karthik Nadig (karthiknadig) deleted the karthiknadig/copilot-cli-code-deeplink branch November 3, 2026 23:54
@vs-code-engineering vs-code-engineering Bot added this to the 1.146.0 milestone Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants