@@ -4314,6 +4314,147 @@ describe("ClineProvider", () => {
43144314 } )
43154315 } )
43164316
4317+ describe ( "view-local secrets and reset invalidation" , ( ) => {
4318+ it ( "clears every live buffer before awaiting any sibling post" , async ( ) => {
4319+ const provider = new ClineProvider (
4320+ mockContext ,
4321+ mockOutputChannel ,
4322+ "sidebar" ,
4323+ new ContextProxy ( mockContext ) ,
4324+ new WebviewFocusTracker ( ) ,
4325+ )
4326+ const firstSibling = new ClineProvider (
4327+ mockContext ,
4328+ mockOutputChannel ,
4329+ "editor" ,
4330+ new ContextProxy ( mockContext ) ,
4331+ new WebviewFocusTracker ( ) ,
4332+ )
4333+ const secondSibling = new ClineProvider (
4334+ mockContext ,
4335+ mockOutputChannel ,
4336+ "sidebar" ,
4337+ new ContextProxy ( mockContext ) ,
4338+ new WebviewFocusTracker ( ) ,
4339+ )
4340+ provider [ "viewLocalState" ] = { mode : "architect" }
4341+ firstSibling [ "viewLocalState" ] = { mode : "plan" }
4342+ secondSibling [ "viewLocalState" ] = { mode : "code" }
4343+
4344+ // The first sibling stalls and then fails; what it can still see is the point.
4345+ let secondBufferAtFirstPost : unknown
4346+ vi . spyOn ( firstSibling , "postStateToWebview" ) . mockImplementation ( async ( ) => {
4347+ secondBufferAtFirstPost = secondSibling [ "viewLocalState" ] . mode
4348+ throw new Error ( "sibling post stalled" )
4349+ } )
4350+ const secondPost = vi . spyOn ( secondSibling , "postStateToWebview" ) . mockResolvedValue ( undefined )
4351+
4352+ await provider . broadcastResetToAllInstances ( )
4353+
4354+ // Clearing happens before any await, so a stalled sibling cannot leave a later view
4355+ // serving a selection this reset already threw away, and the failure does not stop the
4356+ // reset from reaching the remaining view.
4357+ expect ( secondBufferAtFirstPost ) . toBeUndefined ( )
4358+ expect ( provider [ "viewLocalState" ] ) . toEqual ( { } )
4359+ expect ( firstSibling [ "viewLocalState" ] ) . toEqual ( { } )
4360+ expect ( secondSibling [ "viewLocalState" ] ) . toEqual ( { } )
4361+ expect ( secondPost ) . toHaveBeenCalledTimes ( 1 )
4362+
4363+ await provider . dispose ( )
4364+ await firstSibling . dispose ( )
4365+ await secondSibling . dispose ( )
4366+ } )
4367+
4368+ it ( "invalidates and refreshes live views when a reset step rejects after the shared reset" , async ( ) => {
4369+ const provider = new ClineProvider (
4370+ mockContext ,
4371+ mockOutputChannel ,
4372+ "sidebar" ,
4373+ new ContextProxy ( mockContext ) ,
4374+ new WebviewFocusTracker ( ) ,
4375+ )
4376+ provider [ "viewLocalState" ] = { mode : "architect" }
4377+ const broadcast = vi . spyOn ( provider , "broadcastResetToAllInstances" ) . mockResolvedValue ( undefined )
4378+ const post = vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
4379+ vi . spyOn ( provider [ "providerSettingsManager" ] , "resetAllConfigs" ) . mockRejectedValue (
4380+ new Error ( "profile reset failed" ) ,
4381+ )
4382+ // @ts -ignore - the real resetCustomModes writes to disk; this path must not reach it.
4383+ provider . customModesManager = { resetCustomModes : vi . fn ( ) . mockResolvedValue ( undefined ) , dispose : vi . fn ( ) }
4384+ vi . mocked ( vscode . window . showInformationMessage ) . mockResolvedValue (
4385+ t ( "common:answers.yes" ) as unknown as vscode . MessageItem ,
4386+ )
4387+
4388+ await expect ( provider . resetState ( ) ) . rejects . toThrow ( "profile reset failed" )
4389+
4390+ // The shared reset had already landed, so the live views had to be invalidated and
4391+ // refreshed rather than left serving the discarded modes and credentials; the original
4392+ // failure is still what the caller sees.
4393+ expect ( broadcast ) . toHaveBeenCalledTimes ( 1 )
4394+ expect ( post ) . toHaveBeenCalledTimes ( 1 )
4395+
4396+ await provider . dispose ( )
4397+ } )
4398+
4399+ it ( "drops the overlay credentials from the public values without touching the buffer" , async ( ) => {
4400+ const provider = new ClineProvider (
4401+ mockContext ,
4402+ mockOutputChannel ,
4403+ "sidebar" ,
4404+ new ContextProxy ( mockContext ) ,
4405+ new WebviewFocusTracker ( ) ,
4406+ )
4407+ provider [ "viewLocalState" ] = { apiConfiguration : { apiKey : "sk-view-local" , apiModelId : "m1" } }
4408+
4409+ const values = provider . getValues ( )
4410+
4411+ // getValues is the surface getConfiguration exposes to an API client, and that client can
4412+ // hand the same object back to setConfiguration: the profile credential must not ride
4413+ // along. The buffer keeps it, because this view still has to serve its own profile.
4414+ expect ( values . apiConfiguration ) . toEqual ( { apiModelId : "m1" } )
4415+ expect ( provider [ "viewLocalState" ] . apiConfiguration ) . toEqual ( {
4416+ apiKey : "sk-view-local" ,
4417+ apiModelId : "m1" ,
4418+ } )
4419+
4420+ await provider . dispose ( )
4421+ } )
4422+
4423+ it ( "keeps a credential-bearing overlay out of the shared write" , async ( ) => {
4424+ const contextProxy = new ContextProxy ( mockContext )
4425+ const provider = new ClineProvider (
4426+ mockContext ,
4427+ mockOutputChannel ,
4428+ "sidebar" ,
4429+ contextProxy ,
4430+ new WebviewFocusTracker ( ) ,
4431+ )
4432+ const sharedWrites : Record < string , unknown > [ ] = [ ]
4433+ vi . spyOn ( contextProxy , "setValues" ) . mockImplementation ( async ( values ) => {
4434+ sharedWrites . push ( { ...values } )
4435+ } )
4436+
4437+ // An API payload shape: the overlay key is not part of RooCodeSettings, but the
4438+ // mutation path accepts it and that is how a client passing getConfiguration back
4439+ // delivers it.
4440+ const payload : Partial < RooCodeSettings > & Partial < ExtensionState > = {
4441+ apiConfiguration : { apiKey : "sk-overlay" , apiModelId : "m2" } ,
4442+ }
4443+ await provider . setValues ( payload )
4444+
4445+ // The overlay is view-local: its place is the per-view record and secret storage, not the
4446+ // shared slot, where an API payload would persist the profile credential in global state.
4447+ expect ( sharedWrites ) . toHaveLength ( 1 )
4448+ expect ( sharedWrites [ 0 ] ) . not . toHaveProperty ( "apiConfiguration" )
4449+ expect ( provider [ "viewLocalState" ] . apiConfiguration ) . toEqual ( {
4450+ apiKey : "sk-overlay" ,
4451+ apiModelId : "m2" ,
4452+ } )
4453+
4454+ await provider . dispose ( )
4455+ } )
4456+ } )
4457+
43174458 describe ( "setViewStateId" , ( ) => {
43184459 it ( 'should ignore "__proto__" and keep the temporary viewId' , async ( ) => {
43194460 const provider = new ClineProvider (
@@ -5357,9 +5498,10 @@ describe("ClineProvider", () => {
53575498 apiProvider : providerIdentifiers . openrouter ,
53585499 apiKey : "stale-key" ,
53595500 } )
5501+ // The public getter is the API surface: it reports the overlay without its credential,
5502+ // which still identifies which source serves the view, and the buffer keeps the credential.
53605503 expect ( provider . getValues ( ) . apiConfiguration ) . toEqual ( {
53615504 apiProvider : providerIdentifiers . openrouter ,
5362- apiKey : "stale-key" ,
53635505 } )
53645506
53655507 await provider . activateProviderProfile ( { name : "new-profile" } )
@@ -5524,9 +5666,10 @@ describe("ClineProvider", () => {
55245666 apiProvider : providerIdentifiers . anthropic ,
55255667 apiKey : "stale-key" ,
55265668 } )
5669+ // The public getter is the API surface: it reports the overlay without its credential,
5670+ // which still identifies which source serves the view, and the buffer keeps the credential.
55275671 expect ( provider . getValues ( ) . apiConfiguration ) . toEqual ( {
55285672 apiProvider : providerIdentifiers . anthropic ,
5529- apiKey : "stale-key" ,
55305673 } )
55315674
55325675 await provider . upsertProviderProfile ( "fresh-profile" , { apiProvider : providerIdentifiers . openrouter } )
@@ -5930,9 +6073,10 @@ describe("ClineProvider", () => {
59306073 apiProvider : providerIdentifiers . openrouter ,
59316074 apiKey : "stale-key" ,
59326075 } )
6076+ // The public getter is the API surface: it reports the overlay without its credential,
6077+ // which still identifies which source serves the view, and the buffer keeps the credential.
59336078 expect ( provider . getValues ( ) . apiConfiguration ) . toEqual ( {
59346079 apiProvider : providerIdentifiers . openrouter ,
5935- apiKey : "stale-key" ,
59366080 } )
59376081
59386082 await provider . deleteProviderProfile ( oldProfile )
@@ -6811,8 +6955,13 @@ describe("ClineProvider", () => {
68116955
68126956 expect ( values . mode ) . toBe ( "architect" )
68136957 expect ( values . currentApiConfigName ) . toBe ( "view-profile" )
6958+ // The credential stays in the buffer; the public getter reports the overlay without it,
6959+ // so the merge claim is carried by the provider identity.
68146960 expect ( values . apiConfiguration ) . toEqual ( {
68156961 apiProvider : providerIdentifiers . openrouter ,
6962+ } )
6963+ expect ( provider [ "viewLocalState" ] . apiConfiguration ) . toEqual ( {
6964+ apiProvider : providerIdentifiers . openrouter ,
68166965 openRouterApiKey : "view-key" ,
68176966 } )
68186967 expect ( values . customModePrompts ) . toEqual ( { code : { roleDefinition : "shared" } } )
0 commit comments