Skip to content

fix(chat): keep the canvas and conversation when switching renderers - #269

Merged
jgindin merged 1 commit into
a2ui-project:mainfrom
jerelvelarde:jerel/keep-canvas-on-renderer-switch
Oct 19, 2026
Merged

jgindin merged 1 commit into
a2ui-project:mainfrom
jerelvelarde:jerel/keep-canvas-on-renderer-switch

Conversation

@jerelvelarde

@jerelvelarde jerelvelarde commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Before and after switching from Angular Basic to Lit Basic in Settings: the edited canvas and the conversation are both still there

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. StateSync replaces the draft with the new renderer's sample whenever the selected renderer changes, and ChatCoordinator clears 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 main today.

The approach

ChatCoordinator decides once, when the renderer URL changes:

  • Edited canvas: kept, along with the conversation, and drawn by the new renderer.
  • Unedited canvas: replaced with the new renderer's sample, and the conversation is cleared, as before.
  • Either way, a response still streaming for the old renderer is cancelled. Its message says This response was stopped because the renderer changed., and it isn't tracked as a user cancel.

"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.

StateSync no 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

  • A kept canvas is drawn as is. If the new renderer has no catalog matching the canvas's createSurface.catalogId, it can't draw it: the bridge reports Catalog 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.
  • There's no "start fresh on switch" option. New Session still clears both.

Verification

Based on main at 8155592; head is 3d63709. Formatting, lint, typechecks, the production build and all unit tests pass (1,749 in the shell). Full e2e: all 89 pass.

  • chat-coordinator.spec.ts covers 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.ts covers replacing an unedited sample, and keeping a canvas the user or the assistant edited, when the renderer changes.
  • monaco-editor.spec.ts checks the model is set to LF.
  • copilot-assistant.e2e.ts adds 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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +110 to +112
if (this.activePromptId || this.activeStreamResponse) {
this.cancelActiveStream();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
if (this.activePromptId || this.activeStreamResponse) {
this.cancelActiveStream();
}
if (this.isProgrammaticStreamActive() || this.activeStreamResponse) {
this.cancelActiveStream();
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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). Only CancelReason.USER calls trackChatCancel, so switching renderers never records telemetry, idle or not.
  • activeStreamResponse is cleared in submitPrompt's finally. On an idle chat, a renderer switch therefore cancels nothing. It sets cancelReason, which the next submitPrompt resets 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.
@jerelvelarde
jerelvelarde force-pushed the jerel/keep-canvas-on-renderer-switch branch from c5180a9 to 3d63709 Compare October 16, 2026 01:14
@github-actions

Copy link
Copy Markdown
Contributor

⚡ A2UI Composer PR Preview

Your automated preview is successfully live (commit 3d63709):
👉 Launch PR Preview

Note: This environment will be wiped automatically when the PR is merged or closed.

@jgindin
jgindin merged commit 37744e6 into a2ui-project:main Oct 19, 2026
13 checks passed
jerelvelarde added a commit to jerelvelarde/a2ui-composer that referenced this pull request Oct 19, 2026
…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants