@@ -148,6 +148,13 @@ type PersistedViewState = NonNullable<GlobalState["viewStates"]>[string]
148148 */
149149type 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