Skip to content

Commit 361a48b

Browse files
committed
fix: address heartbeat recovery review feedback
1 parent c90830a commit 361a48b

5 files changed

Lines changed: 98 additions & 9 deletions

File tree

‎apps/vscode-e2e/src/visual/electronHarness.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,20 @@ export async function startScene(
9191
const readyPath = path.join(temporaryRoot, "ready.json")
9292
const donePath = path.join(temporaryRoot, "done")
9393
const logPath = path.join(temporaryRoot, "scene.log")
94-
await Promise.all([fs.mkdir(workspacePath), fs.mkdir(userDataPath), fs.mkdir(extensionsPath)])
9594

9695
let app: ElectronApplication | undefined
9796
let mock: InstanceType<typeof LLMock> | undefined
9897
let page: Page | undefined
9998
try {
99+
// Settle all directory operations before failure cleanup removes their parent.
100+
const directories = await Promise.allSettled([
101+
fs.mkdir(workspacePath),
102+
fs.mkdir(userDataPath),
103+
fs.mkdir(extensionsPath),
104+
])
105+
for (const result of directories) {
106+
if (result.status === "rejected") throw result.reason
107+
}
100108
if (scenario.startMock) {
101109
mock = new LLMock({ port: 0 })
102110
mock.addFixture({
@@ -157,7 +165,7 @@ export async function startScene(
157165
return { app, donePath, logPath, mock, page, temporaryRoot, userDataPath, taskIds: ready.taskIds }
158166
} catch (error) {
159167
if (page) await page.screenshot({ path: testInfo.outputPath("workbench.png") }).catch(() => undefined)
160-
await attachLogs(userDataPath, logPath, testInfo)
168+
await attachLogs(userDataPath, logPath, testInfo).catch(() => undefined)
161169
await fs.writeFile(donePath, "done\n", "utf8").catch(() => undefined)
162170
if (app) await stopTracing(app, testInfo)
163171
await app?.close().catch(() => undefined)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ test.beforeAll(async () => {
9090
test("recovers when heartbeat delivery stops and resumes periodic heartbeats", async ({
9191
browserName: _browserName,
9292
}, testInfo) => {
93-
test.setTimeout(4 * 60_000)
93+
// Allow startup, stale detection, recovery observations, and cleanup.
94+
test.setTimeout(8 * 60_000)
9495
const running = await startScene(vscodeExecutablePath, scenario, testInfo)
9596
try {
9697
const frame = await waitForContent(running.page)
@@ -121,7 +122,8 @@ test("recovers when heartbeat delivery stops and resumes periodic heartbeats", a
121122
})
122123

123124
test("recovers after the isolated Zoo Code renderer is terminated", async ({ browserName: _browserName }, testInfo) => {
124-
test.setTimeout(4 * 60_000)
125+
// Allow startup, stale detection, recovery observations, and cleanup.
126+
test.setTimeout(8 * 60_000)
125127
const running = await startScene(
126128
vscodeExecutablePath,
127129
{ ...scenario, scene: "chat", startMock: true, landmark: CHAT_RESULT },
@@ -193,7 +195,8 @@ test("recovers after the isolated Zoo Code renderer is terminated", async ({ bro
193195
test("keeps the same document when the sidebar is hidden past the stale threshold", async ({
194196
browserName: _browserName,
195197
}, testInfo) => {
196-
test.setTimeout(4 * 60_000)
198+
// Allow 210s startup, 125s hidden, two 45s polls, and cleanup.
199+
test.setTimeout(8 * 60_000)
197200
const running = await startScene(vscodeExecutablePath, scenario, testInfo)
198201
try {
199202
const frame = await waitForContent(running.page)

‎apps/vscode-e2e/src/visual/sceneController.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,14 @@ export async function run(): Promise<void> {
159159
} catch {
160160
return
161161
}
162+
let fixture: WebviewThemeFixture
163+
try {
164+
fixture = await api.captureWebviewThemeFixture()
165+
} catch (error) {
166+
await log(`theme probe will retry: ${String(error)}`)
167+
return
168+
}
162169
await fs.unlink(request)
163-
const fixture = await api.captureWebviewThemeFixture()
164170
await fs.writeFile(`${request}.result`, JSON.stringify({ fixture, taskIds: api.getCurrentTaskStack() }), "utf8")
165171
})
166172
}

‎src/core/webview/ClineProvider.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export class ClineProvider
252252
private webviewHostRecoveryInProgress = false
253253
private webviewRecoveryFailureReported = false
254254
private lastReceivedWebviewHeartbeatAt: number | undefined
255+
private webviewHeartbeatCount = 0
255256
private webviewHealthySince: number | undefined
256257
private static readonly WEBVIEW_RECOVERY_CONFIRM_MS = 10_000
257258
private static readonly WEBVIEW_HEALTHY_WINDOW_MS = 60_000
@@ -3570,6 +3571,7 @@ export class ClineProvider
35703571
this.webviewHealthySince = now
35713572
}
35723573
this.lastReceivedWebviewHeartbeatAt = now
3574+
this.webviewHeartbeatCount++
35733575
if (this.webviewRecoveryConfirmation) {
35743576
this.log("[Zoo Code] Webview heartbeat returned after recovery")
35753577
}
@@ -3619,7 +3621,10 @@ export class ClineProvider
36193621
}
36203622
if (now - this.lastWebviewHeartbeatAt <= ClineProvider.WEBVIEW_HEARTBEAT_STALE_MS) return
36213623
if (this.webviewRecoveryAttempts >= ClineProvider.WEBVIEW_RECOVERY_ATTEMPT_LIMIT) {
3622-
this.reportWebviewRecoveryFailure("Recovery attempt limit reached without sustained heartbeats")
3624+
// Let the final attempt finish generating HTML before declaring recovery stopped.
3625+
if (this.webviewRecoveryInFlightEpoch !== this.webviewRecoveryEpoch) {
3626+
this.reportWebviewRecoveryFailure("Recovery attempt limit reached without sustained heartbeats")
3627+
}
36233628
return
36243629
}
36253630
this.log("[Zoo Code] Webview heartbeat stale while visible; attempting recovery")
@@ -3801,7 +3806,7 @@ export class ClineProvider
38013806
this.webviewRecoveryFailureReported = true
38023807
this.webviewRecoveryAttempts = ClineProvider.WEBVIEW_RECOVERY_ATTEMPT_LIMIT
38033808
this.log(`[Zoo Code] Webview recovery stopped: ${reason}`)
3804-
const revision = this.webviewHeartbeatRevision
3809+
const heartbeatCount = this.webviewHeartbeatCount
38053810
void Promise.resolve(
38063811
vscode.window.showWarningMessage(
38073812
"Zoo Code's interface is not responding. Reloading the VS Code window will restart extensions and may interrupt running tasks.",
@@ -3810,7 +3815,7 @@ export class ClineProvider
38103815
),
38113816
)
38123817
.then(async (action) => {
3813-
if (action === "Reload Window" && !this._disposed && this.webviewHeartbeatRevision === revision) {
3818+
if (action === "Reload Window" && !this._disposed && this.webviewHeartbeatCount === heartbeatCount) {
38143819
await vscode.commands.executeCommand("workbench.action.reloadWindow")
38153820
}
38163821
if (action === "Show Logs") this.outputChannel.show()

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,11 +1255,16 @@ describe("ClineProvider", () => {
12551255
expect(assignHtml).toHaveBeenCalledTimes(3)
12561256
// One mount heartbeat is insufficient: a new healthy window must
12571257
// span a full minute of periodic heartbeats before replenishing.
1258+
const attempts = provider["webviewRecoveryAttempts"]
1259+
expect(attempts).toBe(3)
12581260
provider.updateWebviewHeartbeat()
1261+
expect(provider["webviewRecoveryAttempts"]).toBe(attempts)
12591262
await vi.advanceTimersByTimeAsync(30_000)
12601263
provider.updateWebviewHeartbeat()
1264+
expect(provider["webviewRecoveryAttempts"]).toBe(attempts)
12611265
await vi.advanceTimersByTimeAsync(30_000)
12621266
provider.updateWebviewHeartbeat()
1267+
expect(provider["webviewRecoveryAttempts"]).toBe(0)
12631268
await vi.advanceTimersByTimeAsync(120_000)
12641269
expect(assignHtml).toHaveBeenCalledTimes(4)
12651270
})
@@ -1404,6 +1409,44 @@ describe("ClineProvider", () => {
14041409
expect(vscode.commands.executeCommand).not.toHaveBeenCalledWith("workbench.action.reloadWindow")
14051410
})
14061411

1412+
test("keeps the Reload Window action available after visibility grace resets", async () => {
1413+
await provider.resolveWebviewView(mockWebviewView)
1414+
vi.mocked(vscode.commands.getCommands).mockResolvedValue([])
1415+
let choose: (value: string) => void = () => {
1416+
throw new Error("Notification not shown")
1417+
}
1418+
const choice = new Promise<string>((resolve) => {
1419+
choose = resolve
1420+
})
1421+
vi.mocked(vscode.window.showWarningMessage).mockImplementation(vi.fn().mockReturnValue(choice))
1422+
await vi.advanceTimersByTimeAsync(130_000)
1423+
1424+
visibilityCallback()
1425+
choose("Reload Window")
1426+
await vi.advanceTimersByTimeAsync(0)
1427+
1428+
expect(vscode.commands.executeCommand).toHaveBeenCalledWith("workbench.action.reloadWindow")
1429+
})
1430+
1431+
test("waits for the final HTML attempt to settle before reporting the limit", async () => {
1432+
await provider.resolveWebviewView(mockWebviewView)
1433+
provider["webviewRecoveryAttempts"] = 2
1434+
let rejectHtml: (error: Error) => void = () => {
1435+
throw new Error("HTML not requested")
1436+
}
1437+
const html = new Promise<string>((_resolve, reject) => {
1438+
rejectHtml = reject
1439+
})
1440+
provider["getWebviewHtml"] = vi.fn<(webview: vscode.Webview) => Promise<string>>().mockReturnValue(html)
1441+
await vi.advanceTimersByTimeAsync(240_000)
1442+
expect(provider["webviewRecoveryAttempts"]).toBe(3)
1443+
expect(vscode.window.showWarningMessage).not.toHaveBeenCalled()
1444+
1445+
rejectHtml(new Error("cannot generate"))
1446+
await vi.advanceTimersByTimeAsync(60_000)
1447+
expect(vscode.window.showWarningMessage).toHaveBeenCalledTimes(1)
1448+
})
1449+
14071450
test("bounds failed HTML generation and reports the exhausted budget once", async () => {
14081451
await provider.resolveWebviewView(mockWebviewView)
14091452
const generate = vi.spyOn(provider, "getState").mockRejectedValue(new Error("cannot generate"))
@@ -2068,6 +2111,30 @@ describe("ClineProvider", () => {
20682111
} as unknown as vscode.WebviewPanel
20692112
})
20702113

2114+
test("disposes the current tab provider and its watchdog registration", async () => {
2115+
const onDispose: Array<() => unknown> = []
2116+
const registration = { dispose: vi.fn() }
2117+
vi.mocked(mockWebviewPanel.onDidDispose).mockImplementation((listener, _thisArg, disposables) => {
2118+
onDispose.push(listener)
2119+
const disposable = onDispose.length === 1 ? registration : { dispose: vi.fn() }
2120+
disposables?.push(disposable)
2121+
return disposable
2122+
})
2123+
await provider.resolveWebviewView(mockWebviewPanel)
2124+
expect(provider["webviewWatchdogInterval"]).not.toBeNull()
2125+
const dispose = vi.spyOn(provider, "dispose")
2126+
const html = mockWebviewPanel.webview.html
2127+
2128+
await onDispose[0]()
2129+
2130+
expect(dispose).toHaveBeenCalledTimes(1)
2131+
expect(provider["_disposed"]).toBe(true)
2132+
expect(provider["webviewWatchdogInterval"]).toBeNull()
2133+
expect(registration.dispose).toHaveBeenCalledTimes(1)
2134+
await vi.advanceTimersByTimeAsync(600_000)
2135+
expect(mockWebviewPanel.webview.html).toBe(html)
2136+
})
2137+
20712138
test.each([false, true])(
20722139
"replaces the tab host without disposing its provider or task (interrupted: %s)",
20732140
async (interrupted) => {

0 commit comments

Comments
 (0)