Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(auth): 登录准备态失败或超时时解锁到重试 (#3794)
Dash91c10ae3d4b7
diff --git a/apps/desktop/src/main/authStartupGate.ts b/apps/desktop/src/main/authStartupGate.tsindex 3cebf162a8..a02112b16d 100644--- a/apps/desktop/src/main/authStartupGate.ts+++ b/apps/desktop/src/main/authStartupGate.ts@@ -1,5 +1,7 @@import { AuthApiError } from '@cindy/auth-client';+import { LOGIN_PREPARING_UNLOCK_TIMEOUT_MS } from '../shared/authIpc';+/*** 冷启动 auth 流程的「限时等待」编排 —— 与 Electron / 网络层解耦的纯逻辑,* 供 authManager 的 `initialize()`(splash)与 `loadLoginProviders()`(登录准备态)@@ -83,7 +85,7 @@ export async function awaitWithStartupTimeout<T>(* initialize 完成、GuestRoute 放行之后才出现的。这里复用同一把闸,时限更保守* (30s),超时后走既有 error 步(「暂时无法登录」+ 重试);不 abort 在途 getProviders。*/-export const LOGIN_PREPARING_GATE_TIMEOUT_MS = 30_000;+export const LOGIN_PREPARING_GATE_TIMEOUT_MS = LOGIN_PREPARING_UNLOCK_TIMEOUT_MS;export interface PreparingGateLog {info: (...args: unknown[]) => void;diff --git a/apps/desktop/src/renderer/contexts/AuthContext.tsx b/apps/desktop/src/renderer/contexts/AuthContext.tsxindex f529472a2e..4328edfc78 100644--- a/apps/desktop/src/renderer/contexts/AuthContext.tsx+++ b/apps/desktop/src/renderer/contexts/AuthContext.tsx@@ -53,6 +53,7 @@ import { rememberSsoOrgIdentifier } from '@/state/ssoOrgHistory';import { setDeferredUiAssignmentOwner } from '@/features/cc-agent/deferredUiAssignment';import { invalidateProvidersSnapshot } from '@/lib/providersSnapshotStore';import { preloadLocalCatalogSnapshot } from '@/lib/localCatalogSnapshot';+import { awaitDesktopLoginStateLoad } from '../../shared/authIpc';import { getDataOwnerGeneration, setDataOwnerGeneration } from './dataOwnerGeneration';/**@@ -355,7 +356,12 @@ export function AuthProvider({}, [confirm, enableSessionExpiredPrompt, t]);const loadLoginState = useCallback(async (): Promise<DesktopLoginActionResult> => {- const result = await authServiceRef.current!.getLoginState();+ // preparing 只允许在 load 进行中出现。settle / throw / 30s 超时都必须落到+ // identifier 或既有 error 步,避免 AUTH_FLOW_SUPERSEDED + state=null 或 IPC+ // 挂起把「正在连接登录服务」变成永不结束。+ const result = await awaitDesktopLoginStateLoad(() =>+ authServiceRef.current!.getLoginState(),+ );setLoginState(result.state);return result;}, []);diff --git a/apps/desktop/src/shared/__tests__/authIpc.test.ts b/apps/desktop/src/shared/__tests__/authIpc.test.tsindex dc373e99bd..e1d6a0a217 100644--- a/apps/desktop/src/shared/__tests__/authIpc.test.ts+++ b/apps/desktop/src/shared/__tests__/authIpc.test.ts@@ -1,9 +1,12 @@-import { describe, expect, it } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import {+ LOGIN_PREPARING_UNLOCK_TIMEOUT_MS,+ awaitDesktopLoginStateLoad,parseDesktopAccountDeletionConfirmInput,parseDesktopAccountKey,parseDesktopLoginAction,+ settleDesktopLoginResult,} from '../authIpc';describe('desktop auth IPC validation', () => {@@ -187,3 +190,86 @@ describe('parseDesktopAccountDeletionConfirmInput', () => {).toBeNull();});});++describe('settleDesktopLoginResult', () => {+ it('keeps a successful identifier state', () => {+ const state = {+ step: 'identifier' as const,+ providers: {+ region: 'global' as const,+ attribution: 'email' as const,+ email: true,+ phone: false,+ social: [],+ },+ };+ expect(settleDesktopLoginResult({ success: true, state })).toEqual({+ success: true,+ state,+ });+ });++ it('maps a failed null state onto the retryable error step', () => {+ expect(+ settleDesktopLoginResult({+ success: false,+ code: 'AUTH_FLOW_SUPERSEDED',+ state: null,+ }),+ ).toEqual({+ success: false,+ code: 'AUTH_FLOW_SUPERSEDED',+ state: { step: 'error', code: 'AUTH_FLOW_SUPERSEDED', recoverTo: 'identifier' },+ });+ });+});++describe('awaitDesktopLoginStateLoad', () => {+ beforeEach(() => {+ vi.useFakeTimers();+ });+ afterEach(() => {+ vi.useRealTimers();+ });++ it('returns a settled identifier before the preparing timeout', async () => {+ const state = {+ step: 'identifier' as const,+ providers: {+ region: 'global' as const,+ attribution: 'email' as const,+ email: true,+ phone: false,+ social: [],+ },+ };+ await expect(+ awaitDesktopLoginStateLoad(async () => ({ success: true, state })),+ ).resolves.toEqual({ success: true, state });+ });++ it('unlocks preparing after 30s when getLoginState never settles', async () => {+ const hung = () => new Promise<never>(() => undefined);+ const p = awaitDesktopLoginStateLoad(hung);+ const rejected = expect(p).resolves.toEqual({+ success: false,+ code: 'AUTH_SERVICE_UNAVAILABLE',+ state: { step: 'error', code: 'AUTH_SERVICE_UNAVAILABLE', recoverTo: 'identifier' },+ });+ await vi.advanceTimersByTimeAsync(LOGIN_PREPARING_UNLOCK_TIMEOUT_MS - 1);+ await vi.advanceTimersByTimeAsync(1);+ await rejected;+ });++ it('maps a thrown getLoginState onto the retryable error step', async () => {+ await expect(+ awaitDesktopLoginStateLoad(async () => {+ throw new Error('ipc exploded');+ }),+ ).resolves.toEqual({+ success: false,+ code: 'AUTH_SERVICE_UNAVAILABLE',+ state: { step: 'error', code: 'AUTH_SERVICE_UNAVAILABLE', recoverTo: 'identifier' },+ });+ });+});diff --git a/apps/desktop/src/shared/authIpc.ts b/apps/desktop/src/shared/authIpc.tsindex a5edf66242..19c9ad1fa1 100644--- a/apps/desktop/src/shared/authIpc.ts+++ b/apps/desktop/src/shared/authIpc.ts@@ -31,6 +31,67 @@ export type DesktopLoginActionResult =| { success: true; state: AuthFlowState }| { success: false; code: string; state: AuthFlowState | null };+/**+ * 登录准备态(「正在连接登录服务」)最多转圈的时长。+ * main 的 getProviders 闸与 renderer `loadLoginState` 共用,超时后落到既有 error 步。+ */+export const LOGIN_PREPARING_UNLOCK_TIMEOUT_MS = 30_000;++export type SettledDesktopLoginActionResult =+ | { success: true; state: AuthFlowState }+ | { success: false; code: string; state: AuthFlowState };++export function loginPreparingErrorState(+ code = 'AUTH_SERVICE_UNAVAILABLE',+): Extract<AuthFlowState, { step: 'error' }> {+ return { step: 'error', code, recoverTo: 'identifier' };+}++/** IPC 失败且 `state == null` 时不得让 renderer 停在 preparing。 */+export function settleDesktopLoginResult(+ result: DesktopLoginActionResult,+): SettledDesktopLoginActionResult {+ if (result.state) {+ return result.success+ ? { success: true, state: result.state }+ : { success: false, code: result.code, state: result.state };+ }+ const code = result.success === false ? result.code : 'AUTH_SERVICE_UNAVAILABLE';+ return { success: false, code, state: loginPreparingErrorState(code) };+}++/**+ * 限时等待 getLoginState。超时或 throw 都落到可重试 error 步,不 abort 在途请求。+ * 迟到 settle 忽略(与 main 准备态闸一致:只解锁 UI)。+ */+export async function awaitDesktopLoginStateLoad(+ getLoginState: () => Promise<DesktopLoginActionResult>,+ timeoutMs: number = LOGIN_PREPARING_UNLOCK_TIMEOUT_MS,+): Promise<SettledDesktopLoginActionResult> {+ const load = getLoginState().then(settleDesktopLoginResult, () => ({+ success: false as const,+ code: 'AUTH_SERVICE_UNAVAILABLE',+ state: loginPreparingErrorState(),+ }));+ if (timeoutMs <= 0) return load;++ let timer: ReturnType<typeof setTimeout> | undefined;+ const timeout = new Promise<SettledDesktopLoginActionResult>((resolve) => {+ timer = setTimeout(() => {+ resolve({+ success: false,+ code: 'AUTH_SERVICE_UNAVAILABLE',+ state: loginPreparingErrorState(),+ });+ }, timeoutMs);+ });+ try {+ return await Promise.race([load, timeout]);+ } finally {+ if (timer !== undefined) clearTimeout(timer);+ }+}+export interface DesktopSavedAccount {/** Opaque main-owned identifier. Renderer must not derive realm or membership ids from it. */accountKey: string;