@@ -1148,16 +1148,26 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
11481148
11491149 const persistedAccountIndicators = new Map < string , PersistedAccountIndicatorEntry > ( ) ;
11501150 let persistedAccountIndicatorRevision = 0 ;
1151+ let persistedAccountCountHint = 0 ;
11511152
11521153 const nextPersistedAccountIndicatorRevision = ( ) : number => {
11531154 persistedAccountIndicatorRevision += 1 ;
11541155 return persistedAccountIndicatorRevision ;
11551156 } ;
11561157
1158+ const updatePersistedAccountCountHint = (
1159+ count : number | null | undefined ,
1160+ ) : void => {
1161+ if ( ! Number . isFinite ( count ) || count === undefined || count === null ) {
1162+ return ;
1163+ }
1164+ persistedAccountCountHint = Math . max ( 0 , Math . trunc ( count ) ) ;
1165+ } ;
1166+
11571167 const resolvePersistedAccountSessionID = (
11581168 ...candidates : Array < string | null | undefined >
11591169 ) : string | undefined => {
1160- for ( const candidate of [ process . env . CODEX_THREAD_ID , ... candidates ] ) {
1170+ for ( const candidate of [ ... candidates , process . env . CODEX_THREAD_ID ] ) {
11611171 const sessionID = candidate ?. toString ( ) . trim ( ) ;
11621172 if ( sessionID ) {
11631173 return sessionID ;
@@ -1841,6 +1851,7 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
18411851 }
18421852
18431853 await saveAccounts ( storage ) ;
1854+ updatePersistedAccountCountHint ( storage . accounts . length ) ;
18441855
18451856 // Reload manager from disk so we don't overwrite newer rotated
18461857 // refresh tokens with stale in-memory state.
@@ -1916,10 +1927,11 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
19161927 }
19171928 if ( ! lastUserMessage ) return Promise . resolve ( ) ;
19181929
1919- const sessionID =
1930+ const sessionID = resolvePersistedAccountSessionID (
19201931 typeof lastUserMessage . info . sessionID === "string"
19211932 ? lastUserMessage . info . sessionID
1922- : undefined ;
1933+ : undefined ,
1934+ ) ;
19231935 const indicator = getPersistedAccountIndicatorLabel ( sessionID ) ;
19241936 if ( ! indicator ) return Promise . resolve ( ) ;
19251937
@@ -1981,6 +1993,9 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
19811993 }
19821994 let accountManager = await accountManagerPromise ;
19831995 cachedAccountManager = accountManager ;
1996+ updatePersistedAccountCountHint (
1997+ ( await loadAccounts ( ) ) ?. accounts . length ?? accountManager . getAccountCount ( ) ,
1998+ ) ;
19841999 const refreshToken = authFallback ?. refresh ?? "" ;
19852000 const needsPersist =
19862001 refreshToken &&
@@ -2854,9 +2869,10 @@ while (attempted.size < Math.max(1, accountCount)) {
28542869
28552870 accountManager . recordSuccess ( account , modelFamily , model ) ;
28562871 if ( persistAccountFooter ) {
2857- const persistedStorage = await loadAccounts ( ) ;
2858- const persistedAccountCount = persistedStorage ?. accounts . length ??
2859- accountManager . getAccountCount ( ) ;
2872+ const persistedAccountCount =
2873+ persistedAccountCountHint > 0
2874+ ? persistedAccountCountHint
2875+ : accountManager . getAccountCount ( ) ;
28602876 setPersistedAccountIndicator (
28612877 threadIdCandidate ,
28622878 account ,
0 commit comments