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): 保持插件批准凭据可回滚

david723402137706
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostInstallReceipt.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostInstallReceipt.test.ts
index b4bafb846c..e5855a743f 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostInstallReceipt.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostInstallReceipt.test.ts
@@ -8,6 +8,7 @@ import { validateGhostManifest } from '../../../shared/ghost';
import {
createGhostInstallReceipt,
+ type GhostInstallReceipt,
GhostInstallReceiptStore,
} from '../ghostInstallReceipt';
@@ -30,6 +31,57 @@ describe('GhostInstallReceiptStore cleanup', () => {
await fs.promises.rm(workDir, { recursive: true, force: true });
});
+ function createSetupReceipt(): GhostInstallReceipt {
+ const parsed = validateGhostManifest({
+ schemaVersion: 2,
+ id: 'hello',
+ name: 'Hello',
+ version: '1.0.0',
+ kind: 'chip',
+ entry: 'main.js',
+ settingsHtml: 'settings.html',
+ slots: ['tool', 'network'],
+ tools: [{ name: 'do_thing', description: 'Do something' }],
+ network: {
+ hosts: ['api.example.com'],
+ secrets: [
+ {
+ key: 'api_key',
+ label: 'API Key',
+ inject: { header: 'Authorization', format: 'Bearer {value}' },
+ },
+ ],
+ connections: [
+ {
+ key: 'account',
+ label: 'Account',
+ inject: { header: 'X-Account', format: '{value}' },
+ },
+ ],
+ },
+ setup: {
+ requires: [
+ { anyOf: ['secret:api_key'] },
+ { anyOf: ['connection:account', { kv: 'repoDir', label: '本机 cindy 项目目录' }] },
+ ],
+ },
+ });
+ if (!parsed.ok) throw new Error(parsed.reason);
+
+ return createGhostInstallReceipt({
+ manifest: parsed.manifest,
+ localeResources: {},
+ enabled: true,
+ trust: {
+ level: 'unverified',
+ publisherSigned: false,
+ publisherVerified: false,
+ reviewed: false,
+ },
+ skillContentSha256: {},
+ });
+ }
+
it('removes a regular receipt and managed snapshot tree', async () => {
const receipt = path.join(stateRoot, 'hello.json');
const snapshot = path.join(stateRoot, 'skill-snapshots', 'hello', 'revision');
@@ -118,45 +170,42 @@ describe('GhostInstallReceiptStore cleanup', () => {
expect(fs.existsSync(path.join(stateRoot, 'hello.json'))).toBe(false);
});
- it('writes and reads a receipt containing a normalized setup kv requirement', async () => {
- const parsed = validateGhostManifest({
- schemaVersion: 2,
- id: 'hello',
- name: 'Hello',
- version: '1.0.0',
- kind: 'chip',
- entry: 'main.js',
- settingsHtml: 'settings.html',
- slots: ['tool'],
- tools: [{ name: 'do_thing', description: 'Do something' }],
- setup: {
- requires: [{ anyOf: [{ kv: 'repoDir', label: '本机 cindy 项目目录' }] }],
- },
- });
- expect(parsed.ok).toBe(true);
- if (!parsed.ok) return;
+ it('writes setup in the author format accepted by the v0.1.48 receipt reader', async () => {
+ const receipt = createSetupReceipt();
+ await store.write(receipt);
- const receipt = createGhostInstallReceipt({
- manifest: parsed.manifest,
- localeResources: {},
- enabled: true,
- trust: {
- level: 'unverified',
- publisherSigned: false,
- publisherVerified: false,
- reviewed: false,
+ const persisted = JSON.parse(
+ await fs.promises.readFile(path.join(stateRoot, 'hello.json'), 'utf8'),
+ ) as { manifest: unknown };
+ expect(persisted.manifest).toMatchObject({
+ setup: {
+ requires: [
+ { anyOf: ['secret:api_key'] },
+ {
+ anyOf: [
+ 'connection:account',
+ { kv: 'repoDir', label: '本机 cindy 项目目录' },
+ ],
+ },
+ ],
},
- skillContentSha256: {},
});
+ // v0.1.48 reads receipt manifests through this author-format validator.
+ expect(validateGhostManifest(persisted.manifest).ok).toBe(true);
- await store.write(receipt);
expect(store.read('hello')).toMatchObject({
state: 'approved',
receipt: {
manifest: {
setup: {
requires: [
- { anyOf: [{ kind: 'kv', key: 'repoDir', label: '本机 cindy 项目目录' }] },
+ { anyOf: [{ kind: 'secret', key: 'api_key' }] },
+ {
+ anyOf: [
+ { kind: 'connection', key: 'account' },
+ { kind: 'kv', key: 'repoDir', label: '本机 cindy 项目目录' },
+ ],
+ },
],
},
},
@@ -164,6 +213,20 @@ describe('GhostInstallReceiptStore cleanup', () => {
});
});
+ it('still reads normalized setup receipts emitted by affected builds', async () => {
+ const receipt = createSetupReceipt();
+ await fs.promises.writeFile(
+ path.join(stateRoot, 'hello.json'),
+ `${JSON.stringify(receipt, null, 2)}\n`,
+ 'utf8',
+ );
+
+ expect(store.read('hello')).toMatchObject({
+ state: 'approved',
+ receipt: { manifest: receipt.manifest },
+ });
+ });
+
it('treats only a missing migration marker as absent', () => {
expect(store.hasMigrationMarker('hello')).toBe(false);
});
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index 239a8120d8..2dcefd9dc6 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -6,6 +6,7 @@ import { readBoundedFileNoFollowSync } from '../utils/readBoundedFile.js';
import {
GHOST_LOCALE_MAX_BYTES,
GHOST_SKILL_MD_MAX_BYTES,
+ ghostManifestToAuthorFormat,
isValidGhostId,
validateGhostManifestLocaleResource,
validateNormalizedGhostManifest,
@@ -273,8 +274,12 @@ export class GhostInstallReceiptStore {
root,
`.${receipt.id}-${process.pid}-${crypto.randomBytes(6).toString('hex')}.tmp`,
);
+ const persistedReceipt = {
+ ...validated.receipt,
+ manifest: ghostManifestToAuthorFormat(validated.receipt.manifest),
+ };
try {
- await fs.promises.writeFile(temp, `${JSON.stringify(receipt, null, 2)}\n`, {
+ await fs.promises.writeFile(temp, `${JSON.stringify(persistedReceipt, null, 2)}\n`, {
encoding: 'utf8',
flag: 'wx',
mode: 0o600,
diff --git a/apps/desktop/src/shared/ghost.ts b/apps/desktop/src/shared/ghost.ts
index 7ee880c1db..5bd8a41548 100644
--- a/apps/desktop/src/shared/ghost.ts
+++ b/apps/desktop/src/shared/ghost.ts
@@ -5321,7 +5321,11 @@ export function validateGhostManifest(raw: unknown): ManifestValidation {
* validateGhostManifest,因此作者直接写内部格式仍会被拒绝。
*/
export function validateNormalizedGhostManifest(raw: unknown): ManifestValidation {
- if (!isPlainObject(raw) || raw.setup === undefined) return validateGhostManifest(raw);
+ // Durable Host state is written in author format so released clients can read it
+ // after a rollback. Still accept normalized snapshots produced by affected dev
+ // builds and passed between current Host code paths.
+ const authorResult = validateGhostManifest(raw);
+ if (authorResult.ok || !isPlainObject(raw) || raw.setup === undefined) return authorResult;
if (!isPlainObject(raw.setup) || !Array.isArray(raw.setup.requires)) {
return { ok: false, reason: '标准化清单 setup 必须是带 requires 数组的对象' };
}
@@ -5355,6 +5359,29 @@ export function validateNormalizedGhostManifest(raw: unknown): ManifestValidatio
return validateGhostManifest({ ...raw, setup: { requires } });
}
+/**
+ * 把 Host 归一化清单投影回 ghost.json 作者格式,供跨版本持久化。
+ *
+ * receipt schema v2 已随 v0.1.48 发布;旧版读取时会直接调用
+ * validateGhostManifest,因此不能把内部 `{ kind, key }` setup 形态写盘。
+ */
+export function ghostManifestToAuthorFormat(manifest: GhostManifest): Record<string, unknown> {
+ if (manifest.setup === undefined) return { ...manifest };
+ return {
+ ...manifest,
+ setup: {
+ requires: manifest.setup.requires.map((group) => ({
+ anyOf: group.anyOf.map((requirement) => {
+ if (requirement.kind === 'kv') {
+ return { kv: requirement.key, label: requirement.label };
+ }
+ return `${requirement.kind}:${requirement.key}`;
+ }),
+ })),
+ },
+ };
+}
+
/**
* ── 管子(脑机接口)消息协议(docs/dev-rules/plugin-security-and-authoring.md)──
*