Skip to content

agentHost: speed up new chat session startup - #349064

Merged
Connor Peet (connor4312) merged 22 commits into
mainfrom
agent-host/session-startup-perf
Nov 18, 2026
Merged

Connor Peet (connor4312) merged 22 commits into
mainfrom
agent-host/session-startup-perf

Conversation

@connor4312

@connor4312 Connor Peet (connor4312) commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Speeds up starting a new chat session in the agent host. I timed where the delay goes, and each commit fixes one source of it.

Changes

Turn-start checkpoint no longer blocks the first message

  • The before-tools git snapshot used to be awaited before a turn was sent to the provider. In large repositories, the remaining git status and working-tree capture took up to several hundred ms on the critical path.
  • Providers can now opt in with IAgentChats.awaitsTurnStartCheckpoint. Copilot sends the turn immediately and awaits the pending capture in its onPreToolUse hook, so no tool runs before the snapshot exists. The capture now overlaps the model's time to first token.
  • If the capture fails, every tool call in that turn is denied with the reason.
  • Providers that don't opt in still wait before dispatch. So do sessions whose managed policy uses allowManagedHooksOnly, because then the runtime drops SDK hook callbacks.

Git work when creating a session and sending the first message

  • Folder isolation doesn't need the default branch, so that lookup is skipped. HEAD is read by name only, and overlapping repository-root probes for the same folder share one git call.
  • A redundant baseline-checkpoint lookup is removed.
  • The turn-start checkpoint now starts as soon as the folder is known. The baseline commit and ref are written after the snapshot, in the same per-session queue.
  • origin/HEAD is read at the same time as the remote list.
  • Assisted-approval repository context (git remote -v) is loaded only when the session uses Assisted mode.

Agent host process startup

  • The management channel is registered before the local endpoint is set up. Before this, the renderer's first plugin and marketplace calls hit the 1 s unknown-channel timeout in every fresh window.
  • Claude's JSON-schema-to-Zod adapter is loaded only when Claude tools are built, so Zod isn't loaded at startup.
  • On Windows, the endpoint metadata directories are secured once per startup instead of twice, and the user SID is looked up once. This runs 5 whoami/icacls processes instead of 12.

Renderer

  • Agent host customizations are recomputed only when a session-state field they depend on changes, not on status or title updates.
  • Repeated customization-migration notice checks are combined into one.
  • KaTeX is loaded only for markdown that may contain math.
  • The input pickers stop compacting once they fit.

Temporary

  • Sessions pass lifecycleDiagnostics: { persist: false }. The runtime bundled with SDK v2 otherwise runs an uncached Team-repository git probe on every session create, which costs about 90 ms in folders that aren't git repositories. This is fixed upstream, and a TODO marks the option for removal on the next SDK bump.

Measurements

Local Windows runs on SDK v2: baseline main at 1d20abca144 against this branch at da4a7f5cce7. These use a development build with model responses replayed locally, interleaved runs, and medians in ms. Both builds use the lifecycle-diagnostics opt-out, so the ~90 ms Team probe described above is excluded from both and the comparison measures only this branch. A cold session is the first one after the agent host process starts; a warm one is a later new session in the same process.

Scenario Cold Warm
Create → first text, repo, with hooks 9,062 → 9,171 (+1%) 2,811 → 1,580 (−44%)
Create → first text, repo, without hooks 2,913 → 1,952 (−33%) 2,532 → 1,241 (−51%)
Create → first text, empty folder 1,531 → 1,574 (+3%) 826 → 712 (−14%)
Workbench: Send → visible text 11,432 → 10,094 (−12%) 2,676 → 1,505 (−44%)

The replayed model answers in about 15 ms, which hides the checkpoint overlap. With a simulated 1.5 s delay before the model's first token, and that delay subtracted:

Scenario, 1.5 s simulated model delay Cold Warm
Send → first text, repo, without hooks 1,929 → 1,079 (−44%) 1,686 → 814 (−52%)
Workbench: Send → visible text 11,298 → 10,106 (−11%) 2,484 → 1,396 (−44%)

On Windows, the cold first session with hooks is dominated by command hooks, mainly a PowerShell launch stall fixed in github/copilot-agent-runtime#43900. That fix will arrive with a future runtime update.

Reviewer notes

  • The turn-start checkpoint is now awaited at the first tool call. Command hooks that run before the model, such as SessionStart and UserPromptSubmit, may now overlap the snapshot. Code comments note this.
  • If the managed allowManagedHooksOnly policy is only seen after a turn was sent, that turn's tools don't wait for the checkpoint. Once seen, the session waits before dispatch.
  • AgentHostCheckpointService.captureTurnStartCheckpoint resolves once the trees are captured. The baseline write still runs in the per-session queue, so turn end, discard and later captures wait for it.
  • In the Windows endpoint setup, the server now starts listening before the metadata directory is secured. The directory is still secured before any metadata file is written.

Testing

  • pnpm run typecheck-client: passed.
  • Targeted unit tests (scripts\test.bat --run …) for every touched area passed. The checkpoint change was covered by the agent side effects, Copilot agent session, Copilot agent, session launcher and checkpoint service suites.
  • The one known failure, ChatInputPickerResponsiveLayout "lays out once per frame…", also fails on main.

The caller already checked for an existing baseline, so avoid a second git process on the first turn.

Co-authored-by: Copilot <[email protected]>
Overlap the turn-start checkpoint snapshot with base-branch persistence. Both still settle before the message is dispatched. Warm new-session send to first text: ~1.9s -> ~1.47s in a git repo.

Co-authored-by: Copilot <[email protected]>
Folder isolation without a pull request does not need the default branch, saving several git processes in createSession (~800ms -> ~600ms).

Co-authored-by: Copilot <[email protected]>
… changes

Status/title updates during send no longer trigger customization migration and discovery recomputes.

Co-authored-by: Copilot <[email protected]>
Avoids loading the math renderer and re-rendering every response.

Co-authored-by: Copilot <[email protected]>
- Folder isolation only needs the HEAD branch name, so skip the full branch lookup.
- Share in-flight repository root probes for the same folder instead of queueing duplicates.

Warm repo createSession ~610ms -> ~470ms; empty folder ~345ms -> ~260ms.

Co-authored-by: Copilot <[email protected]>
… mode

Skips a git remote read on send for sessions that are not using Assisted permissions; switching to Assisted still applies it before any judge decision.

Co-authored-by: Copilot <[email protected]>
The renderer's first management calls (plugin inventory, marketplaces) no longer hit the 1s unknown-channel timeout in fresh windows.

Co-authored-by: Copilot <[email protected]>
Avoids loading Zod at agent host startup when Claude tools are not built (~120ms module load).

Co-authored-by: Copilot <[email protected]>
Publishing already secures the metadata directory before writing, so drop the duplicate preparation, and resolve the Windows user SID once per process. Startup now runs 5 instead of 12 whoami/icacls processes on Windows (endpoint stage ~880ms -> ~390ms).

Co-authored-by: Copilot <[email protected]>
- Resolve the turn-start checkpoint as soon as the before-tools working tree snapshot exists; the baseline commit/ref write continues in the same per-session queue, so turn end, discard and later captures still wait for it.
- Read origin/HEAD alongside the remote list when resolving the default branch.

Warm repo send to first text ~1.05-1.12s -> ~0.85-0.94s.

Co-authored-by: Copilot <[email protected]>
Defer the refresh by a task so the several context changes during a draft-to-session handoff trigger a single check after the turn is dispatched (cold send renderer time ~690ms -> ~530ms).

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 13:00

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

Incremental streaming renders each register a KaTeX completion callback, causing redundant full Markdown rerenders when loading finishes because callbacks from disposed render stores are not skipped.

1 open issue
What changed in this PR

This PR reduces new chat session startup latency across the agent host and renderer by removing redundant work, overlapping independent operations, and deferring optional dependencies.

Changes:

  • Streamlines Git discovery and overlaps turn-start snapshots with session preparation while preserving queued checkpoint writes.
  • Registers management channels earlier and defers Claude schema loading and Assisted-mode repository discovery.
  • Reduces customization refreshes, loads math support on demand, and stops picker compaction once controls fit.
