@@ -5237,6 +5237,137 @@ describe("ClineProvider", () => {
52375237
52385238 await provider . dispose ( )
52395239 } )
5240+
5241+ it ( "clears the settings overlay of a view re-pinned when the survivor's settings cannot be resolved" , async ( ) => {
5242+ const contextProxy = new ContextProxy ( mockContext )
5243+ const deleter = new ClineProvider (
5244+ mockContext ,
5245+ mockOutputChannel ,
5246+ "sidebar" ,
5247+ contextProxy ,
5248+ new WebviewFocusTracker ( ) ,
5249+ )
5250+ const pinned = new ClineProvider (
5251+ mockContext ,
5252+ mockOutputChannel ,
5253+ "editor" ,
5254+ new ContextProxy ( mockContext ) ,
5255+ new WebviewFocusTracker ( ) ,
5256+ )
5257+ const profileToDelete : ProviderSettingsEntry = { name : "deleted" , id : "deleted-id" }
5258+ const deletedSettings = { apiProvider : providerIdentifiers . openrouter , openRouterModelId : "deleted-model" }
5259+
5260+ await contextProxy . setValue ( "listApiConfigMeta" , [
5261+ { name : "deleted" , id : "deleted-id" } ,
5262+ { name : "survivor" , id : "survivor-id" } ,
5263+ ] )
5264+ await contextProxy . setValue ( "currentApiConfigName" , "deleted" )
5265+ await contextProxy . setProviderSettings ( deletedSettings )
5266+
5267+ for ( const view of [ deleter , pinned ] ) {
5268+ await view . saveViewState ( "currentApiConfigName" , "deleted" )
5269+ // The buffer a completed loadViewState leaves behind: this view serves the deleted
5270+ // profile's configuration.
5271+ await view [ "_saveViewLocalStateFromMutation" ] ( { apiConfiguration : deletedSettings } )
5272+ }
5273+
5274+ vi . spyOn ( deleter [ "providerSettingsManager" ] , "deleteConfig" ) . mockResolvedValue ( undefined )
5275+ vi . spyOn ( deleter [ "providerSettingsManager" ] , "activateProfile" ) . mockResolvedValue ( {
5276+ name : "survivor" ,
5277+ id : "survivor-id" ,
5278+ apiProvider : providerIdentifiers . anthropic ,
5279+ } )
5280+ vi . spyOn ( deleter [ "providerSettingsManager" ] , "getProfile" ) . mockRejectedValue (
5281+ new Error ( "profile store unavailable" ) ,
5282+ )
5283+
5284+ await deleter . deleteProviderProfile ( profileToDelete )
5285+
5286+ // Both views are re-pointed at the survivor, so neither may keep serving the deleted
5287+ // profile's configuration under the survivor's name.
5288+ expect ( deleter [ "viewLocalState" ] . currentApiConfigName ) . toBe ( "survivor" )
5289+ expect ( deleter [ "viewLocalState" ] ) . not . toHaveProperty ( "apiConfiguration" )
5290+ expect ( pinned [ "viewLocalState" ] . currentApiConfigName ) . toBe ( "survivor" )
5291+ expect ( pinned [ "viewLocalState" ] ) . not . toHaveProperty ( "apiConfiguration" )
5292+
5293+ await deleter . dispose ( )
5294+ await pinned . dispose ( )
5295+ } )
5296+
5297+ it ( "rebuilds a live task that still runs on the deleted profile, in this view and in a sibling view" , async ( ) => {
5298+ const contextProxy = new ContextProxy ( mockContext )
5299+ const deleter = new ClineProvider (
5300+ mockContext ,
5301+ mockOutputChannel ,
5302+ "sidebar" ,
5303+ contextProxy ,
5304+ new WebviewFocusTracker ( ) ,
5305+ )
5306+ const sibling = new ClineProvider (
5307+ mockContext ,
5308+ mockOutputChannel ,
5309+ "editor" ,
5310+ new ContextProxy ( mockContext ) ,
5311+ new WebviewFocusTracker ( ) ,
5312+ )
5313+ const profileToDelete : ProviderSettingsEntry = { name : "deleted" , id : "deleted-id" }
5314+ const survivingSettings = { apiProvider : providerIdentifiers . anthropic , apiKey : "survivor-key" }
5315+
5316+ await contextProxy . setValue ( "listApiConfigMeta" , [
5317+ { name : "deleted" , id : "deleted-id" } ,
5318+ { name : "survivor" , id : "survivor-id" } ,
5319+ ] )
5320+ await contextProxy . setValue ( "currentApiConfigName" , "deleted" )
5321+
5322+ const views = [ deleter , sibling ]
5323+ const tasks = views . map ( ( view ) => {
5324+ // The double only backs the calls the delete path makes on a live task.
5325+ const task = new Task ( defaultTaskOptions )
5326+ Object . defineProperty ( task , "taskId" , { value : `task-of-${ view . viewId } ` , writable : true } )
5327+ Object . defineProperty ( task , "taskApiConfigName" , { value : "deleted" , writable : true } )
5328+ Object . defineProperty ( task , "apiConfiguration" , {
5329+ value : { apiProvider : providerIdentifiers . openrouter , openRouterModelId : "deleted-model" } ,
5330+ writable : true ,
5331+ } )
5332+ task . setTaskApiConfigName = vi . fn ( )
5333+ task . updateApiConfiguration = vi . fn ( )
5334+ return task
5335+ } )
5336+
5337+ for ( const [ index , view ] of views . entries ( ) ) {
5338+ await view . saveViewState ( "currentApiConfigName" , "deleted" )
5339+ await view . addClineToStack ( tasks [ index ] )
5340+ }
5341+
5342+ vi . spyOn ( deleter [ "providerSettingsManager" ] , "deleteConfig" ) . mockResolvedValue ( undefined )
5343+ vi . spyOn ( deleter [ "providerSettingsManager" ] , "activateProfile" ) . mockResolvedValue ( {
5344+ name : "survivor" ,
5345+ id : "survivor-id" ,
5346+ apiProvider : providerIdentifiers . anthropic ,
5347+ } )
5348+ vi . spyOn ( deleter [ "providerSettingsManager" ] , "getProfile" ) . mockResolvedValue ( {
5349+ name : "survivor" ,
5350+ id : "survivor-id" ,
5351+ ...survivingSettings ,
5352+ } )
5353+
5354+ await deleter . deleteProviderProfile ( profileToDelete )
5355+
5356+ // Re-pointing the view is not enough: a task that still names the deleted profile runs on a
5357+ // handler built from its settings and keeps it as its sticky profile until it is rebuilt.
5358+ for ( const task of tasks ) {
5359+ expect ( task . updateApiConfiguration ) . toHaveBeenCalledWith (
5360+ expect . objectContaining ( {
5361+ apiProvider : providerIdentifiers . anthropic ,
5362+ apiKey : "survivor-key" ,
5363+ } ) ,
5364+ )
5365+ expect ( task . setTaskApiConfigName ) . toHaveBeenCalledWith ( "survivor" )
5366+ }
5367+
5368+ await deleter . dispose ( )
5369+ await sibling . dispose ( )
5370+ } )
52405371 } )
52415372
52425373 describe ( "updateCustomMode" , ( ) => {
@@ -6613,6 +6744,64 @@ describe("ClineProvider", () => {
66136744 await provider . dispose ( )
66146745 } )
66156746
6747+ it ( "should not roll back a shared key that a newer write replaced during a partial setValues" , async ( ) => {
6748+ const provider = new ClineProvider (
6749+ mockContext ,
6750+ mockOutputChannel ,
6751+ "sidebar" ,
6752+ new ContextProxy ( mockContext ) ,
6753+ new WebviewFocusTracker ( ) ,
6754+ )
6755+ await provider . contextProxy . setValue ( "mode" , "architect" )
6756+ await provider . contextProxy . setValue ( "currentApiConfigName" , "current-config" )
6757+ const originalUpdate = mockContext . globalState . update
6758+ let rejectConfig : ( error : Error ) => void = ( ) => { }
6759+ mockContext . globalState . update = vi . fn ( ) . mockImplementation ( ( key : string ) => {
6760+ if ( key === "currentApiConfigName" ) {
6761+ return new Promise < void > ( ( _ , reject ) => ( rejectConfig = reject ) )
6762+ }
6763+ return Promise . resolve ( )
6764+ } )
6765+
6766+ // The mode write commits while the profile-name write is still in flight.
6767+ const attempt = provider . setValues ( { mode : "code" , currentApiConfigName : "other-config" } )
6768+ await vi . waitFor ( ( ) => expect ( provider . getValue ( "mode" ) ) . toBe ( "code" ) )
6769+ // A newer write for the already committed key lands before the rollback runs.
6770+ await provider . setValue ( "mode" , "plan" )
6771+ rejectConfig ( new Error ( "storage unavailable" ) )
6772+ await expect ( attempt ) . rejects . toThrow ( "storage unavailable" )
6773+
6774+ // A rollback may only undo keys that still hold the value this attempt wrote; replaying
6775+ // every captured value would clobber the newer write for every view that reads shared state.
6776+ expect ( provider . getValue ( "mode" ) ) . toBe ( "plan" )
6777+ expect ( provider . getValue ( "currentApiConfigName" ) ) . toBe ( "current-config" )
6778+
6779+ mockContext . globalState . update = originalUpdate
6780+ await provider . dispose ( )
6781+ } )
6782+
6783+ it ( "should serve this view's restored mode through getState()" , async ( ) => {
6784+ const provider = new ClineProvider (
6785+ mockContext ,
6786+ mockOutputChannel ,
6787+ "sidebar" ,
6788+ new ContextProxy ( mockContext ) ,
6789+ new WebviewFocusTracker ( ) ,
6790+ )
6791+ await provider . contextProxy . setValue ( "mode" , "architect" )
6792+ await provider [ "setViewStateId" ] ( "mode-view" )
6793+ // A history or profile restore writes the mode into this view's buffer only; the shared store
6794+ // keeps the mode the last committing view chose.
6795+ await provider . saveViewState ( "mode" , "code" )
6796+
6797+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
6798+ // getState() is what task creation and getStateToPostToWebview read, so the restored mode has
6799+ // to be visible there and not only through getValues().
6800+ expect ( ( await provider . getState ( ) ) . mode ) . toBe ( "code" )
6801+
6802+ await provider . dispose ( )
6803+ } )
6804+
66166805 describe ( "viewId uniqueness" , ( ) => {
66176806 it ( "should assign unique viewId to each instance" , async ( ) => {
66186807 const provider1 = new ClineProvider (
@@ -6731,12 +6920,11 @@ describe("ClineProvider", () => {
67316920 resolveProfile ( { name : "cfg-a" , apiProvider : providerIdentifiers . openrouter , openRouterModelId : "model-x" } )
67326921 await load
67336922
6734- // The in-flight selection wins over the loaded state.
6923+ // The in-flight selection wins over the loaded state, and cfg-a's settings are not parked
6924+ // under cfg-b's name: a name this view changed on its own pins no settings, so reads fall back
6925+ // to the shared store until an activation resolves cfg-b together with its configuration.
67356926 expect ( provider [ "viewLocalState" ] . currentApiConfigName ) . toBe ( "cfg-b" )
6736- expect ( provider [ "viewLocalState" ] . apiConfiguration ) . toEqual ( {
6737- apiProvider : providerIdentifiers . openrouter ,
6738- openRouterModelId : "model-x" ,
6739- } )
6927+ expect ( provider [ "viewLocalState" ] ) . not . toHaveProperty ( "apiConfiguration" )
67406928 expect ( provider [ "viewLocalState" ] ) . not . toHaveProperty ( "mode" )
67416929 await provider . dispose ( )
67426930 } )
0 commit comments