Skip to content

fix(jetbrains): harden agent manager worktrees - #13431

Merged
kirillk merged 2 commits into
mainfrom
sleepy-meadow
Aug 25, 2026
Merged

fix(jetbrains): harden agent manager worktrees#13431
kirillk merged 2 commits into
mainfrom
sleepy-meadow

Conversation

@kirillk

@kirillk kirillk commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Issue

No linked issue; fixes a JetBrains Agent Manager worktree data-loss regression discovered during worktree cleanup.

Context

JetBrains Agent Manager could create new worktrees under the currently opened linked worktree instead of the repository's main worktree storage. Deleting the parent worktree could then recursively remove nested child worktree directories and leave stale git worktree metadata behind.

This PR hardens creation, listing, removal, and missing-workspace handling so deleted or misplaced worktrees do not cause repeated backend errors or misleading Gateway advice.

Implementation

  • Resolve Agent Manager worktree storage from the git main worktree while preserving the current base-ref semantics for branch creation.
  • Parse git's prunable marker, hide dead managed worktrees from the list, constrain managed entries to direct children of .kilo/worktrees, and reconcile stale JetBrains worktree state.
  • Validate branch slugs before creating the filesystem path, prune and retry once when git reports stale worktree registration, and prune metadata after successful removal.
  • Refuse removal of unmanaged paths and of worktrees containing live nested worktrees; dead nested metadata does not block cleanup.
  • Add a distinct MISSING workspace state and frontend copy for deleted folders so users do not see unrelated JetBrains Gateway guidance.

Screenshots / Video

N/A - backend/worktree safety and connection-state copy changes only; no visual layout changes.

How to Test

Manual/local verification

Agent-run verification:

  • ./gradlew :backend:test --tests ai.kilocode.backend.rpc.KiloWorktreeRpcApiImplTest
  • ./gradlew :backend:test --tests ai.kilocode.backend.workspace.KiloBackendWorkspaceTest
  • ./gradlew :frontend:test --tests ai.kilocode.client.agentManager.WorktreeControllerTest --tests ai.kilocode.client.session.controller.ConnectionDelayTest
  • ./gradlew typecheck
  • ./gradlew test
  • git diff --check

Reviewer test steps

  1. Open a repository root in the JetBrains plugin and create a worktree; confirm it lands under <repo>/.kilo/worktrees/<slug>.
  2. Open that linked worktree as the IDE project and create another worktree; confirm it still lands under the main repo's .kilo/worktrees, not under the linked worktree.
  3. Create or simulate a nested live worktree under a managed worktree and try to delete the parent; confirm deletion is refused and the error names the nested child.
  4. Delete a managed worktree directory externally and refresh Agent Manager; confirm it disappears without repeated workspace loading failures.
  5. Open a session/editor pointing at a deleted directory; confirm the workspace banner says the folder is missing and does not include JetBrains Gateway advice.

Blocked checks and substitute verification

  • None.

Checklist

  • Issue linked above, or exception explained
  • Tests/verification described
  • Screenshots/video included for visual changes, or marked N/A
  • Changeset considered for user-facing changes
  • I personally reviewed the diff and can explain the changes, including any AI-assisted work.

Get in Touch

Available in the Kilo Code Discord.

@kilo-code-bot

kilo-code-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/rpc/KiloWorktreeRpcApiImpl.kt
  • packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/workspace/KiloBackendWorkspaceManager.kt
Previous Review Summary (commit 2127b8b)

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

Previous review (commit 2127b8b)

Status: 3 Issues Found | Recommendation: Address before merge

Overview

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

WARNING

File Line Issue
packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/rpc/KiloWorktreeRpcApiImpl.kt 377 Nested-worktree detection can miss live children when path forms differ
packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/rpc/KiloWorktreeRpcApiImpl.kt 390 Prunable worktrees skip directory deletion even when the checkout still exists
packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/rpc/KiloWorktreeRpcApiImpl.kt 412 Workspace cache eviction can miss the cached instance
Files Reviewed (13 files)
  • .changeset/jetbrains-worktree-safety.md - 0 issues
  • packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/rpc/KiloWorkspaceRpcApiImpl.kt - 0 issues
  • packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/rpc/KiloWorktreeRpcApiImpl.kt - 3 issues
  • packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/workspace/KiloBackendWorkspace.kt - 0 issues
  • packages/kilo-jetbrains/backend/src/main/kotlin/ai/kilocode/backend/workspace/KiloWorkspaceState.kt - 0 issues
  • packages/kilo-jetbrains/backend/src/test/kotlin/ai/kilocode/backend/rpc/KiloWorktreeRpcApiImplTest.kt - 0 issues
  • packages/kilo-jetbrains/backend/src/test/kotlin/ai/kilocode/backend/workspace/KiloBackendWorkspaceTest.kt - 0 issues
  • packages/kilo-jetbrains/frontend/src/main/kotlin/ai/kilocode/client/session/controller/SessionController.kt - 0 issues
  • packages/kilo-jetbrains/frontend/src/main/resources/messages/KiloBundle.properties - 0 issues
  • packages/kilo-jetbrains/frontend/src/test/kotlin/ai/kilocode/client/agentManager/WorktreeControllerTest.kt - 0 issues
  • packages/kilo-jetbrains/frontend/src/test/kotlin/ai/kilocode/client/session/controller/ConnectionDelayTest.kt - 0 issues
  • packages/kilo-jetbrains/shared/src/main/kotlin/ai/kilocode/rpc/dto/KiloWorkspaceStateDto.kt - 0 issues
  • packages/kilo-jetbrains/shared/src/main/kotlin/ai/kilocode/rpc/dto/WorktreeDto.kt - 0 issues

Fix these issues in Kilo Cloud


Reviewed by grok-4.6 · Input: 170.3K · Output: 14.4K · Cached: 429.7K

Review guidance: REVIEW.md from base branch main

@kirillk
kirillk merged commit a7839e8 into main Aug 25, 2026
24 checks passed
@kirillk
kirillk deleted the sleepy-meadow branch August 25, 2026 19:08
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.

2 participants