Skip to content

Fix worktree leak on agent session archive (#325974)#326216

Open
adityajha77 wants to merge 2 commits into
microsoft:mainfrom
adityajha77:fix/issue-325974-agent-worktree-leak
Open

Fix worktree leak on agent session archive (#325974)#326216
adityajha77 wants to merge 2 commits into
microsoft:mainfrom
adityajha77:fix/issue-325974-agent-worktree-leak

Conversation

@adityajha77

Copy link
Copy Markdown

Description

This PR fixes a Git worktree resource leak that occurs when archiving agent sessions (#325974).

Currently, when a session is marked "done" (archived) in the Agents window, the worktree is not deleted if it has uncommitted changes. Since agent changes are often uncommitted, this safety check causes orphaned worktrees to accumulate indefinitely in the {repo}.worktrees folder.

To resolve this, we remove the safety check and aggressively clean up the worktree when a session is archived.

Changes

src/vs/platform/agentHost/node/shared/worktreeIsolation.ts

  • Removed the safety check checking dirty (via hasUncommittedChanges) and skipping the cleanup. The _cleanupWorktreeOnArchive method now proceeds to remove the worktree directly.

src/vs/platform/agentHost/test/node/shared/worktreeIsolation.test.ts

  • Inverted/rewrote the test archive skips removal when the worktree has uncommitted changes to assert that a dirty worktree is forcefully removed.
  • Confirmed that removeWorktree is successfully called on the mocked Git service and the directory is deleted.

Verification Plan

Automated Tests

  • Running the unit tests in src/vs/platform/agentHost/test/node/shared/worktreeIsolation.test.ts will verify that removeCalls is equal to 1 and the worktree directory is removed even when uncommitted changes exist.
Copilot AI review requested due to automatic review settings July 16, 2026 20:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a Git worktree leak when agent sessions are archived by ensuring the session’s worktree is removed even if it contains uncommitted changes (addressing orphaned {repo}.worktrees/* directories accumulating over time).

Changes:

  • Removes the “skip cleanup when dirty” guard in WorktreeIsolation.cleanupWorktreeOnArchive, allowing forced worktree removal on archive.
  • Updates the unit test to assert a dirty worktree is still removed during archive.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/vs/platform/agentHost/node/shared/worktreeIsolation.ts Removes the uncommitted-changes safety check so archiving triggers forced worktree cleanup.
src/vs/platform/agentHost/test/node/shared/worktreeIsolation.test.ts Updates the archive test to expect removal even when hasUncommittedChanges is true.
Comments suppressed due to low confidence (1)

src/vs/platform/agentHost/node/shared/worktreeIsolation.ts:614

  • cleanupWorktreeOnArchive now force-removes the worktree regardless of uncommitted changes. Given this is a destructive behavior change, it would be helpful to leave a short in-code note here to prevent future maintainers from reintroducing the old “skip when dirty” behavior (the existing JSDoc above still describes the old clean/dirty semantics).
		try {
			await this._gitService.removeWorktree(repositoryRoot, worktreePath);
			this._logService.info(`[${this._logLabel}:${sessionId}] Removed worktree '${worktreePath.fsPath}' on archive`);
		} catch (error) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants