← Back to PRs

#19343: Refactor chat state management: reset chat messages and queue on session change, and ensure chat history loading respects session key

by saurav470 open 2026-02-17 18:08 View on GitHub →
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