Skip to content

Canvases: restore canvases from a persisted record after idle release or Agent Host restart - #346111

Draft
Ulugbek Abdullaev (ulugbekna) wants to merge 4 commits into
mainfrom
ulugbekna/agents/canvas-persisted-continuity
Draft

Ulugbek Abdullaev (ulugbekna) wants to merge 4 commits into
mainfrom
ulugbekna/agents/canvas-persisted-continuity

Conversation

@ulugbekna

Copy link
Copy Markdown
Contributor

Fixes microsoft/vscode-internalbacklog#10570
Fixes #344999

An alternative to #345740 (idle release) and #345872 (Agent Host restart). It covers both cases in one mechanism.

Summary

When the Agent Host releases an idle Copilot runtime, or the Agent Host restarts, returning to a chat brings back its transcript but not its canvases. They only came back, on new channels, after the next message.

This change stores each chat's open canvases, without their URLs, in the chat's own storage, as a copilot.canvasContinuity metadata record tied to the chat's SDK session. Whenever the chat's runtime resumes, the Copilot provider reads the record. It republishes the canvases on their original channels, unavailable at first, and they become live when the runtime reports them open again. The runtime rehydrates each canvas's input itself; VS Code doesn't replay input or issue a second canvas.open.

Because the record lives in storage, idle release and process restart take the same path. Compared with #345740 + #345872:

  • No in-memory continuity cache. There is no cache to bound, evict or invalidate, and no special handling on release.
  • No residency veto latch or release reason. Release is unchanged.
  • Stale and archived records are checked when the record is read:
    • a record for a different SDK session, or one that can't be read, is deleted;
    • an archived cold chat isn't resumed for its canvases.
  • Size: 571 production lines added, versus 691 + 816 for the two PRs.

Commits

