fix(cas): seal cross-chat response leaks with eager sessions + end-on-switch - #123
Draft
scottcmg wants to merge 1 commit into
Draft
fix(cas): seal cross-chat response leaks with eager sessions + end-on-switch#123scottcmg wants to merge 1 commit into
scottcmg wants to merge 1 commit into
Conversation
Contributor
📊 Coverage Report
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the conversational agent chat widget against cross-chat streaming leaks by ending/detaching sessions on conversation switches and by eagerly creating conversations/sessions to remove timing windows that could misroute responses. It also improves sidebar usability by showing an immediate optimistic title from the first user message and ensuring generated titles land even when a conversation is backgrounded.
Changes:
- End active sessions on switch/new-chat/unmount and avoid calling
stopResponse()on open, preventing server-side turn abortion while still clearing local streaming UI state. - Add eager (deduped) conversation+session warm-up and abandon sends that race a switch, preventing cross-chat response rendering.
- Add optimistic sidebar labels + global
labelUpdatedhandling with server refresh fallbacks, so titles update promptly and reliably.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/conversational-agent-chat/src/ConversationalAgentChat.tsx | Implements end-on-switch session lifecycle, eager/deduped session creation, switch-safe send behavior, and improved sidebar labeling (optimistic + global updates + refresh fallbacks). |
| packages/conversational-agent-chat/src/tests/ConversationalAgentChat.test.tsx | Adds coverage for switch/new-chat races, orphan session flush+end behavior, stream UI clearing on open, eager warm reuse, and optimistic/generated label flows (including background delivery). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
726
to
+733
| const endActiveSession = useCallback(() => { | ||
| const sessionHelper = session.current; | ||
| activeExchange.current = null; | ||
| session.current = null; | ||
| // Detach any in-flight creation from "current" so a fresh session is built | ||
| // for the next conversation instead of reusing this abandoned one. | ||
| sessionPromiseRef.current = null; | ||
| if (!sessionHelper) return; |
| applyConversationLabel(event.conversationId, event.labelUpdated.label); | ||
| } | ||
| }); | ||
| }, [chatService, applyConversationLabel]); |
…-switch - End the conversation's session on switch/new-chat/unmount so an in-flight turn (still running server-side) stops streaming into the shared chat surface; reopened conversations load latest state from history. - Clear streaming UI directly on open instead of Apollo stopResponse(), which would end the turn. - Preserve a pending send across a too-early switch: flush on sessionStarted, then end the orphaned session. - Create the conversation + session eagerly (launch, New Chat, inputs submit) so the first send has no create-await and can't race a switch; dedup the in-flight creation so a racing send reuses it. - Sidebar labels: show the first user message (150-char) optimistically; apply the generated title globally via events.onAny so it lands for backgrounded chats; fetch fallback on open and turn-end. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
scottcmg
force-pushed
the
fix/cas-shared-event-chat-output
branch
from
July 26, 2026 22:05
ee24457 to
d164381
Compare
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.
Problem
Agent responses could render into the wrong chat (or appear to vanish) when the user switched conversations or started a new chat while a turn was in flight. Separately, a fresh chat showed "New chat" in the sidebar until a second message was sent.
Changes
Session model — end-on-switch
stopResponse()— that publishesStopResponseand would end the exchange server-side.sessionStarted) no longer drops the buffered send: it flushes when the session starts, then the orphaned session ends.Eager conversation + session creation
await. The wiring completes synchronously, so a subsequent switch (a separate macrotask) can't interleave and misdirect the turn.Sidebar labels
conversations.events.onAny, so it lands even for a backgrounded conversation whose session has ended (a per-session listener would miss it). Fetch fallbacks on open and turn-end cover non-delivery.Tests
228 passing. Added coverage for switch/new-chat send abandonment, flush-then-end durability, optimistic + generated labels, background label delivery, and eager warm-on-New-Chat reuse.
Notes / tradeoffs
🤖 Generated with Claude Code