Skip to content

fix(agent-manager): release worktree locks on Windows - #13468

Merged
marius-kilocode merged 6 commits into
mainfrom
fix-worktree-cleanup-on-windows
Aug 31, 2026
Merged

fix(agent-manager): release worktree locks on Windows#13468
marius-kilocode merged 6 commits into
mainfrom
fix-worktree-cleanup-on-windows

Conversation

@marius-kilocode

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

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Deleting an Agent Manager worktree could leave its folder locked on Windows until the machine restarted. Git removal failures were swallowed, so the worktree disappeared from Agent Manager even when its directory and branch remained.

Closes #13402.

Why This Change Was Made

Worktree deletion did not close VS Code integrated terminals, stop all associated agent sessions and background processes, wait for Run tasks to terminate, or dispose directory-scoped backend resources before removing the checkout. Windows keeps directory handles open while those processes exist, exposing cleanup failures that are easy to miss on macOS and Linux.

User Impact

Deleting a worktree now releases its associated terminals, tasks, sessions, background processes, diff resources, and backend instance before Git cleanup. If a directory is still locked, the worktree and branch remain available, an error is shown, and deletion can be retried instead of leaving an untracked folder behind.

Evidence

  • Run the native Windows regression in the first existing Windows unit-test shard whenever the general test matrix is scheduled. No extra runner, dependency install, or expanded path filters are added.
  • Added a native Windows regression that starts a real process with its working directory inside a worktree, verifies deletion fails while Windows holds the directory, terminates the process, and verifies retry removes both the directory and branch.
  • Added coverage for Windows path normalization, integrated terminal disposal, PTY cleanup ordering, backend instance disposal, Run-task termination, and failed-deletion recovery.
  • Full VS Code extension suite: 4,210 passed, 1 Windows-only test skipped on macOS.
  • Extension lint, typecheck, compile, Knip, workflow allowlist, and formatting checks passed.
  • Manually created a worktree in isolated VS Code, ran an instant-exit Run task, opened its integrated terminal, deleted the worktree, and verified that the terminal, directory, Git metadata, and branch were removed.

@kilo-code-bot

kilo-code-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (14 files)
  • .changeset/fix-windows-worktree-cleanup.md
  • .github/workflows/test.yml
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts
  • packages/kilo-vscode/src/agent-manager/SessionTerminalManager.ts
  • packages/kilo-vscode/src/agent-manager/WorktreeManager.ts
  • packages/kilo-vscode/src/agent-manager/provider-lifecycle.ts
  • packages/kilo-vscode/src/agent-manager/pty-cleanup.ts
  • packages/kilo-vscode/src/agent-manager/run/task.ts
  • packages/kilo-vscode/tests/unit/agent-manager-arch.test.ts
  • packages/kilo-vscode/tests/unit/agent-manager-provider-lifecycle.test.ts
  • packages/kilo-vscode/tests/unit/pty-cleanup.test.ts
  • packages/kilo-vscode/tests/unit/run-script-manager.test.ts
  • packages/kilo-vscode/tests/unit/session-terminal-manager.test.ts
  • packages/kilo-vscode/tests/unit/worktree-manager.test.ts
Previous Review Summaries (2 snapshots, latest commit dad0f3f)

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

Previous review (commit dad0f3f)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (13 files)
  • .changeset/fix-windows-worktree-cleanup.md
  • .github/workflows/test.yml
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts
  • packages/kilo-vscode/src/agent-manager/SessionTerminalManager.ts
  • packages/kilo-vscode/src/agent-manager/WorktreeManager.ts
  • packages/kilo-vscode/src/agent-manager/provider-lifecycle.ts
  • packages/kilo-vscode/src/agent-manager/pty-cleanup.ts
  • packages/kilo-vscode/src/agent-manager/run/task.ts
  • packages/kilo-vscode/tests/unit/agent-manager-arch.test.ts
  • packages/kilo-vscode/tests/unit/pty-cleanup.test.ts
  • packages/kilo-vscode/tests/unit/run-script-manager.test.ts
  • packages/kilo-vscode/tests/unit/session-terminal-manager.test.ts
  • packages/kilo-vscode/tests/unit/worktree-manager.test.ts

Previous review (commit 298c978)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (13 files)
  • .changeset/fix-windows-worktree-cleanup.md
  • .github/workflows/test-vscode.yml
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts
  • packages/kilo-vscode/src/agent-manager/SessionTerminalManager.ts
  • packages/kilo-vscode/src/agent-manager/WorktreeManager.ts
  • packages/kilo-vscode/src/agent-manager/provider-lifecycle.ts
  • packages/kilo-vscode/src/agent-manager/pty-cleanup.ts
  • packages/kilo-vscode/src/agent-manager/run/task.ts
  • packages/kilo-vscode/tests/unit/agent-manager-arch.test.ts
  • packages/kilo-vscode/tests/unit/pty-cleanup.test.ts
  • packages/kilo-vscode/tests/unit/run-script-manager.test.ts
  • packages/kilo-vscode/tests/unit/session-terminal-manager.test.ts
  • packages/kilo-vscode/tests/unit/worktree-manager.test.ts

Reviewed by grok-4.6 · Input: 362.7K · Output: 14.6K · Cached: 325.1K

Review guidance: REVIEW.md from base branch main

Comment thread packages/kilo-vscode/src/agent-manager/run/task.ts Outdated
@WebReflection

Copy link
Copy Markdown
Contributor

We actually have:

Prefer Promise.withResolvers<T>() for deferreds when runtime/types support it; allow callback/event executors, not async executors or redundant Promise wrapping.

I am not sure why patterns like those used in here keeps showing up, maybe that "when runtime/types support it" is not strong enough as guard? The compatibility is baseline since 2024 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers#browser_compatibility so I am not sure that helps avoiding things like:

const withResolvers = {};
const promise = new Promise((reject, resolve) => {
  withResolvers.reject = reject;
  withResolvers.resolve = resolve;
});

when all we need is const ref = Promise.withResolvers()

@marius-kilocode
marius-kilocode merged commit 9aa11e1 into main Aug 31, 2026
34 checks passed
@marius-kilocode
marius-kilocode deleted the fix-worktree-cleanup-on-windows branch August 31, 2026 10:23
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.

Agent Manager Worktree Folder Issues

2 participants