File Description
src/​vs/​workbench/​contrib/​sessions/​test/​browser/​chat/​newChatMigrationNotice.test.ts Adapts tests to deferred notice checks.
src/​vs/​workbench/​contrib/​chat/​test/​browser/​widget/​input/​chatInputPickerResponsiveLayout.test.ts Updates expected layout measurements.
src/​vs/​workbench/​contrib/​chat/​test/​browser/​widget/​chatContentParts/​chatMarkdownContentPart.test.ts Tests conditional math loading.
src/​vs/​workbench/​contrib/​chat/​test/​browser/​aiCustomization/​chatCustomizationMigrationNotice.test.ts Tests coalesced discovery updates.
src/​vs/​workbench/​contrib/​chat/​test/​browser/​agentSessions/​workbenchAgentHostCustomizationService.test.ts Tests selective customization notifications.
src/​vs/​workbench/​contrib/​chat/​browser/​widget/​input/​chatInputPickerResponsiveLayout.ts Stops compaction when pickers fit.
src/​vs/​workbench/​contrib/​chat/​browser/​widget/​chatContentParts/​chatMarkdownContentPart.ts Loads KaTeX only for potential math.
src/​vs/​workbench/​contrib/​chat/​browser/​aiCustomization/​customizationMigrationServiceImpl.ts Reuses one marketplace configuration inspection.
src/​vs/​workbench/​contrib/​chat/​browser/​aiCustomization/​chatCustomizationMigrationNotice.ts Coalesces synchronous notice refreshes.
src/​vs/​workbench/​contrib/​chat/​browser/​agentSessions/​agentHost/​agentHostCustomizationService.ts Filters unrelated session-state changes.
src/​vs/​platform/​agentHost/​test/​node/​shared/​worktreeIsolation.test.ts Tests reduced folder-isolation Git reads.
src/​vs/​platform/​agentHost/​test/​node/​copilotAgentSession.test.ts Tests deferred Assisted-mode context discovery.
src/​vs/​platform/​agentHost/​test/​node/​agentSideEffects.test.ts Tests overlapping preparation and checkpoint capture.
src/​vs/​platform/​agentHost/​test/​node/​agentService.test.ts Verifies early folder checkpoint capture.
src/​vs/​platform/​agentHost/​test/​node/​agentHostGitService.test.ts Tests shared repository probes and retries.
src/​vs/​platform/​agentHost/​test/​node/​agentHostGitService.integrationTest.ts Covers optimized Git reads with real repositories.
src/​vs/​platform/​agentHost/​test/​node/​agentHostCheckpointService.test.ts Tests early snapshot completion and queued publication.
src/​vs/​platform/​agentHost/​node/​shared/​worktreeIsolation.ts Skips unnecessary branch details for folder isolation.
src/​vs/​platform/​agentHost/​node/​localAgentHostMetadata.ts Caches the Windows user SID lookup.
src/​vs/​platform/​agentHost/​node/​copilot/​copilotAgentSession.ts Defers repository context until Assisted mode needs it.
src/​vs/​platform/​agentHost/​node/​claude/​clientTools/​claudeClientToolMcpServer.ts Lazily imports client-tool schema conversion.
src/​vs/​platform/​agentHost/​node/​claude/​claudeServerToolMcpServer.ts Lazily imports server-tool schema conversion.
src/​vs/​platform/​agentHost/​node/​agentSideEffects.ts Starts checkpoints when working directories resolve.
src/​vs/​platform/​agentHost/​node/​agentService.ts Exposes resolved folders before base-branch persistence.
src/​vs/​platform/​agentHost/​node/​agentHostMain.ts Registers channels before endpoint setup and avoids duplicate directory preparation.
src/​vs/​platform/​agentHost/​node/​agentHostGitService.ts Shares repository probes and streamlines branch reads.
src/​vs/​platform/​agentHost/​node/​agentHostCheckpointService.ts Resolves after tree capture while queuing baseline writes.
src/​vs/​platform/​agentHost/​common/​agentHostGitService.ts Documents optimized Git lookup contracts.

Review effort: Balanced

Preserve SDK v2 integration and MCP diagnostics while retaining startup optimizations.

Co-authored-by: Copilot <[email protected]>
Refresh only the active markdown render when shared KaTeX loading completes.

