Skip to content

Commit 6b76027

Browse files
fix(webview): reset isViewLaunched when the renderer is replaced
isViewLaunched was written twice in production: initialised false and set true by the webviewDidLaunch handler. Nothing lowered it. Replacing the view (clearWebviewResources runs for a new view, a disposed sidebar view and clearAllTasks) and replacing the renderer in place (reloadWebviewForRecovery assigns new HTML) both discard the document that held the message listener, yet the provider kept believing a post could be received. A durable view-state mutation in that window took the post branch of postViewStateClaimToWebview instead of queueing, and a post that resolves true against a page with no listener deletes the claim - leaving a durable entry whose claim no renderer ever received, so the next launch could not adopt this view's state. clearWebviewResources now lowers the flag for every path that drops the view, and reloadWebviewForRecovery lowers it before assigning new HTML. The gate itself is unchanged: with the flag down the queue holds the exact token, and the replacement renderer's own webviewDidLaunch re-arms the flag and flushes it. Control (CTRL-2226D): with both resets removed the two new tests fail at the isViewLaunched expectation immediately after the replacement - the replacement-view save reports claim 'delivered' and posts into an unlaunched renderer, and the recovery case keeps the flag true after the HTML swap. The unpatched file was restored byte-identically and core/webview (34 files, 900 tests) and the launched-flag consumers (activate, extension, api, diagnostics: 28 files, 300 tests) pass on the fixed tree; eslint --max-warnings=0 on both touched files and src check-types are clean, and src/eslint-suppressions.json is untouched.
1 parent 2f2485e commit 6b76027

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,11 @@ export class ClineProvider
15091509
private clearWebviewResources() {
15101510
this.rejectPendingThemeFixtureProbes(new Error("Webview was disposed before the theme fixture probe completed"))
15111511
this.stopWebviewWatchdog()
1512+
// The renderer behind this view is gone or about to be replaced, and its message
1513+
// listener dies with it: a post can still resolve true while nobody is there to receive
1514+
// it. The flag goes down here so a claim minted before the replacement launches is held
1515+
// in the queue and flushed by that replacement's own `webviewDidLaunch`.
1516+
this.isViewLaunched = false
15121517
// Invalidate any recovery reload still awaiting its HTML so it cannot
15131518
// reassign webview.html on the disposed view.
15141519
this.webviewRecoveryEpoch++
@@ -4556,6 +4561,10 @@ export class ClineProvider
45564561
) {
45574562
return
45584563
}
4564+
// Assigning new HTML discards the live document and its message listener, so the launched
4565+
// flag goes down with it; the regenerated renderer re-arms it from its own
4566+
// `webviewDidLaunch`, and any claim queued in between reaches it then.
4567+
this.isViewLaunched = false
45594568
view.webview.html = html
45604569
this.resetWebviewHeartbeatGrace()
45614570
const revision = this.webviewHeartbeatRevision

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

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6192,6 +6192,132 @@ describe("ClineProvider", () => {
61926192
await detached.dispose()
61936193
})
61946194

