diff --git a/src/agents/auth-profiles.ensureauthprofilestore.test.ts b/src/agents/auth-profiles.ensureauthprofilestore.test.ts index 05ef10d6b1a..91ac1021732 100644 --- a/src/agents/auth-profiles.ensureauthprofilestore.test.ts +++ b/src/agents/auth-profiles.ensureauthprofilestore.test.ts @@ -487,7 +487,7 @@ describe("ensureAuthProfileStore", () => { const store = loadAuthProfileStoreForRuntime(agentDir, { readOnly: true }); - expect(store.order?.["openai-codex"]).toEqual([freshProfileId]); + expect(store.order?.["openai-codex"]).toEqual([freshProfileId, defaultProfileId]); expect(store.profiles[defaultProfileId]).toMatchObject({ type: "oauth", provider: "openai-codex", diff --git a/src/agents/auth-profiles/store.ts b/src/agents/auth-profiles/store.ts index 515df101138..25599413cda 100644 --- a/src/agents/auth-profiles/store.ts +++ b/src/agents/auth-profiles/store.ts @@ -10,7 +10,7 @@ import { log, } from "./constants.js"; import { overlayExternalAuthProfiles, shouldPersistExternalAuthProfile } from "./external-auth.js"; -import { hasOAuthIdentity, isSafeToAdoptMainStoreOAuthIdentity } from "./oauth-shared.js"; +import { isSafeToAdoptMainStoreOAuthIdentity } from "./oauth-shared.js"; import { ensureAuthStoreFile, resolveAuthStatePath, @@ -86,8 +86,10 @@ function isInheritedMainOAuthCredential(params: { return ( mainCredential?.type === "oauth" && (isDeepStrictEqual(mainCredential, params.credential) || - (hasOAuthIdentity(params.credential) && - isSafeToAdoptMainStoreOAuthIdentity(params.credential, mainCredential))) + shouldUseMainOwnerForLocalOAuthCredential({ + local: params.credential, + main: mainCredential, + })) ); } @@ -228,6 +230,31 @@ function buildLocalAuthProfileStoreForSave(params: { }), ), ); + const keptProfileIds = new Set(Object.keys(localStore.profiles)); + localStore.order = localStore.order + ? Object.fromEntries( + Object.entries(localStore.order) + .map(([provider, profileIds]) => [ + provider, + profileIds.filter((profileId) => keptProfileIds.has(profileId)), + ]) + .filter(([, profileIds]) => profileIds.length > 0), + ) + : undefined; + localStore.lastGood = localStore.lastGood + ? Object.fromEntries( + Object.entries(localStore.lastGood).filter(([, profileId]) => + keptProfileIds.has(profileId), + ), + ) + : undefined; + localStore.usageStats = localStore.usageStats + ? Object.fromEntries( + Object.entries(localStore.usageStats).filter(([profileId]) => + keptProfileIds.has(profileId), + ), + ) + : undefined; return localStore; }