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 重试尝试(#5028 review)

ficowang422b655dd764
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index 55ecee7221..20e8f684db 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -555,12 +555,24 @@ export class GhostManager {
* 事务目录 rename(staging→final / final→backup / backup→final)。瞬时错误按
* `options.renameRetry` 有界重试;重试耗尽或非瞬时错误照常抛出,由各调用点的
* 既有回滚/journal 逻辑处理。不做任何 rm/复制,失败时源目录保持原样。
+ *
+ * `beforeAttempt` 在**每次**尚未提交的 rename 尝试之前同步执行(含首次):新装发布的
+ * 取消/有效性检查必须覆盖退避等待窗口,否则「等待重试期间取消」仍会在下一次尝试
+ * 把包发布出去并写成功 receipt。它抛出即视为非瞬时错误、立即上抛,不再重试;一旦
+ * rename 成功就不再调用,已提交的发布不会被误报成取消失败。回滚路径不传此钩子。
*/
- private async renameManagedDir(from: string, to: string, stage: string, id: string): Promise<void> {
+ private async renameManagedDir(
+ from: string,
+ to: string,
+ stage: string,
+ id: string,
+ beforeAttempt?: () => void,
+ ): Promise<void> {
const retry = this.options.renameRetry;
const enabled = retry?.enabled ?? process.platform === 'win32';
const delays = enabled ? (retry?.delaysMs ?? DEFAULT_RENAME_RETRY_DELAYS_MS) : [];
for (let attempt = 0; ; attempt += 1) {
+ beforeAttempt?.();
try {
await fs.promises.rename(from, to);
if (attempt > 0) {
@@ -2654,7 +2666,10 @@ export class GhostManager {
expectedPackageSha256?: string;
trustOverride?: GhostHostTrustOverride;
installOrigin?: 'agent-forge';
- /** Synchronous live-authority check immediately before publishing the staged package. */
+ /**
+ * Synchronous live-authority / cancellation check run immediately before **every**
+ * not-yet-committed placement attempt (first try and each transient-error retry).
+ */
beforePackagePlacement?: () => void;
},
) {
@@ -2759,22 +2774,22 @@ export class GhostManager {
});
this.untrustedApprovals.add(this.isolationKey(manifest.id));
try {
- opts?.beforePackagePlacement?.();
- } catch (error) {
- // No package bytes were published. Clear the prepared journal so a
- // cancelled request cannot leave an installation waiting for recovery.
- await this.receiptStore.clearPendingMutation(manifest.id);
- this.untrustedApprovals.delete(this.isolationKey(manifest.id));
- throw error;
- }
- try {
- await this.renameManagedDir(stagingDir, finalDir, 'install placement', manifest.id);
+ // 取消/有效性检查(beforePackagePlacement)接在每次尚未提交的发布尝试之前:
+ // 首次尝试前与每次瞬时错误退避之后都会重查。等待重试期间取消 → 这里抛出,
+ // 下面按「没有发布任何字节」清理,不写成功 receipt,插件可重新安装。
+ await this.renameManagedDir(
+ stagingDir,
+ finalDir,
+ 'install placement',
+ manifest.id,
+ opts?.beforePackagePlacement,
+ );
} catch (error) {
- // rename 没成功就没有发布任何字节(#5026):此时若留下 install journal 与内存
- // 隔离标记,之后每次审批检查都会判 invalid、插件停在停用态,且用户手工放入
- // 目录也没有正常入口恢复。只有 lstat 明确 ENOENT 才能清;目录已出现(rename
- // 半途成功)或 lstat 本身报权限/IO 错误时都保留 journal 交给启动恢复——journal
- // 正是用来阻止无 receipt 的目录被迁移当作存量批准,不能凭"看不见"就清。
+ // rename 没成功(或发布前检查拒绝)就没有发布任何字节(#5026):此时若留下 install
+ // journal 与内存隔离标记,之后每次审批检查都会判 invalid、插件停在停用态,且用户
+ // 手工放入目录也没有正常入口恢复。只有 lstat 明确 ENOENT 才能清;目录已出现
+ // (rename 半途成功)或 lstat 本身报权限/IO 错误时都保留 journal 交给启动恢复——
+ // journal 正是用来阻止无 receipt 的目录被迁移当作存量批准,不能凭"看不见"就清。
const finalDirAbsent = await fs.promises
.lstat(finalDir)
.then(() => false)
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 d85532fc1f..815ad4b078 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -1622,6 +1622,53 @@ describe('GhostManager · 安装事务目录 rename 失败(#5026)', () => {
expect(fs.existsSync(pendingMarkerPath())).toBe(false);
});
+ it('新装 rename 退避等待期间取消:不再发布、不写成功 receipt,清理后可重新安装(#5028 review)', async () => {
+ const local = managerWithRenameRetry({ enabled: true, delaysMs: [20, 20, 20] });
+ const finalDir = path.join(rootDir, 'hello');
+ const receiptPath = path.join(workDir, 'ghosts-install-state', 'hello.json');
+ const realRename = fs.promises.rename;
+ const controller = new AbortController();
+ let placementAttempts = 0;
+ let guardChecks = 0;
+ const spy = vi.spyOn(fs.promises, 'rename').mockImplementation(async (from, to) => {
+ if (path.resolve(String(to)) === path.resolve(finalDir)) {
+ placementAttempts += 1;
+ if (placementAttempts === 1) {
+ // 第一次 rename 吃到瞬时 EPERM;请求在随后的退避等待期间被取消。
+ setTimeout(() => controller.abort(), 5);
+ throw Object.assign(new Error('EPERM: operation not permitted, rename'), { code: 'EPERM' });
+ }
+ }
+ return realRename(from as never, to as never);
+ });
+ const guard = vi.fn(() => {
+ guardChecks += 1;
+ expect(fs.existsSync(finalDir)).toBe(false);
+ controller.signal.throwIfAborted();
+ });
+ try {
+ await expectRejection(await local.install(await makeCindy('a.cindy', goodManifest()), {
+ beforePackagePlacement: guard,
+ }), 'io');
+ } finally {
+ spy.mockRestore();
+ }
+ // 取消发生在成功 rename 之前:检查必须在重试前再跑一次并拦下,第二次 rename 不能发生。
+ expect(guardChecks).toBe(2);
+ expect(placementAttempts).toBe(1);
+ expect(fs.existsSync(finalDir)).toBe(false);
+ expect(fs.existsSync(receiptPath)).toBe(false);
+ expect(fs.existsSync(pendingMarkerPath())).toBe(false);
+ expect(local.list()).toEqual([]);
+ expect(fs.readdirSync(rootDir).filter((name) => name.startsWith('.cindy-installing-'))).toEqual([]);
+
+ // 清理完整:同一插件可重新安装并获得批准。
+ const retried = await local.install(await makeCindy('b.cindy', goodManifest()));
+ expect('rejection' in retried).toBe(false);
+ expect(local.list()[0]).toMatchObject({ enabled: true, approval: { state: 'approved' } });
+ expect(fs.existsSync(receiptPath)).toBe(true);
+ });
+
it('新装 rename 失败后目录状态查不清(lstat 非 ENOENT)时保留 journal,不误清隔离标记', async () => {
const local = managerWithRenameRetry({ enabled: false });
const finalDir = path.join(rootDir, 'hello');