Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(codex): preserve legacy workspace identity during auth recovery
Lizi1de86d7bb28b
diff --git a/apps/desktop/src/main/maker-host/__tests__/codexAuthIsolatedSandbox.test.ts b/apps/desktop/src/main/maker-host/__tests__/codexAuthIsolatedSandbox.test.tsindex 919247fa04..39094c48e5 100644--- a/apps/desktop/src/main/maker-host/__tests__/codexAuthIsolatedSandbox.test.ts+++ b/apps/desktop/src/main/maker-host/__tests__/codexAuthIsolatedSandbox.test.ts@@ -363,6 +363,30 @@ describe('dev 沙箱凭证隔离(XDT_ISOLATED_AUTH)', () => {expect(getPreferredSharedCodexAuthPath('owner-a')).toBe(releaseAuth);});+ // 同一恢复入口也支持旧 auth.json 的 id_token workspace;sub 不能证明 workspace 一致。+ it.each([+ { label: '缺少 account_id 时使用嵌套 workspace', accountId: undefined, claims: { 'https://api.openai.com/auth': { chatgpt_account_id: 'acct-1' } }, useCli: true },+ { label: '空 account_id 时使用顶层 workspace', accountId: '', claims: { chatgpt_account_id: 'acct-1' }, useCli: true },+ { label: '只有 sub 时不能确认 workspace', accountId: undefined, claims: { sub: 'acct-1' }, useCli: false },+ { label: '不同 workspace 时不切换', accountId: undefined, claims: { chatgpt_account_id: 'acct-other', sub: 'acct-1' }, useCli: false },+ { label: '非空 account_id 优先于 id_token workspace', accountId: 'acct-other', claims: { chatgpt_account_id: 'acct-1' }, useCli: false },+ ])('$label', ({ accountId, claims, useCli }) => {+ const { releaseAuth, systemAuth } = fixture();+ const now = Math.floor(Date.now() / 1000);+ const token = (payload: unknown) =>+ `header.${Buffer.from(JSON.stringify(payload)).toString('base64url')}.signature`;+ fs.mkdirSync(path.dirname(releaseAuth), { recursive: true });+ fs.writeFileSync(releaseAuth, JSON.stringify({ tokens: {+ access_token: token({ exp: now - 3600 }), account_id: accountId, id_token: token(claims),+ } }));+ fs.writeFileSync(systemAuth, JSON.stringify({ tokens: {+ access_token: token({ exp: now + 3600 }), account_id: useCli ? accountId : 'acct-1', id_token: token(claims),+ } }));+ bindReleaseOpenAi(releaseAuth);++ expect(getPreferredSharedCodexAuthPath('owner-a')).toBe(useCli ? systemAuth : releaseAuth);+ });+it('共享 Dev 直接使用 Release 登录态,不重链接或改 binding', async () => {const { releaseAuth, systemAuth } = fixture();h.userDataDir = path.join(h.appDataDir, 'CindyGlobal');diff --git a/apps/desktop/src/main/maker-host/auth-adapters.ts b/apps/desktop/src/main/maker-host/auth-adapters.tsindex cf99b8e615..d9b9d79d7e 100644--- a/apps/desktop/src/main/maker-host/auth-adapters.ts+++ b/apps/desktop/src/main/maker-host/auth-adapters.ts@@ -57,6 +57,7 @@ import {resolveWindowsAclPrincipal,} from './codex-auth-link.js';import { getCodexCliAuthPath, getPreferredSharedCodexAuthPath, getReleaseCodexAuthPath } from './codex-shared-auth.js';+import { codexWorkspaceIdFromTokens, readChatgptIdTokenClaims } from './codex-auth-identity.js';export { resolveWindowsAclPrincipal } from './codex-auth-link.js';import {@@ -199,12 +200,7 @@ async function readCodexAccountId(authPath: string): Promise<string | null> {function codexAccountIdFromAuthJson(raw: string): string | null {try {const obj = JSON.parse(raw) as { tokens?: { account_id?: unknown; id_token?: unknown } };- if (typeof obj.tokens?.account_id === 'string' && obj.tokens.account_id.length > 0) {- return obj.tokens.account_id;- }- if (typeof obj.tokens?.id_token === 'string') {- return chatgptWorkspaceIdFromIdToken(obj.tokens.id_token);- }+ return codexWorkspaceIdFromTokens(obj.tokens);} catch {/* 解析失败 → null */}@@ -235,39 +231,13 @@ function readCodexRecoveryCredentialProof(}}-interface ChatgptIdTokenClaims {- chatgpt_account_id?: unknown;- sub?: unknown;- 'https://api.openai.com/auth'?: { chatgpt_account_id?: unknown };-}--function readChatgptIdTokenClaims(idToken: string): ChatgptIdTokenClaims | null {- try {- const part = idToken.split('.')[1];- if (!part) return null;- const b64 = part.replace(/-/g, '+').replace(/_/g, '/');- return JSON.parse(Buffer.from(b64, 'base64').toString('utf-8')) as ChatgptIdTokenClaims;- } catch {- return null;- }-}--/** Strict workspace identity; unlike header compatibility it never falls back to JWT sub. */-function chatgptWorkspaceIdFromIdToken(idToken: string): string | null {- const claims = readChatgptIdTokenClaims(idToken);- const nested = claims?.['https://api.openai.com/auth']?.chatgpt_account_id;- if (typeof nested === 'string' && nested.length > 0) return nested;- const topLevel = claims?.chatgpt_account_id;- return typeof topLevel === 'string' && topLevel.length > 0 ? topLevel : null;-}-/*** 从 id_token JWT 提取 ChatGPT 账号 id(chatgpt-account-id 头用)。* 优先 `https://api.openai.com/auth`.chatgpt_account_id,回落顶层 chatgpt_account_id / sub。* export 供 anthropic-responses-bridge-host 复用(同一 auth.json、同一 claim 布局,单点维护)。*/export function chatgptAccountIdFromIdToken(idToken: string): string | null {- const workspaceId = chatgptWorkspaceIdFromIdToken(idToken);+ const workspaceId = codexWorkspaceIdFromTokens({ id_token: idToken });if (workspaceId) return workspaceId;const sub = readChatgptIdTokenClaims(idToken)?.sub;return typeof sub === 'string' && sub.length > 0 ? sub : null;diff --git a/apps/desktop/src/main/maker-host/codex-auth-identity.ts b/apps/desktop/src/main/maker-host/codex-auth-identity.tsnew file mode 100644index 0000000000..221471b024--- /dev/null+++ b/apps/desktop/src/main/maker-host/codex-auth-identity.ts@@ -0,0 +1,30 @@+interface ChatgptIdTokenClaims {+ chatgpt_account_id?: unknown;+ sub?: unknown;+ 'https://api.openai.com/auth'?: { chatgpt_account_id?: unknown };+}++export function readChatgptIdTokenClaims(idToken: string): ChatgptIdTokenClaims | null {+ try {+ const part = idToken.split('.')[1];+ if (!part) return null;+ const b64 = part.replace(/-/g, '+').replace(/_/g, '/');+ return JSON.parse(Buffer.from(b64, 'base64').toString('utf-8')) as ChatgptIdTokenClaims;+ } catch {+ return null;+ }+}++/** Workspace identity for reconciliation/recovery; JWT sub cannot identify a workspace. */+export function codexWorkspaceIdFromTokens(+ tokens: { account_id?: unknown; id_token?: unknown } | undefined,+): string | null {+ if (typeof tokens?.account_id === 'string' && tokens.account_id.length > 0) {+ return tokens.account_id;+ }+ const claims = typeof tokens?.id_token === 'string' ? readChatgptIdTokenClaims(tokens.id_token) : null;+ const nested = claims?.['https://api.openai.com/auth']?.chatgpt_account_id;+ if (typeof nested === 'string' && nested.length > 0) return nested;+ const topLevel = claims?.chatgpt_account_id;+ return typeof topLevel === 'string' && topLevel.length > 0 ? topLevel : null;+}diff --git a/apps/desktop/src/main/maker-host/codex-shared-auth.ts b/apps/desktop/src/main/maker-host/codex-shared-auth.tsindex 3a209506c4..3d02056abd 100644--- a/apps/desktop/src/main/maker-host/codex-shared-auth.ts+++ b/apps/desktop/src/main/maker-host/codex-shared-auth.ts@@ -6,6 +6,7 @@ import path from 'node:path';import { brandUserDataDirName } from '@cindy/maker-shared/brand-identity';import { CURRENT_CINDY_REGION } from '../../shared/brandRegion.js';import { shouldSuppressLocalCodexAuth } from './codex-auth-invalidation.js';+import { codexWorkspaceIdFromTokens } from './codex-auth-identity.js';/** 本机 Codex CLI 默认维护的 OpenAI 登录态。 */export function getCodexCliAuthPath(): string {@@ -57,7 +58,7 @@ type CodexOAuthCandidate = { accountId: string | null; expiresAtMs: number | nulfunction readCodexOAuthCandidate(pathname: string): CodexOAuthCandidate | null {try {const parsed = JSON.parse(fs.readFileSync(pathname, 'utf8')) as {- tokens?: { access_token?: unknown; account_id?: unknown };+ tokens?: { access_token?: unknown; account_id?: unknown; id_token?: unknown };};const token = parsed.tokens?.access_token;if (typeof token !== 'string' || token.length === 0) return null;@@ -73,7 +74,7 @@ function readCodexOAuthCandidate(pathname: string): CodexOAuthCandidate | null {// Unknown expiry must not discard an otherwise supported login format.}return {- accountId: typeof parsed.tokens?.account_id === 'string' ? parsed.tokens.account_id : null,+ accountId: codexWorkspaceIdFromTokens(parsed.tokens),expiresAtMs,};} catch {