Skip to content

fix(agent-manager): preserve named worktrees on collision - #11001

Merged
marius-kilocode merged 7 commits into
mainfrom
fix-agent-manager-branchname
Jun 11, 2026
Merged

fix(agent-manager): preserve named worktrees on collision#11001
marius-kilocode merged 7 commits into
mainfrom
fix-agent-manager-branchname

Conversation

@marius-kilocode

Copy link
Copy Markdown
Collaborator

Calling agent_manager more than once with the same explicit branchName could remove the first live worktree, leave its branch behind, and then create a timestamp-suffixed replacement. This broke the first agent while making the second agent's branch unpredictable. The same collision could occur through the Advanced Worktree modal, where repeated names are valid requests for separate agents.

Resolve branch availability before creating or removing anything. The requested name remains the first branch, subsequent independent requests receive deterministic numeric suffixes such as echo-hello-world-2, and existing worktrees remain untouched. Exact replay of one Agent Manager event is deduplicated by requestID, while distinct tool calls continue to create distinct worktree-backed sessions even when they share a branch-name seed.

Closes #10976

@marius-kilocode
marius-kilocode enabled auto-merge (squash) June 8, 2026 11:11
Comment thread packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts Outdated
Comment thread packages/kilo-vscode/src/agent-manager/WorktreeManager.ts Outdated
@kilo-code-bot

kilo-code-bot Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: 1 Issue Found | Recommendation: Merge (advisory only)

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 1
Issue Details (click to expand)

SUGGESTION

File Line Issue
packages/kilo-vscode/src/agent-manager/WorktreeManager.ts 309 Unbounded for(;;) loop in availableBranch — safe in practice but has no upper bound
Previously Flagged Issues — Now Resolved
File Previous Issue Status
packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts WARNING: Off-by-one in LRU eviction — set grew to 101 before pruning ✅ Fixed: >= 100 check now evicts before adding, keeping set at exactly 100
packages/kilo-vscode/src/agent-manager/WorktreeManager.ts SUGGESTION: Timestamp fallback in availableBranch was redundant ✅ Fixed: Date.now() fallback removed; loop is now unbounded and deterministic
Other Observations (not in diff)

prepareWorktreePath behavior change: When existingBranch is not provided and the directory already exists, the code now throws "Worktree path already exists" rather than silently removing it. In practice resolveBranch + availableBranch always picks a fresh directory upfront (via the fs.existsSync check in available), so this throw is only reachable in extreme edge cases (e.g., a directory appears between resolveBranch and prepareWorktreePath). The stricter behavior is correct.

Retry path double-collision: If two concurrent processes both pass resolveBranch and both hit the already exists catch, the second resolveBranch(params) call in the retry will resolve to a new available name, but there is no catch after the retry runWorktreeAdd — a third concurrent collision would throw. This is an acceptable tradeoff for a rare scenario.

Latest commit (style): The newest commit (6e4fc389) is a pure formatting change — it reformats the resolveBranch parameter object type from a single line to multi-line. No logic changes.

Files Reviewed (6 files)
  • .changeset/calm-agents-keep-worktrees.md — no issues
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts — previous WARNING resolved
  • packages/kilo-vscode/src/agent-manager/WorktreeManager.ts — previous SUGGESTION remains; latest commit is formatting-only
  • packages/kilo-vscode/src/agent-manager/tool-start.ts — no issues
  • packages/kilo-vscode/tests/unit/agent-manager-tool-start.test.ts — no issues
  • packages/kilo-vscode/tests/unit/worktree-manager.test.ts — no issues

Fix these issues in Kilo Cloud


Reviewed by claude-4.6-sonnet-20260217 · 165,776 tokens

Review guidance: REVIEW.md from base branch main

Comment thread packages/kilo-vscode/src/agent-manager/WorktreeManager.ts
@marius-kilocode
marius-kilocode merged commit b64d7e0 into main Jun 11, 2026
20 checks passed
@marius-kilocode
marius-kilocode deleted the fix-agent-manager-branchname branch June 11, 2026 13:56
t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
…1001)

* fix(agent-manager): preserve named worktrees on collision

* fix(agent-manager): keep collision fallback deterministic

* style(agent-manager): format branch resolver
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.

Calling agent_manager with branchName and worktree mode fails

2 participants