Skip to content

fix(agent-manager): reconcile worktree sessions after window reload#8077

Open
kilo-code-bot[bot] wants to merge 1 commit intomainfrom
fix/agent-manager-session-persistence
Open

fix(agent-manager): reconcile worktree sessions after window reload#8077
kilo-code-bot[bot] wants to merge 1 commit intomainfrom
fix/agent-manager-session-persistence

Conversation

@kilo-code-bot
Copy link
Copy Markdown
Contributor

@kilo-code-bot kilo-code-bot bot commented Apr 1, 2026

Summary

  • Add session reconciliation that runs after Agent Manager state initialization to recover worktree sessions lost during window reload or VSCode restart
  • Extract reconciliation logic into reconcile-sessions.ts (vscode-free helper module) to stay within the AgentManagerProvider maxLines cap

Problem

When a user reloads the window or restarts VSCode while Agent Manager sessions are running, worktree sessions can become stale. The persisted session IDs in agent-manager.json may no longer exist on the CLI backend (e.g. if storage was cleared or sessions were deleted externally). Previously there was no recovery mechanism — worktrees appeared in the sidebar but their sessions were missing.

Solution

After initializeState() loads the persisted state and pushes it to the webview (so the UI appears immediately), a non-blocking reconciliation runs:

  1. Waits for the CLI backend to connect (up to 10s timeout)
  2. Lists actual sessions from each worktree directory on the backend
  3. Removes stale session associations that no longer exist
  4. For worktrees left with no sessions, adopts the most recent existing session or creates a fresh one
  5. Updates state and refreshes the webview with the corrected session mappings

The reconciliation is extracted into reconcile-sessions.ts following the same pattern as fork-session.ts — pure orchestration with no vscode imports.

Closes #8074

After a VS Code reload or restart, persisted worktree session IDs in
agent-manager.json may no longer exist on the CLI backend (e.g. if
storage was cleared). Previously there was no recovery mechanism —
worktrees appeared in the sidebar but their sessions were missing.

Add session reconciliation that runs asynchronously after state
initialization:
- Waits for the CLI backend to connect (up to 10s timeout)
- Lists actual sessions from each worktree directory
- Removes stale session associations that no longer exist on backend
- Adopts the most recent existing session or creates a fresh one
- Updates state and refreshes the webview

Extract reconciliation logic into reconcile-sessions.ts (vscode-free)
to stay within the AgentManagerProvider maxLines cap.

Closes #8074
}

// If the worktree still has valid sessions, skip
if (state.getSessions(wt.id).length > 0) continue
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

WARNING: This also recovers intentionally empty worktrees

managed can already be empty here when the user explicitly closed the last session in a worktree. In that case the code still falls through into the adopt/create branch, so reloading the window will repopulate worktrees that were deliberately left empty. Recovery should only run when this pass actually removed stale tracked sessions, or when the worktree previously had persisted sessions.

}

if (recovered > 0 || removed > 0) {
ctx.pushState()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

WARNING: Recovered sessions are registered before the worktree mapping reaches the webview

ctx.registerSession() emits sessionCreated immediately, but pushState() does not happen until here. AgentManagerApp decides whether a sessionCreated event is local from the current managed-session state, so recovered/adopted worktree sessions can be added to localSessionIDs during reload and show up as local tabs. Pushing the updated state before emitting sessionCreated, or using a worktree-specific registration path, avoids that misclassification.

@kilo-code-bot
Copy link
Copy Markdown
Contributor Author

kilo-code-bot bot commented Apr 1, 2026

Code Review Summary

Status: 2 Issues Found | Recommendation: Address before merge

Overview

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

WARNING

File Line Issue
packages/kilo-vscode/src/agent-manager/reconcile-sessions.ts 139 Reconciliation recreates sessions for worktrees that were intentionally left empty after the last session was closed.
packages/kilo-vscode/src/agent-manager/reconcile-sessions.ts 174 Recovered worktree sessions are emitted before the updated managed-session state is pushed, so the webview can classify them as local tabs during reload.
Other Observations (not in diff)

No additional out-of-diff issues found.

Fix these issues in Kilo Cloud

Files Reviewed (2 files)
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts - 0 issues
  • packages/kilo-vscode/src/agent-manager/reconcile-sessions.ts - 2 issues

Reviewed by gpt-5.4-20260305 · 1,565,475 tokens

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.

bug(vscode): Agent Manager session lost on window reload or VSCode restart

0 participants