@@ -472,16 +472,10 @@ export function createOAuthManager(adapter: OAuthManagerAdapter) {
472472 profileId : string ;
473473 refreshed : OAuthCredential ;
474474 } ) : Promise < OAuthCredential | null > {
475- const currentStore = loadStoredOAuthRefreshStore ( params . agentDir ) ;
476- const current = currentStore . profiles [ params . profileId ] ;
477- if ( current ?. type !== "oauth" || current . provider !== params . refreshed . provider ) {
478- return null ;
479- }
480- if ( ! hasMatchingOAuthIdentity ( current , params . refreshed ) ) {
481- return hasUsableOAuthCredential ( current ) ? current : null ;
482- }
483-
484- let saved = false ;
475+ // Single locked pass decides both outcomes so no relog can slip between a
476+ // pre-read and the update: same identity persists the rotation, different
477+ // identity adopts the stored (re-logged) credential for this call.
478+ let adopted : OAuthCredential | null = null ;
485479 const result = await updateAuthProfileStoreWithLock ( {
486480 agentDir : params . agentDir ,
487481 updater : ( store ) => {
@@ -493,13 +487,14 @@ export function createOAuthManager(adapter: OAuthManagerAdapter) {
493487 // losers must win the store or the token family is bricked.
494488 if ( hasMatchingOAuthIdentity ( existing , params . refreshed ) ) {
495489 store . profiles [ params . profileId ] = { ...params . refreshed } ;
496- saved = true ;
490+ adopted = params . refreshed ;
497491 return true ;
498492 }
493+ adopted = hasUsableOAuthCredential ( existing ) ? existing : null ;
499494 return false ;
500495 } ,
501496 } ) ;
502- return result !== null && saved ? params . refreshed : null ;
497+ return result === null ? null : adopted ;
503498 }
504499
505500 async function doRefreshOAuthTokenWithLock ( params : {
0 commit comments