Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop/maker-core): 识别 Codex refresh token 被撤销的鉴权失效,重连替代无效重试 (#608)

Lizifcc4401cbb09
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/ErrorBannerCodexOAuth.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/ErrorBannerCodexOAuth.test.tsx
index c296bf0d00..fbec62c711 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/ErrorBannerCodexOAuth.test.tsx
+++ b/apps/desktop/src/renderer/components/chat/__tests__/ErrorBannerCodexOAuth.test.tsx
@@ -240,6 +240,25 @@ describe('ErrorBanner OpenAI connection recovery', () => {
expect(screen.getByRole('button', { name: 'chat.errorBanner.retry' })).toBeTruthy();
});
+ it('classifies the wrapped thread/resume refresh-token failure as reconnect, not retry', () => {
+ render(
+ <ErrorBanner
+ error="LAZY_CREATE_FAILED: Failed to resume Codex thread: Error: codex app-server thread/resume error -32600: failed to load configuration: Your access token could not be refreshed because your refresh token was revoked. Please log out and sign in again."
+ retryText="retry this turn"
+ onRetry={vi.fn()}
+ agentKind="codex"
+ modelId="gpt-5.4"
+ providerId="openai"
+ />,
+ );
+
+ expect(screen.getByText('chat.errorBanner.codexSessionExpired')).toBeTruthy();
+ expect(
+ screen.getByRole('button', { name: 'chat.errorBanner.codexSessionExpiredLogin' }),
+ ).toBeTruthy();
+ expect(screen.queryByRole('button', { name: 'chat.errorBanner.retry' })).toBeNull();
+ });
+
it('does not classify a non-OpenAI provider error as ChatGPT reconnect', () => {
render(
<ErrorBanner
diff --git a/apps/desktop/src/renderer/hooks/__tests__/codexAuthRecovery.test.ts b/apps/desktop/src/renderer/hooks/__tests__/codexAuthRecovery.test.ts
new file mode 100644
index 0000000000..cc7c883182
--- /dev/null
+++ b/apps/desktop/src/renderer/hooks/__tests__/codexAuthRecovery.test.ts
@@ -0,0 +1,49 @@
+import { describe, expect, it } from 'vitest';
+
+import { isCodexOAuthReconnectRequired } from '../codexAuthRecovery';
+
+describe('isCodexOAuthReconnectRequired', () => {
+ it('matches invalidation reason tokens from auth adapters', () => {
+ for (const reason of [
+ 'app_session_terminated',
+ 'token_invalidated',
+ 'token_revoked',
+ 'refresh_token_reused',
+ ]) {
+ expect(isCodexOAuthReconnectRequired(reason)).toBe(true);
+ }
+ });
+
+ it('matches every codex-rs permanent refresh failure sentence variant', () => {
+ // codex-rs login/src/auth/manager.rs 的 REFRESH_TOKEN_*_MESSAGE 家族:
+ // 共享前缀 "access token could not be refreshed",全部为永久失败、需重登。
+ const variants = [
+ 'Your access token could not be refreshed because your refresh token was revoked. Please log out and sign in again.',
+ 'Your access token could not be refreshed because your refresh token has expired. Please log out and sign in again.',
+ 'Your access token could not be refreshed because your refresh token was already used. Please log out and sign in again.',
+ 'Your access token could not be refreshed. Please log out and sign in again.',
+ 'Your access token could not be refreshed because you have since logged out or signed in to another account. Please sign in again.',
+ ];
+ for (const variant of variants) {
+ expect(isCodexOAuthReconnectRequired(variant)).toBe(true);
+ }
+ });
+
+ it('matches the full wrapped thread/resume config-load failure end to end', () => {
+ expect(
+ isCodexOAuthReconnectRequired(
+ 'LAZY_CREATE_FAILED: Failed to resume Codex thread: Error: codex app-server thread/resume error -32600: failed to load configuration: Your access token could not be refreshed because your refresh token was revoked. Please log out and sign in again.',
+ ),
+ ).toBe(true);
+ });
+
+ it('does not match unrelated errors', () => {
+ expect(isCodexOAuthReconnectRequired(undefined)).toBe(false);
+ expect(isCodexOAuthReconnectRequired('')).toBe(false);
+ expect(isCodexOAuthReconnectRequired('fetch failed: ECONNREFUSED')).toBe(false);
+ expect(isCodexOAuthReconnectRequired('failed to load configuration: invalid TOML')).toBe(
+ false,
+ );
+ expect(isCodexOAuthReconnectRequired('401 Missing bearer token')).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/renderer/hooks/codexAuthRecovery.ts b/apps/desktop/src/renderer/hooks/codexAuthRecovery.ts
index 9d33b4a767..ed20319f35 100644
--- a/apps/desktop/src/renderer/hooks/codexAuthRecovery.ts
+++ b/apps/desktop/src/renderer/hooks/codexAuthRecovery.ts
@@ -6,7 +6,10 @@
*/
export function isCodexOAuthReconnectRequired(reason: string | undefined): boolean {
if (!reason) return false;
- return /app_session_terminated|token_invalidated|token_revoked|refresh_token_reused|Your session has ended|authentication token has been invalidated|authentication token has been revoked|refresh token was already used|refresh_token.*already used|bridge auth unavailable for chatgpt\//i.test(
+ // "access token could not be refreshed" 覆盖 codex-rs 永久刷新失败的整个文案家族
+ // (revoked / expired / already used / account mismatch / unknown)——这些句式只在
+ // refresh token 已不可用时产生,重试必然无效,只能重新连接。
+ return /app_session_terminated|token_invalidated|token_revoked|refresh_token_reused|Your session has ended|authentication token has been invalidated|authentication token has been revoked|refresh token was already used|refresh_token.*already used|access token could not be refreshed|refresh token (?:was|has been) revoked|bridge auth unavailable for chatgpt\//i.test(
reason,
);
}
diff --git a/packages/maker-core/src/agents/codex/app-server/client.test.ts b/packages/maker-core/src/agents/codex/app-server/client.test.ts
index 7ef9089146..87f6adb52d 100644
--- a/packages/maker-core/src/agents/codex/app-server/client.test.ts
+++ b/packages/maker-core/src/agents/codex/app-server/client.test.ts
@@ -100,6 +100,75 @@ describe('detectAuthInvalidationReason', () => {
).toBe('token_invalidated');
});
+ it('maps structured cloudConfigBundle auth errors from config load failures', () => {
+ // codex-rs app-server config_errors.rs: thread/resume 等在配置加载阶段撞
+ // 鉴权失败时返回 -32600 + reason=cloudConfigBundle (detail 带 refresh 失败句)。
+ expect(
+ detectAuthInvalidationReason({
+ code: -32600,
+ message:
+ 'failed to load configuration: Your access token could not be refreshed because your refresh token was revoked. Please log out and sign in again.',
+ data: {
+ reason: 'cloudConfigBundle',
+ errorCode: 'Auth',
+ action: 'relogin',
+ statusCode: 401,
+ detail:
+ 'Your access token could not be refreshed because your refresh token was revoked. Please log out and sign in again.',
+ },
+ }),
+ ).toBe('token_revoked');
+ expect(
+ detectAuthInvalidationReason({
+ code: -32600,
+ message: 'failed to load configuration: request failed',
+ data: { reason: 'cloudConfigBundle', errorCode: 'Network' },
+ }),
+ ).toBeNull();
+ });
+
+ it('maps the text-only config-load token refresh failure without structured data', () => {
+ expect(
+ detectAuthInvalidationReason({
+ code: -32600,
+ message:
+ 'failed to load configuration: Your access token could not be refreshed because your refresh token was revoked. Please log out and sign in again.',
+ }),
+ ).toBe('token_revoked');
+ expect(
+ detectAuthInvalidationReason({
+ code: -32600,
+ message:
+ 'failed to load configuration: Your access token could not be refreshed because your refresh token has expired. Please log out and sign in again.',
+ }),
+ ).toBe('token_invalidated');
+ expect(
+ detectAuthInvalidationReason({
+ code: -32600,
+ message:
+ 'failed to load configuration: Your access token could not be refreshed because your refresh token was already used. Please log out and sign in again.',
+ }),
+ ).toBe('refresh_token_reused');
+ });
+
+ it('rejects config-load errors and bare refresh sentences that lack the paired signal', () => {
+ // 非鉴权的配置加载失败: 不能清凭证。
+ expect(
+ detectAuthInvalidationReason({
+ code: -32600,
+ message: 'failed to load configuration: invalid TOML in config.toml',
+ }),
+ ).toBeNull();
+ // 孤立句子没有 config-load 包装也没有结构化 provenance: 保持窄门。
+ expect(
+ detectAuthInvalidationReason({
+ code: -32000,
+ message:
+ 'Your access token could not be refreshed because your refresh token was revoked.',
+ }),
+ ).toBeNull();
+ });
+
it('rejects keyword matches without the structured cloud auth provenance', () => {
expect(
detectAuthInvalidationReason({
diff --git a/packages/maker-core/src/agents/codex/app-server/client.ts b/packages/maker-core/src/agents/codex/app-server/client.ts
index 9089a3f550..888344ca00 100644
--- a/packages/maker-core/src/agents/codex/app-server/client.ts
+++ b/packages/maker-core/src/agents/codex/app-server/client.ts
@@ -74,24 +74,39 @@ function isRecord(value: unknown): value is Record<string, unknown> {
/**
* Detect a definitive Codex authentication failure from a correlated JSON-RPC
* error response. Codex Desktop uses this same protocol boundary: stderr stays
- * diagnostic-only, while auth state changes require cloudRequirements plus an
- * explicit Auth/relogin action from app-server.
+ * diagnostic-only, while auth state changes require a structured Auth/relogin
+ * signal from app-server (cloudRequirements / cloudConfigBundle), or the
+ * narrowly-matched config-load wrapper around codex-rs' permanent token
+ * refresh failure.
*/
export function detectAuthInvalidationReason(error: JsonRpcErrorObject): string | null {
const data = isRecord(error.data) ? error.data : null;
- if (
- data?.reason !== 'cloudRequirements' ||
- (data.errorCode !== 'Auth' && data.action !== 'relogin')
- ) {
+ // 两种结构化 provenance 都是 app-server 主动声明的鉴权失效:
+ // - cloudRequirements: turn 级 cloud 依赖检查失败 (Auth/relogin)。
+ // - cloudConfigBundle: 配置加载阶段 cloud config bundle 拉取撞鉴权失败
+ // (config_errors.rs 对 Auth code 附 action=relogin)。
+ const hasStructuredAuthSignal =
+ (data?.reason === 'cloudRequirements' || data?.reason === 'cloudConfigBundle') &&
+ (data?.errorCode === 'Auth' || data?.action === 'relogin');
+ // 文本兜底 (窄门): 非 cloud-config-bundle 的错误链不带结构化 data, 但 message 仍是
+ // app-server 生成的 "failed to load configuration: {err}" 包装; 其中 codex-rs 的
+ // "Your access token could not be refreshed ..." 句族只出自永久性 refresh 失败
+ // (revoked / expired / reused / account mismatch), 重试必然复现。两段都要求命中,
+ // 避免 correlated response error 里偶发回显的孤立关键词触发凭证清除。
+ const isTextualConfigLoadRefreshFailure =
+ !hasStructuredAuthSignal &&
+ /failed to load configuration:/i.test(error.message) &&
+ /access token could not be refreshed/i.test(error.message);
+ if (!hasStructuredAuthSignal && !isTextualConfigLoadRefreshFailure) {
return null;
}
- const nestedError = isRecord(data.error) ? data.error : null;
+ const nestedError = isRecord(data?.error) ? data.error : null;
const diagnostic = [
error.message,
- typeof data.message === 'string' ? data.message : '',
- typeof data.detail === 'string' ? data.detail : '',
- typeof data.code === 'string' ? data.code : '',
+ typeof data?.message === 'string' ? data.message : '',
+ typeof data?.detail === 'string' ? data.detail : '',
+ typeof data?.code === 'string' ? data.code : '',
typeof nestedError?.message === 'string' ? nestedError.message : '',
typeof nestedError?.code === 'string' ? nestedError.code : '',
].join('\n');
@@ -102,15 +117,20 @@ export function detectAuthInvalidationReason(error: JsonRpcErrorObject): string
if (/token_invalidated|authentication token has been invalidated/i.test(diagnostic)) {
return 'token_invalidated';
}
- if (/token_revoked|authentication token has been revoked/i.test(diagnostic)) {
+ if (
+ /token_revoked|authentication token has been revoked|refresh token (?:was|has been) revoked/i.test(
+ diagnostic,
+ )
+ ) {
return 'token_revoked';
}
if (/refresh token was already used|refresh_token.*already used/i.test(diagnostic)) {
return 'refresh_token_reused';
}
- // The structured Auth/relogin signal is itself definitive even when the
- // app-server does not expose the provider-specific token error code.
+ // Both gates above are definitive on their own (structured Auth/relogin, or the
+ // permanent refresh-failure sentence family — e.g. the "has expired" / account
+ // mismatch variants) even without a provider-specific token error code.
return 'token_invalidated';
}