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(auth): preserve login across startup timeout recovery

CHEYAN515c352bc01f
diff --git a/apps/desktop/src/main/__tests__/authStartupGate.test.ts b/apps/desktop/src/main/__tests__/authStartupGate.test.ts
index 6422eb0f09..0041624fca 100644
--- a/apps/desktop/src/main/__tests__/authStartupGate.test.ts
+++ b/apps/desktop/src/main/__tests__/authStartupGate.test.ts
@@ -192,6 +192,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
const recover = vi.fn(async () => ({ mode: 'signed-out', ownerBoundaryPending: false }));
const result = await resolveColdStartTimeoutRecovery({
ownerTransitionPending: true,
+ authChangedSinceInitialize: false,
timeoutStartEpoch: 7,
currentEpoch: 7,
currentState: { mode: 'cloud', ownerBoundaryPending: false },
@@ -209,6 +210,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
const recover = vi.fn(async () => recoveredState);
await expect(resolveColdStartTimeoutRecovery({
ownerTransitionPending: false,
+ authChangedSinceInitialize: false,
timeoutStartEpoch: 7,
currentEpoch: 7,
currentState,
@@ -224,6 +226,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
await expect(resolveColdStartTimeoutRecovery({
ownerTransitionPending: false,
+ authChangedSinceInitialize: false,
timeoutStartEpoch: 7,
currentEpoch: 8,
currentState: loggedInState,
@@ -234,6 +237,28 @@ describe('ColdStartAuthFallbackRegistry', () => {
expect(recover).not.toHaveBeenCalled();
});
+ it('preserves login completed during async startup prereads before the sibling timeout', async () => {
+ const initializeStartEpoch = 7;
+ let authStateEpoch = initializeStartEpoch;
+ const loggedInState = { mode: 'cloud', userId: 'new-owner' } as const;
+ const recover = vi.fn(async () => ({ mode: 'signed-out' as const }));
+
+ // reconcileDesktopActiveAuthSession/loadClientEndpointsForRealm are awaited
+ // before the shared refresh starts; login may commit while either is pending.
+ authStateEpoch += 1;
+ await expect(resolveColdStartTimeoutRecovery({
+ ownerTransitionPending: false,
+ authChangedSinceInitialize: authStateEpoch !== initializeStartEpoch,
+ timeoutStartEpoch: initializeStartEpoch,
+ currentEpoch: authStateEpoch,
+ currentState: loggedInState,
+ pendingState: { mode: 'signed-out', ownerBoundaryPending: true },
+ recover,
+ })).resolves.toBe(loggedInState);
+
+ expect(recover).not.toHaveBeenCalled();
+ });
+
it('preserves a late successful refresh across consecutive renderer startup timeouts', async () => {
vi.useFakeTimers();
const registry = new ColdStartAuthFallbackRegistry();
@@ -248,15 +273,15 @@ describe('ColdStartAuthFallbackRegistry', () => {
const previousEpoch = currentEpoch;
currentEpoch += 1;
registry.record(previousEpoch, currentEpoch);
- return { isAuthenticated: false };
+ return Promise.resolve('signed-out');
};
// Two renderer windows share the same refresh, but each owns a startup timer.
const primary = awaitWithStartupTimeout(refresh, { timeoutMs: 1000, onTimeout: recover });
const secondary = awaitWithStartupTimeout(refresh, { timeoutMs: 1500, onTimeout: recover });
await vi.advanceTimersByTimeAsync(1500);
- await expect(primary).resolves.toEqual({ isAuthenticated: false });
- await expect(secondary).resolves.toEqual({ isAuthenticated: false });
+ await expect(primary).resolves.toBe('signed-out');
+ await expect(secondary).resolves.toBe('signed-out');
expect(currentEpoch).toBe(9);
resolveRefresh('owner');
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index ddb0fccd70..d9202f6b6a 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -4538,6 +4538,7 @@ export async function updateServerProfile(
}
export async function initialize(options: AuthInitializeOptions = {}): Promise<AuthState> {
+ const initializeStartEpoch = authStateEpoch;
// A renderer can mount after the boundary-pending broadcast. Keep startup
// fail-closed until the serialized owner transition settles instead of
// restoring the outgoing owner's credentials and turns from disk.
@@ -4714,7 +4715,7 @@ export async function initialize(options: AuthInitializeOptions = {}): Promise<A
// 黑洞 / captive-portal 网络护栏:限时等待,超时先以未登录返回解锁 splash,
// 流程继续后台跑;迟到成功由流程内部广播登录态(renderer 自动跳回主界面)。
const coldStartCompletion = coldStartAuthInFlight;
- const startupTimeoutEpoch = authStateEpoch;
+ const startupTimeoutEpoch = initializeStartEpoch;
return awaitWithStartupTimeout(coldStartCompletion, {
timeoutMs: COLD_START_AUTH_GATE_TIMEOUT_MS,
onTimeout: async () => {
@@ -4732,6 +4733,7 @@ export async function initialize(options: AuthInitializeOptions = {}): Promise<A
// the transition's own commit/rollback broadcast publish the settled state.
const recovery = await resolveColdStartTimeoutRecovery({
ownerTransitionPending: ownerChangeShellGate.isPending(),
+ authChangedSinceInitialize: authStateEpoch !== initializeStartEpoch,
timeoutStartEpoch: startupTimeoutEpoch,
currentEpoch: authStateEpoch,
currentState: snapshotAuthState(),
diff --git a/apps/desktop/src/main/authStartupGate.ts b/apps/desktop/src/main/authStartupGate.ts
index 0486db0ecd..cf57942b4d 100644
--- a/apps/desktop/src/main/authStartupGate.ts
+++ b/apps/desktop/src/main/authStartupGate.ts
@@ -79,6 +79,7 @@ export async function awaitWithStartupTimeout<T>(
export async function resolveColdStartTimeoutRecovery<TCurrent, TPending, TRecovered>(input: {
ownerTransitionPending: boolean;
+ authChangedSinceInitialize: boolean;
timeoutStartEpoch: number;
currentEpoch: number;
currentState: TCurrent;
@@ -93,7 +94,9 @@ export async function resolveColdStartTimeoutRecovery<TCurrent, TPending, TRecov
// Sibling renderers share one refresh but own independent startup timers. A
// user may complete login after one timeout exposes the login page and before
// another timer fires; that later timeout must preserve the authenticated state.
- if (input.currentEpoch !== input.timeoutStartEpoch) return input.currentState;
+ if (input.authChangedSinceInitialize || input.currentEpoch !== input.timeoutStartEpoch) {
+ return input.currentState;
+ }
return input.recover();
}