Skip to content

Commit 3dd0854

Browse files
fix(webview): undo a re-key whose view died before the entry reached a renderer
The review-body ask on rekeyPersistedViewStateEntry measured true: the move was written out first and only then checked isViewStateGenerationLive, so a view that went away during its own storage write left the selection under the stable view id that no renderer will ever present, with the temporary name already deleted. The entry was bound to a claim that never reached a renderer and nothing on the dead path was rolled back. The committed move is now undone when the generation is gone: the entry is put back under the temporary id its author can still find it by and wroteUnderTemporaryViewStateId stays set, so the next launch re-keys it. When the entry had already been merged into a stable one it cannot be pulled back out whole, so there the temporary name is gone either way and authorship is cleared instead - leaving it set would make every later launch claim an entry that no longer exists under the temporary id. The restore write is best effort: if it fails too the entry stays under the stable id rather than disappearing. The live path is untouched. Tests (three cases, two red before the change, one green as the two-sided control): - view dies during the write, entry moved whole: expects the entry back under the temporary id, absent under the stable id, authorship still set. Red before: expected undefined to be 'architect'. - view dies during the write, entry merged into a stable entry: expects the merged entry kept under the stable id, the temporary id gone, authorship cleared. Red before: expected true to be false. The generation-mover double delegates to the real setValue so the write still lands. - generation still live: expects the re-key applied and authorship cleared. Green before and after, which is what pins the fix to the dead-generation path. Controls (ClineProvider.ts snapshotted at 4f80687, tsc 0 for each, restored by content hash after each): neutralise the restore arm -> only the moved-whole case fails (expected undefined to be 'architect'); drop the authorship clear in the merged branch -> only the merged case fails (expected true to be false); drop the live path's authorship clear -> only the live case fails (expected true to be false), so the untouched path is still load-bearing. Gates: prettier --check clean, tsc --noEmit -p . clean, eslint --max-warnings=0 clean on both files with eslint-suppressions.json untouched; vitest core/webview + core/config 44 files / 1280 tests green (+3 cases). Scope: extension package 512 changed executable lines against e943530 (was 494 on the previous head) with the types package at 8; ClineProvider.ts now 6390 lines. The advisory 500-line cap is a soft ceiling, not a merge blocker.
1 parent abe5683 commit 3dd0854

2 files changed

Lines changed: 113 additions & 2 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,26 @@ export class ClineProvider
11051105
await provider.contextProxy.setValue("viewStates", provider.prunePersistedViewStates(states))
11061106

11071107
if (!provider.isViewStateGenerationLive(scheduledGeneration)) {
1108-
// The move landed but the view is gone: keep the authorship flag as dispose
1109-
// left it rather than mutating a disposed provider.
1108+
// The move committed while the view it was written for went away. Leaving it applied
1109+
// binds the entry to a claim no renderer will ever present: the selection is stranded
1110+
// under a dead view id and the temporary name is already deleted. Put the entry back
1111+
// under the name its author can still find it by, and keep the authorship flag so the
1112+
// next launch re-keys it. The restore is best effort - if that write fails too the
1113+
// entry stays under the stable id rather than disappearing.
1114+
if (existingStableEntry) {
1115+
// Merged into a stable entry it cannot be pulled back out whole, so the temporary
1116+
// name is gone either way; authorship must not survive it, or every later launch
1117+
// keeps treating this view as the author of an entry that no longer exists.
1118+
provider.wroteUnderTemporaryViewStateId = false
1119+
return
1120+
}
1121+
const after = provider.getPersistedViewStates({ fresh: true })
1122+
const moved = after[nextViewStateId]
1123+
if (moved) {
1124+
delete after[nextViewStateId]
1125+
after[previousViewStateId] = moved
1126+
await provider.contextProxy.setValue("viewStates", provider.prunePersistedViewStates(after))
1127+
}
11101128
return
11111129
}
11121130

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6800,6 +6800,25 @@ describe("ClineProvider", () => {
68006800
})
68016801
})
68026802