Co-authored-by: Copilot <[email protected]>
@connor4312

Copy link
Copy Markdown
Member Author

Merged origin/main after #348894 landed (merge commit dc1a371), preserving SDK v2 and MCP diagnostics. The KaTeX review is fixed and resolved; CI formatting is corrected.

Validation:

  • pnpm run typecheck-client — passed.
  • pnpm run build-fast --client-only — passed.
  • scripts\test.bat --reporter dot with --run for all 11 branch-touched unit-test files — 3,857 passed; picker timeouts remain. The isolated picker rerun passed 22 tests and reproduced only the previously documented “lays out once per frame…” timeout.
  • scripts\test.bat --reporter dot --run src\vs\workbench\contrib\chat\test\browser\widget\chatContentParts\chatMarkdownContentPart.test.ts — 117 passed, including the new streaming regression.
  • pnpm run oxlint <all 28 changed TypeScript files> — passed; formatter verification of those files passed.
  • scripts\test-integration.bat --reporter dot --run src\vs\platform\agentHost\test\node\agentHostGitService.integrationTest.ts --grep "shares a negative result|name-only HEAD|getHEAD returns undefined for an unborn HEAD" — all four targeted Git regression cases passed. Full real-Git runs encountered Windows timeouts; the isolated cache-eviction case passed.

The full pnpm install hit Windows file-access errors in Copilot extension dependencies; pnpm install --filter . succeeded. Local full hygiene is blocked by its Windows Q:\Q:\... path error; the corrected formatting is now covered by passing CI hygiene.

Written by Connor's agent

@connor4312
Connor Peet (connor4312) marked this pull request as ready for review November 18, 2026 17:56
@connor4312

Copy link
Copy Markdown
Member Author

Required build/test CI is green, so this is now ready for review. The remaining pending VS Code PR Check is an approval gate, not a running build: it requires two collaborator approvals for the current unsigned head commit. All review threads are resolved. I'll continue watching reviews and CI, then use normal auto-merge/merge queue once approved.

Written by Connor's agent

…of before dispatch

Providers can opt in with IAgentChats.awaitsTurnStartCheckpoint to receive the
pending capture as turnStartCheckpoint on the send context. The host then
dispatches the turn immediately, so the git snapshot overlaps model
time-to-first-token. Copilot opts in and awaits the capture in its pre-tool
hook, denying tools if it failed. Other providers still wait before dispatch.

Co-authored-by: Copilot <[email protected]>
…olicy may suppress SDK hooks

The runtime drops SDK hook callbacks, including the pre-tool checkpoint gate,
when allowManagedHooksOnly is in effect or managed settings fail closed.

Co-authored-by: Copilot <[email protected]>
Keep first-tool checkpoint gating alongside current session and turn telemetry changes.

Co-authored-by: Copilot <[email protected]>
@connor4312
Connor Peet (connor4312) added this pull request to the merge queue Nov 18, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 18, 2026
Serialize checkpoint reads behind already queued capture work so fast no-tool turns cannot cache a missing compare-turns checkpoint.

Co-authored-by: Copilot <[email protected]>
@connor4312

Copy link
Copy Markdown
Member Author

Fixed the merge-queue Windows E2E failure in 23060bf. Deferring the checkpoint until the first tool exposed a read/publication race: a fast no-tool follow-up no longer guarantees earlier checkpoint writes have settled, and an early compare request could cache a missing-checkpoint error. Checkpoint-pair reads now await already-queued publication; dispatch remains unblocked.

Validation:

  • pnpm run typecheck-client — passed.
  • pnpm run build-fast --client-only — passed.
  • scripts\test.bat --reporter dot --run src\vs\platform\agentHost\test\node\agentHostCheckpointService.test.ts — 53 passed, including explicit/default-chat publication regressions.
  • scripts\test-integration.bat --reporter dot --run src\vs\platform\agentHost\test\node\e2e\providers\copilotAgentHostE2E.integrationTest.ts --grep "provider checkpoints:" — all five passed on Windows in strict replay. The historical-contents case reproduced before the fix.
  • pnpm run oxlint <the three changed files> and formatter verification — passed.

Written by Connor's agent

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.

4 participants