Chat sessions flash Exited and reorder during desktop restart recovery
Bug
Closing and reopening the desktop app makes every live Chat UI session briefly appear Exited, then causes sessions to move around the left sidebar as controllers reconnect in waves. The sessions normally recover without being terminated, but the UI presents a planned app restart like a fleet-wide failure and makes navigation unstable during recovery.
Observed on: macOS arm64, desktop 0.12.0-pr4562.202608280157 (PR #4562, combining the direct-PTY preview with PR #4553 through 7c21a210)
Analyzed against: ce259665003619517c268acfc63d2bf95bd7d9a8 (origin/main) and PR #4553 head e16473cee0f2b2139b31e29d70d7882130fd4dfa
Confidence: High — reproduced in the desktop app and correlated with exact durable change events and code paths.
Priority: P2 — sessions recover, but a routine restart looks like data loss and destabilizes core navigation.
Reproduction
- Start several live Chat UI sessions in AO.
- Leave them idle or active.
- Close the desktop app normally.
- Reopen the app.
- Watch the session cards and expanded project sections in the left sidebar.
Actual behavior
- On planned shutdown, each Chat session changes to
activity=exitedwhileisTerminated=false. - The restarted daemon exposes the API/UI before background controller reconciliation completes.
- The UI initially renders those durable rows as Exited.
- Chat controllers reconnect in a bounded worker pool, so rows return to Idle/Working in waves.
- Each recovery updates
sessions.updated_at; the sidebar sorts workers by that field, so rows visibly reorder after every wave.
One captured restart showed:
08:11:40 UTC: planned shutdown wroteactivity=exited,isTerminated=falsefor Chat sessions.08:11:49 UTC: replacement daemon started.08:11:51.56–08:11:56.10 UTC: the same AO session IDs recovered to Idle in several waves.- No session was marked terminated during this restart.
Expected behavior
A planned app restart should present one coherent recovery phase:
- AO session identity, transcript, worktree/branch, and provider-native conversation remain unchanged.
- Durable user-facing activity does not falsely transition through Exited merely because the owning daemon is intentionally restarting.
- Controller availability is shown as Recovering/Reconnecting, distinct from agent activity and durable termination.
- Input remains safely fenced until that session's controller is ready.
- Sidebar/project/session ordering remains stable throughout recovery.
- A real unexpected controller exit remains distinguishable and visible as Exited.
Root cause
Three boundaries currently interact poorly:
- Planned and unexpected controller stops share the same durable activity transition.
backend/internal/service/chat/controller.go:2001-2024reportschat.controller.stopped -> ActivityExitedwhenever the provider stream ends. That is correct for an unexpected loss, but daemon context cancellation during a planned desktop shutdown follows the same path. - Readiness excludes slow controller restoration.
backend/internal/daemon/daemon.go:536-549startsSessionManager.ReconcileBackgroundonly afterRunWithReadyexposes the server. This is intentionally good for fast access to durable metadata, but the API has no explicit daemon/session recovery projection, so the UI renders raw Exited states while reconciliation runs. - Navigation order depends on operational mutation time.
frontend/src/renderer/types/workspace.ts:250-254orders workers byupdatedAt, andfrontend/src/renderer/components/Sidebar.tsx:1001-1008reapplies that order whenever workspace data changes. Controller recovery writes therefore become navigation reorder events.
This is separate from the destructive-reconciliation regression fixed by PR #4553: preserving isTerminated=false prevents data loss, but does not provide a coherent restart presentation.
Architecture-aligned direction
Do not solve this with delayed UI timers, optimistic Idle labels, retries, or by blocking all metadata/API access until every provider resumes.
Instead, establish and test these boundaries:
- Keep durable session lifecycle/activity separate from ephemeral controller attachment/availability for the current daemon epoch.
- Treat planned daemon shutdown as relinquishing controller ownership, not evidence that the agent durably exited. Preserve enough last-known activity to render continuity.
- Expose a daemon-wide and/or per-session
recoveringprojection while startup reconciliation is pending; sending input must remain fenced until controller readiness is proven. - Use a stable navigation ordering key (explicit user order or another non-operational key). Lifecycle probes, controller-generation changes, and recovery timestamps must not reorder the sidebar.
- Preserve the existing rule that unknown/failed probes are not proof of termination, and preserve generation fencing so a stale controller cannot publish after replacement.
Acceptance criteria
- [ ] Production-shaped test: several Chat sessions are live, planned daemon shutdown occurs, the new daemon starts, and each controller resumes with the same AO session ID and provider conversation ID.
- [ ] During that sequence, no session is shown as Exited solely because of planned daemon restart; it is shown as Recovering until ready.
- [ ] Input is rejected/fenced while recovering and succeeds on the same native conversation after readiness.
- [ ] A transient resume failure remains recoverable and retryable without durable termination or a false Idle/Working claim.
- [ ] An unexpected provider/controller exit still yields the truthful Exited state.
- [ ] Sidebar row order is identical before, during, and after recovery, including when recovery completes out of order.
- [ ] Tests cover more Chat sessions than the reconciliation worker count so queued sessions cannot appear healthy or reorder prematurely.
- [ ] Existing TUI/direct-PTY adoption behavior and terminated-session semantics remain unchanged.
Related
I’d like to work on this. I’ll add a production-shaped restart regression covering more sessions than the reconciliation worker count, then separate planned controller recovery from durable agent exit and stabilize sidebar ordering without weakening generation fences or termination semantics.
Assigned to @Pulkit7070
From the Sep 28 daily call — same cluster as the resume-after-restart failure discussed live.
What was said: ronish: "when you close the app and you start it up again, most of your sessions are dead for some reason… when I close the app and open again, I realized that all of them are dead." He clarified the sessions being terminated is expected, but the UI shows them fully stopped (red dot / exited) and the pre-existing Resume agent button errors out instead of resuming. -Prasad-: toggling Chat UI away and back also clears it. prateek: "sometimes agents will show exited status, but they're not really exited. I can still send them messages… that exited status error goes away."
Decisions: root-cause fix requested (not the restart-the-agent workaround); ronish to discuss with Dhruv Sharma. ronish's coming-week plan includes exactly this: "continue root-cause investigation for dead/exited session behavior."
Source: Call Meeting Sep 28 thread
closing as superseded and fixed