#19343: Refactor chat state management: reset chat messages and queue on session change, and ensure chat history loading respects session key
app: web-ui
size: XS
Fix Control UI session isolation bug causing chat history to leak between dropdown sessions.
Fixes #19187
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a session isolation bug in the Control UI where chat history would leak between sessions when switching via the dropdown or navigation tabs. The fix has two parts: (1) `loadChatHistory` now snapshots the `sessionKey` before the async request and guards all state writes (including `chatLoading` cleanup in `finally`) against session mismatches, and (2) session-switch paths in `renderChatControls` and `resetChatStateForSessionSwitch` now eagerly clear `chatMessages` and `chatQueue` so stale data is immediately wiped before the new history loads.
- `controllers/chat.ts`: Added session key snapshot and post-await session checks in `loadChatHistory` to prevent stale responses from overwriting the current session's state; the `finally` block now conditionally clears `chatLoading` only when the session key still matches.
- `app-render.helpers.ts`: `resetChatStateForSessionSwitch` now clears `chatMessages` and `chatQueue`; the tab-click handler now calls `loadChatHistory` after resetting state; the session dropdown handler also clears `chatMessages` and `chatQueue`.
- `app-render.ts`: The chat view's `onSessionKeyChange` callback now clears `chatMessages` on session change (it already cleared `chatQueue`).
- Import reordering in all three files follows Oxfmt's type-first import ordering conventions.
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge — it addresses a real session-isolation regression with targeted, well-reasoned changes and no regressions introduced.
- The core `loadChatHistory` guard logic is correct and handles multi-switch races properly. The eager state clearing across all session-switch paths is consistent. No new APIs are introduced and the changes are confined to UI state management. Minor pre-existing inconsistency remains in the overview tab's `onSessionKeyChange` handler (which still doesn't clear `chatMessages`/`chatQueue`), but that handler doesn't directly trigger `loadChatHistory` and was already that way before this PR.
- No files require special attention; the changes are focused and correct.
<sub>Last reviewed commit: c7aed1a</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#14309: fix(ui): resolve chat event session key mismatch
by justonlyforyou · 2026-02-11
82.7%
#14966: fix(webchat): preserve user message visibility after chat.send
by BenediktSchackenberg · 2026-02-12
82.0%
#18694: fix(ui): reset session key to main after /new in webchat
by Phineas1500 · 2026-02-17
81.7%
#9218: Fix Control UI chat resync on gaps and terminal events
by figitaki · 2026-02-05
80.0%
#4567: Webchat: canonicalize main session key for /new (fix #4446)
by selfboot · 2026-01-30
79.0%
#9195: Fix: Control UI fails to render new messages after chat.history Web...
by vishaltandale00 · 2026-02-05
79.0%
#3721: fix(ui): webchat not displaying chat responses
by maxmaxrouge-rgb · 2026-01-29
78.9%
#18232: fix: webchat rapid messages create orphan sessions
by MisterGuy420 · 2026-02-16
78.6%
#3182: fix(gateway): use canonical session key in chat.send
by chrisherold · 2026-01-28
78.4%
#13548: feat(control-ui): Add quote reply and send message to session features
by Annaxiebot · 2026-02-10
78.3%