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(desktop): 修复同账号冷启动远程连接恢复 (#2172)

Dashcba45371cb18
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
index a5ae27743a..54957173b6 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
@@ -1,6 +1,8 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
+
+import { shouldTeardownColdStartRuntime } from '../authColdStartBoundary';
/** Regression guard for login progress that is intentionally owned by Electron main. */
describe('auth login-flow reset', () => {
@@ -192,6 +194,42 @@ describe('auth login-flow reset', () => {
expect(refreshBody).toContain('preservePersistedRefreshToken: true');
});
+ it('does not call teardown for a fresh signed-out/null cold-start session', () => {
+ const teardown = vi.fn();
+ const previousAppSession = {
+ mode: 'signed-out' as const,
+ dataOwnerId: null,
+ generation: 0,
+ };
+
+ if (shouldTeardownColdStartRuntime(previousAppSession, 'account-1')) {
+ teardown();
+ }
+
+ expect(teardown).not.toHaveBeenCalled();
+ });
+
+ it('tears down only an already-committed runtime that crosses an owner boundary', () => {
+ expect(
+ shouldTeardownColdStartRuntime(
+ { mode: 'cloud', dataOwnerId: 'account-1', generation: 1 },
+ 'account-1',
+ ),
+ ).toBe(false);
+ expect(
+ shouldTeardownColdStartRuntime(
+ { mode: 'cloud', dataOwnerId: 'account-1', generation: 1 },
+ 'account-2',
+ ),
+ ).toBe(true);
+ expect(
+ shouldTeardownColdStartRuntime(
+ { mode: 'local', dataOwnerId: 'local-v1', generation: 1 },
+ 'account-1',
+ ),
+ ).toBe(true);
+ });
+
it('drops a runtime refresh result after logout or a newer login changes auth generation', () => {
const refreshStart = source.indexOf('export async function refresh(): Promise<boolean> {');
const refreshEnd = source.indexOf('\n}\n\nexport async function logout()', refreshStart);
diff --git a/apps/desktop/src/main/authColdStartBoundary.ts b/apps/desktop/src/main/authColdStartBoundary.ts
new file mode 100644
index 0000000000..48331257fe
--- /dev/null
+++ b/apps/desktop/src/main/authColdStartBoundary.ts
@@ -0,0 +1,20 @@
+import type { ActiveAppSession } from './appSessionState.js';
+
+/**
+ * Decide whether cold-start auth may need to tear down an existing owner runtime.
+ *
+ * `appSessionState` is process-local. A new process intentionally initializes a
+ * persisted `cloud` intent as `signed-out` with generation 0, before the
+ * refresh token proves the membership. There is no previous owner runtime in
+ * that process to tear down; the account-switch teardown hook is for a runtime
+ * that this process has already committed and may still own.
+ */
+export function shouldTeardownColdStartRuntime(
+ previousAppSession: Pick<ActiveAppSession, 'mode' | 'dataOwnerId' | 'generation'>,
+ nextOwnerId: string,
+): boolean {
+ if (previousAppSession.generation === 0) return false;
+ return (
+ previousAppSession.mode !== 'cloud' || previousAppSession.dataOwnerId !== nextOwnerId
+ );
+}
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index 41c6ad524a..9c832f2051 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -95,6 +95,7 @@ import {
getActiveDataOwnerPushStamp,
type AppSessionMode,
} from './appSessionState.js';
+import { shouldTeardownColdStartRuntime } from './authColdStartBoundary.js';
import {
claimLegacyOwnerNamespace,
recordLegacyGhostMigrationResult,
@@ -2219,14 +2220,25 @@ async function runColdStartRefreshFlow(
commitActiveAppSession('signed-out');
return snapshotLoggedOutAuthState();
}
- if (accountSwitchTeardown) {
+ const previousAppSession = getActiveAppSession();
+ const needsColdStartAccountBoundary = shouldTeardownColdStartRuntime(
+ previousAppSession,
+ refreshData.membership.id,
+ );
+ if (accountSwitchTeardown && needsColdStartAccountBoundary) {
// Cold-start refresh may outlive initialize()'s startup timeout. Keep
// the owner boundary held for the entire late commit sequence so stale
// IPC cannot reopen the previous owner's database while teardown,
// namespace claiming, and session publication are still in flight.
+ //
+ // A normal restart of the same cloud account is not an account switch.
+ // In that case the new process has no old in-memory owner runtime to
+ // tear down; calling this hook after localDb:ensure-ready would dispose
+ // the freshly-created DbClient and stop device-link, leaving the process
+ // permanently stuck at "DbClient not ready" until another restart.
releaseBoundary = beginAppSessionBoundary();
await accountSwitchTeardown({
- previousUserId: getActiveAppSession().dataOwnerId ?? 'signed-out',
+ previousUserId: previousAppSession.dataOwnerId ?? 'signed-out',
nextUserId: refreshData.membership.id,
});
if (epochChanged('after-cold-start-teardown')) {