fix(auth): keep newer agent oauth credentials

This commit is contained in:
Peter Steinberger
2026-04-29 13:43:53 +01:00
parent 65b0927490
commit 6fcddbbd96
2 changed files with 31 additions and 4 deletions

View File

@@ -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",

View File

@@ -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;
}