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): add per-id migration marker to guard first receipt writes

Yuewei-LLc1d00dea68a2
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index 2d42fd53fb..fe42029825 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -1319,6 +1319,12 @@ export class GhostManager {
blockedByPendingJournal.push(id);
continue;
}
+ // Per-id 迁移标记:新模型 receipt 落账时同步写入。无 receipt 但有标记 = 新模型
+ // 安装后 receipt 被删,不是 legacy —— 绝不从可变安装目录重铸批准。
+ if (this.receiptStore.hasMigrationMarker(id)) {
+ result.skipped.push(id);
+ continue;
+ }
candidates.push(id);
}
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 020613d2da..227c0d9920 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -1270,13 +1270,13 @@ describe('GhostManager · review 第 6 轮回归(P0/P1 修复钉住)', () => {
// 攻击:删掉 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 攻击面略宽(仍需写状态根)。
+ // Per-id migration marker prevents backfill. Without the marker, the
+ // coordinator would re-approve from the current mutable directory. With
+ // the marker, the system knows this was a new-model install whose receipt
+ // was deleted — not a legacy install. It stays fail-closed.
const outcome = await manager.migrateLegacyApprovalsOnce();
- expect(outcome.migrated).toEqual(['hello']);
- expect(manager.list()[0].approval.state).toBe('approved');
+ expect(outcome).toEqual({ migrated: [], skipped: ['hello'], failed: [], retryPending: [] });
+ expect(manager.list()[0].approval.state).toBe('legacy-unapproved');
});
it('P0-2:安装不自动落 migration ledger —— 落账失败也不影响安装', async () => {
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index a00156efd3..2c027c42e2 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -254,9 +254,12 @@ export class GhostInstallReceiptStore {
// 扫描因瞬时故障失败、且 builtin/市场写入首份 receipt 后永久关闭迁移门,使所有
// 存量插件变成 legacy-unapproved(P0 红线)。
//
- // “有效 receipt + 无 ledger”的 mixed 状态由 migrateLegacyApprovalsOnce 的扫描-跳过
- // 逻辑安全处理:已有 receipt 的 id 会被跳过,其余 legacy 目录照常迁移;全数落定后
- // 才由 coordinator 统一写 completed。任何先于迁移的 receipt 写入都不能抢断迁移门。
+ // 代之以 per-id 迁移标记:receipt 初次落账时同步写 `.migrated-<id>` 哨兵文件。
+ // coordinator 扫描时见到”无 receipt + 有哨兵”就跳过 —— 这是新模型装的插件、
+ // 不是 legacy,删掉 receipt 不能骗到一次从可变目录重铸批准。
+ if (!this.hasMigrationLedger()) {
+ await this.ensureMigrationMarker(receipt.id);
+ }
const target = this.receiptPath(receipt.id);
const temp = path.join(
root,
@@ -502,6 +505,42 @@ export class GhostInstallReceiptStore {
}
}
+ /**
+ * Per-id migration marker path. 与 receipt 同位(状态根内),coordinator 扫描
+ * 时用它区分"安装了 receipt 之前就是 legacy"与"新模型安装后 receipt 被删"。
+ */
+ private migrationMarkerPath(id: string): string {
+ if (!isValidGhostId(id)) throw new Error('invalid ghost id for migration marker path');
+ return path.join(this.rootDir(), `.migrated-${id}`);
+ }
+
+ /**
+ * 确保 per-id 迁移标记存在。receipt 初次落账时调用;标记是零字节普通文件,
+ * 在 migration ledger 按 completed 关闭前钉住"此 id 已进入新模型"。
+ */
+ async ensureMigrationMarker(id: string): Promise<void> {
+ const root = this.rootDir();
+ await fs.promises.mkdir(root, { recursive: true });
+ const target = this.migrationMarkerPath(id);
+ const temp = path.join(root, `.migrated-${id}-${process.pid}-${crypto.randomBytes(4).toString('hex')}.tmp`);
+ try {
+ await fs.promises.writeFile(temp, '', { encoding: 'utf8', flag: 'wx', mode: 0o600 });
+ await fs.promises.rename(temp, target);
+ } finally {
+ await fs.promises.rm(temp, { force: true }).catch(() => undefined);
+ }
+ }
+
+ /** 是否存在 per-id 迁移标记(表明此 id 是通过新模型安装,而非 legacy)。 */
+ hasMigrationMarker(id: string): boolean {
+ try {
+ fs.lstatSync(this.migrationMarkerPath(id));
+ return true;
+ } catch {
+ return false;
+ }
+ }
+
async writeMigrationLedger(ledger: GhostLegacyMigrationLedger): Promise<void> {
const root = this.rootDir();
await fs.promises.mkdir(root, { recursive: true });