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): retain consecutive sibling timeout fallbacks

CHEYANdd59631c8b26
diff --git a/apps/desktop/src/main/__tests__/authStartupGate.test.ts b/apps/desktop/src/main/__tests__/authStartupGate.test.ts
index 0041624fca..1892598164 100644
--- a/apps/desktop/src/main/__tests__/authStartupGate.test.ts
+++ b/apps/desktop/src/main/__tests__/authStartupGate.test.ts
@@ -193,6 +193,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
const result = await resolveColdStartTimeoutRecovery({
ownerTransitionPending: true,
authChangedSinceInitialize: false,
+ isStartupFallbackEpoch: () => false,
timeoutStartEpoch: 7,
currentEpoch: 7,
currentState: { mode: 'cloud', ownerBoundaryPending: false },
@@ -211,6 +212,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
await expect(resolveColdStartTimeoutRecovery({
ownerTransitionPending: false,
authChangedSinceInitialize: false,
+ isStartupFallbackEpoch: () => false,
timeoutStartEpoch: 7,
currentEpoch: 7,
currentState,
@@ -227,6 +229,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
await expect(resolveColdStartTimeoutRecovery({
ownerTransitionPending: false,
authChangedSinceInitialize: false,
+ isStartupFallbackEpoch: () => false,
timeoutStartEpoch: 7,
currentEpoch: 8,
currentState: loggedInState,
@@ -249,6 +252,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
await expect(resolveColdStartTimeoutRecovery({
ownerTransitionPending: false,
authChangedSinceInitialize: authStateEpoch !== initializeStartEpoch,
+ isStartupFallbackEpoch: () => false,
timeoutStartEpoch: initializeStartEpoch,
currentEpoch: authStateEpoch,
currentState: loggedInState,
@@ -300,6 +304,49 @@ describe('ColdStartAuthFallbackRegistry', () => {
expect(committedUser).toBe('owner');
});
+ it('keeps every unobserved sibling timeout edge until the shared refresh commits', async () => {
+ const registry = new ColdStartAuthFallbackRegistry();
+ const flowEpoch = 7;
+ let currentEpoch = flowEpoch;
+ const recover = vi.fn(async () => {
+ const fromEpoch = currentEpoch;
+ currentEpoch += 1;
+ registry.record(fromEpoch, currentEpoch);
+ return { mode: 'signed-out' as const };
+ });
+ const loggedOut = { mode: 'signed-out' as const };
+
+ await expect(resolveColdStartTimeoutRecovery({
+ ownerTransitionPending: false,
+ authChangedSinceInitialize: false,
+ isStartupFallbackEpoch: (epoch) => registry.containsOnlyFallbacks(flowEpoch, epoch),
+ timeoutStartEpoch: flowEpoch,
+ currentEpoch,
+ currentState: loggedOut,
+ pendingState: { mode: 'signed-out', ownerBoundaryPending: true },
+ recover,
+ })).resolves.toEqual(loggedOut);
+ await expect(resolveColdStartTimeoutRecovery({
+ ownerTransitionPending: false,
+ authChangedSinceInitialize: false,
+ isStartupFallbackEpoch: (epoch) => registry.containsOnlyFallbacks(flowEpoch, epoch),
+ timeoutStartEpoch: flowEpoch,
+ currentEpoch,
+ currentState: loggedOut,
+ pendingState: { mode: 'signed-out', ownerBoundaryPending: true },
+ recover,
+ })).resolves.toEqual(loggedOut);
+
+ expect(currentEpoch).toBe(9);
+ expect(commitColdStartRefreshIfCurrent({
+ registry,
+ flowEpoch,
+ currentEpoch,
+ commit: () => 'owner',
+ })).toEqual({ superseded: false, flowEpoch: 9, value: 'owner' });
+ expect(recover).toHaveBeenCalledTimes(2);
+ });
+
it('does not commit a refresh after an explicit login interrupts the fallback chain', () => {
const registry = new ColdStartAuthFallbackRegistry();
registry.record(8, 9);
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index d9202f6b6a..ffd89c7f97 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -4734,6 +4734,9 @@ export async function initialize(options: AuthInitializeOptions = {}): Promise<A
const recovery = await resolveColdStartTimeoutRecovery({
ownerTransitionPending: ownerChangeShellGate.isPending(),
authChangedSinceInitialize: authStateEpoch !== initializeStartEpoch,
+ isStartupFallbackEpoch: (epoch) =>
+ epoch > initializeStartEpoch &&
+ coldStartTimeoutFallbacks.containsOnlyFallbacks(initializeStartEpoch, epoch),
timeoutStartEpoch: startupTimeoutEpoch,
currentEpoch: authStateEpoch,
currentState: snapshotAuthState(),
diff --git a/apps/desktop/src/main/authStartupGate.ts b/apps/desktop/src/main/authStartupGate.ts
index cf57942b4d..01ed1cd92a 100644
--- a/apps/desktop/src/main/authStartupGate.ts
+++ b/apps/desktop/src/main/authStartupGate.ts
@@ -80,6 +80,7 @@ export async function awaitWithStartupTimeout<T>(
export async function resolveColdStartTimeoutRecovery<TCurrent, TPending, TRecovered>(input: {
ownerTransitionPending: boolean;
authChangedSinceInitialize: boolean;
+ isStartupFallbackEpoch: (epoch: number) => boolean;
timeoutStartEpoch: number;
currentEpoch: number;
currentState: TCurrent;
@@ -95,7 +96,12 @@ export async function resolveColdStartTimeoutRecovery<TCurrent, TPending, TRecov
// 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.authChangedSinceInitialize || input.currentEpoch !== input.timeoutStartEpoch) {
- return input.currentState;
+ if (
+ input.currentEpoch === input.timeoutStartEpoch ||
+ !input.isStartupFallbackEpoch(input.currentEpoch)
+ ) {
+ return input.currentState;
+ }
}
return input.recover();
}