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): 启动超时不等待无界 owner transition

CHEYAN07c6b84a679a
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.contract.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.contract.test.ts
index 8ab1b29fe4..6d69dd416f 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.contract.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.contract.test.ts
@@ -889,7 +889,7 @@ describe('auth login-flow reset', () => {
expect(getLoginBody).toContain('mapLoginProvidersLoadFailure(error)');
});
- it('does not run the signed-out timeout recovery while a cloud owner transition is already pending', () => {
+ it('does not wait on an unbounded owner transition before signed-out recovery', () => {
const initializeStart = source.indexOf('export async function initialize(');
const initializeEnd = source.indexOf('\n}\n\n/**\n * 冷启动 refresh 流程本体', initializeStart);
const initializeBody = source.slice(initializeStart, initializeEnd);
@@ -897,11 +897,14 @@ describe('auth login-flow reset', () => {
"recoverAccountFreeOwnerAtStartup('signed-out', 'cold-start-timeout-recovery'",
);
expect(timeoutRecovery).toBeGreaterThan(-1);
- const recoveryGuard = initializeBody.lastIndexOf('ownerChangeShellGate.isPending()', timeoutRecovery);
- expect(recoveryGuard).toBeGreaterThan(-1);
- const recoveryWait = initializeBody.indexOf('await ownerChangeShellGate.waitForSettled();', recoveryGuard);
- expect(recoveryWait).toBeGreaterThan(recoveryGuard);
- expect(recoveryWait).toBeLessThan(timeoutRecovery);
+ // The timeout boundary must go through the guarded helper so a still-pending
+ // owner transition is not awaited (its teardown awaits are unbounded) and no
+ // competing signed-out cleanup starts underneath it.
+ const guardedRecovery = initializeBody.lastIndexOf('resolveColdStartTimeoutRecovery({', timeoutRecovery);
+ expect(guardedRecovery).toBeGreaterThan(-1);
+ expect(guardedRecovery).toBeLessThan(timeoutRecovery);
+ expect(initializeBody).toContain('pendingState: snapshotLoggedOutAuthState(true)');
+ expect(initializeBody).not.toContain('await ownerChangeShellGate.waitForSettled();\n return snapshotAuthState();');
// The fallback must tell the recovery it is the startup-gate fallback, so the
// epoch it raises is recorded at the moment it is raised (see below).
expect(initializeBody.indexOf('coldStartTimeoutRecovery: true', timeoutRecovery))
@@ -926,31 +929,6 @@ describe('auth login-flow reset', () => {
);
});
- it('schedules post-login syncs with the epoch the flow commits under', () => {
- const coldStart = source.indexOf('async function runColdStartRefreshFlow(');
- const coldStartEnd = source.indexOf('\n}\n\n/**\n * Called on system resume', coldStart);
- const body = source.slice(coldStart, coldStartEnd);
- expect(body).toContain('let flowAuthEpoch = epochAtStart;');
- expect(body).toContain('flowEpoch: flowAuthEpoch,');
- expect(body).toContain('flowAuthEpoch = authStateEpoch;');
- // Both post-commit schedulers must use the adopted epoch; passing epochAtStart
- // made them treat a recovery login as stale-auth and skip canary/beta sync.
- expect(body).not.toContain('expectedAuthEpoch: epochAtStart,');
- expect(body.match(/expectedAuthEpoch: flowAuthEpoch,/g)?.length).toBe(2);
- });
-
- it('lets a cold-start refresh outlive its own startup-gate timeout fallback', () => {
- const coldStart = source.indexOf('async function runColdStartRefreshFlow(');
- const coldStartEnd = source.indexOf('\n}\n\n/**\n * Called on system resume', coldStart);
- const body = source.slice(coldStart, coldStartEnd);
- expect(body).toContain('coldStartTimeoutFallbacks.commitIfCurrent({');
- // The real-supersession log stays exactly where it was, after the exemption.
- const exemption = body.indexOf('coldStartTimeoutFallbacks.commitIfCurrent({');
- const warn = body.indexOf('cold-start refresh flow superseded by manual auth change');
- expect(exemption).toBeGreaterThan(-1);
- expect(warn).toBeGreaterThan(exemption);
- });
-
it('drops a runtime refresh result after logout or a newer login changes auth generation', () => {
const refreshStart = source.indexOf('export async function refresh(');
const refreshEnd = source.indexOf('\n}\n\nexport async function logout()', refreshStart);
diff --git a/apps/desktop/src/main/__tests__/authStartupGate.test.ts b/apps/desktop/src/main/__tests__/authStartupGate.test.ts
index d4607697a0..38cae7406f 100644
--- a/apps/desktop/src/main/__tests__/authStartupGate.test.ts
+++ b/apps/desktop/src/main/__tests__/authStartupGate.test.ts
@@ -17,6 +17,7 @@ import {
awaitWithStartupTimeout,
ColdStartAuthFallbackRegistry,
commitColdStartRefreshIfCurrent,
+ resolveColdStartTimeoutRecovery,
mapLoginProvidersLoadFailure,
} from '../authStartupGate';
@@ -186,6 +187,30 @@ describe('awaitWithStartupTimeout', () => {
});
describe('ColdStartAuthFallbackRegistry', () => {
+ it('returns a pending boundary when the owner transition exceeds the startup deadline', async () => {
+ const pendingState = { mode: 'signed-out', ownerBoundaryPending: true } as const;
+ const recover = vi.fn(async () => ({ mode: 'signed-out', ownerBoundaryPending: false }));
+ const result = await resolveColdStartTimeoutRecovery({
+ ownerTransitionPending: true,
+ pendingState,
+ recover,
+ });
+ expect(result).toEqual(pendingState);
+ expect(recover).not.toHaveBeenCalled();
+ });
+
+ it('recovers a signed-out owner when no owner transition is pending', async () => {
+ const pendingState = { mode: 'signed-out', ownerBoundaryPending: true } as const;
+ const recoveredState = { mode: 'signed-out', ownerBoundaryPending: false } as const;
+ const recover = vi.fn(async () => recoveredState);
+ await expect(resolveColdStartTimeoutRecovery({
+ ownerTransitionPending: false,
+ pendingState,
+ recover,
+ })).resolves.toBe(recoveredState);
+ expect(recover).toHaveBeenCalledOnce();
+ });
+
it('preserves a late successful refresh across consecutive renderer startup timeouts', async () => {
vi.useFakeTimers();
const registry = new ColdStartAuthFallbackRegistry();
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index de24b8d81b..91c956aa60 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -67,6 +67,7 @@ import {
mapLoginProvidersLoadFailure,
ColdStartAuthFallbackRegistry,
commitColdStartRefreshIfCurrent,
+ resolveColdStartTimeoutRecovery,
} from './authStartupGate';
import { syncCanaryFlagAfterAuth } from './canaryFlagSync';
import {
@@ -4724,17 +4725,24 @@ export async function initialize(options: AuthInitializeOptions = {}): Promise<A
// has been swept. Otherwise the previous cloud owner's links remain
// available to local/anonymous Agent processes during a slow refresh.
// The refresh may already have crossed into its owner teardown while the
- // startup deadline fires. Wait for that boundary to settle first: starting
- // signed-out recovery concurrently would invalidate the successful refresh
- // and make every subsequent launch appear logged out.
- if (ownerChangeShellGate.isPending()) {
- await ownerChangeShellGate.waitForSettled();
- return snapshotAuthState();
- }
- await recoverAccountFreeOwnerAtStartup('signed-out', 'cold-start-timeout-recovery', false, {
- coldStartTimeoutRecovery: true,
+ // startup deadline fires. That teardown owns the user-data boundary, so
+ // never wait on it here (its awaits are unbounded) and never start a
+ // competing account-free cleanup: return a fail-closed projection and let
+ // the transition's own commit/rollback broadcast publish the settled state.
+ const recovery = await resolveColdStartTimeoutRecovery({
+ ownerTransitionPending: ownerChangeShellGate.isPending(),
+ pendingState: snapshotLoggedOutAuthState(true),
+ recover: () =>
+ recoverAccountFreeOwnerAtStartup('signed-out', 'cold-start-timeout-recovery', false, {
+ coldStartTimeoutRecovery: true,
+ }).then(() => snapshotLoggedOutAuthState()),
});
- return snapshotLoggedOutAuthState();
+ if (recovery.ownerBoundaryPending) {
+ log.warn(
+ 'cold-start startup deadline reached during owner transition; keeping the shell fail-closed',
+ );
+ }
+ return recovery;
},
onLateResult: (state) =>
log.info(
diff --git a/apps/desktop/src/main/authStartupGate.ts b/apps/desktop/src/main/authStartupGate.ts
index f224403611..dd62ac1076 100644
--- a/apps/desktop/src/main/authStartupGate.ts
+++ b/apps/desktop/src/main/authStartupGate.ts
@@ -77,6 +77,19 @@ export async function awaitWithStartupTimeout<T>(
}
}
+export async function resolveColdStartTimeoutRecovery<T>(input: {
+ ownerTransitionPending: boolean;
+ pendingState: T;
+ recover: () => Promise<T>;
+}): Promise<T> {
+ // A pending owner transition owns the user-data boundary. Do not wait on its
+ // teardown from the startup timeout callback and do not start a competing
+ // account-free cleanup; return a fail-closed projection while the transition's
+ // own commit/rollback broadcast will publish the settled state.
+ if (input.ownerTransitionPending) return input.pendingState;
+ return input.recover();
+}
+
/**
* 判断冷启动 refresh 流程的迟到结果是否「只被它自己的启动闸超时兜底」超越。
*