fix(agent-manager): safely clean up deleted worktree snapshots - #13476
Merged
Conversation
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (3 files)
Previous Review Summaries (5 snapshots, latest commit 672f48f)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 672f48f)Status: No Issues Found | Recommendation: Merge Files Reviewed (7 files)
Previous review (commit 38dec02)Status: No Issues Found | Recommendation: Merge Files Reviewed (1 file)
Previous review (commit 67064b5)Status: No Issues Found | Recommendation: Merge Files Reviewed (13 files)
Previous review (commit 97409b0)Status: No Issues Found | Recommendation: Merge Files Reviewed (13 files)
Previous review (commit 45202c0)Status: 4 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
SUGGESTION
Files Reviewed (23 files)
Reviewed by grok-4.6 · Input: 122.2K · Output: 7.5K · Cached: 212.6K Review guidance: REVIEW.md from base branch |
marius-kilocode
enabled auto-merge
August 27, 2026 10:12
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 creates an independent Git-backed checkpoint repository for each worktree. Removing a worktree previously removed its files and sidebar entry, but left its checkpoint Git objects and private Git LFS storage behind indefinitely. One inspected installation had 3.1 GB of snapshot data across 38 repositories, including about 33 repositories belonging to removed worktrees. Complete repositories consumed approximately 86 MB each, split between Git objects and duplicated Git LFS data.
Deleting sessions could also leave running or attention indicators visible when delayed status, permission, question, suggestion, or session-update events arrived after the deletion.
Why This Change Was Made
A missing directory is not proof of intentional deletion: an external drive, network mount, or symlink target may be unavailable temporarily. This change therefore adds no global missing-path or age-based collector. It attaches checkpoint cleanup to the explicit Agent Manager worktree delete action.
Checkpoints belong to the old worktree's on-device file state. Conversation history is separate. Retaining a conversation does not prevent cleanup after its worktree is intentionally deleted, and cleanup never deletes the conversation to make the worktree eligible.
Exact deletion sequence
snapshot/<project-id>/<sha1(worktree-path)>. It checks managed-worktree scope, canonical path containment, unexpected symlinks, continued worktree absence, and materialization markers while holding the existing snapshot lock.The new cleanup is immediate and action-driven. It has no seven-day or 24-hour grace period.
User Impact
When this PR prunes checkpoint repositories
.<hash>.cleanup-<uuid>leftovers for that exact worktree, then complete removalThe parent project does not need to be deleted. Only the explicitly removed managed worktree is the target.
What is kept
Deleting a session from history still performs the existing session-deletion behavior. The additional session-related change only prevents delayed events from recreating deleted session rows or activity badges; it does not add checkpoint pruning to session deletion.
Failure and retry behavior
If checkpoint cleanup fails after Git has already removed the worktree, a native VS Code error notification reports that checkpoint data remains. The notification stays visible independently of the closed conversation tab. Agent Manager still preserves conversation history and completes the worktree UI removal. It does not silently claim checkpoint cleanup succeeded.
Interrupted quarantines are recoverable by a repeated authenticated cleanup request for the same worktree. Unsafe or still-materializing quarantines are preserved. This PR does not add a background retry job or an automatic sweep of those leftovers.
Existing cleanup that is unchanged
The pre-existing per-instance snapshot maintenance still starts one minute after snapshot initialization and runs hourly while enabled. It removes snapshot refs older than seven days and runs
git gc --prune=7.days. That is Git-object garbage collection, not the new whole-repository cleanup, and it does not remove session records or sweep orphaned worktree repositories and their Git LFS storage.Session retention policies and SQLite compaction are intentionally unchanged.
Evidence
cf0a209229passed all CI gates, including Linux, macOS, Windows, JetBrains, typechecks, visual regression, CodeQL analysis, and the HTTP API exerciser. Automated review found no issues, and all inline review threads are resolved.Snapshot.trackcaptures and real agent turns produced Git tree refs and snapshot-specific Git LFS objects. Twelve HTTP/filesystem scenarios passed: exact-target deletion, sibling/project isolation, live-path refusal, authentication, session-only deletion, manually missing worktrees, materialization refusal, symlink refusal, actual filesystem-removal failure, quarantine recovery, repeated cleanup, and unsafe target refusal.A retained conversation completes a new turn from the project root after its worktree checkpoints are removed:
A cleanup failure is visible even after the conversation tab closes:
A pending question blocks deletion without showing a running spinner:
Active worktree deletion is unavailable while the session is running:
Conversation history remains available after the worktree and its checkpoints are removed: