Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 收口 isolated env 推导、custom 共库保护与 mismatch 墓碑
Dashd907962fd8d1
diff --git a/apps/desktop/src/main/__tests__/authPassiveSharedInstance.test.ts b/apps/desktop/src/main/__tests__/authPassiveSharedInstance.test.tsindex 58367f5485..52b2bfb147 100644--- a/apps/desktop/src/main/__tests__/authPassiveSharedInstance.test.ts+++ b/apps/desktop/src/main/__tests__/authPassiveSharedInstance.test.ts@@ -58,6 +58,11 @@ describe('passive shared-userData instance auth isolation', () => {);const preserveIdx = authSource.indexOf('preservePersistedRefreshToken: true', runtimeIdx);expect(preserveIdx).toBeGreaterThan(runtimeIdx);+ expect(authSource).toContain('let foreignDeviceLocalSignOut = false');+ expect(authSource).toContain("foreignDeviceLocalSignOut = true");+ expect(authSource).toContain(+ "if (foreignDeviceLocalSignOut) {",+ );});it('clearAuth:passive 共享实例不删磁盘 refresh token', () => {diff --git a/apps/desktop/src/main/__tests__/devCliFlags.test.ts b/apps/desktop/src/main/__tests__/devCliFlags.test.tsindex ac7f687127..0b3c5f89af 100644--- a/apps/desktop/src/main/__tests__/devCliFlags.test.ts+++ b/apps/desktop/src/main/__tests__/devCliFlags.test.ts@@ -508,7 +508,7 @@ describe('resolveDevProfileKind / isIsolatedIdentityOnProductionProfile', () =>});describe('shouldEnforcePassiveMigrationCompatibility', () => {- it('只对共享正式 profile 的 passive dev 启用,沙箱 / custom / packaged 不启用', () => {+ it('对正式 profile 与非隔离 custom 的 passive 启用,沙箱 / packaged 不启用', () => {expect(shouldEnforcePassiveMigrationCompatibility({isPackaged: false,@@ -520,14 +520,14 @@ describe('shouldEnforcePassiveMigrationCompatibility', () => {shouldEnforcePassiveMigrationCompatibility({isPackaged: false,schedulerPassive: true,- profileKind: 'isolated-sandbox',+ profileKind: 'custom',}),- ).toBe(false);+ ).toBe(true);expect(shouldEnforcePassiveMigrationCompatibility({isPackaged: false,schedulerPassive: true,- profileKind: 'custom',+ profileKind: 'isolated-sandbox',}),).toBe(false);expect(diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.tsindex 709c8f4f44..bca29a6d6c 100644--- a/apps/desktop/src/main/authManager.ts+++ b/apps/desktop/src/main/authManager.ts@@ -366,9 +366,9 @@ function createAuthClient(* 发生过一次,当时只把静默半死改成明确弹重登(见 authSessionExpiredDetection.test.ts),* 没有堵住 passive 的销毁权。*- * packaged 恒不设置该 env(index.ts 启动时对 packaged / 非正式共享 profile 显式- * delete 兜底,防 ambient env 污染),线上零影响。env 由解析后的 profileKind ===- * production-shared 且 passive 落地,不看 isolated 旗标。+ * packaged 恒不设置该 env(index.ts 启动时对 packaged / isolated-sandbox 显式+ * delete 兜底,防 ambient env 污染),线上零影响。env 由解析后的 profileKind+ * 不是 isolated-sandbox 且 passive 落地,覆盖正式目录与非隔离 custom 共库。*/function isPassiveSharedUserDataInstance(): boolean {return !app.isPackaged && process.env.XDT_PASSIVE_SHARED_USER_DATA === '1';@@ -384,6 +384,12 @@ function isPassiveSharedUserDataInstance(): boolean {*/let passiveLocalSignOut = false;+/**+ * DEVICE_MISMATCH 后本进程已确认配不上磁盘 token。token 留给真正的设备,+ * 但 initialize() 不能反复拿同一枚去撞 401。直到显式登录或进程重启为止。+ */+let foreignDeviceLocalSignOut = false;+// ── safeStorage helpers ─────────────────────────────────────────────────────const SAFE_STORAGE_DIR = () => path.join(app.getPath('userData'), 'safe-storage');@@ -2317,6 +2323,11 @@ export async function initialize(options: AuthInitializeOptions = {}): Promise<AcommitVolatileAppSession('signed-out');return snapshotLoggedOutAuthState();}+ if (foreignDeviceLocalSignOut) {+ log.info('foreign-device instance stays signed out locally (tombstone)');+ commitVolatileAppSession('signed-out');+ return snapshotLoggedOutAuthState();+ }// release-relogin-on-update: if the auto-updater dropped a relogin marker// for *this* version, wipe persisted auth and force the user back to the@@ -2520,6 +2531,7 @@ async function runColdStartRefreshFlow(log.warn('cold-start refresh: DEVICE_MISMATCH — this process starts logged out and keeps the persisted refresh token',);+ foreignDeviceLocalSignOut = true;} else if (action.kind === 'replacement-retry') {log.warn(`cold-start refresh failed for a stale token after ${attempts} attempt(s) — keeping latest refresh token, starting logged out`,@@ -2745,8 +2757,9 @@ async function completeLogin(clearReloginFlag();}accountDeletionRestoredNoticePending = deletionWasRestored;- // 显式登录解除 passive 本地登出墓碑(见 passiveLocalSignOut)。+ // 显式登录解除本进程登出墓碑(passive / foreign-device)。passiveLocalSignOut = false;+ foreignDeviceLocalSignOut = false;currentUser = nextUser;commitCloudAppSession(currentUser.id);pendingAuthRealm = null;@@ -3241,6 +3254,7 @@ export async function refresh(): Promise<boolean> {log.warn('runtime refresh: DEVICE_MISMATCH — expiring this process and keeping the persisted refresh token',);+ foreignDeviceLocalSignOut = true;const previousUserId =currentUser?.id ?? getActiveAppSession().dataOwnerId ?? 'signed-out';await expireRuntimeAuth(previousUserId, 'device-mismatch', {diff --git a/apps/desktop/src/main/devCliFlags.ts b/apps/desktop/src/main/devCliFlags.tsindex 9a192587b6..74f74471cc 100644--- a/apps/desktop/src/main/devCliFlags.ts+++ b/apps/desktop/src/main/devCliFlags.ts@@ -243,15 +243,15 @@ export function resolveSingleInstanceLockUserDataDir(input: {* 不接受任何 dev-only passive 语义。调用方会把这个纯判定同步成内部 env,供延后加载* 的 localDb 模块在首次打开用户数据库时执行硬闸。*- * 判定看解析后的 profileKind,不看 isolated 旗标:旗标说 isolated、目录却是正式版时- * 仍按 production-shared 处理(启动器应先拒绝这种组合)。+ * 判定看解析后的真实 profile:正式目录与非隔离 custom 覆写(两进程共一个裸+ * XDT_USER_DATA_DIR)都受保护;只有真正的 isolated-sandbox 自己迁、自己删。*/export function shouldEnforcePassiveMigrationCompatibility(input: {isPackaged: boolean;schedulerPassive: boolean;profileKind: DevProfileKind;}): boolean {- return !input.isPackaged && input.schedulerPassive && input.profileKind === 'production-shared';+ return !input.isPackaged && input.schedulerPassive && input.profileKind !== 'isolated-sandbox';}export function resolveOfficialUserDataDirs(defaultUserDataDir: string): string[] {diff --git a/scripts/__tests__/restart-desktop-remote.test.mjs b/scripts/__tests__/restart-desktop-remote.test.mjsindex ab8785e94b..44fe7dceb9 100644--- a/scripts/__tests__/restart-desktop-remote.test.mjs+++ b/scripts/__tests__/restart-desktop-remote.test.mjs@@ -187,6 +187,21 @@ test("isolated restart target pointing at the other region's official profile isassert.equal(isOfficialProductionUserDataDir(globalFromCn), true);});+test("env-only XDT_ISOLATED=1 derives the default sandbox, not the official profile", () => {+ const target = resolveRestartTargetUserDataDir({+ isolatedEnv: "1",+ selectedRegion: "cn",+ });+ assert.equal(target, defaultIsolatedUserDataDir("", "cn"));+ assert.equal(isOfficialProductionUserDataDir(target), false);+ const named = resolveRestartTargetUserDataDir({+ isolatedEnv: "1",+ isolatedName: "review",+ selectedRegion: "global",+ });+ assert.equal(named, defaultIsolatedUserDataDir("review", "global"));+});+test("isolated official-profile refuse happens before mkdir in the restart main flow", () => {const source = fs.readFileSync(new URL("../restart-desktop-remote.mjs", import.meta.url), "utf8");const refuseIdx = source.indexOf("isOfficialProductionUserDataDir(targetUserDataDir)");diff --git a/scripts/restart-desktop-remote.mjs b/scripts/restart-desktop-remote.mjsindex 1624858629..1cba8a4791 100644--- a/scripts/restart-desktop-remote.mjs+++ b/scripts/restart-desktop-remote.mjs@@ -467,11 +467,17 @@ export function isOfficialProductionUserDataDir(dir) {export function resolveRestartTargetUserDataDir({envUserDataDir,isolatedArg,+ isolatedEnv,+ isolatedName,selectedRegion,}) {+ const isolationName = isolatedArg+ ? parseIsolationName(isolatedArg)+ : (typeof isolatedName === 'string' ? isolatedName.trim() : '');+ const isolated = Boolean(isolatedArg) || isolatedEnv === '1';return envUserDataDir- || (isolatedArg- ? defaultIsolatedUserDataDir(parseIsolationName(isolatedArg), selectedRegion)+ || (isolated+ ? defaultIsolatedUserDataDir(isolationName, selectedRegion): productionUserDataDir(selectedRegion));}@@ -914,6 +920,8 @@ async function main() {const targetUserDataDir = resolveRestartTargetUserDataDir({envUserDataDir: process.env.XDT_USER_DATA_DIR,isolatedArg,+ isolatedEnv: process.env.XDT_ISOLATED,+ isolatedName: process.env.XDT_ISOLATED_NAME,selectedRegion,});if (@@ -925,9 +933,15 @@ async function main() {'Omit XDT_USER_DATA_DIR, or point it at a sandbox directory.',);}- if (startupConfig && isolatedArg && process.env.XDT_USER_DATA_DIR) {+ if (startupConfig && hasIsolationIntent(argv, process.env)) {+ if (!process.env.XDT_USER_DATA_DIR) {+ process.env.XDT_USER_DATA_DIR = targetUserDataDir;+ process.env.XDT_USER_DATA_DIR_EPOCH = '1';+ }fs.mkdirSync(process.env.XDT_USER_DATA_DIR, { recursive: true });- const isolationName = parseIsolationName(isolatedArg);+ const isolationName = isolatedArg+ ? parseIsolationName(isolatedArg)+ : (process.env.XDT_ISOLATED_NAME || '');console.log(`==> Isolated dev user data${isolationName ? ` (sandbox "${isolationName}")` : ''}: ${process.env.XDT_USER_DATA_DIR}`);}if (!preserveRunning) {