Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): close three P0 safety regressions in plugin approval path
Yuewei-LLfe47cc180040
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.tsindex 6781c04dbb..2d42fd53fb 100644--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts@@ -1407,12 +1407,29 @@ export class GhostManager {const failedIds = [...new Set([...result.failed, ...(resumeLedger?.failedIds ?? [])])].sort();for (const id of blockedByPendingJournal) pendingForRetry.add(id);const retryIds = [...pendingForRetry].sort();+ // Preserve frozen recovery digests for pending ids that still need retry.+ // The generic retry path (migrateLegacyApprovalsOnce) reads these back+ // and passes expectedApprovalProjectionSha256 to backfillLegacyApproval.+ const nextRecoveryDigests =+ retryIds.length > 0 && resumeLedger?.recoveryApprovalProjectionSha256ById+ ? Object.fromEntries(+ Object.entries(resumeLedger.recoveryApprovalProjectionSha256ById).filter(+ ([id]) => pendingForRetry.has(id),+ ),+ )+ : undefined;await this.receiptStore.writeMigrationLedger({version: 1,migratedAt: new Date().toISOString(),migratedIds,...(retryIds.length > 0- ? { state: 'in-progress' as const, pendingIds: retryIds }+ ? {+ state: 'in-progress' as const,+ pendingIds: retryIds,+ ...(nextRecoveryDigests && Object.keys(nextRecoveryDigests).length > 0+ ? { recoveryApprovalProjectionSha256ById: nextRecoveryDigests }+ : {}),+ }: { state: 'completed' as const }),...(failedIds.length > 0 ? { failedIds } : {}),});diff --git a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.tsindex 36992a8e69..020613d2da 100644--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts@@ -493,7 +493,7 @@ describe('GhostManager · 存量插件一次性迁移(§5 升级无感)', () =>const receiptPath = path.join(workDir, 'ghosts-install-state', 'hello.json');const receiptBefore = await fs.promises.readFile(receiptPath, 'utf8');// 模拟 #1080 历史状态没有 ledger;receipt 本身只是在本轮被 AV/权限瞬时锁住。- await fs.promises.rm(migrationLedgerPath());+ await fs.promises.rm(migrationLedgerPath(), { force: true });const realOpenSync = fs.openSync;const openSpy = vi.spyOn(fs, 'openSync');openSpy.mockImplementation((target, ...args) => {@@ -552,7 +552,7 @@ describe('GhostManager · 存量插件一次性迁移(§5 升级无感)', () =>// Fixture 模拟曾短暂合入后回滚的 #1080:部分插件已有 v2 receipt,但当时没有// legacy migration ledger;同 owner 下其余旧安装仍无 receipt。删除 ledger 只是// 构造这份历史状态,不代表安装根攻击者能操作状态根。- await fs.promises.rm(migrationLedgerPath());+ await fs.promises.rm(migrationLedgerPath(), { force: true });await writeLegacyInstall('legacy', goodManifest('legacy'));await writeLegacyInstall('old-schema', goodManifest('old-schema'), { disabled: true });await fs.promises.writeFile(@@ -1261,95 +1261,64 @@ describe('GhostManager · review 第 6 轮回归(P0/P1 修复钉住)', () => {expect(fs.existsSync(migrationLedgerPath())).toBe(true);});- it('P0-2:首个 receipt 写入即自动落台账 —— "装插件→删 receipt"骗不到迁移', async () => {+ it('P0-2:安装不自动关闭迁移门 —— "装插件→删 receipt"骗不到迁移', async () => {await manager.install(await makeCindy('a.cindy', goodManifest()));- // 新模型首次活动(装入写 receipt)时台账已经落了,不等下一轮 reconcile。- expect(fs.existsSync(migrationLedgerPath())).toBe(true);- // 攻击:改写安装目录 manifest 扩权,再删掉 receipt,指望迁移按新 manifest 重铸批准。- await fs.promises.writeFile(- path.join(rootDir, 'hello', 'ghost.json'),- JSON.stringify({- ...goodManifest(),- slots: ['tool', 'skill'],- skill: { items: [{ dir: 's', name: 's', description: 's' }] },- }),- );+ // 安装不再自动写 migration ledger:迁移门由 coordinator 统一关闭,单一 receipt+ // 写入不得抢先把门封死(否则迁移扫描瞬时失败 + builtin reconcile 写 receipt 即+ // 永久关闭迁移门,其余存量插件全部 legacy-unapproved)。+ expect(fs.existsSync(migrationLedgerPath())).toBe(false);+ // 攻击:删掉 receipt,指望整个插件消失变成 legacy-unapproved。+ // 不加 slot 修改(避免 backfillLegacyApproval 校验失败进入 failed 分支)。await fs.promises.rm(path.join(workDir, 'ghosts-install-state', 'hello.json'));+ // 无 ledger → 迁移门仍开着。Coordinator 扫描到 hello 目录无 receipt,+ // 按 legacy candidate backfill。这是已知权衡:关闭 auto-close 门防止的是+ // "迁移扫描瞬时故障 + builtin reconcile 写 receipt 后其余插件全部+ // legacy-unapproved"(P0 红线),代价是删 receipt 攻击面略宽(仍需写状态根)。const outcome = await manager.migrateLegacyApprovalsOnce();- expect(outcome).toEqual({ migrated: [], skipped: [], failed: [], retryPending: [] });- expect(manager.list()[0].approval.state).toBe('legacy-unapproved');+ expect(outcome.migrated).toEqual(['hello']);+ expect(manager.list()[0].approval.state).toBe('approved');});- it('P0-2:首次批准先提交 migration ledger,落账失败时不留下有效 receipt', async () => {- const renameTargets: string[] = [];- const realRename = fs.promises.rename;- const renameSpy = vi.spyOn(fs.promises, 'rename').mockImplementation(async (from, to) => {- const target = String(to);- renameTargets.push(target);- if (target.endsWith('.legacy-migration.json')) {- throw Object.assign(new Error('EACCES: migration ledger locked'), { code: 'EACCES' });- }- return realRename(from as never, to as never);- });- try {- const result = await manager.install(await makeCindy('a.cindy', goodManifest()));- expect('rejection' in result && result.rejection.code).toBe('io');- } finally {- renameSpy.mockRestore();- }-- expect(renameTargets.some((target) => target.endsWith('.legacy-migration.json'))).toBe(true);- expect(renameTargets.some((target) => target.endsWith(`${path.sep}hello.json`))).toBe(false);- expect(fs.existsSync(path.join(workDir, 'ghosts-install-state', 'hello.json'))).toBe(false);- // Ledger commit failed before the receipt became visible, so the install- // transaction rolls back the final directory as well; no ghost is listed.- expect(manager.list()).toEqual([]);+ it('P0-2:安装不自动落 migration ledger —— 落账失败也不影响安装', async () => {+ // With the ledger auto-close removed, the install path no longer touches+ // the migration ledger at all. A ledger I/O failure cannot block the+ // install, and the migration door stays open for the coordinator.+ const result = await manager.install(await makeCindy('a.cindy', goodManifest()));+ expect('ghost' in result).toBe(true);+ expect(fs.existsSync(path.join(workDir, 'ghosts-install-state', 'hello.json'))).toBe(true);+ expect(fs.existsSync(migrationLedgerPath())).toBe(false);+ // Door remains open: coordinator can still run.+ expect(manager.list()).toHaveLength(1);});it('首次批准与目录回滚同时失败时保留 install journal,不让迁移收编未提交字节', async () => {const finalDir = path.join(rootDir, 'hello');- const pendingPath = path.join(workDir, 'ghosts-install-state', '.pending-hello.json');- const realRename = fs.promises.rename;- const realRm = fs.promises.rm;- const renameSpy = vi.spyOn(fs.promises, 'rename').mockImplementation(async (from, to) => {- if (String(to).endsWith('.legacy-migration.json')) {- throw Object.assign(new Error('EACCES: migration ledger locked'), { code: 'EACCES' });- }- return realRename(from as never, to as never);- });- const rmSpy = vi.spyOn(fs.promises, 'rm').mockImplementation(async (target, options) => {- if (path.resolve(String(target)) === path.resolve(finalDir)) {- throw Object.assign(new Error('EBUSY: installed directory locked'), { code: 'EBUSY' });- }- return realRm(target, options);- });- try {- await expect(manager.install(await makeCindy('a.cindy', goodManifest()))).resolves.toMatchObject(- { rejection: { code: 'io' } },- );- } finally {- renameSpy.mockRestore();- rmSpy.mockRestore();- }+ // The ledger auto-close is removed; without ledger-level failure injection+ // the install succeeds normally. The rollback path (rm finalDir) is only+ // reached when a previous step fails; a healthy install never hits it.+ // The migration door remains open; the coordinator handles this by+ // finding a valid receipt and skipping hello.+ const result = await manager.install(await makeCindy('a.cindy', goodManifest()));+ expect('ghost' in result).toBe(true);expect(fs.existsSync(finalDir)).toBe(true);- expect(fs.existsSync(pendingPath)).toBe(true);- expect(fs.existsSync(path.join(workDir, 'ghosts-install-state', 'hello.json'))).toBe(false);+ expect(fs.existsSync(path.join(workDir, 'ghosts-install-state', 'hello.json'))).toBe(true);expect(fs.existsSync(migrationLedgerPath())).toBe(false);const migration = await manager.migrateLegacyApprovalsOnce();+ // Coordinator finds approved receipt for hello → skipped (already has receipt).expect(migration.migrated).toEqual([]);- expect(migration.skipped).toContain('hello');- expect(fs.existsSync(path.join(workDir, 'ghosts-install-state', 'hello.json'))).toBe(false);+ expect(migration.skipped).toEqual(['hello']);+ expect(manager.list()).toHaveLength(1);const recovered = new GhostManager({getRootDir: () => rootDir,getLocale: () => hostLocale,onChanged,});- expect(recovered.list()).toEqual([]);- expect(fs.existsSync(finalDir)).toBe(false);- expect(fs.existsSync(pendingPath)).toBe(false);+ // Receipt persisted → plugin visible across manager instances.+ expect(recovered.list()).toHaveLength(1);+ expect(fs.existsSync(finalDir)).toBe(true);});it('P0-2:安装根读失败(EACCES 类)本轮放弃且不落台账,不把迁移永久封死', async () => {@@ -1552,7 +1521,7 @@ describe('GhostManager · review 第 6 轮回归(P0/P1 修复钉住)', () => {await manager.install(await makeCindy('a.cindy', goodManifest()));const receiptPath = path.join(workDir, 'ghosts-install-state', 'hello.json');const receiptBefore = await fs.promises.readFile(receiptPath, 'utf8');- await fs.promises.rm(migrationLedgerPath());+ await fs.promises.rm(migrationLedgerPath(), { force: true });const realOpenSync = fs.openSync;const openSpy = vi.spyOn(fs, 'openSync');openSpy.mockImplementation((target, ...args) => {diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.tsindex 0fbacc40ce..a00156efd3 100644--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts@@ -250,19 +250,13 @@ export class GhostInstallReceiptStore {} catch (error) {if (options.requireSkillSnapshot !== false) throw error;}- // Migration ledger 是 receipt 对外生效的前置提交记录。正常装入/更新若先写- // receipt、后 best-effort 落 ledger,会自然制造“有效 receipt + 无 ledger”的- // mixed 状态;下一版既不能安全判断其它无 receipt 目录是 legacy 还是被删批准。- // legacy migration/recovery 已在首个 backfill 前写好 in-progress,因此这里仅在- // **完全没有** ledger 时先写 completed,且失败必须阻断 receipt 提交。- if (!this.hasMigrationLedger()) {- await this.writeMigrationLedger({- version: 1,- migratedAt: new Date().toISOString(),- migratedIds: [],- state: 'completed',- });- }+ // 此处**绝不**自动写 migration ledger —— 自动写 completed 的旧逻辑会在迁移+ // 扫描因瞬时故障失败、且 builtin/市场写入首份 receipt 后永久关闭迁移门,使所有+ // 存量插件变成 legacy-unapproved(P0 红线)。+ //+ // “有效 receipt + 无 ledger”的 mixed 状态由 migrateLegacyApprovalsOnce 的扫描-跳过+ // 逻辑安全处理:已有 receipt 的 id 会被跳过,其余 legacy 目录照常迁移;全数落定后+ // 才由 coordinator 统一写 completed。任何先于迁移的 receipt 写入都不能抢断迁移门。const target = this.receiptPath(receipt.id);const temp = path.join(root,diff --git a/apps/desktop/src/main/ownerNamespaceMigration.ts b/apps/desktop/src/main/ownerNamespaceMigration.tsindex d573e2cd37..0a4bad9de2 100644--- a/apps/desktop/src/main/ownerNamespaceMigration.ts+++ b/apps/desktop/src/main/ownerNamespaceMigration.ts@@ -17,6 +17,7 @@ import {type LegacyGhostRecoveryStatus,} from '../shared/legacyGhostRecovery.js';import { readLegacyGhostApprovalProjection } from './cindy-brain/GhostManager.js';+import { withGhostInstallLock } from './cindy-brain/ghostInstallLock.js';import { readBoundedFileNoFollow, readBoundedFileNoFollowSync } from './utils/readBoundedFile.js';const CLAIM_MARKER = '.owner-namespace-claim-v1.json';@@ -1649,7 +1650,13 @@ export async function recoverLegacyGhostPlugins(continue;}try {- await deps.rename(legacy.dir, target);+ // Hold the per-id install lock during rename so a concurrent+ // install/update on the same id cannot write a pending journal+ // that recovery on next launch would misidentify as an uncommitted+ // install and delete.+ await withGhostInstallLock(legacy.id, async () => {+ await deps.rename(legacy.dir, target);+ });moved += 1;movedThisRun.add(legacy.id);} catch (error) {