@@ -782,7 +782,43 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
782782
783783 const pruneRefreshTokenCollisions = ( ) : void => {
784784 const indicesToRemove = new Set < number > ( ) ;
785- const refreshMap = new Map < string , { byOrg : Map < string , number > ; fallbackIndex ?: number } > ( ) ;
785+ const refreshMap = new Map <
786+ string ,
787+ { byOrg : Map < string , number > ; preferredOrgIndex ?: number ; fallbackIndex ?: number }
788+ > ( ) ;
789+
790+ const pickPreferredOrgIndex = (
791+ existingIndex : number | undefined ,
792+ candidateIndex : number ,
793+ ) : number => {
794+ if ( existingIndex === undefined ) return candidateIndex ;
795+ return pickNewestAccountIndex ( existingIndex , candidateIndex ) ;
796+ } ;
797+
798+ const collapseFallbackIntoPreferredOrg = ( entry : {
799+ byOrg : Map < string , number > ;
800+ preferredOrgIndex ?: number ;
801+ fallbackIndex ?: number ;
802+ } ) : void => {
803+ if ( entry . preferredOrgIndex === undefined || entry . fallbackIndex === undefined ) {
804+ return ;
805+ }
806+
807+ const preferredOrgIndex = entry . preferredOrgIndex ;
808+ const fallbackIndex = entry . fallbackIndex ;
809+ if ( preferredOrgIndex === fallbackIndex ) {
810+ entry . fallbackIndex = undefined ;
811+ return ;
812+ }
813+
814+ const target = accounts [ preferredOrgIndex ] ;
815+ const source = accounts [ fallbackIndex ] ;
816+ if ( target && source ) {
817+ mergeAccountRecords ( preferredOrgIndex , fallbackIndex ) ;
818+ indicesToRemove . add ( fallbackIndex ) ;
819+ entry . fallbackIndex = undefined ;
820+ }
821+ } ;
786822
787823 for ( let i = 0 ; i < accounts . length ; i += 1 ) {
788824 const account = accounts [ i ] ;
@@ -792,7 +828,11 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
792828 const orgKey = account . organizationId ?. trim ( ) ?? "" ;
793829 let entry = refreshMap . get ( refreshToken ) ;
794830 if ( ! entry ) {
795- entry = { byOrg : new Map < string , number > ( ) , fallbackIndex : undefined } ;
831+ entry = {
832+ byOrg : new Map < string , number > ( ) ,
833+ preferredOrgIndex : undefined ,
834+ fallbackIndex : undefined ,
835+ } ;
796836 refreshMap . set ( refreshToken , entry ) ;
797837 }
798838
@@ -804,9 +844,13 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
804844 mergeAccountRecords ( newestIndex , obsoleteIndex ) ;
805845 indicesToRemove . add ( obsoleteIndex ) ;
806846 entry . byOrg . set ( orgKey , newestIndex ) ;
847+ entry . preferredOrgIndex = pickPreferredOrgIndex ( entry . preferredOrgIndex , newestIndex ) ;
848+ collapseFallbackIntoPreferredOrg ( entry ) ;
807849 continue ;
808850 }
809851 entry . byOrg . set ( orgKey , i ) ;
852+ entry . preferredOrgIndex = pickPreferredOrgIndex ( entry . preferredOrgIndex , i ) ;
853+ collapseFallbackIntoPreferredOrg ( entry ) ;
810854 continue ;
811855 }
812856
@@ -817,9 +861,11 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
817861 mergeAccountRecords ( newestIndex , obsoleteIndex ) ;
818862 indicesToRemove . add ( obsoleteIndex ) ;
819863 entry . fallbackIndex = newestIndex ;
864+ collapseFallbackIntoPreferredOrg ( entry ) ;
820865 continue ;
821866 }
822867 entry . fallbackIndex = i ;
868+ collapseFallbackIntoPreferredOrg ( entry ) ;
823869 }
824870
825871 if ( indicesToRemove . size > 0 ) {
@@ -893,7 +939,13 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
893939
894940 const clampedActiveIndex = Math . max ( 0 , Math . min ( Math . floor ( activeIndex ) , accounts . length - 1 ) ) ;
895941 const activeIndexByFamily : Partial < Record < ModelFamily , number > > = { } ;
896- for ( const family of MODEL_FAMILIES ) {
942+ const familiesToPersist = replaceAll
943+ ? [ ]
944+ : MODEL_FAMILIES . filter ( ( family ) => {
945+ const storedFamilyIndex = stored ?. activeIndexByFamily ?. [ family ] ;
946+ return typeof storedFamilyIndex === "number" && Number . isFinite ( storedFamilyIndex ) ;
947+ } ) ;
948+ for ( const family of familiesToPersist ) {
897949 const storedFamilyIndex = stored ?. activeIndexByFamily ?. [ family ] ;
898950 const remappedFamilyIndex = replaceAll
899951 ? undefined
0 commit comments