Skip to content

feat(agent-manager): pre-warm worktrees to speed up session creation - #14050

Merged
marius-kilocode merged 11 commits into
mainfrom
optimize-worktree-creation-latency
Sep 14, 2026
Merged

feat(agent-manager): pre-warm worktrees to speed up session creation#14050
marius-kilocode merged 11 commits into
mainfrom
optimize-worktree-creation-latency

Conversation

@marius-kilocode

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Creating a new Agent Manager worktree blocks on git worktree add, which checks out every tracked file. On the Kilo repository (about 10,000 tracked files) that checkout takes about 2.1 s, and it scales with repository size, so large repositories pay many seconds before a new session is usable.

Why This Change Was Made

Register a detached worktree in the background and claim it for a new session with a cheap ref update instead of running a second full checkout at creation time. The behavior is scoped to the new "Pre-warm worktrees" Agent Manager setting (kilo-code.new.agentManager.worktreePool), enabled by default. Per-phase creation timing was added so the remaining cost is measurable. The change also fixes a claim path that could reset an existing local branch whose name matched a prepared worktree directory, and raises the fallback git worktree add worker count.

User Impact

  • Warm sessions start several times faster. A claim miss falls back to the previous creation path, so behavior and latency are unchanged when no worktree is ready.
  • Small repositories behave the same as before.
  • Pre-warming can be turned off in Agent Manager settings, which stops preparation and removes idle prepared worktrees.
  • Pre-warming uses the disk space of one extra checkout per open project.

Evidence

Creation timing measured through WorktreeManager.createWorktree on a local clone, median of 3 runs, macOS/APFS, no setup script.

Repository Pre-warm off No warm worktree Warm worktree claimed
About 10,200 tracked files 2167 ms 2126 ms 272 ms
120 tracked files 323 ms 319 ms 340 ms

Raw git on the large clone: full git worktree add about 2.1 s, registration only about 75 ms, a ref-only claim about 70 ms, and a cold-index git status about 400 ms. Raising the fallback worker count from 2 to 4 saves about 150 ms.

Limitations: these are worktree creation timings, not end-to-end time to first token. The model provider still dominates time to first token, and a configured setup script still runs after creation. Measurements were taken outside VS Code, so file watcher interaction is not included.

Register a detached worktree in the background and claim it for a new
session instead of running a full worktree checkout at creation time.
Add per-phase creation timing and the "Pre-warm worktrees" Agent Manager
setting, enabled by default.

Also fix a claim path that could reset an existing branch whose name
matched a pooled worktree directory, and raise the worktree add worker
count for the fallback path.
Comment thread packages/kilo-vscode/src/agent-manager/worktree-pool.ts Outdated
Comment thread packages/kilo-vscode/src/agent-manager/worktree-pool.ts
Comment thread packages/kilo-vscode/webview-ui/src/components/settings/Settings.tsx Outdated
Comment thread packages/kilo-vscode/tests/unit/worktree-pool.test.ts Outdated
@kilo-code-bot

kilo-code-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Incremental review of 1aa8e5bd19 (on top of previously reviewed 6435aa954f).

Resolved since last review
  • packages/opencode/src/kilocode/snapshot/cleanup.ts - the seed pin release now invokes git update-ref -d through Process.run instead of the AppProcess service, so KiloSnapshotCleanup.remove no longer depends on an AppProcess layer the caller does not provide. Behavior on the success, early-return, and failure paths is unchanged: nothrow still surfaces a non-zero exit to the existing warning log.
Files Reviewed (1 file)
  • packages/opencode/src/kilocode/snapshot/cleanup.ts
Previous Review Summaries (6 snapshots, latest commit 6435aa9)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 6435aa9)

Status: No Issues Found | Recommendation: Merge

Incremental review of 6435aa954f (incremental commit 6435aa954f, on top of previously reviewed a81cdf905f).

Resolved since last review
  • packages/opencode/src/kilocode/snapshot/cleanup.ts - the seed pin release on the success path is now handled by Effect.tap, so every successful exit (including a repository already removed by an interrupted earlier cleanup) releases refs/kilo/materialize/<hash>. Verified against the new regression assertion in snapshot-prepare.test.ts.
Files Reviewed (2 files)
  • packages/opencode/src/kilocode/snapshot/cleanup.ts
  • packages/opencode/test/kilocode/snapshot-prepare.test.ts

Previous review (commit a81cdf9)

Status: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 1
Issue Details (click to expand)

SUGGESTION

