Skip to content

Commit cb37bbc

Browse files
fix(webview): address review findings on view-state durability
- omit viewStates (host-minted claim tokens) from settings export - keep a pre-launch selection when registration re-keys onto an existing stable entry - narrow saveViewState keys to the durable per-view subset - skip rollback when another provider wrote a newer shared value - restore the ContextProxy cache when globalState.update rejects - clarify that the claim is enforced by setViewStateId; launch wiring follows
1 parent 692a818 commit cb37bbc

3 files changed

Lines changed: 61 additions & 11 deletions

File tree

‎packages/types/src/vscode-extension-host.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,10 @@ export interface WebviewMessage {
681681
viewStateId?: string
682682
/**
683683
* For `webviewDidLaunch`: the host-minted claim previously delivered for `viewStateId`.
684-
* The webview only echoes what it was given; the host rejects an id whose stored claim
685-
* does not match, so a view cannot adopt another view's persisted selections.
684+
* The webview only echoes what it was given; the host enforces the match in
685+
* `ClineProvider.setViewStateId` when a view registers this id, so a view cannot adopt
686+
* another view's persisted selections. Forwarding these fields from the launch handler
687+
* is wired by the follow-up launch unit.
686688
*/
687689
viewStateClaim?: string
688690
taskId?: string

‎src/core/config/ContextProxy.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,23 @@ export class ContextProxy {
363363
return value !== undefined ? value : defaultValue
364364
}
365365

366-
updateGlobalState<K extends GlobalStateKey>(key: K, value: GlobalState[K]) {
366+
async updateGlobalState<K extends GlobalStateKey>(key: K, value: GlobalState[K]) {
367367
if (isPassThroughStateKey(key)) {
368368
return this.originalContext.globalState.update(key, value)
369369
}
370370

371+
const previousCachedValue = this.stateCache[key]
371372
this.stateCache[key] = value
372-
return this.originalContext.globalState.update(key, value)
373+
374+
try {
375+
return await this.originalContext.globalState.update(key, value)
376+
} catch (error) {
377+
// A rejected write must not leave the cache ahead of storage: callers that then
378+
// read the cached value (e.g. the view-state load path) would act on a selection
379+
// that was never persisted.
380+
this.stateCache[key] = previousCachedValue
381+
throw error
382+
}
373383
}
374384

375385
private getAllGlobalState(): GlobalState {

‎src/core/webview/ClineProvider.ts‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ type PersistedViewState = NonNullable<GlobalState["viewStates"]>[string]
148148
*/
149149
type ViewLocalStateValues = Partial<RooCodeSettings> & Partial<ExtensionState>
150150

151+
/**
152+
* The subset of view-local values that has a durable per-view record. `saveViewState`
153+
* and the mutation path handle exactly these keys, so the generic stays narrow enough
154+
* for the compiler to reject e.g. `writeDelayMs`.
155+
*/
156+
type PersistedViewStateValues = Pick<ViewLocalStateValues, "mode" | "currentApiConfigName" | "apiConfiguration">
157+
151158
/**
152159
* https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
153160
* https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
@@ -774,8 +781,19 @@ export class ClineProvider
774781

775782
delete states[previousViewStateId]
776783

777-
if (!states[nextViewStateId]) {
784+
const existingStableEntry = states[nextViewStateId]
785+
786+
if (!existingStableEntry) {
778787
states[nextViewStateId] = previous
788+
} else {
789+
// Registration can bind a claim-less legacy key just before this pre-launch entry
790+
// is re-keyed. Keep the selections the stable entry already carries, but fill in
791+
// anything it lacks so a mode chosen before launch is not silently dropped.
792+
states[nextViewStateId] = {
793+
...previous,
794+
...existingStableEntry,
795+
updatedAt: Math.max(previous.updatedAt ?? 0, existingStableEntry.updatedAt ?? 0),
796+
}
779797
}
780798

781799
await this.contextProxy.setValue("viewStates", this.prunePersistedViewStates(states))
@@ -1024,9 +1042,9 @@ export class ClineProvider
10241042
* non-secret subset (mode, currentApiConfigName) is persisted durably under the view
10251043
* id active when the change was made, re-keyed to the stable id on registration.
10261044
*/
1027-
public async saveViewState<K extends keyof ViewLocalStateValues>(
1045+
public async saveViewState<K extends keyof PersistedViewStateValues>(
10281046
key: K,
1029-
value: ViewLocalStateValues[K] | undefined,
1047+
value: PersistedViewStateValues[K] | undefined,
10301048
): Promise<void> {
10311049
await this._saveViewLocalStateFromMutation({ [key]: value } as ViewLocalStateValues)
10321050

@@ -4076,7 +4094,9 @@ export class ClineProvider
40764094
} catch (error) {
40774095
// The per-view record did not land: restore the prior shared value so the two
40784096
// persistence layers cannot diverge, then surface the original failure.
4079-
await this._restoreSharedValuesAfterFailedViewWrite(previousSharedValues)
4097+
await this._restoreSharedValuesAfterFailedViewWrite(previousSharedValues, {
4098+
[key]: value,
4099+
} as RooCodeSettings)
40804100
throw error
40814101
}
40824102
}
@@ -4113,7 +4133,7 @@ export class ClineProvider
41134133
} catch (error) {
41144134
// The per-view record did not land: restore the prior shared values so the two
41154135
// persistence layers cannot diverge, then surface the original failure.
4116-
await this._restoreSharedValuesAfterFailedViewWrite(previousSharedValues)
4136+
await this._restoreSharedValuesAfterFailedViewWrite(previousSharedValues, sanitizedValues)
41174137
throw error
41184138
}
41194139
}
@@ -5556,9 +5576,27 @@ export class ClineProvider
55565576
* per-view persistence failed so the shared store cannot move ahead of the per-view
55575577
* record. A failed rollback is logged; the original failure is what callers see.
55585578
*/
5559-
private async _restoreSharedValuesAfterFailedViewWrite(previous: RooCodeSettings): Promise<void> {
5579+
private async _restoreSharedValuesAfterFailedViewWrite(
5580+
previous: RooCodeSettings,
5581+
attempted: RooCodeSettings,
5582+
): Promise<void> {
5583+
// Roll back only the keys this attempt wrote and that still hold the value it wrote.
5584+
// A concurrent provider may have written a newer value in the meantime; restoring the
5585+
// captured previous value would then clobber it for every view that reads shared state.
5586+
const rollback: RooCodeSettings = {} as RooCodeSettings
5587+
5588+
for (const key of Object.keys(attempted) as (keyof RooCodeSettings)[]) {
5589+
if (this.contextProxy.getValue(key) === attempted[key]) {
5590+
;(rollback as Record<string, unknown>)[key] = previous[key]
5591+
}
5592+
}
5593+
5594+
if (Object.keys(rollback).length === 0) {
5595+
return
5596+
}
5597+
55605598
try {
5561-
await this.contextProxy.setValues(previous)
5599+
await this.contextProxy.setValues(rollback)
55625600
} catch (rollbackError) {
55635601
this.log(
55645602
`[ClineProvider] Shared-state rollback after failed per-view write failed: ${rollbackError instanceof Error ? rollbackError.message : String(rollbackError)}`,

0 commit comments

Comments
 (0)