Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(plugin): preserve v2 market compatibility
liugaowei8110208febab
diff --git a/apps/desktop/src/main/__tests__/installedGhostManifest.test.ts b/apps/desktop/src/main/__tests__/installedGhostManifest.test.tsindex e8c7257046..5e3409bd5e 100644--- a/apps/desktop/src/main/__tests__/installedGhostManifest.test.ts+++ b/apps/desktop/src/main/__tests__/installedGhostManifest.test.ts@@ -7,7 +7,9 @@ import { afterEach, describe, expect, it } from 'vitest';import {parseInstalledGhostManifest,readInstalledGhostManifest,+ readInstalledGhostManifestDigestFormats,} from '../installedGhostManifest.js';+import { ghostManifestDigest } from '../plugin-market/ledger.js';const roots: string[] = [];@@ -130,4 +132,26 @@ describe('installed ghost manifest compatibility', () => {expect(parsed.manifest.id).toBe('legacy-plugin');expect(parsed.manifest).not.toHaveProperty('manual');});++ it('keeps the upgrade-time v2 digest candidate in the original slot order', () => {+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'cindy-installed-v2-digest-'));+ roots.push(root);+ const raw = {+ ...legacyBrokerManifest(),+ slots: ['notify', 'network'],+ };+ fs.writeFileSync(path.join(root, 'ghost.json'), JSON.stringify(raw));++ const parsed = parseInstalledGhostManifest(raw);+ expect(parsed.ok).toBe(true);+ if (!parsed.ok) return;+ const legacy = { ...parsed.manifest, slots: ['notify', 'network'] };+ delete legacy.notify;++ const digestCandidates = readInstalledGhostManifestDigestFormats(root, 64 * 1024).map(+ ghostManifestDigest,+ );+ expect(digestCandidates).toContain(ghostManifestDigest(legacy));+ expect(parsed.manifest).toMatchObject({ notify: true });+ });});diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.tsindex 9c7c850f4b..01a8ae3b6c 100644--- a/apps/desktop/src/main/cindy-brain/index.ts+++ b/apps/desktop/src/main/cindy-brain/index.ts@@ -298,7 +298,7 @@ import { getGhostGrantConfirmBridge } from './ghostGrantConfirmBridge.js';import { getSessionFsSnapshot, getSessionRowSnapshot } from '../localDb/ipc/sessions.js';import { getTeamByWorkerSession } from '../localDb/orcaTeamStore.js';import { getDirDepositVault, getSaveDepositVault, isPathInsideDir } from './dirDeposit.js';-import { readInstalledGhostManifest } from '../installedGhostManifest.js';+import { readInstalledGhostManifestDigestFormats } from '../installedGhostManifest.js';import {ghostManifestDigest,PluginMarketLedger,@@ -2543,8 +2543,13 @@ function readInstalledGhostManifestDigest(ghostId: string): string | null {.list().find((candidate) => candidate.manifest.id === ghostId);if (!ghost) return null;- const parsed = readInstalledGhostManifest(ghost.dir, GHOST_INSTALL_MANIFEST_MAX_BYTES);- return parsed.ok ? ghostManifestDigest(parsed.manifest) : null;+ const digests = readInstalledGhostManifestDigestFormats(+ ghost.dir,+ GHOST_INSTALL_MANIFEST_MAX_BYTES,+ ).map(ghostManifestDigest);+ const expected = getPluginMarketLedger().installationForGhost(ghostId)?.manifestDigest;+ if (expected !== undefined && digests.includes(expected)) return expected;+ return digests[0] ?? null;}/** Resolve Connection metadata from trusted organization installs or legacy Forge receipts. */diff --git a/apps/desktop/src/main/installedGhostManifest.ts b/apps/desktop/src/main/installedGhostManifest.tsindex 42f04906b2..607f4f04f9 100644--- a/apps/desktop/src/main/installedGhostManifest.ts+++ b/apps/desktop/src/main/installedGhostManifest.ts@@ -1,6 +1,10 @@import path from 'node:path';-import { validateGhostManifest, type GhostManifest } from '../shared/ghost.js';+import {+ ghostManifestToLegacyV2DigestFormat,+ validateGhostManifest,+ type GhostManifest,+} from '../shared/ghost.js';import { readBoundedFileNoFollowSync } from './utils/readBoundedFile.js';export type InstalledGhostManifestParse =@@ -44,3 +48,22 @@ export function readInstalledGhostManifest(return { ok: false, reason: 'manifest could not be read' };}}++/** 当前投影与升级前 v2 slots 投影;调用方可兼容核对既有持久摘要。 */+export function readInstalledGhostManifestDigestFormats(+ dir: string,+ maxBytes: number,+): unknown[] {+ try {+ const bytes = readBoundedFileNoFollowSync(path.join(dir, 'ghost.json'), maxBytes);+ if (bytes === null) return [];+ const raw = JSON.parse(bytes.toString('utf8')) as unknown;+ const parsed = parseInstalledGhostManifest(raw);+ if (!parsed.ok) return [];+ const current = parsed.manifest;+ const legacy = ghostManifestToLegacyV2DigestFormat(current, raw);+ return JSON.stringify(current) === JSON.stringify(legacy) ? [current] : [current, legacy];+ } catch {+ return [];+ }+}diff --git a/apps/desktop/src/main/plugin-market/ledger.ts b/apps/desktop/src/main/plugin-market/ledger.tsindex 45faf35fe0..790072c07f 100644--- a/apps/desktop/src/main/plugin-market/ledger.ts+++ b/apps/desktop/src/main/plugin-market/ledger.ts@@ -2,6 +2,7 @@ import crypto from 'node:crypto';import path from 'node:path';import type { PluginScope } from '@cindy/plugin-protocol';+import { ghostManifestToLegacyV2DigestFormat } from '../../shared/ghost.js';import {atomicWriteFileSync,readAtomicFileSync,@@ -56,7 +57,10 @@ function canonicalJson(value: unknown): string {/** manifest 的稳定摘要:语义相同的 manifest 无论键序/来源如何,摘要一致。 */export function ghostManifestDigest(manifest: unknown): string {- return crypto.createHash('sha256').update(canonicalJson(manifest)).digest('hex');+ return crypto+ .createHash('sha256')+ .update(canonicalJson(ghostManifestToLegacyV2DigestFormat(manifest)))+ .digest('hex');}interface PluginMarketLedgerData {diff --git a/apps/desktop/src/main/plugin-market/service.ts b/apps/desktop/src/main/plugin-market/service.tsindex a3bab2e8ab..d63af2beb3 100644--- a/apps/desktop/src/main/plugin-market/service.ts+++ b/apps/desktop/src/main/plugin-market/service.ts@@ -72,7 +72,10 @@ import {GHOST_MANIFEST_MAX_BYTES,readBoundedFileNoFollowWithStat,} from '../utils/readBoundedFile.js';-import { readInstalledGhostManifest } from '../installedGhostManifest.js';+import {+ readInstalledGhostManifest,+ readInstalledGhostManifestDigestFormats,+} from '../installedGhostManifest.js';import { withGhostInstallLock } from '../cindy-brain/ghostInstallLock.js';import { ghostBrokerRedirectPortInstallError } from '../cindy-brain/ghostBrokerRedirectPort.js';import { PluginMarketApi } from './api.js';@@ -304,9 +307,15 @@ function installedGhostRawManifest(dir: string): GhostManifest | null {return parsed.ok ? parsed.manifest : null;}-function installedGhostRawManifestDigest(dir: string): string | null {- const manifest = installedGhostRawManifest(dir);- return manifest ? ghostManifestDigest(manifest) : null;+function installedGhostRawManifestDigest(+ dir: string,+ expectedDigest?: string,+): string | null {+ const digests = readInstalledGhostManifestDigestFormats(dir, GHOST_MANIFEST_MAX_BYTES).map(+ ghostManifestDigest,+ );+ if (expectedDigest !== undefined && digests.includes(expectedDigest)) return expectedDigest;+ return digests[0] ?? null;}/**@@ -431,7 +440,7 @@ function serverRecordMatchesInstalledGhost(}return (record.manifestDigest === undefined ||- record.manifestDigest === installedGhostRawManifestDigest(ghost.dir)+ record.manifestDigest === installedGhostRawManifestDigest(ghost.dir, record.manifestDigest));}@@ -1285,11 +1294,12 @@ export class PluginMarketService {.list().find((ghost) => ghost.manifest.id === plugin.ghostId);const sourceKey = marketSourceKey(discovered.config.source);- // 选择时刻的已装内容摘要:打包窗口内不能换掉当前包。+ const currentRecord = ledger.installationForGhost(plugin.ghostId);+ // 选择时刻的已装内容摘要:打包窗口内不能换掉当前包。v2 存量记录可继续+ // 使用升级前的 slots 摘要;新记录使用当前稳定投影。const reviewInstalledDigest = existing- ? installedGhostRawManifestDigest(existing.dir)+ ? installedGhostRawManifestDigest(existing.dir, currentRecord?.manifestDigest): null;- const currentRecord = ledger.installationForGhost(plugin.ghostId);const matchesSelectedRoute = Boolean(existing &¤tRecord?.installed &&@@ -1362,7 +1372,11 @@ export class PluginMarketService {: 'Plugin was uninstalled while the install was packaging',);}- if (current && installedGhostRawManifestDigest(current.dir) !== reviewInstalledDigest) {+ if (+ current &&+ installedGhostRawManifestDigest(current.dir, reviewInstalledDigest ?? undefined) !==+ reviewInstalledDigest+ ) {throwIpcError('PRECONDITION_FAILED', 'Installed Plugin changed during the install');}// raw manifest 摘要不含 Host receipt;内容未变但批准态变化也必须拒绝。@@ -2065,7 +2079,10 @@ export class PluginMarketService {});return;}- const installedManifestDigest = installedGhostRawManifestDigest(currentInstalled.dir);+ const installedManifestDigest = installedGhostRawManifestDigest(+ currentInstalled.dir,+ record.manifestDigest,+ );if (approvalEvidence.packageSha256 === null) {if (!approvalEvidence.legacyMigrated) {log.info('disconnected market recovery skipped', {@@ -2256,7 +2273,8 @@ export class PluginMarketService {// 目录里,digest 迁移永远补不上,fail-closed 会让老安装的合法清理永久失效。if (record?.manifestDigest != null &&- installedGhostRawManifestDigest(installed.dir) !== record.manifestDigest+ installedGhostRawManifestDigest(installed.dir, record.manifestDigest) !==+ record.manifestDigest) {return skip(removal, 'manifest-digest-mismatch');}@@ -2526,7 +2544,13 @@ export class PluginMarketService {ghostsById: new Map(ghosts.map((ghost) => [ghost.manifest.id, ghost])),installations,rawDigestByGhostId: new Map(- ghosts.map((ghost) => [ghost.manifest.id, installedGhostRawManifestDigest(ghost.dir)]),+ ghosts.map((ghost) => {+ const record = installations[ghost.manifest.id];+ return [+ ghost.manifest.id,+ installedGhostRawManifestDigest(ghost.dir, record?.manifestDigest),+ ];+ }),),};}diff --git a/apps/desktop/src/shared/__tests__/ghost.test.ts b/apps/desktop/src/shared/__tests__/ghost.test.tsindex 0d06a84c68..f9367c7887 100644--- a/apps/desktop/src/shared/__tests__/ghost.test.ts+++ b/apps/desktop/src/shared/__tests__/ghost.test.ts@@ -4508,6 +4508,33 @@ describe('ghostPermissionProjectionFingerprint', () => {);});+ it('真实包收窄 preview hosts 不算扩权,新增或替换 host 仍拒绝', () => {+ const reviewed = validateGhostManifest({+ ...goodChipManifest(),+ slots: ['panel', 'preview'],+ preview: { hosts: ['a.example.com', 'b.example.com'] },+ });+ const narrowed = validateGhostManifest({+ ...goodChipManifest(),+ slots: ['panel', 'preview'],+ preview: { hosts: ['a.example.com'] },+ });+ const expanded = validateGhostManifest({+ ...goodChipManifest(),+ slots: ['panel', 'preview'],+ preview: { hosts: ['a.example.com', 'c.example.com'] },+ });+ expect(reviewed.ok && narrowed.ok && expanded.ok).toBe(true);+ if (!reviewed.ok || !narrowed.ok || !expanded.ok) return;++ expect(unreviewedGhostPermissionItems(reviewed.manifest, undefined, narrowed.manifest)).toEqual(+ [],+ );+ expect(+ unreviewedGhostPermissionItems(reviewed.manifest, undefined, expanded.manifest),+ ).toEqual([expect.objectContaining({ key: 'preview' })]);+ });+it('语义相同时指纹稳定(与字段/条目顺序无关)', () => {const a = validateGhostManifest(goodChipManifest());const b = validateGhostManifest(JSON.parse(JSON.stringify(goodChipManifest())));diff --git a/apps/desktop/src/shared/ghost.ts b/apps/desktop/src/shared/ghost.tsindex 616946850d..287ae66c71 100644--- a/apps/desktop/src/shared/ghost.ts+++ b/apps/desktop/src/shared/ghost.ts@@ -2451,12 +2451,22 @@ export function unreviewedGhostPermissionItems(approved.add(ghostPermissionProjectionKey(item));approvedKeys.add(item.key);}+ const previewWithinCap = (cap: GhostManifest): boolean =>+ actual.preview === undefined ||+ (cap.preview !== undefined &&+ actual.preview.hosts.every((host) => cap.preview?.hosts.includes(host)));+ const previewApproved =+ previewWithinCap(reviewed) ||+ (previouslyInstalled !== undefined && previewWithinCap(previouslyInstalled));return ghostPermissionItems(actual).filter((item) =>!approved.has(ghostPermissionProjectionKey(item)) &&// code 的 detailKey 只是在复述已单列的 network/node 等能力。实际包少一项// 能力时文案会降档,但不能因此把“更少能力”反判为扩权。- !(item.key === 'code' && approvedKeys.has(item.key)),+ !(item.key === 'code' && approvedKeys.has(item.key)) &&+ // preview 的展示 detail 包含完整 hosts;真实包删减 host 是收权,不应因+ // detail 字符串变化被反判成新增能力。新增或替换 host 仍会保留为未审查项。+ !(item.key === 'preview' && previewApproved),);}@@ -5858,15 +5868,39 @@ export function validateNormalizedGhostManifest(raw: unknown): ManifestValidatio/** 把无 slots 的 v2 运行时投影还原成旧客户端能读取的作者清单。 */function withLegacyAuthorSlots(raw: Record<string, unknown>): Record<string, unknown> {- if (raw.schemaVersion !== 2 || Array.isArray(raw.slots)) return raw;- const slots: string[] = [];- for (const [field, slot] of V3_DECLARATION_TO_LEGACY_SLOT) {- if (raw[field] !== undefined) slots.push(slot);+ if (raw.schemaVersion !== 2) return raw;+ const slots: unknown[] = Array.isArray(raw.slots) ? [...raw.slots] : [];+ if (!Array.isArray(raw.slots)) {+ for (const [field, slot] of V3_DECLARATION_TO_LEGACY_SLOT) {+ if (raw[field] !== undefined) slots.push(slot);+ }+ for (const field of V3_BOOLEAN_CAPABILITY_FIELDS) {+ if (raw[field] === true) slots.push(V3_BOOLEAN_TO_LEGACY_SLOT[field]);+ }}- for (const field of V3_BOOLEAN_CAPABILITY_FIELDS) {- if (raw[field] === true) slots.push(V3_BOOLEAN_TO_LEGACY_SLOT[field]);+ const legacy: Record<string, unknown> = { ...raw, slots };+ for (const field of V3_BOOLEAN_CAPABILITY_FIELDS) delete legacy[field];+ return legacy;+}++/** 同一份 v2 清单在移除运行时 slots 前后的持久摘要必须保持一致。 */+export function ghostManifestToLegacyV2DigestFormat(+ manifest: unknown,+ source?: unknown,+): unknown {+ if (!isPlainObject(manifest)) return manifest;+ if (+ manifest.schemaVersion === 2 &&+ isPlainObject(source) &&+ source.schemaVersion === 2 &&+ Array.isArray(source.slots)+ ) {+ return withLegacyAuthorSlots({+ ...manifest,+ slots: source.slots.map((slot) => (slot === 'model' ? 'cindy' : slot)),+ });}- return { ...raw, slots };+ return withLegacyAuthorSlots(manifest);}/**