Skip to content

agentHost: Improve perf by keeping pre-warmed Copilot sessions usable and initialize their tools before the first send - #349153

Merged
Vijay Upadya (vijayupadya) merged 4 commits into
mainfrom
uniform-tyrannosaurus
Nov 19, 2026
Merged

Vijay Upadya (vijayupadya) merged 4 commits into
mainfrom
uniform-tyrannosaurus

Conversation

@vijayupadya

Copy link
Copy Markdown
Contributor

Makes the first message of a new Copilot agent host chat faster when it starts from a pre-warmed session. A pre-warmed session was often thrown away at send time, and when it was used, the runtime still built its tool catalog and reloaded MCP on the first turn.

Changes3

Re-warm while composing instead of discarding at send

  • The editor window syncs the untitled draft's composer (text, model, custom agent) to the draft's default chat as a debounced chat/draftChanged (ProvisionalDraftSync). It starts from the draft's launch selection, so an untouched draft sends nothing. Clearing the text or going back to the built-in agent is still sent.
  • AgentHostSessionPrewarmer follows these changes and calls warmChat again with the composer's selection (IAgentWarmChatSelection).
  • CopilotAgent._warmChat keeps a launch that still matches and restarts its 5-minute lifetime. It replaces a launch made with another model or agent, one whose workspace trust changed, or one whose draft-scoped customizations changed (in-memory state only, so composing never waits on a sync). It also launches again after expiry or teardown, but not after warmFailed.
  • Before, a model picked or changed after the draft was created made the first send discard the warm launch (modelSelected / modelChanged) and start cold. Now that send uses the re-warmed launch.
  • The draft's backend session is still never recreated on a model change, so session-scoped choices such as a disabled MCP server are kept.

Initialize tools while pre-warming

  • CopilotAgentSession.warmUp replaces startMcpServers. It runs session.mcp.list(), waits for the launch-time Connector reconciliation, and then calls session.tools.initializeAndValidate(). The runtime caches the tool catalog (including cloud memory preparation) before the user sends, and the first turn reuses it.
  • Warm-up runs outside the chat queue. It stops before initializing tools when the launch is claimed, discarded or torn down (warmUpToken), or when a turn has started.

Correctness

  • ChatDraftContribution doesn't persist drafts of idle provisional sessions, like ChatConfigContribution. These sessions have no database before their first send and are never restored, so composing no longer creates session.db for a draft.
  • _warmChat checks again for shutdown after each await, so a launch that finishes during shutdown doesn't leak its expiry timer.

Measurements

Local Windows runs of a development build, main at 53fa24195c2 against this change, with the same signed-in profile and settings (chat.agentHost.experimental.prewarmSessions: true, OTel file export). These are real model requests in the vscode repository with a one-word prompt, interleaved scenarios, and medians in ms. The phases come from the Copilot runtime's session.first_turn / session.timing.* spans and the Agent Host turn_timing span. Each scenario opens a new chat, waits 30 s so the draft is pre-warmed, then sends.

New chat, model unchanged (pre-warmed session used in both builds, n=3)

Runtime phase main This PR
tool_descriptors 924 12 (−99%)
mcp_initialization 371 0
turn_preparation 1,293 87 (−93%)
Send → first model request 1,517 463 (−69%)

New chat, model changed in the picker before sending (n=3)

main This PR
Pre-warmed session at send discarded (modelChanged), 3/3 re-warmed and used, 3/3
Cold launch on the send path (create + finalize + refresh) 1,526 7
Send → first model request 1,969 582 (−70%)

Send → first model request is the provider dispatch plus the runtime's firstModelCallStartOffsetMs. It doesn't include the turn-start checkpoint, which #349064 moves off the send path. In these runs the checkpoint also varied with the repository's uncommitted state, which differed between the two builds. The model's own time to first token (1–8 s here) dominates end-to-end time to first progress and varied too much at this sample size to compare.

Testing

  • pnpm run typecheck-client: passed.
  • node build/oxlint.ts <changed files>: passed.
  • scripts\test.bat --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/agentService.test.ts --run src/vs/platform/agentHost/test/node/chatContributions.test.ts --run src/vs/workbench/contrib/chat/test/browser/agentSessions/agentHostUntitledProvisionalSessionService.test.ts: 4,585 passing, 16 pending.
  • New tests cover:
    • re-warming with another model, and extending a matching launch;
    • initializing tools after MCP listing, and stopping warm-up when the session is claimed mid-start;
    • composer sync (an untouched draft sends nothing, and clearing the text is still sent);
    • not persisting drafts of idle provisional sessions.
  • Live: both builds were driven through the scenarios above with Playwright, and the agent host log was checked for the expected [warm] discard, re-warm and claim sequence.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 21:02

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

The warm-chat check still performs uncancellable database and RPC I/O while holding the chat queue, so background revalidation can delay a Send even when only draft text changed.

1 open issue
What changed in this PR

This PR aims to reduce first-message latency in Copilot Agent Host chats by keeping pre-warmed sessions aligned with the composer and preparing their tools before Send.

Changes:

  • Syncs draft text, model, and custom-agent selections so matching warm sessions stay alive and stale ones are replaced.
  • Initializes tools after MCP startup and Connector reconciliation, with cancellation checks before tool initialization.
  • Keeps idle provisional drafts in memory and adds tests for composer sync, re-warming, expiry, and warm-up cancellation.
File Description
src/​vs/​workbench/​contrib/​chat/​test/​browser/​agentSessions/​agentHostUntitledProvisionalSessionService.test.ts Tests composer synchronization and draft clearing.
src/​vs/​workbench/​contrib/​chat/​browser/​agentSessions/​agentHost/​agentHostUntitledProvisionalSessionService.ts Adds debounced composer sync and handoff cleanup.
src/​vs/​platform/​agentHost/​test/​node/​copilotAgent.test.ts Tests tool warm-up, cancellation, model replacement, and expiry.
src/​vs/​platform/​agentHost/​test/​node/​agentService.test.ts Tests composer-driven re-warming and memory-only drafts.
src/​vs/​platform/​agentHost/​node/​copilot/​copilotAgentSession.ts Initializes tools after MCP startup and Connector reconciliation.
src/​vs/​platform/​agentHost/​node/​copilot/​copilotAgent.ts Revalidates, extends, or replaces warm launches and manages warm-up cancellation.
src/​vs/​platform/​agentHost/​node/​chatContributions/​chatDraft/​chatDraftContribution.ts Skips persistence for idle provisional drafts.
src/​vs/​platform/​agentHost/​node/​agentHostSessionPrewarmer.ts Tracks draft changes and coalesces gated re-warm requests.
src/​vs/​platform/​agentHost/​common/​agent.ts Extends the warm-chat contract with composer selections.
src/​vs/​platform/​agentHost/​AGENTS.md Documents tool preparation and composer-driven pre-warming.

Review effort: Balanced

@vijayupadya
Vijay Upadya (vijayupadya) marked this pull request as ready for review November 18, 2026 23:14
# Conflicts:
#	src/vs/platform/agentHost/node/copilot/copilotAgent.ts
@vijayupadya
Vijay Upadya (vijayupadya) added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit ea78600 Nov 19, 2026
37 checks passed
@vijayupadya
Vijay Upadya (vijayupadya) deleted the uniform-tyrannosaurus branch November 19, 2026 00:18
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.

3 participants