Skip to content

Commit 6160a47

Browse files
fix(webview): restore the view-local overlays when a setValues write fails
Review follow-up: setValues snapshots the sidebar view local mode/currentApiConfigName overrides (including whether each key was present) before applying a mutation. On failure it restores the shared ContextProxy values and then restores each prior local override through saveViewState, clearing keys that had no override and logging a failed overlay restore without masking the original error. The per-view persistence rollback test now gives the view its own overrides and asserts they survive the failed write. ClineProvider.spec.ts 317 passed; tsc clean.
1 parent 4cf697b commit 6160a47

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,12 +4632,46 @@ export class ClineProvider
46324632
),
46334633
) as Partial<RooCodeSettings>
46344634

4635+
// Snapshot this view's local overlays for the keys the mutation touches, including
4636+
// whether each key had an override: a failed write must restore the shared store AND the
4637+
// view-local half (or its absence), not just the shared values.
4638+
const localOverlaySnapshot: Partial<
4639+
Record<"mode" | "currentApiConfigName", { present: boolean; value: unknown }>
4640+
> = {}
4641+
for (const key of ["mode", "currentApiConfigName"] as const) {
4642+
if (key in sanitizedValues) {
4643+
localOverlaySnapshot[key] = { present: key in this.viewLocalState, value: this.viewLocalState[key] }
4644+
}
4645+
}
4646+
46354647
await this.contextProxy.setValues(sanitizedValues)
46364648

46374649
try {
46384650
await this._saveViewLocalStateFromMutation(sanitizedValues)
46394651
} catch (error) {
46404652
await this.contextProxy.setValues(previousValues as RooCodeSettings)
4653+
// Restore the local overlay from the snapshot: the shared rollback alone would leave
4654+
// the view pinned to the overrides the failed write tried to apply.
4655+
const restoredOverlay: Partial<RooCodeSettings> = {}
4656+
for (const [key, snapshot] of Object.entries(localOverlaySnapshot)) {
4657+
;(restoredOverlay as Record<string, unknown>)[key] = snapshot.present ? snapshot.value : undefined
4658+
}
4659+
this._updateViewLocalStateFromMutation(restoredOverlay)
4660+
for (const key of Object.keys(localOverlaySnapshot) as ("mode" | "currentApiConfigName")[]) {
4661+
const snapshot = localOverlaySnapshot[key]!
4662+
try {
4663+
await this.saveViewState(key, snapshot.present ? (snapshot.value as never) : undefined)
4664+
} catch (restoreError) {
4665+
// The original write failure is what the caller must see; a failed overlay restore
4666+
// is logged rather than replacing it.
4667+
this.log(
4668+
"[ClineProvider#setValues] Failed to restore the local view override for " +
4669+
key +
4670+
": " +
4671+
(restoreError instanceof Error ? restoreError.message : String(restoreError)),
4672+
)
4673+
}
4674+
}
46414675
throw error
46424676
}
46434677
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2921,6 +2921,10 @@ describe("ClineProvider", () => {
29212921
)
29222922
await provider.contextProxy.setValue("mode", "architect")
29232923
await provider.contextProxy.setValue("currentApiConfigName", "current-config")
2924+
// This view has its own overrides before the failed write; the rollback must restore
2925+
// them (the shared rollback alone would leave the view pinned to the failed values).
2926+
await provider.saveViewState("mode", "architect")
2927+
await provider.saveViewState("currentApiConfigName", "current-config")
29242928
const originalUpdate = mockContext.globalState.update
29252929
mockContext.globalState.update = vi.fn().mockImplementation((key: string) => {
29262930
if (key === "viewStates") {
@@ -2934,7 +2938,8 @@ describe("ClineProvider", () => {
29342938
)
29352939
expect(provider.getValue("mode")).toBe("architect")
29362940
expect(provider.getValue("currentApiConfigName")).toBe("current-config")
2937-
expect(provider["viewLocalState"].mode).toBeUndefined()
2941+
expect(provider["viewLocalState"].mode).toBe("architect")
2942+
expect(provider["viewLocalState"].currentApiConfigName).toBe("current-config")
29382943

29392944
mockContext.globalState.update = originalUpdate
29402945
await provider.dispose()

0 commit comments

Comments
 (0)