6195+
test("holds a claim minted after a renderer replacement and delivers it on the next launch", async () => {
6196+
// The interleaving the queue exists for: replacing the view drops the renderer's message
6197+
// listener, a durable view-state mutation mints a claim while the replacement renderer has
6198+
// no listener yet, and a post that resolves true against the page being replaced deletes a
6199+
// claim that renderer never received, so its launch cannot adopt this view's state.
6200+
const replaced = createProvider()
6201+
const firstView = {
6202+
...mockWebviewView,
6203+
webview: {
6204+
...mockWebviewView.webview,
6205+
onDidReceiveMessage: vi.fn(),
6206+
postMessage: vi.fn().mockResolvedValue(true),
6207+
},
6208+
}
6209+
await replaced.resolveWebviewView(firstView)
6210+
const firstLaunch = (firstView.webview.onDidReceiveMessage as ReturnType<typeof vi.fn>).mock.calls[0][0]
6211+
await firstLaunch({ type: "webviewDidLaunch" })
6212+
expect(replaced.isViewLaunched).toBe(true)
6213+
6214+
// A different view replaces the first one. The replacement renderer has not sent
6215+
// `webviewDidLaunch`, so nothing listens yet, yet its postMessage still resolves true -
6216+
// exactly the webview that swallows a claim the host believes was delivered.
6217+
const secondView = {
6218+
...mockWebviewView,
6219+
webview: {
6220+
...mockWebviewView.webview,
6221+
onDidReceiveMessage: vi.fn(),
6222+
postMessage: vi.fn().mockResolvedValue(true),
6223+
},
6224+
}
6225+
await replaced.resolveWebviewView(secondView)
6226+
expect(replaced.isViewLaunched).toBe(false)
6227+
6228+
const result = await replaced.saveViewState("mode", "architect")
6229+
const mintedClaim = storedEntries()[replaced.viewId]?.claimToken
6230+
6231+
// Held rather than posted: the entry keeps the token it just minted and the queue holds
6232+
// that same token for the launch that can actually receive it.
6233+
expect(result).toEqual({ persisted: true, claim: "queued" })
6234+
expect(mintedClaim).toEqual(expect.any(String))
6235+
expect(replaced["pendingViewStateClaims"].get(replaced.viewId)).toBe(mintedClaim)
6236+
expect(
6237+
secondView.webview.postMessage.mock.calls
6238+
.map((call: unknown[]) => call[0])
6239+
.filter(
6240+
(message: unknown): message is { type: string; viewStateId: string; viewStateClaim: string } =>
6241+
typeof message === "object" &&
6242+
message !== null &&
6243+
(message as { type?: unknown }).type === "viewStateClaim",
6244+
),
6245+
).toEqual([])
6246+
6247+
// The replacement renderer launches: the claim it never received arrives carrying the token
6248+
// the record already stores, which is what lets that launch adopt this view's state.
6249+
const secondLaunch = (secondView.webview.onDidReceiveMessage as ReturnType<typeof vi.fn>).mock.calls[0][0]
6250+
await secondLaunch({ type: "webviewDidLaunch" })
6251+
6252+
const claimPosts = secondView.webview.postMessage.mock.calls
6253+
.map((call: unknown[]) => call[0])
6254+
.filter(
6255+
(message: unknown): message is { type: string; viewStateId: string; viewStateClaim: string } =>
6256+
typeof message === "object" &&
6257+
message !== null &&
6258+
(message as { type?: unknown }).type === "viewStateClaim",
6259+
)
6260+
expect(claimPosts).toEqual([
6261+
{ type: "viewStateClaim", viewStateId: replaced.viewId, viewStateClaim: mintedClaim },
6262+
])
6263+
expect(replaced["pendingViewStateClaims"].size).toBe(0)
6264+
6265+
await replaced.dispose()
6266+
})
6267+
6268+
test("lowers the launched flag when recovery replaces the renderer's HTML", async () => {
6269+
// Assigning new HTML discards the live document, listener included. A provider that still
6270+
// believes the renderer is launched posts claims into a page that is being replaced.
6271+
// Recovery schedules its own confirmation timer; fake timers keep it off the run so it
6272+
// cannot log after the environment is gone.
6273+
vi.useFakeTimers()
6274+
const recovered = createProvider()
6275+
recovered["view"] = mockWebviewView
6276+
recovered.isViewLaunched = true
6277+
recovered["getWebviewHtml"] = vi.fn().mockResolvedValue("recovered-html")
6278+
const postMessageSpy = vi.spyOn(recovered, "postMessageToWebview").mockResolvedValue(true)
6279+
6280+
await recovered["reloadWebviewForRecovery"]()
6281+
6282+
expect(recovered.isViewLaunched).toBe(false)
6283+
6284+
// A claim minted after the swap is held for the regenerated renderer's own launch instead
6285+
// of being posted into a page whose listener is already gone.
6286+
const result = await recovered.saveViewState("mode", "architect")
6287+
const storedClaim = storedEntries()[recovered.viewId]?.claimToken
6288+
expect(result).toEqual({ persisted: true, claim: "queued" })
6289+
expect(storedClaim).toEqual(expect.any(String))
6290+
expect(recovered["pendingViewStateClaims"].get(recovered.viewId)).toBe(storedClaim)
6291+
expect(
6292+
postMessageSpy.mock.calls
6293+
.map((call: unknown[]) => call[0])
6294+
.filter(
6295+
(message: unknown): message is { type: string; viewStateId: string; viewStateClaim: string } =>
6296+
typeof message === "object" &&
6297+
message !== null &&
6298+
(message as { type?: unknown }).type === "viewStateClaim",
6299+
),
6300+
).toEqual([])
6301+
6302+
// The regenerated renderer reports its own launch and the held claim reaches it.
6303+
recovered.isViewLaunched = true
6304+
await recovered.deliverPendingViewStateClaims()
6305+
expect(
6306+
postMessageSpy.mock.calls
6307+
.map((call: unknown[]) => call[0])
6308+
.filter(
6309+
(message: unknown): message is { type: string; viewStateId: string; viewStateClaim: string } =>
6310+
typeof message === "object" &&
6311+
message !== null &&
6312+
(message as { type?: unknown }).type === "viewStateClaim",
6313+
),
6314+
).toEqual([{ type: "viewStateClaim", viewStateId: recovered.viewId, viewStateClaim: storedClaim }])
6315+
6316+
recovered["clearWebviewRecoveryConfirmation"]()
6317+
postMessageSpy.mockRestore()
6318+
await recovered.dispose()
6319+
vi.useRealTimers()
6320+
})
61956321
test("reports a queued claim on the save result when the webview has not launched", async () => {
61966322
// The persistence-integrity gap this result exists to close: the durable entry carries a
61976323
// claim the webview has never been handed. A save that resolves as a complete success

0 commit comments

Comments
 (0)