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(ghost): rename 重试预算拉到约 10s,耗尽时附用户提示,seed 路径统一走 renameManagedDir(#5028 review)

ficowang26aecab85b20
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index c0e20d1a7b..55ecee7221 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -227,8 +227,14 @@ export interface GhostManagerOptions {
/** Windows 目录句柄竞争的典型瞬时错误码;其它错误码原样上抛,不重试。 */
const TRANSIENT_RENAME_ERROR_CODES: ReadonlySet<string> = new Set(['EPERM', 'EBUSY', 'EACCES']);
-/** 有界退避:累计约 3s,覆盖 #5026 实测的 5–10s 内非单调句柄释放窗口的大部分场景。 */
-const DEFAULT_RENAME_RETRY_DELAYS_MS: readonly number[] = [100, 200, 400, 800, 1600];
+/**
+ * 有界退避:累计约 9.75s。#5026 的对照实验是「等 5s 失败、等 10s 成功」,且失败与包体量
+ * 强相关(100MB+ 稳定失败,安全软件扫描窗口随解包体量变长),预算需覆盖到 10s 量级。
+ * 等待发生在安装 loading 态、互斥锁内,仍保持有界、只认瞬时码;测试可通过 `renameRetry` 覆盖。
+ */
+export const DEFAULT_RENAME_RETRY_DELAYS_MS: readonly number[] = [250, 500, 1000, 2000, 3000, 3000];
+/** 重试耗尽后附在 io 拒绝原因上的用户提示,否则用户看到的仍是裸 `EPERM`。 */
+export const RENAME_RETRY_EXHAUSTED_HINT = '文件可能被安全软件或其它程序占用,请稍后重试';
/** install / update 的失败分类 —— IPC 层据此映射错误码。 */
export type InstallRejection =
@@ -565,7 +571,16 @@ export class GhostManager {
return;
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
- if (attempt >= delays.length || !code || !TRANSIENT_RENAME_ERROR_CODES.has(code)) throw error;
+ const transient = Boolean(code && TRANSIENT_RENAME_ERROR_CODES.has(code));
+ if (transient && delays.length > 0 && attempt >= delays.length) {
+ // 重试耗尽:保留 errno code 供调用点分类,只在 message 上附用户提示。
+ this.options.log?.warn('ghost transaction rename retry exhausted', {
+ id, stage, code, attempts: attempt + 1,
+ });
+ (error as Error).message = `${(error as Error).message};${RENAME_RETRY_EXHAUSTED_HINT}`;
+ throw error;
+ }
+ if (attempt >= delays.length || !transient) throw error;
this.options.log?.warn('ghost transaction rename hit transient error; retrying', {
id, stage, code, attempt: attempt + 1, delayMs: delays[attempt],
});
@@ -3293,7 +3308,7 @@ export class GhostManager {
if (finalKind === 'missing') {
await this.receiptStore.writePendingMutation(id, { kind: 'install', packageSha256 });
this.untrustedApprovals.add(this.isolationKey(id));
- await fs.promises.rename(stagingDir, finalDir);
+ await this.renameManagedDir(stagingDir, finalDir, 'seed install placement', id);
return;
}
@@ -3304,14 +3319,14 @@ export class GhostManager {
phase: 'prepared',
});
this.untrustedApprovals.add(this.isolationKey(id));
- await fs.promises.rename(finalDir, backupDir);
+ await this.renameManagedDir(finalDir, backupDir, 'seed update backup', id);
await this.receiptStore.writePendingMutation(id, {
kind: 'update',
packageSha256,
backupDirName: path.basename(backupDir),
phase: 'backed-up',
});
- await fs.promises.rename(stagingDir, finalDir);
+ await this.renameManagedDir(stagingDir, finalDir, 'seed update placement', id);
await this.receiptStore.writePendingMutation(id, {
kind: 'update',
packageSha256,
@@ -3330,7 +3345,7 @@ export class GhostManager {
});
if (backupKind === 'directory' && publishedKind === null) {
try {
- await fs.promises.rename(backupDir, finalDir);
+ await this.renameManagedDir(backupDir, finalDir, 'seed update rollback', id);
await this.receiptStore.clearPendingMutation(id);
this.untrustedApprovals.delete(this.isolationKey(id));
} catch {
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
index 872ca02959..d85532fc1f 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -13,7 +13,13 @@ import {
validateGhostManifest,
type InstalledGhost,
} from '../../../shared/ghost';
-import { CINDY_OFFICIAL_GHOST_TRUST, GhostManager, readLegacyGhostApprovalProjection } from '../GhostManager';
+import {
+ CINDY_OFFICIAL_GHOST_TRUST,
+ DEFAULT_RENAME_RETRY_DELAYS_MS,
+ GhostManager,
+ RENAME_RETRY_EXHAUSTED_HINT,
+ readLegacyGhostApprovalProjection,
+} from '../GhostManager';
import {
installedFileModeFromZip,
unixPermissionsForRepackedEntry,
@@ -1752,6 +1758,59 @@ describe('GhostManager · 安装事务目录 rename 失败(#5026)', () => {
expect(placementAttempts).toBe(3);
expect(fs.existsSync(pendingMarkerPath())).toBe(false);
});
+
+ // #5028 review:#5026 对照实验「等 5s 失败、等 10s 成功」且 100MB+ 包稳定失败,
+ // 默认预算必须覆盖到 10s 量级,而不是原先累计 3.1s。
+ it('默认重试预算累计约 10s、仍有界且逐步退避(#5028 review)', () => {
+ const total = DEFAULT_RENAME_RETRY_DELAYS_MS.reduce((sum, ms) => sum + ms, 0);
+ expect(total).toBeGreaterThanOrEqual(9_000);
+ expect(total).toBeLessThanOrEqual(11_000);
+ expect(DEFAULT_RENAME_RETRY_DELAYS_MS.length).toBeLessThanOrEqual(8);
+ for (let i = 1; i < DEFAULT_RENAME_RETRY_DELAYS_MS.length; i += 1) {
+ expect(DEFAULT_RENAME_RETRY_DELAYS_MS[i]).toBeGreaterThanOrEqual(DEFAULT_RENAME_RETRY_DELAYS_MS[i - 1]);
+ }
+ });
+
+ it('重试耗尽后的 io 拒绝附带用户可读提示,并保留原始 errno 信息', async () => {
+ const local = managerWithRenameRetry({ enabled: true, delaysMs: [1, 1] });
+ const finalDir = path.join(rootDir, 'hello');
+ const realRename = fs.promises.rename;
+ const spy = vi.spyOn(fs.promises, 'rename').mockImplementation(async (from, to) => {
+ if (path.resolve(String(to)) === path.resolve(finalDir)) {
+ throw Object.assign(new Error('EPERM: operation not permitted, rename'), { code: 'EPERM' });
+ }
+ return realRename(from as never, to as never);
+ });
+ let result: Awaited<ReturnType<GhostManager['install']>>;
+ try {
+ result = await local.install(await makeCindy('a.cindy', goodManifest()));
+ } finally {
+ spy.mockRestore();
+ }
+ expect('rejection' in result).toBe(true);
+ const rejection = (result as { rejection: { code: string; reason: string } }).rejection;
+ expect(rejection.code).toBe('io');
+ expect(rejection.reason).toContain('EPERM');
+ expect(rejection.reason).toContain(RENAME_RETRY_EXHAUSTED_HINT);
+
+ // 未启用重试(非 Windows 缺省)时 EPERM 通常是永久性权限问题,不附「稍后重试」提示。
+ const plain = managerWithRenameRetry({ enabled: false });
+ const spy2 = vi.spyOn(fs.promises, 'rename').mockImplementation(async (from, to) => {
+ if (path.resolve(String(to)) === path.resolve(finalDir)) {
+ throw Object.assign(new Error('EPERM: operation not permitted, rename'), { code: 'EPERM' });
+ }
+ return realRename(from as never, to as never);
+ });
+ let plainResult: Awaited<ReturnType<GhostManager['install']>>;
+ try {
+ plainResult = await plain.install(await makeCindy('b.cindy', goodManifest()));
+ } finally {
+ spy2.mockRestore();
+ }
+ const plainRejection = (plainResult as { rejection: { code: string; reason: string } }).rejection;
+ expect(plainRejection.code).toBe('io');
+ expect(plainRejection.reason).not.toContain(RENAME_RETRY_EXHAUSTED_HINT);
+ });
});
describe('GhostManager · 装入/更新崩溃窗口恢复(事务标记)', () => {