Skip to content

feat(agent-manager): reduce GitStatsPoller load by polling active worktree only#8809

Open
Githubguy132010 wants to merge 5 commits intoKilo-Org:mainfrom
Githubguy132010:feat/agent-manager-worktree-perf
Open

feat(agent-manager): reduce GitStatsPoller load by polling active worktree only#8809
Githubguy132010 wants to merge 5 commits intoKilo-Org:mainfrom
Githubguy132010:feat/agent-manager-worktree-perf

Conversation

@Githubguy132010
Copy link
Copy Markdown
Contributor

Context

Closes #8653.

A worktree with a large diff (e.g. 20 k lines) causes unnecessarily high RAM usage and HTTP traffic because GitStatsPoller called client.worktree.diffSummary() for every worktree on every 5-second tick, regardless of whether the user was looking at it.

PRStatusPoller already solves the same problem: it polls the active worktree every tick and does a full sync of all worktrees only every ~2 minutes. This PR applies the same pattern to GitStatsPoller.

Implementation

GitStatsPoller gains a setActiveWorktreeId(id) method (mirroring PRStatusPoller):

  • On normal ticks, only the active worktree (the one whose session the user has open) is polled via diffSummary.
  • Every FULL_SYNC_EVERY = 6 ticks (~30 s at the default 5 s interval), all non-missing worktrees are polled so inactive badges stay reasonably fresh.
  • Calling setActiveWorktreeId with a new ID immediately triggers an out-of-band poll for that worktree, so the user sees fresh stats the moment they switch sessions — no extra wait.
  • onStats always receives a complete snapshot: fresh data for polled worktrees + cached lastStats for non-polled ones. The webview contract is unchanged.
  • tickCount is reset on stop() so restarted pollers always begin with a full sync.

AgentManagerProvider wires up the new call in the loadMessages and clearSession handlers, alongside the existing prBridge.poller.setActiveWorktreeId() call.

Screenshots

No UI changes — the stats numbers in the Agent Manager panel are unaffected; they just update with lower CPU/network overhead.

How to Test

  1. Open the Agent Manager panel with 2+ worktrees, at least one with a large diff.
  2. Open DevTools → Network tab, filter by XHR/Fetch.
  3. Switch between sessions — confirm diffSummary is called only for the active worktree most of the time, and for all worktrees ~every 30 s.
  4. Confirm stats numbers in the sidebar still update correctly.
  5. Run the unit tests: cd packages/kilo-vscode && bun test tests/unit/git-stats-poller.test.ts (15 tests, 0 failures).

Get in Touch

Githubguy132010 and others added 2 commits April 12, 2026 18:54
…ktree only

Poll only the active worktree's diff stats on most ticks; do a full sync
of all worktrees every FULL_SYNC_EVERY (6) ticks (~30 s at the default
5 s interval).  This mirrors the existing PRStatusPoller pattern and
reduces HTTP traffic and memory pressure when a worktree has a large diff.

- Add setActiveWorktreeId() to GitStatsPoller; triggers an immediate
  poll when the active worktree changes so the user sees fresh stats
  without waiting for the next scheduled tick
- Wire the call in AgentManagerProvider's loadMessages / clearSession
  handlers alongside the existing prBridge.poller call
- Emit a full stats snapshot on every onStats callback (using cached
  lastStats for non-polled worktrees) so the webview contract is unchanged
- Add 5 new unit tests covering partial-tick skipping, full-sync cadence,
  complete snapshot emission, and immediate-poll behaviour

Closes Kilo-Org#8653

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kilo-code-bot
Copy link
Copy Markdown
Contributor

kilo-code-bot bot commented Apr 12, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 files)
  • packages/kilo-vscode/src/agent-manager/GitStatsPoller.ts

Reviewed by gpt-5.4-20260305 · 511,166 tokens

1. Queue pending active-worktree refresh when a poll is already in
   flight instead of silently dropping the request. The queued refresh
   fires immediately after the in-flight fetch completes.

2. Keep periodic full-sync cadence when activeWorktreeId is undefined
   (e.g. after clearSession) instead of reverting to every-tick full
   sync, which would re-introduce the load this PR aims to avoid.

3. Add tests for both fixes.
@Githubguy132010
Copy link
Copy Markdown
Contributor Author

Addressed both review warnings in 4745efe:

1. Busy-poll racesetActiveWorktreeId() now queues a pendingActiveWorktreeId when a fetch is in flight. After the fetch completes, the pending refresh fires immediately instead of waiting for the next scheduled tick.

2. clearSession load spikeactiveWorktreeId === undefined no longer forces full sync on every tick. Instead, the same periodic cadence (FULL_SYNC_EVERY) applies whether or not an active worktree is set, so closing a session doesn't re-introduce the every-tick load.

Added tests for both fixes.

When activeWorktreeId is undefined, non-full-sync ticks now skip
polling entirely instead of polling all worktrees (which would
re-introduce the every-tick load). Stats from the last full sync
remain cached for the webview.

Fix failing test to match the new behaviour.
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.

[FEATURE]: Improve AgentManager worktree performance

1 participant