6803+
const seedPreLaunchEntry = async (provider: ClineProvider) => {
6804+
// A pre-launch selection under the instance's temporary id, authored by this instance.
6805+
await provider["savePersistedViewState"]({ mode: "architect", updatedAt: 1 })
6806+
expect(provider["wroteUnderTemporaryViewStateId"]).toBe(true)
6807+
return provider.viewId
6808+
}
6809+
6810+
// Moves the provider's view-state generation while its own storage write is in flight,
6811+
// which is what a dispose or a re-registration during the write does. The double delegates
6812+
// to the real setter so the write still lands: replacing it outright would hide the move.
6813+
const moveGenerationDuringViewStatesWrite = (provider: ClineProvider) => {
6814+
const original = provider.contextProxy.setValue.bind(provider.contextProxy)
6815+
vi.spyOn(provider.contextProxy, "setValue").mockImplementation(async (...args: unknown[]) => {
6816+
await (original as (...a: unknown[]) => Promise<void>)(...args)
6817+
if (args[0] === "viewStates") {
6818+
provider["viewStateGeneration"]++
6819+
}
6820+
})
6821+
}
68036822
it("keeps a pre-launch selection when registration re-keys it onto an existing stable entry", async () => {
68046823
const provider = new ClineProvider(
68056824
mockContext,
@@ -6825,6 +6844,80 @@ describe("ClineProvider", () => {
68256844
await provider.dispose()
68266845
})
68276846

6847+
it("puts a re-key back under the temporary id when the view died before the entry reached a renderer", async () => {
6848+
const provider = new ClineProvider(
6849+
mockContext,
6850+
mockOutputChannel,
6851+
"sidebar",
6852+
new ContextProxy(mockContext),
6853+
new WebviewFocusTracker(),
6854+
)
6855+
const temporaryId = await seedPreLaunchEntry(provider)
6856+
moveGenerationDuringViewStatesWrite(provider)
6857+
6858+
await provider["rekeyPersistedViewStateEntry"]("stable-view")
6859+
6860+
// The move committed before the generation check ran. Leaving it applied binds the entry
6861+
// to a view id no renderer will ever present, and clears nothing: a later launch reads
6862+
// the temporary id as unauthored and the selection is stranded under the dead claim.
6863+
const states = mockContext.globalState.get<Record<string, { mode?: string }>>("viewStates") ?? {}
6864+
expect(states[temporaryId]?.mode).toBe("architect")
6865+
expect(states["stable-view"]).toBeUndefined()
6866+
expect(provider["wroteUnderTemporaryViewStateId"]).toBe(true)
6867+
6868+
await provider.dispose()
6869+
})
6870+
6871+
it("clears authorship when the dead view had already merged the pre-launch entry", async () => {
6872+
const provider = new ClineProvider(
6873+
mockContext,
6874+
mockOutputChannel,
6875+
"sidebar",
6876+
new ContextProxy(mockContext),
6877+
new WebviewFocusTracker(),
6878+
)
6879+
const temporaryId = await seedPreLaunchEntry(provider)
6880+
const seeded = provider.contextProxy.getValue("viewStates") ?? {}
6881+
await provider.contextProxy.setValue("viewStates", {
6882+
...seeded,
6883+
"stable-view": { claimToken: "host-token", updatedAt: 2 },
6884+
})
6885+
moveGenerationDuringViewStatesWrite(provider)
6886+
6887+
await provider["rekeyPersistedViewStateEntry"]("stable-view")
6888+
6889+
// A merged entry cannot be pulled back out whole, so the temporary name is gone either
6890+
// way; authorship must not survive it, or every later launch keeps treating this view as
6891+
// the author of an entry that no longer exists under the temporary id.
6892+
const states =
6893+
mockContext.globalState.get<Record<string, { mode?: string; claimToken?: string }>>("viewStates") ?? {}
6894+
expect(states["stable-view"]).toMatchObject({ mode: "architect", claimToken: "host-token" })
6895+
expect(states[temporaryId]).toBeUndefined()
6896+
expect(provider["wroteUnderTemporaryViewStateId"]).toBe(false)
6897+
6898+
await provider.dispose()
6899+
})
6900+
6901+
it("keeps the re-key applied and clears authorship while the view generation is live", async () => {
6902+
const provider = new ClineProvider(
6903+
mockContext,
6904+
mockOutputChannel,
6905+
"sidebar",
6906+
new ContextProxy(mockContext),
6907+
new WebviewFocusTracker(),
6908+
)
6909+
const temporaryId = await seedPreLaunchEntry(provider)
6910+
6911+
await provider["rekeyPersistedViewStateEntry"]("stable-view")
6912+
6913+
const states = mockContext.globalState.get<Record<string, { mode?: string }>>("viewStates") ?? {}
6914+
expect(states["stable-view"]?.mode).toBe("architect")
6915+
expect(states[temporaryId]).toBeUndefined()
6916+
expect(provider["wroteUnderTemporaryViewStateId"]).toBe(false)
6917+
6918+
await provider.dispose()
6919+
})
6920+
68286921
describe("view state writes that outlive the provider", () => {
68296922
const createProvider = () =>
68306923
new ClineProvider(

0 commit comments

Comments
 (0)