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): 非法隔离名回落默认沙箱,DEVICE_MISMATCH 不再瞬时重试

Dashc303e94b7417
diff --git a/apps/desktop/src/main/__tests__/authRefreshFailure.test.ts b/apps/desktop/src/main/__tests__/authRefreshFailure.test.ts
index f20ff9b12b..8f54a616be 100644
--- a/apps/desktop/src/main/__tests__/authRefreshFailure.test.ts
+++ b/apps/desktop/src/main/__tests__/authRefreshFailure.test.ts
@@ -406,6 +406,11 @@ describe('runRefreshWithTransientRetry', () => {
status: 401,
data: { error: { code: 'INVALID_REFRESH_TOKEN' } },
};
+ const deviceMismatch: RefreshFetchResult<unknown> = {
+ ok: false,
+ status: 401,
+ data: { error: { code: 'DEVICE_MISMATCH' } },
+ };
/** 立即 resolve 的注入 sleep,记录每次退避时长。 */
const makeSleep = () => {
const calls: number[] = [];
@@ -461,6 +466,29 @@ describe('runRefreshWithTransientRetry', () => {
]);
});
+ it('DEVICE_MISMATCH 立即返回,不按瞬时失败重试', async () => {
+ const doRefresh = vi.fn().mockResolvedValue(deviceMismatch);
+ const { calls, sleep } = makeSleep();
+ const failures: RefreshFailureInfo[] = [];
+ const { result, attempts } = await runRefreshWithTransientRetry(doRefresh, {
+ sleep,
+ onFailure: (info) => failures.push(info),
+ });
+ expect(result).toBe(deviceMismatch);
+ expect(attempts).toBe(1);
+ expect(doRefresh).toHaveBeenCalledTimes(1);
+ expect(calls).toEqual([]);
+ expect(failures).toEqual([
+ {
+ attempt: 1,
+ status: 401,
+ code: 'DEVICE_MISMATCH',
+ definitive: false,
+ willRetry: false,
+ },
+ ]);
+ });
+
it('持续瞬时失败(断网)→ 耗尽重试次数后返回最后一次结果', async () => {
const doRefresh = vi.fn().mockResolvedValue(offline);
const { calls, sleep } = makeSleep();
diff --git a/apps/desktop/src/main/authRefreshFailure.ts b/apps/desktop/src/main/authRefreshFailure.ts
index bde99997fd..4713d360be 100644
--- a/apps/desktop/src/main/authRefreshFailure.ts
+++ b/apps/desktop/src/main/authRefreshFailure.ts
@@ -245,15 +245,18 @@ export async function runRefreshWithTransientRetry<T>(
for (const delayMs of retryDelaysMs) {
if (result.ok) break;
const definitive = isDefinitiveRefreshFailure(result);
+ const foreignDevice = isForeignDeviceRefreshFailure(result);
const skipRateLimited = result.status === 429 && rateLimitDelayMs <= 0;
opts?.onFailure?.({
attempt: attempts,
status: result.status,
code: (result.data as RefreshErrorBody | null)?.error?.code,
definitive,
- willRetry: !definitive && !skipRateLimited,
+ willRetry: !definitive && !foreignDevice && !skipRateLimited,
});
- if (definitive) return { result, attempts };
+ // DEVICE_MISMATCH 不得当瞬时失败重试:会拖 1/2 秒,且后续 429/断网会把
+ // 终态盖成 transient-failure,冷启动就立不上 foreign-device 墓碑。
+ if (definitive || foreignDevice) return { result, attempts };
if (skipRateLimited) return { result, attempts };
const effectiveDelay = result.status === 429 ? rateLimitDelayMs : delayMs;
await sleep(effectiveDelay);
diff --git a/scripts/__tests__/restart-desktop-remote.test.mjs b/scripts/__tests__/restart-desktop-remote.test.mjs
index 44fe7dceb9..e59eae879b 100644
--- a/scripts/__tests__/restart-desktop-remote.test.mjs
+++ b/scripts/__tests__/restart-desktop-remote.test.mjs
@@ -16,6 +16,7 @@ import {
isRepositoryDesktopDevProcess,
officialProductionUserDataDirs,
resolveRestartTargetUserDataDir,
+ sanitizeIsolationName,
formatDesktopStartupFailure,
inspectSharedUserDataRegion,
partitionDesktopDevProcesses,
@@ -202,6 +203,18 @@ test("env-only XDT_ISOLATED=1 derives the default sandbox, not the official prof
assert.equal(named, defaultIsolatedUserDataDir("review", "global"));
});
+test("invalid env isolation name falls back to the default sandbox", () => {
+ assert.equal(sanitizeIsolationName("../Cindy"), "");
+ assert.equal(sanitizeIsolationName("我的沙箱"), "");
+ const target = resolveRestartTargetUserDataDir({
+ isolatedEnv: "1",
+ isolatedName: "../Cindy",
+ selectedRegion: "cn",
+ });
+ assert.equal(target, defaultIsolatedUserDataDir("", "cn"));
+ assert.equal(isOfficialProductionUserDataDir(target), false);
+});
+
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.mjs
index 1cba8a4791..ec0108f8eb 100644
--- a/scripts/restart-desktop-remote.mjs
+++ b/scripts/restart-desktop-remote.mjs
@@ -459,9 +459,28 @@ export function officialProductionUserDataDirs() {
return DESKTOP_DEV_REGIONS.map((region) => productionUserDataDir(region));
}
-export function isOfficialProductionUserDataDir(dir) {
+/** 与 devCliFlags ISOLATION_NAME_RE 一致:非法名字回落默认沙箱,不把路径段写进目录。 */
+export const ISOLATION_NAME_RE = /^[A-Za-z0-9_-]{1,32}$/;
+
+export function sanitizeIsolationName(raw) {
+ const name = typeof raw === 'string' ? raw.trim() : '';
+ return ISOLATION_NAME_RE.test(name) ? name : '';
+}
+
+export function canonicalizeUserDataDir(dir) {
const resolved = path.resolve(dir);
- return officialProductionUserDataDirs().some((official) => path.resolve(official) === resolved);
+ try {
+ return fs.realpathSync.native(resolved);
+ } catch {
+ return resolved;
+ }
+}
+
+export function isOfficialProductionUserDataDir(dir) {
+ const resolved = canonicalizeUserDataDir(dir);
+ return officialProductionUserDataDirs().some(
+ (official) => canonicalizeUserDataDir(official) === resolved,
+ );
}
export function resolveRestartTargetUserDataDir({
@@ -471,9 +490,9 @@ export function resolveRestartTargetUserDataDir({
isolatedName,
selectedRegion,
}) {
- const isolationName = isolatedArg
- ? parseIsolationName(isolatedArg)
- : (typeof isolatedName === 'string' ? isolatedName.trim() : '');
+ const isolationName = sanitizeIsolationName(
+ isolatedArg ? parseIsolationName(isolatedArg) : isolatedName,
+ );
const isolated = Boolean(isolatedArg) || isolatedEnv === '1';
return envUserDataDir
|| (isolated