Skip to content

Commit bc724b8

Browse files
fix(webview): keep memory and storage agreeing when a re-key recovery write fails
The recovery path for a re-key that committed onto a dead view awaited its compensating write unguarded. When that write rejected, the exception escaped past the return at the end of the block and fell into the authorship clear that follows the live-view check, so storage still held the entry under the temporary name while memory claimed the authorship had been consumed, and the next launch would not re-key it. The comment above the block promises best effort for the storage write only; the code was not best-effort for memory, which is the asymmetry the reviewer saw against the forward write, whose authorship is cleared only after a successful write. Catch the recovery write, undo the in-memory re-key alongside it, and leave the authorship claimed so a later launch can still find the entry under the name its author wrote it under. What the measurement separates is the catch, not the rollback. With the case alone against this head the write run rejects out of the queue and the case fails (1 failed | 970 passed, 971 tests). With the catch present it passes (34 test files, 971 tests). With the catch present but rethrowing it fails the same way as the head. Removing only the rollback leaves every assertion green, because ContextProxy already restores its own cache when a state write fails, so the rollback is kept for the ordering it guarantees rather than for a difference this harness can observe. Case: leaves the entry under the temporary id and keeps authorship when the compensating re-key write also fails. Review row: database id 4477965171.
1 parent 3dd0854 commit bc724b8

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,17 @@ export class ClineProvider
11231123
if (moved) {
11241124
delete after[nextViewStateId]
11251125
after[previousViewStateId] = moved
1126-
await provider.contextProxy.setValue("viewStates", provider.prunePersistedViewStates(after))
1126+
try {
1127+
await provider.contextProxy.setValue("viewStates", provider.prunePersistedViewStates(after))
1128+
} catch {
1129+
// The comment above promises best effort for the storage write only. If that write fails
1130+
// the entry is still under the stable id in storage, so the in-memory re-key above has to be
1131+
// undone as well: otherwise memory claims the temporary name is gone while storage still
1132+
// holds it, and authorship is cleared below even though a later launch still has to re-key
1133+
// the entry. Undo the re-key and keep the claim so the next launch can try again.
1134+
delete after[previousViewStateId]
1135+
after[nextViewStateId] = moved
1136+
}
11271137
}
11281138
return
11291139
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6898,6 +6898,52 @@ describe("ClineProvider", () => {
68986898
await provider.dispose()
68996899
})
69006900

6901+
it("leaves the entry under the temporary id and keeps authorship when the compensating re-key write also fails", 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+
moveGenerationDuringViewStatesWrite(provider)
6911+
6912+
// The move is already stored when the generation check runs, so the recovery path is the
6913+
// compensating write. Reject the storage update whose payload puts the entry back under the
6914+
// temporary id and leave every other write behaving normally - payload-keyed so it hits that
6915+
// write and not the queue calls that precede it.
6916+
let compensatingWrites = 0
6917+
const mementoUpdate = mockContext.globalState.update
6918+
mockContext.globalState.update = ((key: string, value: unknown) => {
6919+
const payload = value as Record<string, unknown> | undefined
6920+
if (key === "viewStates" && payload && Object.prototype.hasOwnProperty.call(payload, temporaryId)) {
6921+
compensatingWrites++
6922+
return Promise.reject(new Error("storage rejected the compensating write"))
6923+
}
6924+
return mementoUpdate(key, value)
6925+
}) as typeof mockContext.globalState.update
6926+
6927+
await provider["rekeyPersistedViewStateEntry"]("stable-view")
6928+
6929+
expect(compensatingWrites).toBe(1) // the recovery write was attempted exactly once
6930+
const stored = mockContext.globalState.get<Record<string, { mode?: string }>>("viewStates") ?? {}
6931+
const cached =
6932+
(provider.contextProxy.getValue("viewStates") as Record<string, { mode?: string }> | undefined) ?? {}
6933+
// The recovery write never landed, so storage still holds the entry under the stable id from the
6934+
// forward write. The rollback exists so the cache ContextProxy keeps for the key says the same
6935+
// thing: without it the cache claims the entry moved back under a temporary id that durable
6936+
// storage never received, and every later read of the key serves a value nothing backs.
6937+
expect(stored["stable-view"]?.mode).toBe("architect")
6938+
expect(cached["stable-view"]?.mode).toBe("architect")
6939+
expect(cached[temporaryId]).toBeUndefined()
6940+
// Authorship stays claimed: storage still holds the entry under the stable id and a later launch
6941+
// has to re-key it, which is what the forward path already guarantees for its own failures.
6942+
expect(provider["wroteUnderTemporaryViewStateId"]).toBe(true)
6943+
6944+
await provider.dispose()
6945+
})
6946+
69016947
it("keeps the re-key applied and clears authorship while the view generation is live", async () => {
69026948
const provider = new ClineProvider(
69036949
mockContext,

0 commit comments

Comments
 (0)