fix(agent-manager): reconcile worktree sessions after window reload#8077
fix(agent-manager): reconcile worktree sessions after window reload#8077kilo-code-bot[bot] wants to merge 1 commit intomainfrom
Conversation
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 |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
Code Review SummaryStatus: 2 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
Other Observations (not in diff)No additional out-of-diff issues found. Fix these issues in Kilo Cloud Files Reviewed (2 files)
Reviewed by gpt-5.4-20260305 · 1,565,475 tokens |
Summary
reconcile-sessions.ts(vscode-free helper module) to stay within theAgentManagerProvidermaxLines capProblem
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.jsonmay 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:The reconciliation is extracted into
reconcile-sessions.tsfollowing the same pattern asfork-session.ts— pure orchestration with no vscode imports.Closes #8074