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(cindy-make): 修复 Windows 任务目录清理

Lizi1a27e67e9ef6
diff --git a/apps/desktop/scripts/test-cindy-make-cleanup-asar.mjs b/apps/desktop/scripts/test-cindy-make-cleanup-asar.mjs
new file mode 100644
index 0000000000..1508ca6098
--- /dev/null
+++ b/apps/desktop/scripts/test-cindy-make-cleanup-asar.mjs
@@ -0,0 +1,111 @@
+// Explicit Electron regression: node apps/desktop/scripts/test-cindy-make-cleanup-asar.mjs
+// Synthetic temp fixtures only; never opens Cindy or a user profile.
+import assert from 'node:assert/strict';
+import { execFile } from 'node:child_process';
+import { builtinModules, createRequire } from 'node:module';
+import fs from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { promisify } from 'node:util';
+import { createPackage } from '@electron/asar';
+import { build } from 'vite';
+
+const require = createRequire(import.meta.url);
+const desktop = fileURLToPath(new URL('..', import.meta.url));
+const root = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-make-cleanup-asar-'));
+try {
+ const contents = path.join(root, 'contents');
+ const archive = path.join(root, 'fixture.asar');
+ await fs.mkdir(contents);
+ await fs.writeFile(path.join(contents, 'inside.txt'), 'physical ASAR fixture');
+ await createPackage(contents, archive);
+ await fs.chmod(archive, 0o644);
+ const entry = path.join(root, 'entry.mjs');
+ const modulePath = (name) => JSON.stringify(path.join(desktop, 'src/main/cindy-make', name));
+ await fs.writeFile(
+ entry,
+ `
+export { manageCindyMakeWorkspace } from ${modulePath('taskCleanup.ts')};
+export { makeSourceCheckoutPath, makeTaskWorktreePath, makeTaskBranch } from ${modulePath('sourcePaths.ts')};
+`,
+ );
+ await build({
+ configFile: false,
+ logLevel: 'error',
+ build: {
+ outDir: path.join(root, 'bundle'),
+ minify: false,
+ lib: { entry, formats: ['cjs'], fileName: () => 'cleanup.cjs' },
+ rollupOptions: {
+ external: [
+ 'original-fs',
+ ...builtinModules,
+ ...builtinModules.map((name) => `node:${name}`),
+ ],
+ },
+ },
+ });
+ const probe = path.join(root, 'probe.cjs');
+ await fs.writeFile(
+ probe,
+ String.raw`
+const assert = require('node:assert/strict');
+const fs = require('original-fs').promises;
+const patchedFs = require('node:fs').promises;
+const path = require('node:path');
+const { app } = require('electron');
+const { manageCindyMakeWorkspace, makeSourceCheckoutPath, makeTaskWorktreePath, makeTaskBranch } = require('./bundle/cleanup.cjs');
+const root = ${JSON.stringify(root)};
+const archive = path.join(root, 'fixture.asar');
+(async () => {
+ // Prove this is Electron's ASAR-aware runtime, not the Node unit-test alias.
+ assert.equal((await patchedFs.lstat(archive)).isDirectory(), true);
+ assert.equal((await fs.lstat(archive)).isFile(), true);
+ for (const action of ['end', 'delete']) {
+ const profile = path.join(root, action);
+ const source = makeSourceCheckoutPath(profile);
+ const target = makeTaskWorktreePath(profile, 'run');
+ const branch = makeTaskBranch('run');
+ const resources = path.join(target, 'node_modules/electron/dist/resources');
+ await fs.mkdir(path.join(source, '.git'), { recursive: true });
+ await fs.mkdir(resources, { recursive: true });
+ await fs.copyFile(archive, path.join(resources, 'default_app.asar'));
+ let branchExists = true;
+ const git = async (_env, args) => {
+ if (args[0] === 'branch' && args[1] === '--list') return branchExists ? branch : '';
+ if (args[0] === 'worktree' && args[1] === 'list')
+ return 'worktree ' + source + '\0branch refs/heads/cindy-personal\0\0';
+ if (args[0] === 'update-ref') return '';
+ if (args[0] === 'branch' && args[1] === '-D') {
+ // Never drop the last branch before physical removal succeeds.
+ await assert.rejects(fs.access(target), { code: 'ENOENT' });
+ branchExists = false;
+ return '';
+ }
+ throw new Error('Unexpected Git operation: ' + args.join(' '));
+ };
+ assert.equal(await manageCindyMakeWorkspace(profile, 'run', action, {}, AbortSignal.timeout(5000), {
+ git, preparedWorkspace: { path: target, branch },
+ }), true);
+ await assert.rejects(fs.access(target), { code: 'ENOENT' });
+ assert.equal(branchExists, false);
+ }
+ process.stdout.write('PASS: end/delete remove physical ASAR residue before deleting branches\n');
+ app.exit(0);
+})().catch((error) => { console.error(error); app.exit(1); });
+`,
+ );
+ const env = { ...process.env };
+ delete env.ELECTRON_RUN_AS_NODE;
+ const result = await promisify(execFile)(require('electron'), [probe], {
+ env,
+ timeout: 30_000,
+ windowsHide: true,
+ });
+ assert.match(result.stdout, /PASS:/);
+ process.stdout.write(result.stdout);
+} finally {
+ // The child must exit first so even its ASAR diagnostic handle is released.
+ await fs.rm(root, { recursive: true, force: true });
+}
diff --git a/apps/desktop/src/main/cindy-make/__tests__/taskCleanup.test.ts b/apps/desktop/src/main/cindy-make/__tests__/taskCleanup.test.ts
index 77eb65da00..b82817fe2d 100644
--- a/apps/desktop/src/main/cindy-make/__tests__/taskCleanup.test.ts
+++ b/apps/desktop/src/main/cindy-make/__tests__/taskCleanup.test.ts
@@ -1,8 +1,14 @@
import path from 'node:path';
-import { rm } from 'node:fs/promises';
+import originalFs from 'original-fs';
import os from 'node:os';
import { beforeEach, describe, expect, it, vi } from 'vitest';
-const h = vi.hoisted(() => ({ paths: new Set<string>(), links: new Set<string>() }));
+const h = vi.hoisted(() => ({
+ paths: new Set<string>(),
+ links: new Set<string>(),
+ patchedRm: vi.fn(async () => {
+ throw new Error('Electron ASAR-aware removal must not be used');
+ }),
+}));
vi.mock('../sourceContent', () => ({
contentRef: async () => undefined,
taskContentRef: () => 'ref',
@@ -13,10 +19,18 @@ vi.mock('node:fs/promises', () => ({
return { isSymbolicLink: () => h.links.has(p), isDirectory: () => true };
}),
realpath: vi.fn(async (p: string) => p),
- rm: vi.fn(async (p: string) => {
- h.paths.delete(p);
- }),
+ rm: h.patchedRm,
+}));
+vi.mock('original-fs', () => ({
+ default: {
+ promises: {
+ rm: vi.fn(async (p: string) => {
+ h.paths.delete(p);
+ }),
+ },
+ },
}));
+const { rm } = originalFs.promises;
import { manageCindyMakeWorkspace } from '../taskCleanup.js';
import {
makeSourceCheckoutPath,
@@ -85,6 +99,7 @@ function fixture() {
}
beforeEach(() => {
vi.mocked(rm).mockClear();
+ h.patchedRm.mockClear();
h.links.clear();
h.paths = new Set([
profile,
@@ -191,6 +206,7 @@ describe('managed task cleanup', () => {
expect(rm).toHaveBeenCalledWith(target, expect.objectContaining({ recursive: true }));
expect(h.paths.has(target)).toBe(false);
expect(f.state.branchExists).toBe(false);
+ expect(h.patchedRm).not.toHaveBeenCalled();
});
it.each(['wrong-path', 'wrong-branch', 'git-marker', 'junction', 'no-branch'])(
'rejects unsafe residual recovery: %s',
diff --git a/apps/desktop/src/main/cindy-make/taskCleanup.ts b/apps/desktop/src/main/cindy-make/taskCleanup.ts
index 4b8d0c7837..63041e0828 100644
--- a/apps/desktop/src/main/cindy-make/taskCleanup.ts
+++ b/apps/desktop/src/main/cindy-make/taskCleanup.ts
@@ -1,4 +1,5 @@
-import { lstat, realpath, rm } from 'node:fs/promises';
+import { lstat, realpath } from 'node:fs/promises';
+import originalFs from 'original-fs';
import path from 'node:path';
import { contentRef, snapshotContent, taskContentRef } from './sourceContent.js';
import { runSourceGit } from './sourceGit.js';
@@ -191,7 +192,14 @@ export async function manageCindyMakeWorkspace(
check();
signal.throwIfAborted();
try {
- await rm(target, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
+ // Electron's patched fs opens nested ASARs (including default_app.asar),
+ // locking the files it is trying to remove on Windows. Delete physical files.
+ await originalFs.promises.rm(target, {
+ recursive: true,
+ force: true,
+ maxRetries: 3,
+ retryDelay: 100,
+ });
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (['EBUSY', 'EPERM', 'EACCES', 'ENOTEMPTY'].includes(code ?? ''))
diff --git a/docs/cindy-make-upstream.md b/docs/cindy-make-upstream.md
index b4938ae86a..501145e8ca 100644
--- a/docs/cindy-make-upstream.md
+++ b/docs/cindy-make-upstream.md
@@ -232,7 +232,9 @@ rebase 到新官方基线(禁止 update-refs 自动改动其他任务分支)
Windows 清理启用 Git 长路径支持;Git 移除登记后仍残留目录时,仅确认丢弃的操作允许续清理,
必须同时匹配持久准备记录中的任务、路径与分支,确认原分支仍在、目录没有 `.git`,
且该路径和分支都未登记在其它 worktree。删除前重新核验托管根与链接边界;目录删除成功
- 才删除分支。文件占用时在操作区保留原因和重试入口,执行期间按钮显示加载状态。
+ 才删除分支。残留目录使用 `original-fs` 删除真实文件,避免 Electron 把依赖中的
+ `default_app.asar` 当作归档打开后在 Windows 上锁住待删文件;真实 Electron 回归入口为
+ `node apps/desktop/scripts/test-cindy-make-cleanup-asar.mjs`。文件占用时在操作区保留原因和重试入口,执行期间按钮显示加载状态。
清理操作及失败原因由 Main manager 按任务和当前账号持有,通过同一全局快照推送;
退出设置不取消清理,重进设置或另开窗口继续显示「正在清理」,同一任务的重复请求共用
在途操作。失败后保留原因直到重试;删除前在原准备卡持久化待清理标记,应用退出中断的