fix(agent-manager): parallelize worktree terminal cleanup - #13707
Merged
Merged
Conversation
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (3 files)
Reviewed by grok-4.6 · Input: 58K · Output: 9.9K · Cached: 382.5K Review guidance: REVIEW.md from base branch |
marius-kilocode
enabled auto-merge
September 2, 2026 18:41
kirillk
approved these changes
Sep 2, 2026
This was referenced Sep 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Problem This Solves
Agent Manager removes residual backend terminals one at a time before deleting a worktree. Each removal waits for process-tree termination and verification, so the waits add up when several terminals remain. Profiling reproduced a roughly 4.3-second deletion for eight idle shells, while the webview spent only a few milliseconds executing JavaScript.
Why This Change Was Made
Remove residual PTYs in batches of at most four. This overlaps independent shutdown waits without starting an unbounded number of process-tree scans.
The safety order remains unchanged: block terminal creation, close tracked terminals, await every residual PTY removal, and only then permit disk deletion. Returned SDK errors and thrown rejections are collected across all batches; any cleanup failure still rejects with an
AggregateError. Termination grace periods and process-exit verification are not shortened or skipped.User Impact
Worktrees with multiple residual terminals delete faster. Conversation histories remain available after worktree deletion, and terminals in other worktrees remain running.
This is intentionally limited to terminal cleanup. Checkpoint reclamation, Run-script shutdown, Git operations, and session relocation are unchanged. The separate checkpoint-cleanup bottleneck identified during profiling is not addressed by this PR.
Evidence
Measured the real Agent Manager delete flow on macOS arm64 with
vscode-self-test, comparing the unmodified build at9cbd9e414bagainst the compiled source change. All tests used a new, independent temporary Git repository, isolated VS Code profiles, and separate Kilo HOME/XDG/database paths. No model prompts or real credentials were used.Each trial recreated the same worktree with one retained session and the stated number of idle
/bin/shbackend PTYs. The Local card was selected before deletion. Timing starts at the confirming click and ends when a DOM observer sees the worktree card disappear. Startup is outside the capture. Each row has three trials per version.The single-terminal ranges overlap; this small sample does not establish a single-terminal speedup. For eight terminals, all three after measurements were below all three baseline measurements. Renderer script time was at most 5.35 ms across these captures, and no trace data loss was reported.
Safety checks exercised against the actual backend and UI:
Automated checks passed:
pty-cleanup,agent-manager-provider-lifecycle, andscript-terminal-manager.These are synthetic local macOS measurements, not a Windows or remote-filesystem performance guarantee. Raw profiles and screenshots were retained locally rather than committed. An unrelated VS Code startup delay occurred before the after captures; profiling began only after the UI recovered and was ready.
Manual check: In a disposable project, delete a worktree with several running terminals. Confirm its processes stop, its conversations remain in History, and a sibling worktree's terminal continues running.