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): 批准写入失败时撤销陈旧批准,快照重建守住 SKILL.md 上限

Yuewei-LL40fe328ae083
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 77ed8b0176..ed50cc56af 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -6,6 +6,7 @@ import JSZip from 'jszip';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
+ GHOST_SKILL_MD_MAX_BYTES,
ghostInstallApprovalToken,
type InstalledGhost,
} from '../../../shared/ghost';
@@ -670,6 +671,54 @@ describe('GhostManager · Host approval receipt', () => {
]);
});
+ it('holds the install-time SKILL.md size ceiling when rebuilding from mutable install bytes', async () => {
+ await manager.install(await makeCindy('skill.cindy', skillManifest(), skillFiles()));
+ const snapshotRoot = manager.list()[0].approvedSkillRoot!;
+ const installedSkillMd = path.join(rootDir, 'skilled', 'skills', 'demo', 'SKILL.md');
+ // 快照缺失时取字节的来源是可变安装目录。这里塞的 SKILL.md frontmatter 与批准
+ // manifest 完全一致(躲过一致性校验),只是正文超过装入侧上限 —— 重建必须照样拒,
+ // 否则启用这条路会批准一份装入/更新永远不会接受的超大技能指令,而且要先整份
+ // 读进内存。
+ await fs.promises.writeFile(
+ installedSkillMd,
+ `---\nname: demo\ndescription: Demo skill\n---\n\nApproved instructions\n${'padding '.repeat(
+ GHOST_SKILL_MD_MAX_BYTES / 4,
+ )}`,
+ );
+ expect((await fs.promises.lstat(installedSkillMd)).size).toBeGreaterThan(
+ GHOST_SKILL_MD_MAX_BYTES,
+ );
+ await fs.promises.rm(snapshotRoot, { recursive: true, force: true });
+ expect(await manager.setEnabled('skilled', false)).toEqual({ ok: true });
+
+ await expectRejection(await manager.setEnabled('skilled', true), 'io');
+ expect(manager.list()[0].enabled).toBe(false);
+ expect(fs.existsSync(snapshotRoot)).toBe(false);
+ });
+
+ it('revoking approval fails the install closed, and a later bundled approval heals it', async () => {
+ await manager.install(await makeCindy('approved.cindy', goodManifest()));
+ const approvedManifest = manager.list()[0].manifest;
+
+ // 随包对账在换入新种子字节后写批准失败时走的收敛动作:撤掉陈旧批准。
+ // 撤掉之后插件必须彻底不可运行,而不是继续拿旧批准跑新代码。
+ await manager.removeInstallApproval('hello');
+
+ expect(fs.existsSync(receiptPath())).toBe(false);
+ expect(manager.list()[0]).toMatchObject({
+ enabled: false,
+ approval: { state: 'legacy-unapproved' },
+ });
+ await expectRejection(await manager.setEnabled('hello', true), 'approval-required');
+
+ // 下一轮启动对账重新补批准即自愈,不需要用户介入。
+ expect(await manager.approveTrustedBundledInstall(approvedManifest, true)).toBe(true);
+ expect(manager.list()[0]).toMatchObject({
+ enabled: true,
+ approval: { state: 'approved' },
+ });
+ });
+
it('invalidates a receipt whose locale snapshot keys no longer match the manifest', async () => {
hostLocale = 'en';
const manifest = {
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index 2334231d27..f1997af94e 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -4,6 +4,7 @@ import path from 'node:path';
import {
GHOST_LOCALE_MAX_BYTES,
+ GHOST_SKILL_MD_MAX_BYTES,
isValidGhostId,
validateGhostManifest,
validateGhostManifestLocaleResource,
@@ -176,7 +177,18 @@ export class GhostInstallReceiptStore {
await fs.promises.mkdir(temp, { recursive: false });
for (const item of items) {
const source = path.join(skillSourceDir, ...item.dir.split('/'));
- const skillMd = await fs.promises.readFile(path.join(source, 'SKILL.md'), 'utf8');
+ // 取字节的来源是可变的安装目录(快照缺失时从 dir 重建),所以这里要自己
+ // 复现装入侧的门槛:先 lstat 定长再读,不然本机进程往安装目录塞一个超大
+ // SKILL.md 就能让 Host 整份读进内存。lstat 而非 stat —— 与本文件其余
+ // 位置一致地拒绝软链与非普通文件,不留跟随软链的绕过口。
+ const skillMdPath = path.join(source, 'SKILL.md');
+ const skillMdStat = await fs.promises.lstat(skillMdPath);
+ if (!skillMdStat.isFile() || skillMdStat.size > GHOST_SKILL_MD_MAX_BYTES) {
+ throw new Error(
+ `approved skill ${item.dir}/SKILL.md is not a regular file or exceeds ${GHOST_SKILL_MD_MAX_BYTES} bytes`,
+ );
+ }
+ const skillMd = await fs.promises.readFile(skillMdPath, 'utf8');
const consistencyError = checkSkillMdConsistency(skillMd, item);
if (consistencyError) {
throw new Error(`approved skill ${item.dir} is inconsistent: ${consistencyError}`);
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 08f45d93fc..349b00e184 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -785,8 +785,24 @@ async function reconcileBuiltinGhostsLocked(
!fs.existsSync(path.join(brainRootDir(), manifest.id, '.disabled')),
)) || approvalChanged;
} catch (err) {
+ // 走到这里内容目录可能已经换成新种子字节,旧 receipt 却还是授权事实 ——
+ // 留着它就是拿旧批准跑新代码(新版删掉的 slot 仍被授予、版本与技能快照
+ // 也停在旧 revision)。撤掉批准 fail closed 到 legacy-unapproved:随包
+ // 插件下一轮启动对账会重新补批准,自愈,不需要用户介入。
+ let approvalCleared = false;
+ try {
+ await manager.removeInstallApproval(manifest.id);
+ approvalCleared = true;
+ approvalChanged = true;
+ } catch (cleanupErr) {
+ log.error('builtin ghost stale approval could not be revoked', {
+ id: manifest.id,
+ error: cleanupErr instanceof Error ? cleanupErr.message : String(cleanupErr),
+ });
+ }
log.warn('builtin ghost approval receipt failed', {
id: manifest.id,
+ approvalCleared,
error: err instanceof Error ? err.message : String(err),
});
}