@@ -120,7 +120,7 @@ describe("API - configuration", () => {
120120 } ) ,
121121 // The payload saves this profile: siblings pinned to it are refreshed
122122 // from the payload instead of pinned to a pre-save snapshot.
123- { savedProfileName : "default" } ,
123+ { savedProfileName : "default" , savedProfileSettings : { } } ,
124124 )
125125 expect ( setModeConfig ) . toHaveBeenCalledTimes ( 2 )
126126 expect ( setModeConfig ) . toHaveBeenCalledWith ( "code" , "code-config" )
@@ -730,7 +730,103 @@ describe("API - configuration", () => {
730730 } )
731731 expect ( logged ( ) ) . toContain ( "after a failed read-back: another view wrote it (keys: openRouterModelId)" )
732732 } )
733+ it ( "restores the prior record only while it still carries the id this call wrote" , async ( ) => {
734+ const store = makeProfileStoreDouble ( {
735+ pinned : {
736+ id : "prior-id" ,
737+ settings : storedProfileSettingsFor ( {
738+ currentApiConfigName : "pinned" ,
739+ apiProvider : providerIdentifiers . openrouter ,
740+ apiKey : "before" ,
741+ } ) ,
742+ } ,
743+ } )
744+ const { saveConfig, deleteConfig, hasConfig } = store
745+ // An import replaced the profile in place while this call held the name: the settings now match
746+ // what this call wrote, but the id is not the one this call holds. saveConfig keeps an existing
747+ // id when it overwrites, so a different id can only come from a writer that replaced the
748+ // record, and restoring our settings over it would leave the imported profile holding
749+ // pre-import settings.
750+ const prior = storedProfileSettingsFor ( {
751+ currentApiConfigName : "pinned" ,
752+ apiProvider : providerIdentifiers . openrouter ,
753+ apiKey : "before" ,
754+ } )
755+ const replacement = storedProfileSettingsFor ( {
756+ currentApiConfigName : "pinned" ,
757+ apiProvider : providerIdentifiers . openrouter ,
758+ apiKey : "after" ,
759+ } )
760+ let reads = 0
761+ const getProfile = vi . fn ( ( { name } : { name : string } ) => {
762+ reads += 1
763+ if ( reads === 1 ) {
764+ // The prior record this call snapshots before it writes.
765+ return Promise . resolve ( { name, id : "prior-id" , ...prior } )
766+ }
767+ if ( reads === 2 ) {
768+ return Promise . reject ( new Error ( "secrets storage read failed" ) )
769+ }
770+ return Promise . resolve ( { name, id : "imported-id" , ...replacement } )
771+ } )
772+ const { api, logged, setValues } = makeReadbackHarness ( {
773+ saveConfig,
774+ deleteConfig,
775+ hasConfig,
776+ getProfile,
777+ } )
778+
779+ await expect (
780+ api . setConfiguration ( {
781+ currentApiConfigName : "pinned" ,
782+ apiProvider : providerIdentifiers . openrouter ,
783+ apiKey : "after" ,
784+ } ) ,
785+ ) . rejects . toThrow ( "secrets storage read failed" )
786+
787+ // One save: the initial write. Restoring the prior record would have been the second save, and
788+ // a delete would have thrown the imported profile away.
789+ expect ( saveConfig . mock . calls . length ) . toBe ( 1 )
790+ expect ( deleteConfig . mock . calls ) . toEqual ( [ ] )
791+ expect ( setValues . mock . calls ) . toEqual ( [ ] )
792+ expect ( logged ( ) ) . toContain ( "not this call 'prior-id'" )
793+ } )
733794
795+ it ( "reports the landed record, not the payload, for the sibling profile refresh" , async ( ) => {
796+ const store = makeProfileStoreDouble ( )
797+ const { saveConfig, getProfile, deleteConfig, hasConfig } = store
798+ const { api, setValues } = makeReadbackHarness ( {
799+ saveConfig,
800+ getProfile,
801+ deleteConfig,
802+ hasConfig,
803+ } )
804+ // The record stores only the keys the profile schema knows, while a settings round trip also
805+ // carries the profile pin and host-owned keys. Sibling overlays are rebuilt from the settings
806+ // the caller reports, so those must be the record that landed: a provider the schema rewrote
807+ // on the way in would otherwise stay selected in an idle sibling that cannot create a task
808+ // with it. The recorded arguments come from a double-shaped mock, so reading them needs a
809+ // cast.
810+ type ProfileWriteOptions = {
811+ savedProfileName ?: string
812+ savedProfileSettings ?: Record < string , unknown >
813+ }
814+ const optionsOf = ( call : unknown [ ] ) : ProfileWriteOptions | undefined =>
815+ call [ 1 ] as ProfileWriteOptions | undefined
816+ await api . setConfiguration ( {
817+ currentApiConfigName : "pinned" ,
818+ apiProvider : providerIdentifiers . openrouter ,
819+ apiKey : "after" ,
820+ openRouterModelId : "gpt-4o" ,
821+ modeApiConfigs : { act : "other-profile" } ,
822+ } )
823+ const call = setValues . mock . calls . find ( ( args : unknown [ ] ) => optionsOf ( args ) ?. savedProfileName === "pinned" )
824+ expect ( call ) . toBeDefined ( )
825+ const reported = optionsOf ( call ?? [ ] ) ?. savedProfileSettings
826+ expect ( reported ) . toEqual ( store . entries . get ( "pinned" ) ?. settings )
827+ expect ( reported ) . not . toHaveProperty ( "currentApiConfigName" )
828+ expect ( reported ) . not . toHaveProperty ( "modeApiConfigs" )
829+ } )
734830 it ( "keeps the created record when the re-read matches the settings under a different id" , async ( ) => {
735831 const store = makeProfileStoreDouble ( )
736832 const { saveConfig, deleteConfig, hasConfig } = store
@@ -1744,7 +1840,12 @@ describe("API - configuration", () => {
17441840
17451841 // The settings write goes through the provider, which also carries the profile name the
17461842 // sibling refresh has to agree with.
1747- expect ( setValues ) . toHaveBeenCalledWith ( { [ key ] : value } , { savedProfileName : "default" } )
1843+ // The options object also carries the record that landed, which is what sibling overlays are
1844+ // rebuilt from. These payloads hold no profile keys, so the landed record is empty.
1845+ expect ( setValues ) . toHaveBeenCalledWith (
1846+ { [ key ] : value } ,
1847+ { savedProfileName : "default" , savedProfileSettings : { } } ,
1848+ )
17481849 expect ( log ) . not . toHaveBeenCalled ( )
17491850 } )
17501851
@@ -1775,7 +1876,10 @@ describe("API - configuration", () => {
17751876 // A hand-written value that the typed signature does not allow.
17761877 await api . setConfiguration ( { [ key ] : value , soundEnabled : true } as unknown as RooCodeSettings )
17771878
1778- expect ( setValues ) . toHaveBeenCalledWith ( { soundEnabled : true } , { savedProfileName : "default" } )
1879+ expect ( setValues ) . toHaveBeenCalledWith (
1880+ { soundEnabled : true } ,
1881+ { savedProfileName : "default" , savedProfileSettings : { } } ,
1882+ )
17791883 expect ( log ) . toHaveBeenCalledWith ( `[setConfiguration] Ignored an invalid value for ${ key } ` )
17801884 } )
17811885 } )
0 commit comments