Skip to content

feat(vscode): add git worktree support for agent manager sessions#418

Merged
marius-kilocode merged 13 commits intodevfrom
schaltwerk/clean-reimpl-spec
Feb 18, 2026
Merged

feat(vscode): add git worktree support for agent manager sessions#418
marius-kilocode merged 13 commits intodevfrom
schaltwerk/clean-reimpl-spec

Conversation

@marius-kilocode
Copy link
Collaborator

@marius-kilocode marius-kilocode commented Feb 18, 2026

Summary

  • Adds git worktree support to the Agent Manager, allowing sessions to run in isolated worktrees at .kilocode/worktrees/{branch}/
  • Users can toggle Local/Worktree mode via a popover selector in the prompt input before starting a new session
  • Worktree sessions get a branch badge in the session list and use the global SSE endpoint to receive events across directories

Architecture

  • WorktreeManager — handles git worktree create/remove/discover, session ID persistence, and git exclude management
  • AgentManagerProvider — uses a onBeforeMessage interceptor on KiloProvider to route custom messages (createWorktreeSession) and inject directory on session-scoped messages for worktree sessions
  • KiloProvider — gains registerSession, trackSession, attachToWebview interceptor support, and a per-message messageDirectory override that flows through getWorkspaceDirectory()
  • SSE client — switches to /global/event endpoint and unwraps { directory, payload } envelope
  • WorktreeModeProvider — SolidJS context for Local/Worktree toggle state (only active in Agent Manager)

No SessionTerminalManager or WorktreeSessionManager — kept it minimal.

image image

@kiloconnect
Copy link
Contributor

kiloconnect bot commented Feb 18, 2026

Code Review Summary

Status: 17 Issues Found | Recommendation: Address before merge

Fix these issues in Kilo Cloud

Overview

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

WARNING

File Line Issue
WorktreeManager.ts N/A Removing worktree directory with fs.promises.rm — potential data loss if path resolution is wrong
KiloProvider.ts N/A Race condition with messageDirectory as a shared mutable
KiloProvider.ts N/A Unhandled rejection if onBeforeMessage interceptor throws
KiloProvider.ts 643 sessionDirectories not cleaned up when session is deleted
AgentManagerProvider.ts 208 Missing agent parameter — user's selected mode not forwarded
AgentManagerProvider.ts N/A Recovered worktree sessions not tracked for SSE events
AgentManagerProvider.ts N/A Silent failure of metadata persistence could leave orphaned worktrees
AgentManagerApp.tsx N/A Return value of vscode.onMessage() (unsubscribe) not stored
PromptInput.tsx 276 File attachments silently dropped in worktree mode
PromptInput.tsx 282 Missing agent in worktree session creation
WorktreeManager.ts 82 Retry path always creates a new branch (-b) — may fail if branch already exists

SUGGESTION

File Line Issue
sse-client.ts 71 Consider adding a runtime guard before the type assertion
WorktreeManager.ts 124 Add path validation before recursive rm
WorktreeManager.ts 291 Empty catch blocks violate the project style guide
PromptInput.tsx N/A The as any cast bypasses type safety
KiloProvider.ts N/A Why change messageDirectory on the fly?
WorktreeManager.ts N/A parentBranch uses defaultBranch() instead of persisted value
Positive Observations
  • Comprehensive test coverage: The worktree-manager.test.ts file covers all major WorktreeManager operations including creation, removal, cleanup, metadata persistence, discovery, and git exclude management with real git repos (no mocks)
  • Good error handling: Worktree creation has proper cleanup on failure (removes worktree if session creation fails), and the multi-step removal strategy (clean → force → fs.rm) is robust
  • Clean architecture: The interceptor pattern via onBeforeMessage keeps worktree-specific logic in AgentManagerProvider without polluting KiloProvider
  • Session directory resolution: The getWorkspaceDirectory(sessionId?) pattern cleanly routes operations to the correct directory for both local and worktree sessions
  • Backward compatibility: Legacy session-id file format is still supported alongside the new metadata.json
  • Git hygiene: Worktree directories are automatically added to .git/info/exclude to avoid polluting the repo
