fix(desktop): prevent stale events from corrupting UI after session switch#1582
Conversation
…witch (esengine#1217) When a session switch happens while a turn is running, the old turn's cleanup code may still emit events (, , etc.) that arrive after the new session is active, causing UI lag and state corruption. Add a switching flag to the Tab interface: - Set tab.switching = true before aborting in session_load/new_chat - In runTurn's finally block, check tab.switching to suppress stale events - Reset tab.switching = false after processing This prevents the old turn from emitting events that would interfere with the new session's state. Fixes esengine#1217
|
Thanks for splitting this out of #1579 — clean focused diff, mergeable against main, and the core mechanism is right. One blocker before I can merge, though. The flag gets stuck Sequence that breaks (any session switch, not just abort-then-switch):
The user-visible symptom is worse than #1217 was: the UI sits on "thinking" forever because Minimal fix Only set the flag when there's actually a live turn to abort. Two-line change at both call sites: // session_load and new_chat handlers
if (tab.aborter) tab.switching = true;
abortTurn(tab);That keeps the rest of the PR identical. If you'd rather avoid the shared mutable flag entirely, the cleaner alternative is to snapshot the session in async function runTurn(tab: Tab, text: string, fromQQ = false): Promise<void> {
if (!tab.runtime) return;
const startSession = tab.currentSession;
...
finally {
tab.aborter = null;
if (tab.currentSession === startSession) {
// emit $turn_complete / emitSessions / emitBalance / $plan_cleared / QQ
}
}
}Then drop the Push a fixup and I'll merge. |
…1217) The previous implementation set tab.switching=true unconditionally in session_load/new_chat handlers. When no turn was in flight (the common case), the flag stayed true and suppressed events for the first turn in the new session. Fix: only set switching=true when tab.aborter exists (live turn).
|
Applied your suggested fix: \\ s This prevents the flag from getting stuck when no turn is in flight (the common session-switch path). Verified locally: lint ✅, typecheck ✅. |
|
Fixup looks right — gated on |
Fixes #1217
When a session switch happens while a turn is running, the old turn cleanup may still emit events that corrupt the new session UI state.
Add switching flag to Tab interface:
This prevents the flag from getting stuck when no turn is in flight — the common session-switch path.
Esc + New Session still works because switching is only set when aborter exists, not in Esc handler.
Tested: npm run lint, npm run typecheck