The history is layered for review:

  1. agentHost: carry deferred canvas closes across Copilot runtime handoffs (from Canvases: restore canvases after idle session release #345740): when a canvas is closed while no runtime is running, the next runtime applies the close when it adopts the handoff. A failing close is retried and doesn't fail the resume.
  2. agentHost: report Copilot canvas continuity while the runtime is live: the session reports which canvases it shows, plus closes it still owes the runtime, through a new onDidChangeCanvasContinuity option. It reports only while its runtime is live; handoffs and teardown never report.
  3. agentHost: restore Copilot canvases from their persisted record:
    • the provider writes the record, serialized per chat;
    • both resume paths read it and adopt it into the resumed runtime;
    • a new IAgent.watchChatCanvases shows a cold, observed chat's canvases as unavailable;
    • once a client contributes to that chat, the provider resumes it without a model turn, so the runtime launches with that client's current customizations;
    • closing a canvas of a cold chat records the close for the next runtime;
    • documented in AGENT_HOST_SESSIONS_PROVIDER.md.
  4. agentHost: republish restored canvases once a chat's state loads: after an Agent Host restart, the window can resubscribe before the host has loaded the chat's state, and restored canvases published in that window were dropped. The host now reports when a watched chat's state loads (IAgentChatCanvasWatchHost.onDidLoadState), and the provider republishes.

Known limitations

  • Canvases from window-provided plugins (unverified risk):
    • Reading a cold chat's transcript already resumes its runtime (existing behavior), before the window re-sends its customizations. After an idle release that is about a second earlier; after an Agent Host restart it can be much longer.
    • Canvases from workspace extensions restore fine, because the runtime discovers those itself.
    • A canvas from a plugin the window provides may be missing from that runtime, and its record could then be overwritten. I haven't reproduced this.
    • Possible follow-ups: don't let an early resume claim or write the record until a client contributes; read a cold chat's transcript without resuming it, as credential-scoped chats already do; or pass the SDK's openCanvases resume option.
  • Closed tabs come back: closing a canvas tab doesn't close the canvas in the runtime, so its tab reopens after a release or restart. Closing from the canvas pill closes it for good.
  • Canvases setting: turning canvases off keeps the record, so canvases restore again after they're turned back on.
  • Local only: local Copilot chats only; remote agent hosts are unchanged.

How to test

  1. Enable chat.canvases.enabled. In the Agents window, open a canvas in a local Copilot chat, for example from a project extension in .github/extensions/.
  2. Idle release: launch with VSCODE_AGENT_HOST_SESSION_RESIDENCY_LIMIT=1. Send a message in another session so the first one is released, then go back to the first one without sending anything. The canvas shows as unavailable, then live again on the same tab with the same input.
  3. Agent Host restart: run Developer: Restart Local Agent Host, or quit and relaunch, then open the chat. The canvas comes back on the same tab.
  4. Close the canvas from its canvas pill, release or restart again, and open the chat. It stays closed.

Recordings

The test canvas shows its input and a "provider generation" ID that changes whenever a new provider process serves it. In both recordings, the tab and input stay the same while the generation changes, and no message is sent. The chat footer and model picker are blurred.

Idle release: switch to another session so this one is released, then return.

Restored canvas after an idle release

▶ Watch the idle release recording (MP4)

Agent Host restart: run Developer: Restart Local Agent Host while the chat is open.

Restored canvas after an Agent Host restart

▶ Watch the Agent Host restart recording (MP4)

Validation

Command Result
pnpm run typecheck-client Passed
./scripts/test.sh --reporter dot --run src/vs/platform/agentHost/test/node/copilotAgent.test.ts --run src/vs/platform/agentHost/test/node/copilotAgentSession.test.ts --run src/vs/platform/agentHost/test/node/agentService.test.ts Passed: 3,954 passing, 17 pending
pnpm run eslint <changed .ts files> Passed
git diff --check origin/main...HEAD Clean
Manual, Code OSS Agents window Passed: canvases restored after two idle releases and two Agent Host restarts, on the same tab without a model turn

The manual run was done before rebasing onto current main. The rebase only resolved an import conflict.

A canvas handoff can now carry close intent recorded while no runtime
projected the canvas. A replacement session applies those closes when it
reconciles the handoff with the canvases its runtime restored, keeps the
closed instances unprojected, and retries closes that fail after it adopts
the handoff, so a failing provider close never fails the resume. A fixed-ID
reopen supersedes an earlier close, including one still being flushed, and
late closed events cannot remove a reopened canvas.

Hosts can also republish a live session's projected canvas states before
membership once its chat is registered.

Co-authored-by: ulugbekna <[email protected]>
Co-authored-by: Copilot <[email protected]>
A Copilot session can now report its projected canvases, without their
sources, together with the closes it still owes its runtime, whenever it
publishes canvas state or membership while its runtime is live. Teardown
and pending handoffs never report, so the last report describes what the
chat showed before its runtime went away.

Co-authored-by: Copilot <[email protected]>
An idle residency release or an Agent Host restart tore down a chat's
canvas providers, and returning to the chat restored only its transcript
until a new turn resumed the runtime.

While a chat's runtime is live, the Copilot provider now persists the
canvases it reports, without their sources, in the chat's own storage,
tied to its SDK session. When the runtime is gone:

- observing the chat shows those canvases as unavailable on their
  original channels and, once a client contributes to the chat, resumes
  the runtime without a model turn from that client's current
  customizations, so a plugin disabled or removed meanwhile is not
  relaunched;
- any resume seeds the runtime with the record, so the runtime's
  rehydrated canvases get fresh sources on the same channels;
- closing a canvas records the close for the next runtime to apply.

Releasing a runtime writes nothing, so the record outlives both idle
release and process restarts with one mechanism. Archived chats are not
restored, and a record left by an earlier SDK session is discarded when
read, instead of being cleared on every archive or backing change.

Co-authored-by: Copilot <[email protected]>
The host drops canvas state and membership published for a chat whose
state has not loaded yet, which happens when a client reconnects to a
restarted Agent Host and a restored chat resumes before its history
loads. Restored canvases then stayed unavailable until something else
re-subscribed to the chat.

The host now registers a subscribed chat's watches once its state loads,
and tells the canvas watch when that happens, so the provider republishes
the chat's canvases into the loaded state.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 4, 2026 20:24

Copilot AI 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.

🟡 Changes recommended

Unresolved lifecycle and restoration issues can lose canvas updates, reopen archived canvases, or destroy runtimes that still have active canvas subscriptions.

4 open issues
What changed in this PR

This PR unifies canvas restoration after idle runtime release and Agent Host restart for local Copilot chats, using persisted records to preserve canvas channels without storing stale URLs.

Changes:

  • Persists canvas projections and deferred closes, then reconciles them when the runtime resumes.
  • Adds chat canvas observation and republishes restored canvases after chat state loads.
  • Adds lifecycle regression tests and updates the provider contract.
File Description
src/​vs/​sessions/​contrib/​providers/​agentHost/​AGENT_HOST_SESSIONS_PROVIDER.md Documents persisted restoration and deferred-close behavior.
src/​vs/​platform/​agentHost/​test/​node/​copilotAgentSession.test.ts Tests handoffs, deferred closes, reopening, and continuity reporting.
src/​vs/​platform/​agentHost/​test/​node/​copilotAgent.test.ts Tests persistence, idle release, restart, archive handling, and cold closes.
src/​vs/​platform/​agentHost/​test/​node/​agentService.test.ts Tests canvas-watch lifecycle and host context.
src/​vs/​platform/​agentHost/​node/​copilot/​copilotAgentSession.ts Reports source-less continuity and reconciles deferred closes.
src/​vs/​platform/​agentHost/​node/​copilot/​copilotAgent.ts Stores, restores, adopts, and observes persisted canvases.
src/​vs/​platform/​agentHost/​node/​agentService.ts Connects canvas watches to subscriptions and state loading.
src/​vs/​platform/​agentHost/​common/​agent.ts Defines the optional canvas-watch provider contract.

Review effort: Balanced

Comment on lines +8030 to +8032
if (!this._canvasWatches.has(chat)) {
// Nobody observes the chat anymore, and residency may have released its session while it resumed.
await this._releaseChat(chat, operationContext);
Comment on lines +8096 to +8098
private async _claimPersistedCanvases(chat: URI, storage: URI, sdkSessionId: string): Promise<void> {
if (!this._canvasesEnabled() || this._canvasHandoffs.has(chat)) {
return;
Comment on lines +8115 to +8117
if (handoff) {
this._resumingCanvasHandoffs.delete(chat);
replacement?.adoptCanvasHandoff(handoff);
Comment on lines +8125 to +8127
void this._canvasContinuityIO.queue(chat.toString(), async () => {
if (!this._isShuttingDown && this._findChatByUri(chat) === session) {
await this._writeCanvasContinuity(chat, session.resourceUri, value);

This branch has not been deployed

No deployments
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.

Agents window: canvases don't come back after the agent host process restarts

3 participants