fix(chat): bail refreshMessagesUntilTurn poll on session switch - #209
Merged
Conversation
PR-FE-BUG-HUNT-4 (LOW from kenji-led bug-hunt 2026-06-24 round 1): `refreshMessagesUntilTurn` polls `readMessages` every 40ms for up to 1200ms while waiting for a freshly-sent user turn to land in the session journal. The setState was already gated on `activeIdRef.current === sessionId`, but the IPC call itself kept firing until the deadline — burning bandwidth and CPU on a session the user no longer cared about. Fix: check `activeIdRef.current !== sessionId` at the top of each loop iteration AND immediately after the await. Bail early so the polling cycle stops, not just the setState. Same guard on the deadline-fallthrough `refreshMessages(sessionId)`. Behavior change is invisible when the user stays on the same session (common case). Saves up to 30 IPC roundtrips per session-switch during the post-send window when the user navigates fast. Single function, single file. No conflict with #202 / #204 / #206 / #207 since none of those touch `refreshMessagesUntilTurn`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR-FE-BUG-HUNT-4 (LOW from kenji-led bug-hunt 2026-06-24 round 1).
Bug
`refreshMessagesUntilTurn` polls `readMessages` every 40ms for up to 1200ms while waiting for a freshly-sent user turn to land in the session journal. The setState was already gated on `activeIdRef.current === sessionId`, but the IPC call itself kept firing until the deadline — burning up to 30 IPC roundtrips on a session the user no longer cared about.
Fix
Check `activeIdRef.current !== sessionId` at the top of each loop iteration AND immediately after the `await`. Bail early so the polling cycle stops, not just the setState. Same guard on the deadline-fallthrough `refreshMessages(sessionId)`.
Invisible when the user stays on the session (common case). Saves bandwidth + CPU on fast session-switch scenarios.
Diff
`1 file changed, 13 insertions(+), 3 deletions(-)` — `apps/desktop/src/renderer/main.tsx:2048-2065`. No conflict with the other in-flight PRs.
Verification
Local disk still at 100%, couldn't run `pnpm install && pnpm test` end-to-end. Static logic change, mechanical pattern.