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): do not recover signed-out over a login that settled during sibling startup

CHEYANa84beda248cd
diff --git a/apps/desktop/src/main/__tests__/authStartupGate.test.ts b/apps/desktop/src/main/__tests__/authStartupGate.test.ts
index a8ba90771f..db2251b91c 100644
--- a/apps/desktop/src/main/__tests__/authStartupGate.test.ts
+++ b/apps/desktop/src/main/__tests__/authStartupGate.test.ts
@@ -194,6 +194,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
ownerTransitionPending: true,
authChangedSinceInitialize: false,
loginCommitPending: false,
+ loginCommittedSinceInitialize: false,
isStartupFallbackEpoch: () => false,
timeoutStartEpoch: 7,
currentEpoch: 7,
@@ -214,6 +215,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
ownerTransitionPending: false,
authChangedSinceInitialize: false,
loginCommitPending: false,
+ loginCommittedSinceInitialize: false,
isStartupFallbackEpoch: () => false,
timeoutStartEpoch: 7,
currentEpoch: 7,
@@ -231,6 +233,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
ownerTransitionPending: false,
authChangedSinceInitialize: false,
loginCommitPending: true,
+ loginCommittedSinceInitialize: false,
isStartupFallbackEpoch: () => false,
timeoutStartEpoch: 7,
currentEpoch: 8,
@@ -249,6 +252,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
ownerTransitionPending: false,
authChangedSinceInitialize: false,
loginCommitPending: false,
+ loginCommittedSinceInitialize: false,
isStartupFallbackEpoch: () => false,
timeoutStartEpoch: 7,
currentEpoch: 8,
@@ -273,6 +277,7 @@ describe('ColdStartAuthFallbackRegistry', () => {
ownerTransitionPending: false,
authChangedSinceInitialize: authStateEpoch !== initializeStartEpoch,
loginCommitPending: false,
+ loginCommittedSinceInitialize: false,
isStartupFallbackEpoch: () => false,
timeoutStartEpoch: initializeStartEpoch,
currentEpoch: authStateEpoch,
@@ -284,6 +289,30 @@ describe('ColdStartAuthFallbackRegistry', () => {
expect(recover).not.toHaveBeenCalled();
});
+ it('preserves a login whose commit settled after the sibling initialize entered', async () => {
+ // completeLogin increments authStateEpoch before awaiting the durable
+ // commit, so a sibling initialize entering inside that interval snapshots
+ // the in-progress epoch; when the commit finishes before the sibling timer
+ // fires, every epoch guard compares equal and recovery would clear the new
+ // login. Only the settled-login counter distinguishes this case.
+ const loggedInState = { mode: 'cloud', userId: 'new-owner' } as const;
+ const recover = vi.fn(async () => ({ mode: 'signed-out' as const }));
+ await expect(resolveColdStartTimeoutRecovery({
+ ownerTransitionPending: false,
+ authChangedSinceInitialize: false,
+ loginCommitPending: false,
+ loginCommittedSinceInitialize: true,
+ isStartupFallbackEpoch: () => false,
+ timeoutStartEpoch: 7,
+ currentEpoch: 7,
+ 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();
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index 03a4fd72b5..3c9eebd477 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -437,6 +437,12 @@ let loginFlowEpoch = 0;
// A depth map keeps the guard correct even if two same-epoch commits briefly
// overlap before authStateEpoch rejects the stale one.
const sealedLoginFlowCommitDepths = new Map<number, number>();
+// Monotonic count of accepted login commits that have fully settled (including
+// the post-commit notifications in completeLogin). A sibling initialize()
+// snapshots this at entry: a startup timer firing after the snapshot must not
+// run signed-out recovery over a login that landed while it was pending, even
+// though the login reuses the epoch the sibling snapshotted at its entry.
+let settledLoginCommitCount = 0;
// `accountDeletionRestored` may arrive before membership selection. Keep it
// main-only until the final resource-token login commits.
let pendingAccountDeletionRestored = false;
@@ -4539,6 +4545,7 @@ export async function updateServerProfile(
export async function initialize(options: AuthInitializeOptions = {}): Promise<AuthState> {
const initializeStartEpoch = authStateEpoch;
+ const settledLoginsAtInitialize = settledLoginCommitCount;
// 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.
@@ -4735,6 +4742,13 @@ export async function initialize(options: AuthInitializeOptions = {}): Promise<A
ownerTransitionPending: ownerChangeShellGate.isPending(),
loginCommitPending: sealedLoginFlowCommitDepths.size > 0,
authChangedSinceInitialize: authStateEpoch !== initializeStartEpoch,
+ // A login that committed after this initialize() entered reuses the
+ // epoch snapshotted at entry (completeLogin increments before awaiting
+ // the durable commit), so the epoch guards above cannot see it. Treat a
+ // settled login as an auth change that must never be cleared by the
+ // startup deadline; its own commit already published the settled state.
+ loginCommittedSinceInitialize:
+ settledLoginCommitCount !== settledLoginsAtInitialize,
isStartupFallbackEpoch: (epoch) =>
epoch > initializeStartEpoch &&
coldStartTimeoutFallbacks.containsOnlyFallbacks(initializeStartEpoch, epoch),
@@ -5305,6 +5319,7 @@ async function completeLogin(
loginFlowState = reduceAuthFlow(loginFlowState, { type: 'outcome', outcome });
notifyRenderer();
notifyAuthListeners();
+ settledLoginCommitCount += 1;
return loginFlowState;
} finally {
releaseLoginFlowCommit();
diff --git a/apps/desktop/src/main/authStartupGate.ts b/apps/desktop/src/main/authStartupGate.ts
index db08b19e30..7e4c53a0e4 100644
--- a/apps/desktop/src/main/authStartupGate.ts
+++ b/apps/desktop/src/main/authStartupGate.ts
@@ -81,6 +81,7 @@ export async function resolveColdStartTimeoutRecovery<TCurrent, TPending, TRecov
ownerTransitionPending: boolean;
authChangedSinceInitialize: boolean;
loginCommitPending: boolean;
+ loginCommittedSinceInitialize: boolean;
isStartupFallbackEpoch: (epoch: number) => boolean;
timeoutStartEpoch: number;
currentEpoch: number;
@@ -92,6 +93,10 @@ export async function resolveColdStartTimeoutRecovery<TCurrent, TPending, TRecov
// the auth epoch before its async vault/owner transaction commits. Neither may
// race a timeout recovery; their own commit/rollback publishes the settled state.
if (input.ownerTransitionPending || input.loginCommitPending) return input.pendingState;
+ // A login transaction that settled after this initialize() entered reuses the
+ // epoch snapshotted at entry, so it is invisible to the epoch guards below.
+ // Preserve the authenticated state it published instead of recovering.
+ if (input.loginCommittedSinceInitialize) return input.currentState;
// 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.