Files Reviewed (11 files)
  • packages/kilo-vscode/src/KiloProvider.ts - 3 issues (session directory management, interceptor integration)
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts - 3 issues (missing agent param, recovery tracking, metadata persistence)
  • packages/kilo-vscode/src/agent-manager/WorktreeManager.ts - 4 issues (empty catches, path validation, retry logic, rm safety)
  • packages/kilo-vscode/src/agent-manager/branch-name.ts - 0 issues
  • packages/kilo-vscode/src/services/cli-backend/sse-client.ts - 1 issue (runtime guard)
  • packages/kilo-vscode/webview-ui/agent-manager/AgentManagerApp.tsx - 1 issue (unsubscribe handling)
  • packages/kilo-vscode/webview-ui/agent-manager/agent-manager.css - 0 issues
  • packages/kilo-vscode/webview-ui/src/components/chat/PromptInput.tsx - 3 issues (attachments dropped, missing agent, type cast)
  • packages/kilo-vscode/webview-ui/src/context/worktree-mode.tsx - 0 issues
  • packages/kilo-vscode/webview-ui/src/styles/chat.css - 0 issues
  • packages/kilo-vscode/webview-ui/src/types/messages.ts - 0 issues
  • packages/kilo-vscode/tests/unit/worktree-manager.test.ts - 0 issues
  • bun.lock - skipped (generated)
  • packages/kilo-vscode/package.json - 0 issues

…leak, add cleanup regression tests

- Remove messageDirectory class field; pass dir as parameter to all handler methods
- Only trust directory override when onBeforeMessage interceptor is attached
- Add proper type for CreateWorktreeSessionRequest, remove `as any` cast
- Fix onMessage listener leak with onCleanup in AgentManagerApp
- Use removeWorktree() for pre-creation cleanup instead of inline fs.rm
- Add regression tests for orphaned directory removal and cleanup before re-creation
…l-spec

# Conflicts:
#	packages/kilo-vscode/webview-ui/agent-manager/AgentManagerApp.tsx
#	packages/kilo-vscode/webview-ui/src/components/chat/PromptInput.tsx
…l-spec

# Conflicts:
#	packages/kilo-vscode/webview-ui/agent-manager/AgentManagerApp.tsx
Copy link
Contributor

@iscekic iscekic left a comment

Choose a reason for hiding this comment

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

Nothing stands out. I do believe some of the bot comments are valid or at least cheap to address. Approving to unblock.

…e import

- KiloProvider: add sessionDirectories map and setSessionDirectory()
- getWorkspaceDirectory() resolves by sessionId lookup instead of per-message override
- Remove dir param from all 18 handler methods — they resolve directory from their sessionId
- AgentManagerProvider: call setSessionDirectory() at session creation and recovery
- Remove directory injection from message interceptor (interceptor only routes custom messages)
- Fix duplicate ExtensionMessage import in AgentManagerApp.tsx
- Forward agent param in worktree session creation
@marius-kilocode marius-kilocode enabled auto-merge (squash) February 18, 2026 13:59
…l-spec

# Conflicts:
#	packages/kilo-vscode/webview-ui/src/components/chat/PromptInput.tsx
… track recovered sessions for SSE, log metadata write failures
…ries into loadSessions

- handleLoadSessions fetches sessions from all registered worktree directories
  and merges them into the response (deduped by session ID)
- recoverWorktrees result is awaited by the interceptor before loadSessions
  passes through, ensuring sessionDirectories are populated first
- Fixes: worktree sessions disappearing after VS Code restart
@marius-kilocode marius-kilocode enabled auto-merge (squash) February 18, 2026 14:21
@marius-kilocode marius-kilocode merged commit 4b7e20b into dev Feb 18, 2026
7 checks passed
@marius-kilocode marius-kilocode deleted the schaltwerk/clean-reimpl-spec branch February 18, 2026 14:25
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.

3 participants