Skip to content

fix(session): restore running state when the window reopens - #1915

Merged
vastsa merged 3 commits into
vastsa:mainfrom
FuYukiH:fix/restore-running-sessions
Oct 28, 2026
Merged

vastsa merged 3 commits into
vastsa:mainfrom
FuYukiH:fix/restore-running-sessions

Conversation

@FuYukiH

@FuYukiH FuYukiH commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #1912.

A recreated renderer starts with an empty runningSessions map. 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:

  • 71 targeted Desktop tests passed, including the Main IPC snapshot, bootstrap start/end races, session selection, native session routing, sidebar status and close behavior.
  • node scripts/e2e-running-session-reopen.mjs passed: 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.
  • TypeScript project-reference builds and Desktop typecheck passed.
  • Desktop production build, Biome lint, style-token checks, policy sync and documentation checks passed.
  • check-pr-base-main.mjs passed against 5aa253883c4e34fd65d31c434256d424c352bf0b.

Candidate: 34d12008a1c791565a7a4dfc0a7286349d2641f4. The IPC contract and the E2E scenario are documented.

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 muzimu217 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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:

  • runningSessionIds is [...activeTurns.keys()], so it can name sessions that are absent from the returned sessions list (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:503 already exposes getRunningSessionIds(); passing that into registerSessionIpc instead of the raw map would keep the snapshot in one place. The handler already takes activeTurns for 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.

vastsa and others added 2 commits October 28, 2026 10:02
The compatibility case needs an empty running-state baseline so earlier bootstrap cases cannot leak into its assertion.
@vastsa
vastsa merged commit ea0289e into vastsa:main Oct 28, 2026
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] Mac下关闭窗口重新打开,进行中的任务状态会显示已完成

3 participants