Skip to content

Commit 190f6c1

Browse files
committed
fix(webview): preserve recovery state for delayed sidebar replacements
1 parent 437abce commit 190f6c1

5 files changed

Lines changed: 167 additions & 10 deletions

File tree

‎.github/workflows/visual-regression.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ jobs:
8585

8686
extension-host-visual:
8787
runs-on: ubuntu-latest
88-
timeout-minutes: 20
88+
# Includes dependency setup, builds, repeated visuals, and production watchdog waits.
89+
timeout-minutes: 35
8990
container:
9091
image: mcr.microsoft.com/playwright:v1.62.1-noble@sha256:dcc5531e97840b9b5e794f2814476b21571c5124a3fca2267d73041f56e7580e
9192
options: --ipc=host

‎apps/vscode-e2e/src/visual/heartbeat.electron.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ test("recovers after the isolated Zoo Code renderer is terminated", async ({ bro
155155
expect(processes.webview).not.toBe(processes.workbench)
156156
process.kill(processes.webview, "SIGKILL")
157157

158-
// Use production timers: the next stale tick is at most two minutes away.
158+
// Allow stale detection (up to 150s), confirmation and replacement waits,
159+
// plus webview loading. A passing fast run does not bound the slow path.
159160
await expect
160161
.poll(
161162
async () => {
@@ -171,7 +172,7 @@ test("recovers after the isolated Zoo Code renderer is terminated", async ({ bro
171172
return "unavailable"
172173
}
173174
},
174-
{ timeout: 150_000, intervals: [1_000] },
175+
{ timeout: 210_000, intervals: [1_000] },
175176
)
176177
.toBe("recovered")
177178
await waitForContent(running.page, CHAT_RESULT)

‎apps/vscode-e2e/src/visual/heartbeat.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ The scenarios use isolated VS Code instances and the production 60-second watchd
1515

1616
The message-port interceptor follows the pinned VS Code implementation. Check it when upgrading VS Code. It does not modify the extension or fake extension-host timers. Heartbeat blocking and renderer termination are separate failure modes; passing the former does not establish crash recovery.
1717

18-
At PR head `a985f03ba`, terminating the isolated renderer reproduces a gray sidebar: the watchdog logs recovery, but assigning fresh HTML does not restore the UI within 150 seconds. Recovery now waits ten seconds for a heartbeat after assigning HTML, then recreates the affected host if the renderer has not responded. Sidebar recovery uses the generated view-removal and focus commands after checking their availability; tab recovery replaces its panel while retaining its provider. The crash test covers the real sidebar path. Unit tests cover tab replacement and task ownership.
18+
A gray sidebar can persist after renderer termination even when the watchdog assigns fresh HTML. HTML delivery alone cannot restart a dead renderer. Recovery now waits ten seconds for a heartbeat after assigning HTML, then recreates the affected host if the renderer has not responded. Sidebar recovery uses the generated view-removal and focus commands after checking their availability; tab recovery replaces its panel while retaining its provider. The crash test covers the real sidebar path. Unit tests cover tab replacement and task ownership.
1919

20-
A recovery attempt includes HTML-generation errors. Three attempts exhaust the budget; a startup heartbeat alone does not reset it. Periodic heartbeats must span a 60-second healthy window to replenish the budget. Exhaustion or unavailable/failed scoped host recovery logs once and offers a non-modal Reload Window or Show Logs action. Window reload is never automatic. A late action from an old notification is ignored after liveness returns.
20+
A recovery attempt includes HTML-generation errors. Three attempts exhaust the budget; a startup heartbeat alone does not reset it. Periodic heartbeats must span a 60-second healthy window to replenish the budget. Exhaustion or unavailable/failed scoped host recovery logs once and offers a non-modal Reload Window or Show Logs action. A sidebar replacement that arrives after the ten-second wait retains the incident budget and displayed task. Only sustained healthy heartbeats replenish that budget. Window reload is never automatic. A late action from an old notification is ignored after liveness returns.
2121

2222
Keep exact timing boundaries, individual post-await guards, and reload-budget assertions in provider unit tests. Exercise OS minimize and sleep/resume behavior manually; the Electron scenarios do not reproduce OS suspension or its scheduling order.

‎src/core/webview/ClineProvider.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ export class ClineProvider
250250
private webviewRecoveryAttempts = 0
251251
private webviewRecoveryConfirmation: ReturnType<typeof setTimeout> | undefined
252252
private webviewHostRecoveryInProgress = false
253+
// VS Code may resolve the focused sidebar after our bounded wait expires.
254+
// Keep that repair distinct from a user-opened view until it arrives.
255+
private pendingSidebarRecovery = false
253256
private webviewRecoveryFailureReported = false
254257
private lastReceivedWebviewHeartbeatAt: number | undefined
255258
private webviewHeartbeatCount = 0
@@ -864,7 +867,7 @@ export class ClineProvider
864867
this.webviewRecoveryEpoch++
865868
// User-opened replacements get a fresh budget; recreating a failed host
866869
// must preserve its incident budget so crash loops remain bounded.
867-
if (!this.webviewHostRecoveryInProgress) {
870+
if (!this.webviewHostRecoveryInProgress && !this.pendingSidebarRecovery) {
868871
this.webviewRecoveryAttempts = 0
869872
this.webviewRecoveryFailureReported = false
870873
}
@@ -1073,12 +1076,16 @@ export class ClineProvider
10731076
}
10741077

10751078
async resolveWebviewView(webviewView: vscode.WebviewView | vscode.WebviewPanel) {
1079+
// Capture intent before yielding: host recovery can time out while this
1080+
// resolve is still generating HTML or hydrating state.
1081+
const recoveringHost = this.webviewHostRecoveryInProgress || this.pendingSidebarRecovery
10761082
// A different view replaces the current one: dispose its subscriptions so
10771083
// the replaced view can no longer dispatch messages or refresh provider state.
10781084
if (this.view !== webviewView) {
10791085
this.clearWebviewResources()
10801086
}
10811087

1088+
this.pendingSidebarRecovery = false
10821089
this.view = webviewView
10831090
const inTabMode = "onDidChangeViewState" in webviewView
10841091

@@ -1243,7 +1250,7 @@ export class ClineProvider
12431250
const currentTask = this.getCurrentTask()
12441251
// Host recreation is a UI repair, including when the displayed task is
12451252
// interrupted. It must not evict or resume that task as a side effect.
1246-
if (!this.webviewHostRecoveryInProgress && (!currentTask || currentTask.abandoned || currentTask.abort)) {
1253+
if (!recoveringHost && (!currentTask || currentTask.abandoned || currentTask.abort)) {
12471254
await this.removeClineFromStack()
12481255
}
12491256

@@ -3737,6 +3744,7 @@ export class ClineProvider
37373744
if (this.webviewHostRecoveryInProgress || this._disposed || this.view !== view || !view.visible) return
37383745
const revision = this.webviewHeartbeatRevision
37393746
let started = false
3747+
let sidebarRemoved = false
37403748
try {
37413749
let commands: string[] | undefined
37423750
if (!("onDidChangeViewState" in view)) {
@@ -3777,7 +3785,9 @@ export class ClineProvider
37773785
else replacement.dispose()
37783786
await resolved
37793787
} else {
3788+
this.pendingSidebarRecovery = true
37803789
await vscode.commands.executeCommand(`${ClineProvider.sideBarId}.removeView`)
3790+
sidebarRemoved = true
37813791
if (this._disposed) return
37823792
await vscode.commands.executeCommand(`${ClineProvider.sideBarId}.focus`, { preserveFocus: true })
37833793
await pWaitFor(
@@ -3791,6 +3801,9 @@ export class ClineProvider
37913801
)
37923802
}
37933803
} catch (error) {
3804+
// If removal failed, no replacement is expected. A removed sidebar
3805+
// can still resolve after the wait expires and must retain its task.
3806+
if (started && !sidebarRemoved) this.pendingSidebarRecovery = false
37943807
// A late discovery failure must not stop a recovered/replaced view.
37953808
// Once recreation starts, disposal of the old view is expected.
37963809
if (

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { TaskRegistry } from "../../task/TaskRegistry"
77
import Anthropic from "@anthropic-ai/sdk"
88
import * as vscode from "vscode"
99
import axios from "axios"
10+
import pWaitFor from "p-wait-for"
1011

1112
import {
1213
type ProviderSettingsEntry,
@@ -1294,6 +1295,7 @@ describe("ClineProvider", () => {
12941295
preserveFocus: true,
12951296
})
12961297
expect(provider["view"]).toBe(replacement)
1298+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
12971299
expect(provider["webviewRecoveryAttempts"]).toBe(1)
12981300
expect(provider.getCurrentTask()).toBe(task)
12991301
expect(evict).not.toHaveBeenCalled()
@@ -1303,6 +1305,140 @@ describe("ClineProvider", () => {
13031305
},
13041306
)
13051307

1308+
describe("sidebar replacement timing", () => {
1309+
beforeEach(async () => {
1310+
const actual = await vi.importActual<typeof import("p-wait-for")>("p-wait-for")
1311+
vi.mocked(pWaitFor).mockImplementation(actual.default)
1312+
})
1313+
1314+
afterEach(() => {
1315+
vi.mocked(pWaitFor).mockReset().mockResolvedValue(undefined)
1316+
})
1317+
1318+
function removeSidebarOnCommand() {
1319+
vi.mocked(vscode.commands.executeCommand).mockImplementation(async (command) => {
1320+
if (command === `${ClineProvider.sideBarId}.removeView`) disposeCallback()
1321+
})
1322+
}
1323+
1324+
test("warns once after ten seconds without a replacement and releases recovery ownership", async () => {
1325+
await provider.resolveWebviewView(mockWebviewView)
1326+
removeSidebarOnCommand()
1327+
await vi.advanceTimersByTimeAsync(130_000)
1328+
expect(provider["webviewHostRecoveryInProgress"]).toBe(true)
1329+
await vi.advanceTimersByTimeAsync(9_999)
1330+
expect(vscode.window.showWarningMessage).not.toHaveBeenCalled()
1331+
await vi.advanceTimersByTimeAsync(1)
1332+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
1333+
expect(provider["webviewRecoveryAttempts"]).toBe(3)
1334+
expect(vscode.window.showWarningMessage).toHaveBeenCalledTimes(1)
1335+
await vi.advanceTimersByTimeAsync(120_000)
1336+
expect(vscode.window.showWarningMessage).toHaveBeenCalledTimes(1)
1337+
})
1338+
1339+
test("settles the replacement wait when the provider is disposed", async () => {
1340+
await provider.resolveWebviewView(mockWebviewView)
1341+
removeSidebarOnCommand()
1342+
await vi.advanceTimersByTimeAsync(130_000)
1343+
expect(provider["webviewHostRecoveryInProgress"]).toBe(true)
1344+
await provider.dispose()
1345+
await vi.advanceTimersByTimeAsync(50)
1346+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
1347+
expect(vscode.window.showWarningMessage).not.toHaveBeenCalled()
1348+
})
1349+
1350+
test.each([5_000, 11_000])(
1351+
"retains an interrupted task and budget when replacement starts after %s ms",
1352+
async (delay) => {
1353+
await provider.resolveWebviewView(mockWebviewView)
1354+
const task = new Task(defaultTaskOptions)
1355+
task.abort = true
1356+
provider["taskRegistry"].push(task)
1357+
const remove = vi.spyOn(provider, "removeClineFromStack")
1358+
removeSidebarOnCommand()
1359+
await vi.advanceTimersByTimeAsync(130_000 + delay)
1360+
const attempts = delay < 10_000 ? 1 : 3
1361+
expect(provider["webviewRecoveryAttempts"]).toBe(attempts)
1362+
expect(provider["webviewHostRecoveryInProgress"]).toBe(delay < 10_000)
1363+
1364+
const replacement = { ...mockWebviewView, webview: { ...mockWebviewView.webview, html: "" } }
1365+
await provider.resolveWebviewView(replacement)
1366+
await vi.advanceTimersByTimeAsync(50)
1367+
1368+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
1369+
expect(provider["pendingSidebarRecovery"]).toBe(false)
1370+
expect(provider["webviewRecoveryAttempts"]).toBe(attempts)
1371+
expect(provider["webviewRecoveryFailureReported"]).toBe(delay >= 10_000)
1372+
expect(vscode.window.showWarningMessage).toHaveBeenCalledTimes(delay < 10_000 ? 0 : 1)
1373+
expect(provider.getCurrentTask()).toBe(task)
1374+
expect(remove).not.toHaveBeenCalled()
1375+
expect(task.dispose).not.toHaveBeenCalled()
1376+
expect(task.abortTask).not.toHaveBeenCalled()
1377+
},
1378+
)
1379+
1380+
test("retains recovery intent while replacement HTML generation outlasts the wait", async () => {
1381+
await provider.resolveWebviewView(mockWebviewView)
1382+
const task = new Task(defaultTaskOptions)
1383+
task.abort = true
1384+
provider["taskRegistry"].push(task)
1385+
const remove = vi.spyOn(provider, "removeClineFromStack")
1386+
removeSidebarOnCommand()
1387+
await vi.advanceTimersByTimeAsync(130_000)
1388+
let finishHtml: (html: string) => void = () => {
1389+
throw new Error("HTML generation has not started")
1390+
}
1391+
provider["getWebviewHtml"] = vi.fn(
1392+
() =>
1393+
new Promise<string>((resolve) => {
1394+
finishHtml = resolve
1395+
}),
1396+
)
1397+
const replacement = { ...mockWebviewView, webview: { ...mockWebviewView.webview, html: "" } }
1398+
const resolving = provider.resolveWebviewView(replacement)
1399+
await vi.advanceTimersByTimeAsync(10_000)
1400+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
1401+
finishHtml("replacement-html")
1402+
await resolving
1403+
expect(provider["webviewRecoveryAttempts"]).toBe(3)
1404+
expect(provider.getCurrentTask()).toBe(task)
1405+
expect(remove).not.toHaveBeenCalled()
1406+
})
1407+
})
1408+
1409+
test("recreates the sidebar again after a second incident", async () => {
1410+
await provider.resolveWebviewView(mockWebviewView)
1411+
const replacements = [0, 1].map(() => ({
1412+
...mockWebviewView,
1413+
webview: { ...mockWebviewView.webview, html: "" },
1414+
}))
1415+
vi.mocked(vscode.commands.executeCommand).mockImplementation(async (command) => {
1416+
if (command === `${ClineProvider.sideBarId}.removeView`) disposeCallback()
1417+
if (command === `${ClineProvider.sideBarId}.focus`) {
1418+
const next = replacements.shift()
1419+
if (!next) throw new Error("Unexpected third replacement")
1420+
await provider.resolveWebviewView(next)
1421+
}
1422+
})
1423+
await vi.advanceTimersByTimeAsync(130_000)
1424+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
1425+
provider.updateWebviewHeartbeat()
1426+
await vi.advanceTimersByTimeAsync(30_000)
1427+
provider.updateWebviewHeartbeat()
1428+
await vi.advanceTimersByTimeAsync(30_000)
1429+
provider.updateWebviewHeartbeat()
1430+
expect(provider["webviewRecoveryAttempts"]).toBe(0)
1431+
await vi.advanceTimersByTimeAsync(130_000)
1432+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
1433+
expect(provider["webviewRecoveryAttempts"]).toBe(1)
1434+
expect(replacements).toHaveLength(0)
1435+
expect(
1436+
vi
1437+
.mocked(vscode.commands.executeCommand)
1438+
.mock.calls.filter(([command]) => command === `${ClineProvider.sideBarId}.removeView`),
1439+
).toHaveLength(2)
1440+
})
1441+
13061442
test.each(["heartbeat", "hidden", "disposed", "suspended"] as const)(
13071443
"does not recreate the host after %s during the confirmation window",
13081444
async (event) => {
@@ -1365,6 +1501,8 @@ describe("ClineProvider", () => {
13651501
await vi.advanceTimersByTimeAsync(600_000)
13661502

13671503
expect(assignHtml).toHaveBeenCalledTimes(1)
1504+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
1505+
expect(provider["pendingSidebarRecovery"]).toBe(false)
13681506
expect(vscode.window.showWarningMessage).toHaveBeenCalledTimes(1)
13691507
expect(vscode.commands.executeCommand).not.toHaveBeenCalledWith("workbench.action.reloadWindow")
13701508
expect(mockOutputChannel.appendLine).toHaveBeenCalledWith(
@@ -1501,12 +1639,14 @@ describe("ClineProvider", () => {
15011639
expect(vscode.window.showWarningMessage).toHaveBeenCalledTimes(1)
15021640
})
15031641

1504-
test("gives a replacement view a fresh recovery budget after exhausting attempts", async () => {
1642+
test("gives a user-opened replacement a fresh budget after unavailable host recovery", async () => {
15051643
await provider.resolveWebviewView(mockWebviewView)
1644+
vi.mocked(vscode.commands.getCommands).mockResolvedValue([])
15061645
const generateHtml = vi.spyOn(provider, "getState")
15071646

1508-
await vi.advanceTimersByTimeAsync(480_000)
1509-
expect(generateHtml).toHaveBeenCalledTimes(3)
1647+
await vi.advanceTimersByTimeAsync(130_000)
1648+
expect(generateHtml).toHaveBeenCalledTimes(1)
1649+
expect(provider["webviewRecoveryAttempts"]).toBe(3)
15101650

15111651
disposeCallback()
15121652
const replacementView = {
@@ -2194,6 +2334,7 @@ describe("ClineProvider", () => {
21942334
expect(mockWebviewPanel.dispose).toHaveBeenCalledTimes(1)
21952335
expect(provider["view"]).toBe(replacement)
21962336
expect(provider["_disposed"]).toBe(false)
2337+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
21972338
expect(provider["webviewRecoveryAttempts"]).toBe(1)
21982339
expect(provider.getCurrentTask()).toBe(task)
21992340
expect(evict).not.toHaveBeenCalled()
@@ -2218,6 +2359,7 @@ describe("ClineProvider", () => {
22182359

22192360
expect(mockWebviewPanel.dispose).not.toHaveBeenCalled()
22202361
expect(replacement.dispose).toHaveBeenCalledTimes(1)
2362+
expect(provider["webviewHostRecoveryInProgress"]).toBe(false)
22212363
expect(provider["view"]).toBe(mockWebviewPanel)
22222364
expect(provider.getCurrentTask()).toBe(task)
22232365
expect(task.dispose).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)