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): recover desktop credential projection

xd-bobo1a2ba0ac2501
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
index 00e10a46d3..cd02e4cc5a 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
@@ -341,7 +341,9 @@ describe('auth login-flow reset', () => {
const initializeEnd = source.indexOf('\n}\n\n/**\n * 冷启动 refresh 流程本体', initializeStart);
const initializeBody = source.slice(initializeStart, initializeEnd);
const localGuard = initializeBody.indexOf("getActiveAppSession().mode === 'local'");
- const refreshTokenRead = initializeBody.indexOf('readPersistedAuthSession()');
+ const refreshTokenRead = initializeBody.indexOf(
+ 'await reconcileDesktopActiveAuthSession()',
+ );
expect(localGuard).toBeGreaterThan(-1);
expect(refreshTokenRead).toBeGreaterThan(localGuard);
@@ -350,6 +352,37 @@ describe('auth login-flow reset', () => {
);
});
+ it('repairs an interrupted active-session projection before cold-start refresh', () => {
+ const helperStart = source.indexOf(
+ 'async function reconcileDesktopActiveAuthSession()',
+ );
+ const helperEnd = source.indexOf(
+ '\n}\n\nfunction readPersistedRefreshToken',
+ helperStart,
+ );
+ const helperBody = source.slice(helperStart, helperEnd);
+ expect(helperBody).toContain('authAccountVaultLockPath()');
+ expect(helperBody).toContain("label: 'auth-account-vault-reconcile'");
+ expect(helperBody).toContain('vault.resources[vault.activeAccountKey]');
+ expect(helperBody).toContain('session.refreshToken !== activeResource.refreshToken');
+ expect(helperBody).toContain('writePersistedAuthSessionOrThrow(');
+
+ const initializeStart = source.indexOf('export async function initialize(');
+ const initializeEnd = source.indexOf(
+ '\n}\n\n/**\n * 冷启动 refresh 流程本体',
+ initializeStart,
+ );
+ const initializeBody = source.slice(initializeStart, initializeEnd);
+ const reconcileAt = initializeBody.indexOf(
+ 'await reconcileDesktopActiveAuthSession()',
+ );
+ const refreshAt = initializeBody.indexOf(
+ 'runColdStartRefreshFlow(storedToken, persistedSession.realm)',
+ );
+ expect(reconcileAt).toBeGreaterThan(-1);
+ expect(refreshAt).toBeGreaterThan(reconcileAt);
+ });
+
it('activates a restored realm only after the refreshed membership passes build policy', () => {
const initializeStart = source.indexOf('export async function initialize(');
const initializeEnd = source.indexOf('\n}\n\n/**\n * 冷启动 refresh 流程本体', initializeStart);
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index fa230d1737..4a4f815020 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -1303,6 +1303,43 @@ function readPersistedAuthSession() {
return parseAuthSessionRecord(readSafe(AUTH_SESSION_KEY));
}
+/**
+ * The aggregate vault is the crash-consistent owner record. Repair its
+ * compatibility session projection under the same cross-process lock before
+ * cold start can send a refresh request with a consumed token generation.
+ */
+async function reconcileDesktopActiveAuthSession(): Promise<
+ ReturnType<typeof readPersistedAuthSession>
+> {
+ return withCrossProcessLock(
+ authAccountVaultLockPath(),
+ { label: 'auth-account-vault-reconcile', waitMs: 5_000 },
+ async (status) => {
+ if (!status.held) throw accountVaultLockError(status.reason);
+ const vault = readAuthAccountVault();
+ const session = readPersistedAuthSession();
+ const activeResource = vault.activeAccountKey
+ ? vault.resources[vault.activeAccountKey]
+ : undefined;
+ if (!activeResource) return session;
+ if (
+ session?.realm !== activeResource.realm ||
+ session.refreshToken !== activeResource.refreshToken
+ ) {
+ writePersistedAuthSessionOrThrow(
+ activeResource.refreshToken,
+ activeResource.realm,
+ );
+ }
+ return {
+ version: 1,
+ realm: activeResource.realm,
+ refreshToken: activeResource.refreshToken,
+ };
+ },
+ );
+}
+
function readPersistedRefreshToken(realm = activeAuthRealm): string | null {
const session = readPersistedAuthSession();
return session?.realm === realm ? session.refreshToken : null;
@@ -3622,7 +3659,16 @@ export async function initialize(options: AuthInitializeOptions = {}): Promise<A
removeSafe(LEGACY_REFRESH_TOKEN_KEY);
removeSafe(LEGACY_ACCOUNT_REFRESH_TOKEN_KEY);
}
- let persistedSession = readPersistedAuthSession();
+ let persistedSession: ReturnType<typeof readPersistedAuthSession>;
+ try {
+ persistedSession = await reconcileDesktopActiveAuthSession();
+ } catch (error) {
+ log.warn(
+ 'cold-start active credential reconciliation failed; preserving credentials for retry',
+ error,
+ );
+ return finishColdStartSignedOut('cold-start-credential-reconcile-unavailable');
+ }
if (!persistedSession) {
// 旧版只保存裸 refresh token,没有 realm 可供校验。只有独占 userData 的
// primary 才能按当前构建区域迁移它;passive 若猜 AUTH_REGION,恰好会在旧