fix(desktop): auto-switch to new session after deleting active session#2127
Conversation
When the user deletes the currently-active session via the sidebar, the UI remained stuck on the now-deleted session. Now the backend detects when the deleted session is the active one and: 1. Aborts any in-flight turn 2. Mints a fresh session for the workspace (reusing startNewChatInTab) 3. Emits \$session_loaded to the frontend so it navigates to the new session
esengine
left a comment
There was a problem hiding this comment.
The core fix is correct and the approach is right — minting a new session and pushing it to the frontend via $session_loaded is exactly how session state should be propagated (the frontend reducer keys off $session_loaded to set currentSession + clear messages), and startNewChatInTab already calls emitSessions, so the sidebar refresh is covered without the else branch's emitSessions double-firing. Zero carryover for a fresh session is correct.
One small consistency gap to close before merge: the canonical session_load path emits $session_loaded and then emitCtxBreakdown(tab) (src/cli/commands/desktop.ts:871). This branch only emits $session_loaded. On the frontend, the $session_loaded reducer resets usage via { ...zeroUsage(), ... }, which zeroes reservedTokens (desktop/src/App.tsx:1051) — so after deleting the active session the context-usage bar reads 0 until the next turn fires a fresh $ctx_breakdown, instead of showing the new session's system+tools baseline.
Please add emitCtxBreakdown(tab); right after the emit({ type: "$session_loaded", ... }) in the active-session branch to match session_load. Otherwise this is good to go.
Add emitCtxBreakdown(tab) call after $session_loaded in the session_delete active-session branch, matching the canonical session_load path. Without it, the frontend context-usage bar reads 0 until the next turn fires a fresh $ctx_breakdown.
|
thanks for review. done now |
esengine
left a comment
There was a problem hiding this comment.
Re-reviewed — emitCtxBreakdown(tab) is now emitted right after the $session_loaded in the active-session branch, matching the canonical session_load path, so the context bar no longer reads 0 after deleting the active session. CI green. Thanks for the quick fix — merging.
Summary
When a user deletes the currently-active session via the sidebar, the UI remained stuck on the now-deleted session instead of navigating to a fresh session. This PR fixes the backend `session_delete` RPC handler to detect when the deleted session is the active one and automatically switch to a new session.
Closes #2126.
Changes
`src/cli/commands/desktop.ts` — `session_delete` handler:
Root Cause
The `session_delete` handler only did two things:
It never checked whether `tab.currentSession === msg.name`, so when the active session was deleted the frontend's `currentSession` state remained pointing to the now-deleted session. The `$sessions` event only updates the sidebar list — it does not change which session is displayed.
Fix
When the deleted session matches `tab.currentSession`:
When the deleted session is not the active one, behavior is unchanged (`emitSessions` only).
Verification