Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(models): revoke old ultrafast access on account replacement
Chris8513c94ab3a3
diff --git a/apps/desktop/src/main/maker-host/__tests__/codex-account-auth.test.ts b/apps/desktop/src/main/maker-host/__tests__/codex-account-auth.test.tsindex 75f39f41eb..a422087ce3 100644--- a/apps/desktop/src/main/maker-host/__tests__/codex-account-auth.test.ts+++ b/apps/desktop/src/main/maker-host/__tests__/codex-account-auth.test.ts@@ -220,6 +220,8 @@ describe('native Codex account credentials', () => {await loginCodexAccount('account-a', () => true);await loginCodexAccount('account-b', () => true);await logoutCodexAccount('account-a');+ const cache = path.join(codexAccountHome('account-a'), 'models_cache.json');+ fs.writeFileSync(cache, '{"models":[{"slug":"gpt-6.1-sol","additional_speed_tiers":["ultrafast"]}]}');const retire = vi.fn(async () => {expect(codexAccountState('account-a').authenticated).toBe(false);});@@ -229,7 +231,9 @@ describe('native Codex account credentials', () => {ok: true,firstLogin: false,previousIdentity: '[email protected]',+ identityChanged: subject !== 'person-a' || workspace !== 'workspace',});+ expect(fs.existsSync(cache)).toBe(subject === 'person-a' && workspace === 'workspace');expect(retire).toHaveBeenCalledExactlyOnceWith('account-a');expect(codexAccountState('account-a').identity).toBe(`${subject}@example.test`);expect(codexAccountState('account-b').identity).toBe('[email protected]');@@ -247,12 +251,18 @@ describe('native Codex account credentials', () => {await logoutCodexAccount('account-a');const home = codexAccountHome('account-a');const identity = fs.readFileSync(path.join(home, 'account.json'), 'utf8');+ const cache = path.join(home, 'models_cache.json');+ const cachedModels = '{"models":[{"slug":"gpt-6.1-sol","additional_speed_tiers":["ultrafast"]}]}';+ fs.writeFileSync(cache, cachedModels);state.raw = credential('person-b');const result = await loginCodexAccount('account-a', () => true);expect(result.ok).toBe(true);+ expect(result.identityChanged).toBe(true);+ expect(fs.existsSync(cache)).toBe(false);expect(result.rollbackCredentials?.()).toBe(true);expect(codexAccountState('account-a').authenticated).toBe(false);expect(fs.readFileSync(path.join(home, 'account.json'), 'utf8')).toBe(identity);+ expect(fs.readFileSync(cache, 'utf8')).toBe(cachedModels);expect(fs.existsSync(path.join(home, 'auth.json'))).toBe(false);});it('does not commit a replacement cancelled while retiring the old runtime', async () => {diff --git a/apps/desktop/src/main/maker-host/__tests__/custom-provider-store.test.ts b/apps/desktop/src/main/maker-host/__tests__/custom-provider-store.test.tsindex 7ee3201519..15fee7f144 100644--- a/apps/desktop/src/main/maker-host/__tests__/custom-provider-store.test.ts+++ b/apps/desktop/src/main/maker-host/__tests__/custom-provider-store.test.ts@@ -11,6 +11,7 @@ import { createDbClient } from '../../localDb/client/DbClient.js';import { clearCurrentDbClient, setCurrentDbClient } from '../../localDb/client/current.js';import * as schema from '../../localDb/schema.js';import {+ clearCodexAccountUltrafast,createCustomProvider,deleteCustomProvider,getCustomProvider,@@ -119,6 +120,14 @@ describe('custom provider updates through the runtime database proxy', () => {{ ...current, runtimes: { codex: { ...current.runtimes.codex!, models: refreshed } } });expect(buildUserProvider((await getCustomProvider(native.id))!).models.codex![0].supportsUltrafastMode).toBe(false);}+ // Accepted replacement login clears before best-effort discovery. A failed+ // refresh writes nothing, so a reload must still expose no old entitlement.+ await updateCustomProvider(native.id, restored);+ expect(await updateCustomProviderIfUnchanged(native.id, restored,+ clearCodexAccountUltrafast(restored))).toBe(true);+ const replaced = (await getCustomProvider(native.id))!;+ expect(buildUserProvider(replaced).models.codex![0].supportsUltrafastMode).toBe(false);+ expect(replaced.runtimes.codex!.models[0].discoveredMetadata).toEqual(models[0].discoveredMetadata);expect(validateCustomProviderConfig({ ...next, runtimes: { codex: {...next.runtimes.codex, models: [{ ...models[0], supportsUltrafastMode: 'true' }],} } }).ok).toBe(false);diff --git a/apps/desktop/src/main/maker-host/codex-account-auth.ts b/apps/desktop/src/main/maker-host/codex-account-auth.tsindex 05665ea56d..e7e57c5985 100644--- a/apps/desktop/src/main/maker-host/codex-account-auth.ts+++ b/apps/desktop/src/main/maker-host/codex-account-auth.ts@@ -159,6 +159,7 @@ export async function loginCodexAccount(reason?: string;firstLogin?: boolean;previousIdentity?: string;+ identityChanged?: boolean;rollbackCredentials?: () => boolean;}> {if (!isCodexAccountProvider(providerId)) throw new Error('Unknown Codex account provider');@@ -263,7 +264,8 @@ export async function loginCodexAccount(accountAuthTransitionStarted = true;await retireAccount(providerId);if (!current()) return { ok: false, reason: 'login_cancelled' };- const files = ['auth.json', 'account.json', 'config.toml', 'disconnected', 'invalidated'];+ const identityChanged = previous?.principal !== identity.principal;+ const files = ['auth.json', 'account.json', 'config.toml', 'disconnected', 'invalidated', 'models_cache.json'];const before = files.map((file) => {try {return fs.readFileSync(path.join(home, file));@@ -293,9 +295,11 @@ export async function loginCodexAccount(fs.renameSync(path.join(staging, 'auth.json'), path.join(home, 'auth.json'));fs.rmSync(path.join(home, 'disconnected'), { force: true });fs.rmSync(path.join(home, 'invalidated'), { force: true });+ // An old identity's model cache must not regrant its account-only tiers.+ if (identityChanged) fs.rmSync(path.join(home, 'models_cache.json'), { force: true });broadcastCodexAccountAuthReady(providerId);accountAuthReadySent = true;- return { ok: true, firstLogin: previous === null, previousIdentity: previous?.label, rollbackCredentials };+ return { ok: true, firstLogin: previous === null, previousIdentity: previous?.label, identityChanged, rollbackCredentials };} finally {onBrowserUrl?.(null);if (logins.get(home) === operation) logins.delete(home);diff --git a/apps/desktop/src/main/maker-host/custom-provider-store.ts b/apps/desktop/src/main/maker-host/custom-provider-store.tsindex f2e020a90e..42d4517ef6 100644--- a/apps/desktop/src/main/maker-host/custom-provider-store.ts+++ b/apps/desktop/src/main/maker-host/custom-provider-store.ts@@ -770,6 +770,15 @@ export function mergeDiscoveredModelsIntoConfig(};}+/** Called only after an explicit native login commits a different identity. */+export function clearCodexAccountUltrafast(config: CustomProviderConfig): CustomProviderConfig {+ const runtime = config.runtimes.codex;+ if (config.auth?.native !== 'codex' || !runtime) return config;+ return { ...config, runtimes: { ...config.runtimes, codex: {+ ...runtime, models: runtime.models.map(model => ({ ...model, supportsUltrafastMode: false })),+ } } };+}+/** 安全解析 auth 列 JSON(坏数据 / 结构不完整兜底为 undefined = API key 历史形态)。 */function parseAuth(raw: string | null): CustomProviderConfig['auth'] {if (!raw) return undefined;diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.tsindex 74622fc950..35c1d90191 100644--- a/apps/desktop/src/main/maker-ipc/register.ts+++ b/apps/desktop/src/main/maker-ipc/register.ts@@ -1187,6 +1187,7 @@ import {runGenericOAuthLogin,} from '../maker-host/generic-oauth.js';import {+ clearCodexAccountUltrafast,getCustomProvider,mergeDiscoveredModelsIntoConfig,updateCustomProviderIfUnchanged,@@ -7373,20 +7374,27 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)if (method === 'device') throw new Error('Device login is only available for Grok');if (isCodexAccountProvider(providerId)) {const owner = getActiveAppSession();- const current = () => isCurrent() && getActiveAppSession().generation === owner.generation;+ const sameOwner = () => getActiveAppSession().generation === owner.generation;+ const current = () => isCurrent() && sameOwner();const result = await loginCodexAccount(providerId, isCurrent, onBrowserUrl);if (!result.ok) return result;return { ...result, afterCommit: async () => {- if (!current()) return;+ // Cancellation after acceptance keeps the new credentials, so it must+ // not interrupt invalidation of the old identity's entitlement.+ if (!sameOwner()) return;const previous = await getCustomProvider(providerId);- if (!current()) return;+ if (!sameOwner()) return;const identity = codexAccountState(providerId).identity;- if (previous && identity) {+ if (previous) {+ // Cancellation before acceptance rolls credentials back without+ // invoking afterCommit or changing the saved capabilities.+ let next = result.identityChanged ? clearCodexAccountUltrafast(previous) : previous;const names = new Set(getActiveCatalog().providers.filter((provider) => provider.id !== providerId).map((provider) => provider.name));- const name = codexAccountLoginName(previous.name, result.previousIdentity, identity, names);- if (name !== undefined) {- await updateCustomProviderIfUnchanged(providerId, previous, { ...previous, name });- if (!current()) return;+ const name = identity ? codexAccountLoginName(previous.name, result.previousIdentity, identity, names) : undefined;+ if (name !== undefined) next = { ...next, name };+ if (next !== previous) {+ await updateCustomProviderIfUnchanged(providerId, previous, next);+ if (!sameOwner()) return;await refreshCustomProvidersIntoCatalog();}}diff --git a/docs/product-rules/model-metadata-precedence.md b/docs/product-rules/model-metadata-precedence.mdindex db8ac549a8..beedb11fcc 100644--- a/docs/product-rules/model-metadata-precedence.md+++ b/docs/product-rules/model-metadata-precedence.md@@ -105,6 +105,8 @@ Codex 超快是账号发现能力:`service_tiers` / `additional_speed_tiers``serviceTiers` / `additionalSpeedTiers`)明确声明后,按连接保存 `supportsUltrafastMode`,不进入可继承的 `ModelMetadata`,不按套餐名、型号或公共目录授予。保存、重读和目录投影须保留该值;成功刷新该型号不再声明超快时清除旧资格。+同一连接重新登录为不同身份或工作区时,提交登录后、刷新模型前清除旧资格,+原生模型缓存随身份切换清除;取消登录回滚凭证与缓存,不提交资格变更。仅原生 Codex 账号的 Codex 入口可用,API 同名模型及其它引擎不继承。回归见 `custom-provider-store.test.ts` 与 `modelDiscoveryChannels.test.ts`。