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): 卸载先撤批准再删目录 + 事务标记,消除"孤立 approved receipt"借尸还魂(P0/P1)

Yuewei-LLcf67a761c263
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index cda9719f43..c504cd411e 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -207,7 +207,14 @@ export class GhostManager {
continue;
}
const approval = this.receiptStore.read(id);
- if (marker.kind === 'install') {
+ if (marker.kind === 'uninstall') {
+ // 卸载崩在半途:把批准与内容目录都删净(幂等,顺序无关)。撤批准已在卸载时
+ // 先行,这里兜住"receipt 或目录仍残留"的任意组合 —— 收尾后该 id 干净消失。
+ this.receiptStore.removeSync(id);
+ if (this.isRealDirChild(root, id)) {
+ fs.rmSync(finalDir, { recursive: true, force: true });
+ }
+ } else if (marker.kind === 'install') {
// 未提交装入(无已批准 receipt)= 不完整安装,删 finalDir;有已批准 receipt = 完整
// 安装,保留(绝不删有有效批准的插件)。只删真目录,junction/链接跳过(避免穿透)。
if (approval.state !== 'approved' && this.isRealDirChild(root, id)) {
@@ -2116,15 +2123,23 @@ export class GhostManager {
if (!(await pathExists(dir))) {
return { rejection: { code: 'not-installed', reason: `意识 ${id} 未装入` } };
}
+ // 顺序是安全要点:先撤批准(receipt + 快照 + 隔离),再删内容目录。反过来(旧写法)
+ // 若崩在两步之间,会留下"孤立 approved receipt + 目录暂缺";之后同 id 路径被恢复/
+ // 同权限进程新建目录时,list() 会拿这份陈旧 receipt 授权那个目录(manifest/slots/
+ // trust 全来自 receipt),等于卸载过的插件被"借尸还魂"。事务标记让崩溃后的启动恢复
+ // 把这次卸载收尾干净。
+ await this.receiptStore.writePendingMutation(id, { kind: 'uninstall' });
+ // 走同一个撤销入口:成功即清掉隔离记录,失败由该入口转进程内隔离并记日志。撤批准
+ // 在删目录之前 —— 即便随后删目录失败/崩溃,也不会留下"目录在 + 旧 receipt 授权"。
+ await this.removeInstallApproval(id);
try {
await fs.promises.rm(dir, { recursive: true, force: true });
} catch (err) {
+ // 批准已撤、标记还在:插件此刻已 fail closed(list 无 receipt → legacy-unapproved),
+ // 下次启动恢复据标记把残留目录删净。如实报 io,不假装卸载完成。
return { rejection: { code: 'io', reason: err instanceof Error ? err.message : String(err) } };
}
- // 走同一个撤销入口:成功即清掉隔离记录,失败由该入口转进程内隔离并记日志。
- // 内容目录已经删除,插件不可能再运行;孤立 receipt 与 skill snapshot 仅是待回收
- // 状态,不能把“清理延后”误报成“插件仍已安装”。
- await this.removeInstallApproval(id);
+ await this.receiptStore.clearPendingMutation(id).catch(() => undefined);
this.options.log?.info('ghost uninstalled', { id });
if (options.notify !== false) this.options.onChanged?.(this.list());
return { ok: true };
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 de6c8ed71a..5ec6b297a6 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -978,6 +978,60 @@ describe('GhostManager · 装入/更新崩溃窗口恢复(事务标记)', () =>
expect(manager.list()[0].approval.state).toBe('approved');
});
+ it('卸载先撤批准再删目录:删目录失败时不留"孤立 approved receipt + 目录在"(防借尸还魂)', async () => {
+ await manager.install(await makeCindy('a.cindy', goodManifest()));
+ expect(manager.list()[0].approval.state).toBe('approved');
+ // 让内容目录的 rm 失败(模拟句柄占用),但放行 receipt/快照的 rm。
+ const realRm = fs.promises.rm;
+ const rmSpy = vi.spyOn(fs.promises, 'rm').mockImplementation((async (p: fs.PathLike, ...rest: unknown[]) => {
+ if (String(p) === path.join(rootDir, 'hello')) {
+ rmSpy.mockRestore();
+ const err = new Error('EBUSY: resource busy') as NodeJS.ErrnoException;
+ err.code = 'EBUSY';
+ throw err;
+ }
+ return (realRm as (...a: unknown[]) => Promise<void>)(p, ...rest);
+ }) as typeof fs.promises.rm);
+
+ const res = await manager.uninstall('hello');
+ await expectRejection(res, 'io');
+ // 关键:撤批准在删目录之前 —— receipt 已没了,目录还在但 fail closed(list 报
+ // legacy-unapproved),不会被这份 receipt 授权。孤立标记留给启动恢复收尾。
+ expect(fs.existsSync(receiptPath())).toBe(false);
+ expect(manager.list()[0].approval.state).toBe('legacy-unapproved');
+ expect(fs.existsSync(pendingMarkerPath())).toBe(true);
+ });
+
+ it('卸载崩在撤批准之后、删目录之前:恢复据 uninstall 标记删净残留目录', async () => {
+ await manager.install(await makeCindy('a.cindy', goodManifest()));
+ // 模拟崩溃现场:receipt 已撤(revoke 先行),目录还在,uninstall 标记在。
+ await fs.promises.rm(receiptPath(), { force: true });
+ await fs.promises.writeFile(
+ pendingMarkerPath(),
+ JSON.stringify({ version: 1, id: 'hello', kind: 'uninstall' }),
+ );
+ expect(fs.existsSync(path.join(rootDir, 'hello'))).toBe(true);
+
+ freshManager(); // 构造期恢复
+ expect(fs.existsSync(path.join(rootDir, 'hello'))).toBe(false);
+ expect(fs.existsSync(pendingMarkerPath())).toBe(false);
+ });
+
+ it('卸载崩在撤批准之前:恢复据 uninstall 标记把 receipt 与目录都删净', async () => {
+ await manager.install(await makeCindy('a.cindy', goodManifest()));
+ // 崩在写标记之后、撤批准之前:receipt 与目录都还在。
+ await fs.promises.writeFile(
+ pendingMarkerPath(),
+ JSON.stringify({ version: 1, id: 'hello', kind: 'uninstall' }),
+ );
+ expect(fs.existsSync(receiptPath())).toBe(true);
+
+ freshManager();
+ expect(fs.existsSync(path.join(rootDir, 'hello'))).toBe(false);
+ expect(fs.existsSync(receiptPath())).toBe(false);
+ expect(fs.existsSync(pendingMarkerPath())).toBe(false);
+ });
+
it('setEnabled 不跟随非真目录:<id> 是普通文件时按未装入拒,不越安装根写标记', async () => {
await fs.promises.mkdir(rootDir, { recursive: true });
await fs.promises.writeFile(path.join(rootDir, 'foo'), 'not a dir');
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index fa8f69302b..ea7701dd75 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -14,6 +14,7 @@ import {
} from '../../shared/ghost.js';
import {
classifyGhostDirEntry,
+ classifyGhostDirEntrySync,
collectGhostContentFiles,
hashGhostContentFiles,
isRegularGhostDirEntry,
@@ -126,7 +127,10 @@ export interface GhostLegacyMigrationLedger {
*/
export type GhostPendingMutation =
| { kind: 'install'; packageSha256: string }
- | { kind: 'update'; packageSha256: string; backupDirName: string };
+ | { kind: 'update'; packageSha256: string; backupDirName: string }
+ // uninstall 不带 packageSha256:它的提交信号不是"receipt 写到某版本",而是"receipt +
+ // 内容目录都已移除"。恢复见到它就把两者删干净(顺序无关,幂等)。
+ | { kind: 'uninstall' };
/** Host-owned receipt store:严格读取、同目录临时文件 + rename 原子提交。 */
export class GhostInstallReceiptStore {
@@ -252,6 +256,20 @@ export class GhostInstallReceiptStore {
});
}
+ /** `remove` 的同步版:启动恢复(构造期同步)收尾未完成卸载用,判据同 `remove`。 */
+ removeSync(id: string): void {
+ fs.rmSync(this.receiptPath(id), { force: true });
+ const snapshotsDir = path.join(this.rootDir(), 'skill-snapshots');
+ let kind: string;
+ try {
+ kind = classifyGhostDirEntrySync(snapshotsDir);
+ } catch {
+ return;
+ }
+ if (kind !== 'directory') return;
+ fs.rmSync(path.join(snapshotsDir, id), { recursive: true, force: true });
+ }
+
skillSnapshotRoot(id: string, revision: string): string {
if (!isValidGhostId(id) || !isRevision(revision)) {
throw new Error('invalid ghost skill snapshot identity');
@@ -454,6 +472,7 @@ export class GhostInstallReceiptStore {
} catch {
return null;
}
+ if (raw.kind === 'uninstall') return { kind: 'uninstall' };
if (typeof raw.packageSha256 !== 'string' || !/^[a-f0-9]{64}$/.test(raw.packageSha256)) {
return null;
}