Repository navigation
fix(session): restore running state when the window reopens - #1915
Conversation
A recreated renderer missed the turn's start event, so bootstrap restored older completion notifications without knowing that Main still owned a running turn. Include Main's live session IDs in the existing session-list response and restore them before publishing the shell. Keep start/end events received during bootstrap authoritative over that snapshot. Cover the IPC snapshot, bootstrap races, session selection and the real Sidebar across recreated Electron windows. Fixes vastsa#1912
muzimu217
left a comment
There was a problem hiding this comment.
Checked the three pieces this fix depends on, and they line up.
The flag is what the composer reads, not just the badge. Both selection paths derive the running state from the same map — session-slice.ts:424 (isRunning: state.runningSessions[id] ?? false) and session-coordination.ts:266 (isRunning: current.runningSessions[summary.id] ?? false). So restoring runningSessions during bootstrap is what makes "selecting a restored running session" correct, and the badge follows from sidebarSessionStatus with no second source of truth.
The snapshot cannot resurrect a finished turn. Every settlement path funnels through finishTurn in runtime/plans.ts:147, and releaseTurnClaims deletes the entry under an identity check (:253, if (activeTurns.get(id) === turnId) activeTurns.delete(id)), so a turn that ended before session.list settles is already gone from the map. Taking the snapshot after the list reads settle is the right choice, and the new runtime test pins exactly that (mutation during the read is reflected).
The event race is closed by ordering. The subscription is installed in useAppShellRuntime.tsx:652, in an effect that runs in the same commit that starts bootstrap(), while the snapshot only arrives with the async session.list response. So any start/end event that could still matter is received before the snapshot is applied, and the merge order (snapshot first, then state.runningSessions) lets it win. The agent_end-during-bootstrap test covers the case that would otherwise leave a stale true.
Two non-blocking notes:
runningSessionIdsis[...activeTurns.keys()], so it can name sessions that are absent from the returnedsessionslist (deleted or filtered). Nothing renders for those, so it is harmless, but intersecting with the returned ids would make the contract "running ids within this list" and keep the two fields consistent for any future consumer.main/index.ts:503already exposesgetRunningSessionIds(); passing that intoregisterSessionIpcinstead of the raw map would keep the snapshot in one place. The handler already takesactiveTurnsfor its other ownership checks, so this is a preference, not a defect.
The macOS native close gesture being untested on Windows is called out in the PR body and covered as a manual check in the e2e plan — that is the right disposition for the one path this host cannot exercise.
The compatibility case needs an empty running-state baseline so earlier bootstrap cases cannot leak into its assertion.
Fixes #1912.
A recreated renderer starts with an empty
runningSessionsmap. Bootstrap restored old completion notifications, but the live turn's start event had already happened while the window was absent, so the sidebar could show “Completed” until later activity.The existing session-list response now includes a snapshot of the Desktop session IDs whose turns Main still owns. Bootstrap restores these flags before publishing the shell and preserves any start/end events it has already received. Selecting a restored running session immediately restores its running composer state.
Validation on Windows x64, Node 24.14.0, Electron 43.6.0:
node scripts/e2e-running-session-reopen.mjspassed: real Electron windows are destroyed and recreated with the production bootstrap, Sidebar and selection flow. Both live reopen cases remain running; reopening after completion stays idle. Backend responses are fixtures. The native macOS close gesture was not exercised on this Windows host.check-pr-base-main.mjspassed against5aa253883c4e34fd65d31c434256d424c352bf0b.Candidate:
34d12008a1c791565a7a4dfc0a7286349d2641f4. The IPC contract and the E2E scenario are documented.