Skip to content

Commit 60c5400

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 c3595a1 commit 60c5400

4 files changed

Lines changed: 53 additions & 5 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const globalSettingsExportSchema = globalSettingsSchema.omit({
3636
taskHistory: true,
3737
listApiConfigMeta: true,
3838
currentApiConfigName: true,
39+
// Host-minted claim tokens and per-view selections are not user-settings: exporting
40+
// them would let a settings file be replayed as another view's identity.
41+
viewStates: true,
3942
})
4043

4144
export class ContextProxy {

‎src/core/webview/ClineProvider.ts‎

Lines changed: 21 additions & 3 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
@@ -771,8 +778,19 @@ export class ClineProvider
771778

772779
delete states[previousViewStateId]
773780

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

778796
await this.contextProxy.setValue("viewStates", this.prunePersistedViewStates(states))
@@ -5119,9 +5137,9 @@ export class ClineProvider
51195137
* non-secret subset (mode, currentApiConfigName) is persisted durably under the view
51205138
* id active when the change was made, re-keyed to the stable id on registration.
51215139
*/
5122-
public async saveViewState<K extends keyof ViewLocalStateValues>(
5140+
public async saveViewState<K extends keyof PersistedViewStateValues>(
51235141
key: K,
5124-
value: ViewLocalStateValues[K] | undefined,
5142+
value: PersistedViewStateValues[K] | undefined,
51255143
): Promise<void> {
51265144
await this._saveViewLocalStateFromMutation({ [key]: value } as ViewLocalStateValues)
51275145

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3845,6 +3845,31 @@ describe("ClineProvider", () => {
38453845
}
38463846
})
38473847
})
3848+
3849+
it("keeps a pre-launch selection when registration re-keys it onto an existing stable entry", async () => {
3850+
const provider = new ClineProvider(
3851+
mockContext,
3852+
mockOutputChannel,
3853+
"sidebar",
3854+
new ContextProxy(mockContext),
3855+
new WebviewFocusTracker(),
3856+
)
3857+
3858+
await provider["savePersistedViewState"]({ mode: "architect", updatedAt: 1 })
3859+
const seeded = provider.contextProxy.getValue("viewStates") ?? {}
3860+
await provider.contextProxy.setValue("viewStates", {
3861+
...seeded,
3862+
"stable-view": { claimToken: "host-token", updatedAt: 2 },
3863+
})
3864+
3865+
await provider["rekeyPersistedViewStateEntry"]("stable-view")
3866+
3867+
expect(mockContext.globalState.get("viewStates")).toMatchObject({
3868+
"stable-view": { mode: "architect", claimToken: "host-token" },
3869+
})
3870+
3871+
await provider.dispose()
3872+
})
38483873
})
38493874

38503875
describe("webviewMessageHandler no-floating-promises coverage", () => {

0 commit comments

Comments
 (0)