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): 卸载/恢复内置补来源闸 + 崩溃恢复按解析 id 精确比对 backup

Yuewei-LL764faff65830
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index c504cd411e..2a190c453d 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -268,9 +268,14 @@ export class GhostManager {
continue;
}
const finalDir = path.join(root, id);
- const siblings = backups.filter((other) =>
- other.name.startsWith(`.cindy-updating-${id}-`),
- );
+ // 按解析后的 id 精确比对,不能用前缀 startsWith:合法 id 允许带 `-`,
+ // `.cindy-updating-foo-<hex>` 是 `.cindy-updating-foo-bar-<hex>` 的前缀,
+ // 若用前缀匹配,foo 与 foo-bar 同时留有 backup 时,foo 的唯一 backup 会被
+ // 误统计成多个而判为"多备份,留待人工",崩溃后 foo 持续消失(评审 P1)。
+ const siblings = backups.filter((other) => {
+ const otherMatch = /^\.cindy-updating-(.+)-[0-9a-f]{8}$/.exec(other.name);
+ return otherMatch !== null && otherMatch[1] === id;
+ });
if (fs.existsSync(finalDir)) {
try {
fs.rmSync(backupPath, { recursive: true, force: 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 5ec6b297a6..554303a5eb 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -2116,6 +2116,33 @@ describe('GhostManager · 更新崩溃恢复(两次 rename 之间)', () => {
expect(fs.existsSync(path.join(rootDir, '.cindy-updating-hello-22222222'))).toBe(true);
expect(fs.existsSync(path.join(rootDir, 'hello'))).toBe(false);
});
+
+ it('id 是另一个 id 的 `-` 前缀(hello / hello-x)各留唯一 backup → 两者都搬回,不因前缀误判互相拖累(P1)', async () => {
+ // 回归:siblings 统计曾用 startsWith(`.cindy-updating-${id}-`) 前缀匹配,
+ // `.cindy-updating-hello-<hex>` 是 `.cindy-updating-hello-x-<hex>` 的前缀,
+ // 于是处理 hello 时把 hello-x 的 backup 也算进来 → siblings 变 2 → 判"多备份
+ // 留待人工" → hello 崩溃后持续消失。修复后按解析 id 精确比对,两者各自恢复。
+ await manager.install(await makeCindy('a.cindy', goodManifest('hello')));
+ await manager.install(await makeCindy('b.cindy', goodManifest('hello-x')));
+ // 两个插件都卡在"final→backup 已发生,staging→final 未完成"的崩溃现场,
+ // 且各自只有唯一 backup(合法可恢复的场景)。
+ await fs.promises.rename(
+ path.join(rootDir, 'hello'),
+ path.join(rootDir, '.cindy-updating-hello-11111111'),
+ );
+ await fs.promises.rename(
+ path.join(rootDir, 'hello-x'),
+ path.join(rootDir, '.cindy-updating-hello-x-22222222'),
+ );
+
+ new GhostManager({ getRootDir: () => rootDir, getLocale: () => hostLocale });
+
+ // 两者都应搬回 final,backup 清空 —— hello 不能被 hello-x 的存在拖成"消失"。
+ expect(fs.existsSync(path.join(rootDir, 'hello', 'ghost.json'))).toBe(true);
+ expect(fs.existsSync(path.join(rootDir, 'hello-x', 'ghost.json'))).toBe(true);
+ expect(fs.existsSync(path.join(rootDir, '.cindy-updating-hello-11111111'))).toBe(false);
+ expect(fs.existsSync(path.join(rootDir, '.cindy-updating-hello-x-22222222'))).toBe(false);
+ });
});
describe('GhostManager · setEnabled(启用/停用)', () => {
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostIpcSenderTrust.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostIpcSenderTrust.test.ts
new file mode 100644
index 0000000000..a771cadad2
--- /dev/null
+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostIpcSenderTrust.test.ts
@@ -0,0 +1,58 @@
+import fs from 'node:fs';
+import path from 'node:path';
+
+import { describe, expect, it } from 'vitest';
+
+/**
+ * 源码契约测试:所有会改写插件世界(装/卸/更/启停/导出/恢复内置)或发放
+ * 装入票据的高权限 ghost IPC,handler 体内必须调用 assertTrustedAppRendererEvent,
+ * 按 Electron 持有的真实顶层 frame 判定来源。
+ *
+ * 背景:Renderer / WebView / Ghost 页面都是不可信输入(见 trustedAppRenderer.ts)。
+ * 卸载会停运行时、撤 receipt 批准、清凭证/KV/ghost-fs 并删安装目录;恢复内置会清
+ * 墓碑触发对账装回 —— 这些通道若忽略 event(用 `_event`),不受信页面 invoke 即可
+ * 卸载/改写任意插件。第 9 轮 integration review 实锤 ghosts:uninstall 与
+ * ghosts:restore-builtin 漏了这道闸。
+ *
+ * index.ts 拉起整张 main 进程单例图,handler 无法脱离 electron 直测,所以沿用
+ * 与 ghostIpcMutationLease.test.ts 相同的源码扫描钉契约:新增高权限写路径时把
+ * channel 加进清单;想去掉断言必须先改掉"页面不可信"这个前提。
+ */
+
+const TRUSTED_SENDER_CHANNELS = [
+ 'ghosts:install',
+ 'ghosts:update',
+ 'ghosts:inspect',
+ 'ghosts:uninstall',
+ 'ghosts:export',
+ 'ghosts:set-enabled',
+ 'ghosts:restore-builtin',
+ 'ghosts:reapprove-inspect',
+ 'ghosts:reapprove-installed',
+] as const;
+
+function handlerBlock(source: string, channel: string): string {
+ const start = source.indexOf(`ipcMain.handle('${channel}'`);
+ expect(start, `handler for ${channel} not found`).toBeGreaterThan(-1);
+ const end = source.indexOf('\n });', start);
+ expect(end, `handler block for ${channel} has no terminator`).toBeGreaterThan(start);
+ return source.slice(start, end);
+}
+
+describe('高权限 ghost IPC 的来源闸(源码契约)', () => {
+ const source = fs.readFileSync(path.join(__dirname, '..', 'index.ts'), 'utf8');
+
+ for (const channel of TRUSTED_SENDER_CHANNELS) {
+ it(`${channel} 断言 assertTrustedAppRendererEvent 且不吞 sender`, () => {
+ const block = handlerBlock(source, channel);
+ expect(block, `${channel} 缺 assertTrustedAppRendererEvent`).toContain(
+ 'assertTrustedAppRendererEvent(event)',
+ );
+ // 断言按真实 event 判定,而不是把 sender 声明成 `_event` 后忽略。
+ expect(
+ /ipcMain\.handle\([^,]+,\s*(?:async\s*)?\(\s*_event\b/.test(block),
+ `${channel} 用 _event 忽略了来源,无法校验 sender`,
+ ).toBe(false);
+ });
+ }
+});
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 4b24ed4cae..40bc5d98ff 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -4320,7 +4320,12 @@ export function registerGhostIpc(): void {
};
});
- ipcMain.handle('ghosts:uninstall', async (_event, id: unknown) => {
+ ipcMain.handle('ghosts:uninstall', async (event, id: unknown) => {
+ // 卸载会停运行时、撤 receipt 批准、清凭证/KV/ghost-fs 并删安装目录 —— 与
+ // install/update/set-enabled/export 同级的高权限写路径,来源判定必须由 Main
+ // 按真实顶层 frame 完成,不可信任页面自报;不受信 Ghost/WebView 页面若能
+ // invoke 此 channel 即可卸载任意插件(评审 P1)。
+ assertTrustedAppRendererEvent(event);
if (typeof id !== 'string' || id.trim().length === 0) {
throwIpcError('INVALID_PARAMS', 'id must be a non-empty string');
}
@@ -4412,7 +4417,10 @@ export function registerGhostIpc(): void {
});
// 恢复被抽离的内置意识:清墓碑 + 立即对账(串行链上排队,装回原位)。
- ipcMain.handle('ghosts:restore-builtin', async (_event, id: unknown) => {
+ ipcMain.handle('ghosts:restore-builtin', async (event, id: unknown) => {
+ // 恢复内置意识会清墓碑并触发对账装回,同属改写插件世界的写路径,来源判定
+ // 与其它写通道对齐,不信任页面自报(评审 P1)。
+ assertTrustedAppRendererEvent(event);
if (typeof id !== 'string' || id.trim().length === 0) {
throwIpcError('INVALID_PARAMS', 'id must be a non-empty string');
}