File Line Issue
packages/opencode/src/kilocode/snapshot/cleanup.ts 240 Seed pin is released only after a successful removal, so an interrupted or failed prior cleanup leaves refs/kilo/materialize/<hash> behind
Files Reviewed (5 files)
  • packages/opencode/src/kilocode/snapshot/cleanup.ts - 1 issue
  • packages/opencode/src/kilocode/primary-worktree.ts
  • .changeset/worktree-pool-prewarm.md
  • packages/opencode/test/kilocode/primary-worktree.test.ts
  • packages/opencode/test/kilocode/snapshot-prepare.test.ts

Fix these issues in Kilo Cloud

Previous review (commit eb495fa)

Status: 3 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 2
Issue Details (click to expand)

WARNING

File Line Issue
packages/opencode/src/kilocode/snapshot/cleanup.ts 131 Prepared-but-never-tracked repo can now be removed, but the refs/kilo/materialize/<hash> pin it wrote into the primary git dir is never released

SUGGESTION

File Line Issue
packages/opencode/src/kilocode/primary-worktree.ts 59 Splitting one rev-parse blob on newlines drops support for checkout paths containing newlines
.changeset/worktree-pool-prewarm.md 10 "one git call instead of five" is inaccurate (four rev-parse calls collapsed into one)
Files Reviewed (17 files)
  • .changeset/worktree-pool-prewarm.md
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts
  • packages/kilo-vscode/src/agent-manager/discard-worktree.ts
  • packages/kilo-vscode/src/agent-manager/mcp-warmup.ts
  • packages/kilo-vscode/src/agent-manager/provider-lifecycle.ts
  • packages/kilo-vscode/src/agent-manager/provider-multi-version.ts
  • packages/kilo-vscode/tests/unit/agent-manager-arch.test.ts
  • packages/kilo-vscode/tests/unit/creation-plan.test.ts
  • packages/kilo-vscode/tests/unit/pty-cleanup.test.ts
  • packages/kilo-vscode/tests/unit/sandbox-bootstrap.test.ts
  • packages/opencode/src/kilocode/primary-worktree.ts
  • packages/opencode/src/kilocode/snapshot/cleanup.ts
  • packages/opencode/src/kilocode/snapshot/prepare.ts
  • packages/opencode/src/skill/index.ts
  • packages/opencode/test/kilocode/server/httpapi-exercise-scenarios.ts
  • packages/opencode/test/kilocode/snapshot-prepare.test.ts
  • packages/opencode/test/kilocode/snapshot-repository-cleanup.test.ts

Notes on prior findings: the detached prepareDirectory orphan-snapshot warning and the unbounded preparation.pending wait are resolved (discard/failure paths now call removeWorktreeSnapshot, and snapshot.prepare re-checks the worktree under the snapshot lock). The prepared-but-never-tracked cleanup warning is resolved by the new MARKER branch, and the snapshot-prepare flake is resolved by polling for staging too.

Fix these issues in Kilo Cloud

Previous review (commit 7b18939)

Status: 4 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 3
SUGGESTION 1
Issue Details (click to expand)

WARNING

File Line Issue
packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts 986 prepareDirectory creates snapshot state that discard/failure teardown paths never remove
packages/opencode/src/kilocode/snapshot/prepare.ts 32 Prepared-but-never-tracked snapshot repo can never be cleaned up
packages/opencode/test/kilocode/snapshot-prepare.test.ts 201 Polls only for alternates, then asserts seed-objects gone - flaky

SUGGESTION

File Line Issue
packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts 1010 Awaiting unbounded preparation.pending delays failure and cleanup
Files Reviewed (20 files)
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts
  • packages/kilo-vscode/src/agent-manager/provider-lifecycle.ts
  • packages/kilo-vscode/src/agent-manager/provider-multi-version.ts
  • packages/kilo-vscode/src/agent-manager/tool-start.ts
  • packages/kilo-vscode/src/agent-manager/creation-plan.ts
  • packages/kilo-vscode/src/agent-manager/mcp-warmup.ts
  • packages/opencode/src/kilocode/snapshot/prepare.ts
  • packages/opencode/src/snapshot/index.ts
  • packages/opencode/src/kilocode/server/httpapi/groups/kilocode.ts
  • packages/opencode/src/kilocode/server/httpapi/handlers/kilocode.ts
  • packages/opencode/test/kilocode/snapshot-prepare.test.ts
  • Updated kilo-vscode unit tests and generated SDK

Fix these issues in Kilo Cloud

Previous review (commit b5ab644)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (3 files)
  • packages/kilo-vscode/src/agent-manager/worktree-pool.ts
  • packages/kilo-vscode/tests/unit/worktree-pool.test.ts
  • packages/kilo-vscode/webview-ui/src/components/settings/Settings.tsx

