@@ -10,7 +10,7 @@ import {
1010 TokenRefreshError ,
1111} from "./account-store" ;
1212import { deleteCodexAccount , reconcileMainCodexAccountRuntimeState } from "./account-lifecycle" ;
13- import { checkAccountIdCollision , readCodexTokens } from "./auth-collision" ;
13+ import { checkAccountIdCollision , getMainChatgptAccountId , readCodexTokens } from "./auth-collision" ;
1414export { checkAccountIdCollision , getMainChatgptAccountId } from "./auth-collision" ;
1515export { clearAccountNeedsReauth , isAccountNeedsReauth , markAccountNeedsReauth } from "./account-runtime-state" ;
1616import { clearAccountNeedsReauth , isAccountNeedsReauth , markAccountNeedsReauth } from "./account-runtime-state" ;
@@ -252,12 +252,31 @@ async function isTerminalMainAuthResponse(resp: Response): Promise<boolean> {
252252}
253253
254254export async function fetchMainAccountInfo ( forceRefresh = false ) : Promise < MainAccountInfo > {
255+ return fetchMainAccountInfoAttempt ( forceRefresh , 1 ) ;
256+ }
257+
258+ const EMPTY_MAIN_ACCOUNT_INFO : MainAccountInfo = { email : null , plan : null , quota : null } ;
259+
260+ async function retryMainAccountInfoIfIdentityChanged (
261+ requestAccountId : string | null ,
262+ retriesRemaining : number ,
263+ ) : Promise < MainAccountInfo | null > {
264+ if ( getMainChatgptAccountId ( ) === requestAccountId ) return null ;
265+ reconcileMainCodexAccountRuntimeState ( ) ;
266+ return retriesRemaining > 0
267+ ? fetchMainAccountInfoAttempt ( true , retriesRemaining - 1 )
268+ : EMPTY_MAIN_ACCOUNT_INFO ;
269+ }
270+
271+ async function fetchMainAccountInfoAttempt ( forceRefresh : boolean , retriesRemaining : number ) : Promise < MainAccountInfo > {
272+ reconcileMainCodexAccountRuntimeState ( ) ;
255273 const tokens = readCodexTokens ( ) ;
256274 if ( ! tokens ) {
257275 clearMainAccountInfoCache ( ) ;
258276 markAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ;
259- return { email : null , plan : null , quota : null } ;
277+ return EMPTY_MAIN_ACCOUNT_INFO ;
260278 }
279+ const requestAccountId = extractAccountId ( tokens . id_token , tokens . access_token ) ?? ( tokens . account_id || null ) ;
261280 const cached = getMainAccountInfoCache ( ) ;
262281 if ( ! forceRefresh && cached && Date . now ( ) - cached . ts < MAIN_CACHE_TTL ) {
263282 return cached ;
@@ -268,13 +287,18 @@ export async function fetchMainAccountInfo(forceRefresh = false): Promise<MainAc
268287 signal : AbortSignal . timeout ( 8000 ) ,
269288 } ) ;
270289 if ( ! resp . ok ) {
271- if ( await isTerminalMainAuthResponse ( resp ) ) {
290+ const terminalAuthFailure = await isTerminalMainAuthResponse ( resp ) ;
291+ const retried = await retryMainAccountInfoIfIdentityChanged ( requestAccountId , retriesRemaining ) ;
292+ if ( retried ) return retried ;
293+ if ( terminalAuthFailure ) {
272294 clearMainAccountInfoCache ( ) ;
273295 markAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ;
274296 }
275- return { email : null , plan : null , quota : null } ;
297+ return EMPTY_MAIN_ACCOUNT_INFO ;
276298 }
277299 const data = ( await resp . json ( ) ) as WhamUsageResponse ;
300+ const retried = await retryMainAccountInfoIfIdentityChanged ( requestAccountId , retriesRemaining ) ;
301+ if ( retried ) return retried ;
278302 const result = {
279303 email : data . email ?? null ,
280304 plan : data . plan_type ?? null ,
@@ -298,7 +322,8 @@ export async function fetchMainAccountInfo(forceRefresh = false): Promise<MainAc
298322 }
299323 return result ;
300324 } catch {
301- return { email : null , plan : null , quota : null } ;
325+ const retried = await retryMainAccountInfoIfIdentityChanged ( requestAccountId , retriesRemaining ) ;
326+ return retried ?? EMPTY_MAIN_ACCOUNT_INFO ;
302327 }
303328}
304329
0 commit comments