Skip to content

fix(vscode): defer unused worktree watchers - #12865

Merged
marius-kilocode merged 4 commits into
mainfrom
analyze-kilo-resource-usage
Aug 4, 2026
Merged

fix(vscode): defer unused worktree watchers#12865
marius-kilocode merged 4 commits into
mainfrom
analyze-kilo-resource-usage

Conversation

@marius-kilocode

@marius-kilocode marius-kilocode commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Problem

Agent Manager creates location-scoped services for sessions, PTYs, references, and file operations across many worktrees. The FFF search layer eagerly initialized a native index and filesystem watcher for every location, even when no file search was requested. Managed VS Code backends also retained a Core file watcher whose events are not consumed by the extension.

The previous watcher change, #12593, stopped Kilo bootstrap warmup but did not stop FFF from initializing inside each location scope. A later upstream refactor restored that eager FFF path.

Exact Fix

  • Defer FFF construction until find, glob, or grep is actually called.
  • Share the first FFF initialization within a location scope.
  • Await the initial scan before returning search results.
  • Invalidate failed or interrupted loads so a transient failure can retry.
  • Register an idempotent finalizer immediately after native picker creation and destroy it when the location scope closes.
  • Disable unused FFF mmap and content-index caches.
  • Force managed VS Code servers to set KILO_EXPERIMENTAL_DISABLE_FILEWATCHER=true.
  • Add lifecycle coverage for eager creation, concurrent initialization, initial-scan completion, retry behavior, cache settings, and cleanup.

Measured Result

These measurements compare the live Kilo backend before and after reloading the packaged fix while multiple Agent Manager agents were active. CPU is workload-dependent, so the CPU values are sampled ranges rather than a controlled benchmark.

Resource Before After Improvement
FFF native watcher instances 24-33 0 during normal Agent Manager use No idle per-worktree FFF watchers
Core file watcher instances 1 0 Removed unused VS Code watcher
Kilo server threads About 141 23-28 Removed watcher worker pools
Kilo resident memory About 2.2 GB 1.15-1.3 GB Roughly 40-48% lower
Kilo CPU 47-187% with bursts 18-62%, about 39% average while agents remained active Much lower idle/background overhead

The remaining CPU is workload-driven agent execution, SQLite/event processing, and session handling. Codebase indexing is separate, consent-gated, and was not part of the 24-33 watcher fanout.

Merge Protection

The lifecycle tests fail if a future merge makes FFF eager again, creates duplicate concurrent pickers, skips the initial scan, or drops cleanup. The managed-backend test also verifies that VS Code always disables the unused Core watcher.

Comment thread packages/core/src/filesystem/search.ts Outdated
@kilo-code-bot

kilo-code-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

The two previous suggestions are resolved in fe16f7c8:

  • packages/core/src/filesystem/search.tsmake is now wrapped in Effect.uninterruptible, so the native picker can no longer leak between Fff.create and Scope.addFinalizer; if the location scope is already closed, the finalizer runs immediately.
  • packages/core/test/kilocode/fff.test.ts — the lifecycle test now covers the failure-retry path: a transient first-create failure fails the concurrent first searches exactly once, the invalidated cache retries and succeeds, cache flags are asserted, and the picker is destroyed exactly once on scope close.
Files Reviewed (2 files, incremental)
  • packages/core/src/filesystem/search.ts
  • packages/core/test/kilocode/fff.test.ts
Previous Review Summaries (2 snapshots, latest commit afdcf09)

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

Previous review (commit afdcf09)

Status: 2 Issues Found | Recommendation: Address before merge

Overview

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

SUGGESTION

File Line Issue
packages/core/src/filesystem/search.ts 193 Residual narrow interruption window between Fff.create and Scope.addFinalizer can still leak the native picker (previous warning otherwise fixed)
packages/core/src/filesystem/search.ts 214 The new invalidate-on-error retry path has no test coverage
Files Reviewed (5 files)
  • packages/core/src/filesystem/search.ts - 2 issues
  • packages/core/test/kilocode/fff.test.ts - 0 issues
  • packages/kilo-vscode/src/services/cli-backend/server-manager.ts - 0 issues
  • packages/kilo-vscode/tests/unit/server-manager-utils.test.ts - 0 issues
  • .changeset/calm-location-watchers.md - 0 issues

The previous warning is resolved: Effect.cachedInvalidateWithTTL with invalidate-on-error makes the first-load cache failure-transparent, and interruption during waitForScan now destroys the picker via the closed-guarded release. Concurrent first loads remain deduped, and every created picker is destroyed exactly once on scope close.

Fix these issues in Kilo Cloud

Previous review (commit 35801cb)

Status: 1 Issue Found | Recommendation: Address before merge

Overview

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

WARNING

File Line Issue
packages/core/src/filesystem/search.ts 171 Effect.cached permanently caches a failed or interrupted first FFF load, breaking search for that location scope's lifetime (and a narrow interruption window can leak the native picker)
Files Reviewed (5 files)
  • packages/core/src/filesystem/search.ts - 1 issue
  • packages/core/test/kilocode/fff.test.ts - 0 issues
  • packages/kilo-vscode/src/services/cli-backend/server-manager.ts - 0 issues
  • packages/kilo-vscode/tests/unit/server-manager-utils.test.ts - 0 issues
  • .changeset/calm-location-watchers.md - 0 issues

The deferral design itself is sound: Scope.addFinalizer runs immediately on an already-closed scope, so the close-during-load race is leak-free, concurrent first loads are correctly deduped, and the KILO_EXPERIMENTAL_DISABLE_FILEWATCHER override is safe — the extension consumes neither file.watcher.updated nor vcs.branch.updated, and tool-published watcher events bypass the parcel watcher.

Fix these issues in Kilo Cloud


Reviewed by kimi-k3 · Input: 23.4K · Output: 10.7K · Cached: 328.5K

Review guidance: REVIEW.md from base branch main

Comment thread packages/core/src/filesystem/search.ts Outdated
Comment thread packages/core/src/filesystem/search.ts
@marius-kilocode
marius-kilocode merged commit cd35811 into main Aug 4, 2026
31 checks passed
@marius-kilocode
marius-kilocode deleted the analyze-kilo-resource-usage branch August 4, 2026 16:27
t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
…-usage

fix(vscode): defer unused worktree watchers
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