Repository navigation
fix(chat): keep the canvas and conversation when switching renderers - #269
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the chat coordinator and state synchronization logic to preserve the active conversation and edited canvas drafts when the renderer changes, while still resetting the session if the canvas is an untouched sample. It also cancels any active streaming response during a renderer change. The feedback points out a potential issue where activePromptId is not reset to null when submitPrompt completes, which could cause idle sessions to incorrectly trigger stream cancellation and false telemetry events when the renderer changes. Using isProgrammaticStreamActive() is suggested to resolve this.
| if (this.activePromptId || this.activeStreamResponse) { | ||
| this.cancelActiveStream(); | ||
| } |
There was a problem hiding this comment.
The check this.activePromptId || this.activeStreamResponse can evaluate to true even when there is no active streaming response. This is because activePromptId is never reset to null when submitPrompt completes (either successfully or with an error). As a result, switching renderers (or starting a new session) when the chat is idle will incorrectly trigger cancelActiveStream(), leading to duplicate or false trackChatCancel telemetry events.
To prevent this, consider checking this.isProgrammaticStreamActive() instead of this.activePromptId to determine if a stream is actually in progress, or ensure that activePromptId is reset to null in the finally block of submitPrompt.
| if (this.activePromptId || this.activeStreamResponse) { | |
| this.cancelActiveStream(); | |
| } | |
| if (this.isProgrammaticStreamActive() || this.activeStreamResponse) { | |
| this.cancelActiveStream(); | |
| } |
There was a problem hiding this comment.
Thanks. This was written against the first commit (c5180a9). The current head (3d63709) changed this code, and most of the issue is gone:
- All cancels now go through
cancelStream(reason). OnlyCancelReason.USERcallstrackChatCancel, so switching renderers never records telemetry, idle or not. activeStreamResponseis cleared insubmitPrompt'sfinally. On an idle chat, a renderer switch therefore cancels nothing. It setscancelReason, which the nextsubmitPromptresets before it streams.
The part that remains is the one you describe for New Session: activePromptId stays set after a response finishes, so New Session on an idle chat still records a user cancel. main behaves the same way: its startNewSession uses the same activePromptId || activeStreamResponse guard, and cancelActiveStream tracks whenever activePromptId is set. It predates this PR, so I'll fix it in a follow-up rather than change an approved PR. The fix is to check whether a response is actually streaming (isProgrammaticStreamActive() or activeStreamResponse), not activePromptId.
Switching renderers replaced the canvas with the new renderer's sample and cleared the chat. Both resets date from when switching only happened in Settings: StateSync flushed the draft whenever the selected renderer changed, and ChatCoordinator wiped the conversation whenever the renderer URL changed. Once you can switch while you work, that throws away the canvas and conversation on every switch. A switch now resets only a canvas nobody has edited: an unedited sample becomes the new renderer's sample and the conversation is cleared, as before. An edited canvas stays, along with the conversation, and is drawn by the new renderer. Either way, a response still streaming for the old renderer is cancelled; its message says the renderer change stopped it, and it isn't tracked as a user cancel. ChatCoordinator makes this decision in one place, when the renderer URL changes, and flushes the draft itself; StateSync no longer flushes when the selected renderer changes. StateSync exposes hasEditedDraft() so ChatCoordinator can tell the two cases apart. The JSON editor's model now uses LF line endings. Monaco defaults to the platform's, CRLF on Windows, so every draft Composer wrote into the editor came back as a different string and counted as a user edit. A sample that nobody had touched was then kept on a switch instead of being replaced.
c5180a9 to
3d63709
Compare
⚡ A2UI Composer PR PreviewYour automated preview is successfully live (commit
|
…ject#269 follow-up, snapshot-once Recorded 2026-10-18 on each PR's branch with the e2e Gemini fixture (browser recordings) or vhs (the a2ui-project#269 follow-up's test run).
Left: the assistant has edited the canvas on Angular Basic. Right: the same canvas and conversation after switching to Lit Basic in Settings and returning to the workspace. Captured from this PR's e2e journey before the review changes below; the behaviour shown hasn't changed.
The problem
Switching renderers throws away the session.
StateSyncreplaces the draft with the new renderer's sample whenever the selected renderer changes, andChatCoordinatorclears the conversation whenever the renderer URL changes. Both resets date from when a switch meant starting over. Once you switch while you work (from Settings today, or from the chat with #244), every switch loses the canvas and the conversation that produced it.This was part of #244. Jay asked for it to be a separate PR, since it changes behaviour on its own and applies to the Settings switch on
maintoday.The approach
ChatCoordinatordecides once, when the renderer URL changes:"Edited" means anything replaced the canvas since it was last reset to a sample or a blank New Session canvas: typing in the JSON editor, an assistant response that changed it, or opening a shared link. It stays edited even if changed back to the sample.
StateSync.hasEditedDraft()reports it.The conversation follows the canvas because it is the history of how an edited canvas got its content, and follow-up prompts need it. An unedited canvas is the old renderer's sample; the conversation changed nothing on it and was about the old catalog, so it is cleared with it. A conversation that only asked questions is therefore cleared on a switch.
StateSyncno longer flushes on its own when the selected renderer changes. It used to flush before origin approval and the coordinator flushed again after, so an unedited canvas was reset twice, and the two could disagree if a response finished while the approval dialog was open. One consequence: a refused origin no longer resets the canvas, since the preview never changed.A fix this needed in the JSON editor. Monaco uses the platform's line ending, CRLF on Windows. Composer's drafts use LF, so on Windows every draft written into the editor came back as a different string and was counted as a user edit, and an untouched sample was never replaced. The editor's model now uses LF. Playwright's Chrome reports a Windows user agent, which is how the e2e journey caught it.
What is not covered
createSurface.catalogId, it can't draw it: the bridge reportsCatalog not found: <catalogId>as a render error, which the Angular and Lit samples show in their preview error overlay and Composer shows in the Errors panel and a "Render error" snackbar. The JSON is left unchanged, so the user can fix the catalog ID or switch back.Verification
Based on
mainat8155592; head is3d63709. Formatting, lint, typechecks, the production build and all unit tests pass (1,749 in the shell). Full e2e: all 89 pass.chat-coordinator.spec.tscovers clearing a conversation on an unedited canvas, keeping it on an edited one, and cancelling a streaming response on both paths. The mock cancels the way the real client does, by rejecting with a cancel error, and the tests assert the whole chat history, the pipeline status and that no user cancel is tracked.state-sync.spec.tscovers replacing an unedited sample, and keeping a canvas the user or the assistant edited, when the renderer changes.monaco-editor.spec.tschecks the model is set to LF.copilot-assistant.e2e.tsadds two journeys from the untouched sample on Angular Basic, both switching to Lit Basic in Settings. After the assistant edits the canvas, the canvas, preview and conversation are kept. Without an edit, Lit's sample replaces the canvas and the conversation is cleared.Merge notes
#244 and #269 both add tests to
copilot-assistant.e2e.ts, so whichever merges second needs a small rebase there. #216 is stacked on both and carries this change as its second commit.