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): wait for owner transition before login initialization (#4173)

Dante2eea72cd21ee
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
index 56434335ac..2e2917a8b9 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
@@ -829,6 +829,13 @@ describe('auth login-flow reset', () => {
const getLoginStart = source.indexOf('export async function getLoginState(');
const getLoginEnd = source.indexOf('\n}\n\nasync function completeLogin(', getLoginStart);
const getLoginBody = source.slice(getLoginStart, getLoginEnd);
+ const waitForOwnerChange = getLoginBody.indexOf('await ownerChangeShellGate.waitForSettled();');
+ const captureLoginEpoch = getLoginBody.indexOf(
+ 'const expectedLoginFlowEpoch = loginFlowEpoch;',
+ );
+ expect(waitForOwnerChange).toBeGreaterThan(-1);
+ expect(captureLoginEpoch).toBeGreaterThan(-1);
+ expect(waitForOwnerChange).toBeLessThan(captureLoginEpoch);
expect(getLoginBody).toContain('await loadLoginProviders(expectedLoginFlowEpoch)');
expect(getLoginBody).toContain('mapLoginProvidersLoadFailure(error)');
});
diff --git a/apps/desktop/src/main/__tests__/authOwnerChangeShellGate.test.ts b/apps/desktop/src/main/__tests__/authOwnerChangeShellGate.test.ts
new file mode 100644
index 0000000000..547ea831d1
--- /dev/null
+++ b/apps/desktop/src/main/__tests__/authOwnerChangeShellGate.test.ts
@@ -0,0 +1,54 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import { AuthOwnerChangeShellGate } from '../authOwnerChangeShellGate';
+
+describe('AuthOwnerChangeShellGate', () => {
+ it('releases login initialization only after a nested owner change settles', async () => {
+ const gate = new AuthOwnerChangeShellGate();
+ const settled = vi.fn();
+
+ gate.enter();
+ gate.enter();
+ const waiting = gate.waitForSettled();
+ void waiting.then(settled);
+ await Promise.resolve();
+
+ expect(gate.isPending()).toBe(true);
+ expect(settled).not.toHaveBeenCalled();
+
+ gate.leave();
+ await Promise.resolve();
+ expect(settled).not.toHaveBeenCalled();
+
+ gate.leave();
+ await waiting;
+ expect(gate.isPending()).toBe(false);
+ expect(settled).toHaveBeenCalledOnce();
+ });
+
+ it('waits for a replacement transition that starts as the first one releases', async () => {
+ const gate = new AuthOwnerChangeShellGate();
+ const settled = vi.fn();
+
+ gate.enter();
+ void gate.waitForSettled().then(settled);
+ gate.leave();
+ gate.enter();
+ await Promise.resolve();
+
+ expect(settled).not.toHaveBeenCalled();
+
+ gate.leave();
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(settled).toHaveBeenCalledOnce();
+ });
+
+ it('settles immediately when no owner change is active', async () => {
+ const gate = new AuthOwnerChangeShellGate();
+
+ await expect(gate.waitForSettled()).resolves.toBeUndefined();
+ gate.leave();
+ expect(gate.isPending()).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index 6a91741ddb..862ed5bf47 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -92,6 +92,7 @@ import {
import { resolveLoginScenarioFetch } from '@cindy/auth-client/fixtures';
import { createLogger } from './logger';
+import { AuthOwnerChangeShellGate } from './authOwnerChangeShellGate';
import {
isGhostSkillProjectionBoundaryStableForOwner,
withGhostSkillProjectionOwnerCommit,
@@ -375,18 +376,18 @@ let sessionInvalidationPromise: Promise<void> | null = null;
// Real owner change / logout: keep the renderer fail-closed even if a late
// notifyRenderer() races the teardown. Same-owner Ghost repair must not set
// this — that was the 55-minute /login flash.
-let ownerChangeShellPendingDepth = 0;
+const ownerChangeShellGate = new AuthOwnerChangeShellGate();
function enterOwnerChangeShellPending(): void {
- ownerChangeShellPendingDepth += 1;
+ ownerChangeShellGate.enter();
}
function leaveOwnerChangeShellPending(): void {
- ownerChangeShellPendingDepth = Math.max(0, ownerChangeShellPendingDepth - 1);
+ ownerChangeShellGate.leave();
}
function isOwnerChangeShellPending(): boolean {
- return ownerChangeShellPendingDepth > 0;
+ return ownerChangeShellGate.isPending();
}
/**
* 设备标识。默认绑定物理机(machineIdSync)。
@@ -4902,6 +4903,13 @@ async function discoverOrganizationRealm(org: string, expectedLoginFlowEpoch = l
}
export async function getLoginState(): Promise<DesktopLoginActionResult> {
+ // A logout publishes the signed-out shell before its owner transition has
+ // finished. Start provider discovery only after that transition settles so
+ // this request captures the post-logout login epoch instead of reporting a
+ // recoverable supersession as a terminal login-page error.
+ while (isOwnerChangeShellPending()) {
+ await ownerChangeShellGate.waitForSettled();
+ }
const expectedLoginFlowEpoch = loginFlowEpoch;
try {
if (loginFlowState) return { success: true, state: loginFlowState };
diff --git a/apps/desktop/src/main/authOwnerChangeShellGate.ts b/apps/desktop/src/main/authOwnerChangeShellGate.ts
new file mode 100644
index 0000000000..a8c8251393
--- /dev/null
+++ b/apps/desktop/src/main/authOwnerChangeShellGate.ts
@@ -0,0 +1,43 @@
+/**
+ * Coordinates login-page initialization with real auth owner transitions.
+ * Nested transitions share one settled signal so callers cannot capture an
+ * obsolete login epoch while logout or account replacement is still tearing
+ * down the previous owner.
+ */
+export class AuthOwnerChangeShellGate {
+ private depth = 0;
+ private pending: Promise<void> | null = null;
+ private resolvePending: (() => void) | null = null;
+
+ enter(): void {
+ if (this.depth === 0) {
+ this.pending = new Promise<void>((resolve) => {
+ this.resolvePending = resolve;
+ });
+ }
+ this.depth += 1;
+ }
+
+ leave(): void {
+ if (this.depth === 0) return;
+ this.depth -= 1;
+ if (this.depth !== 0) return;
+
+ const resolve = this.resolvePending;
+ this.pending = null;
+ this.resolvePending = null;
+ resolve?.();
+ }
+
+ isPending(): boolean {
+ return this.depth > 0;
+ }
+
+ async waitForSettled(): Promise<void> {
+ // A second transition can start while a previous wait resumes. Re-read the
+ // active promise until there is no owner-change shell left to observe.
+ while (this.pending) {
+ await this.pending;
+ }
+ }
+}