All four findings from the previous review were re-verified against b5ab644 and are resolved: the warm-up resolves the start point before taking the git lock, adopt trusts the worktree's real HEAD over persisted metadata, the pre-warm toggle uses applySetting for immediate effect, and the fixed test sleeps were removed.

Previous review (commit f90f52a)

Status: 4 Issues Found | Recommendation: Address before merge

Fix these issues in Kilo Cloud

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 3
Issue Details (click to expand)

WARNING

File Line Issue
packages/kilo-vscode/src/agent-manager/worktree-pool.ts 82 Warm-up holds the shared per-repo git lock across a possible git fetch (cold fetch cache), which can stall user-initiated worktree operations and contradicts the documented cache-only start contract.

SUGGESTION

File Line Issue
packages/kilo-vscode/src/agent-manager/worktree-pool.ts 195 Exact claim creates the branch from HEAD rather than the matched oid; an adopted slot with stale metadata can yield the wrong base commit.
packages/kilo-vscode/webview-ui/src/components/settings/Settings.tsx 188 Uses updateSetting, so toggling pre-warm off does not stop the pool until Save; applySetting is the pattern for app-level gates.
packages/kilo-vscode/tests/unit/worktree-pool.test.ts 163 Fixed setTimeout sleeps make the pool tests timing-dependent and potentially flaky on loaded CI.
Files Reviewed (43 files)
  • .changeset/worktree-pool-prewarm.md
  • packages/kilo-vscode/package.json
  • packages/kilo-vscode/src/KiloProvider.ts
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts
  • packages/kilo-vscode/src/agent-manager/WorktreeManager.ts
  • packages/kilo-vscode/src/agent-manager/creation-timing.ts
  • packages/kilo-vscode/src/agent-manager/host.ts
  • packages/kilo-vscode/src/agent-manager/project/context.ts
  • packages/kilo-vscode/src/agent-manager/project/init.ts
  • packages/kilo-vscode/src/agent-manager/project/wiring.ts
  • packages/kilo-vscode/src/agent-manager/provider-lifecycle.ts
  • packages/kilo-vscode/src/agent-manager/provider-multi-version.ts
  • packages/kilo-vscode/src/agent-manager/tool-start.ts
  • packages/kilo-vscode/src/agent-manager/vscode-host.ts
  • packages/kilo-vscode/src/agent-manager/worktree-pool.ts
  • packages/kilo-vscode/src/kilo-provider/config-snapshot.ts
  • packages/kilo-vscode/tests/unit/agent-manager-tool-start.test.ts
  • packages/kilo-vscode/tests/unit/creation-timing.test.ts
  • packages/kilo-vscode/tests/unit/provider-multi-version.test.ts
  • packages/kilo-vscode/tests/unit/worktree-manager.test.ts
  • packages/kilo-vscode/tests/unit/worktree-pool.test.ts
  • packages/kilo-vscode/webview-ui/agent-manager/i18n/*.ts (21 locale files)
  • packages/kilo-vscode/webview-ui/src/components/settings/Settings.tsx

Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

…ed slots

Resolve the base ref and its commit before taking the shared git lock so a
cold fetch cache never stalls user worktree operations. When adopting a
leftover slot, use the worktree's real HEAD instead of persisted metadata.
Apply the pre-warm toggle immediately instead of on Save, and drop fixed
sleeps from the pool tests.
Comment thread packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts Outdated
Comment thread packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts Outdated
Comment thread packages/opencode/src/kilocode/snapshot/prepare.ts
Comment thread packages/opencode/test/kilocode/snapshot-prepare.test.ts
- remove a worktree's snapshot repository on every teardown path that
  deletes the worktree, and stop blocking error reporting on preparation
- let cleanup remove a prepared repository that was never tracked, and
  skip preparation when the worktree is already gone
- require auth for POST /kilocode/snapshot/prepare like snapshot/remove
- keep the multi-version structure test and the snapshot materialization
  poll aligned with the current code shape
Comment thread packages/opencode/src/kilocode/snapshot/cleanup.ts
Comment thread packages/opencode/src/kilocode/primary-worktree.ts Outdated
Comment thread .changeset/worktree-pool-prewarm.md Outdated
…ot repository

- delete the matching refs/kilo/materialize pin from the project checkout
  after a prepared but never tracked snapshot repository is removed
- keep primary checkout paths that contain newlines working by falling
  back to per-field rev-parse when the combined output does not have
  exactly four lines
- correct the changeset wording (four git calls into one)
Comment thread packages/opencode/src/kilocode/snapshot/cleanup.ts Outdated
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