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): defer target-root scan until recovery candidates exist

Yuewei-LL3593497d4043
diff --git a/apps/desktop/src/main/cindy-brain/forgeScaffoldWorkerProcess.ts b/apps/desktop/src/main/cindy-brain/forgeScaffoldWorkerProcess.ts
index 97da1dcfce..b68e1faa4a 100644
--- a/apps/desktop/src/main/cindy-brain/forgeScaffoldWorkerProcess.ts
+++ b/apps/desktop/src/main/cindy-brain/forgeScaffoldWorkerProcess.ts
@@ -1,3 +1,17 @@
+/**
+ * Forge scaffold 的 cwd-bound utility worker。
+ *
+ * 防御策略(见 plugin-security-and-authoring.md §Review 清单 6.5a):
+ * mkdir(atomic no-clobber gate) → pre-check(fast fail)→ per-entry 操作 →
+ * post-loop verifyParent → dev/ino-guarded cleanup(仅 catch 失败时触发)→
+ * finally 清理 staging。
+ * per-entry 复验(lstat + isDir + isSymlink + dev/ino + realpath in bigint)
+ * 在每次 rename 前执行,缩窄 TOCTOU 窗口到单 await 边界。
+ * 最后一个 per-entry 检查与最后一个 rename 之间的间隙是 Node.js 未暴露
+ * renameat 的硬边界,不可消除——dev/ino-guarded cleanup 提供补偿控制。
+ * 再增加 pre-check 不能关闭此间隙;安全方向是保持当前的 post-loop
+ * verifyParent + identity-guarded cleanup,而不是叠加更多路径检查。
+ */
import fs from 'node:fs';
import path from 'node:path';
diff --git a/apps/desktop/src/main/cindy-brain/ghostSnapshotWorkerProcess.ts b/apps/desktop/src/main/cindy-brain/ghostSnapshotWorkerProcess.ts
index b8b9c456fe..3baef0047e 100644
--- a/apps/desktop/src/main/cindy-brain/ghostSnapshotWorkerProcess.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostSnapshotWorkerProcess.ts
@@ -1,3 +1,20 @@
+/**
+ * Skill snapshot 的 cwd-bound utility worker。
+ *
+ * 防御策略(见 plugin-security-and-authoring.md §Review 清单 6.5a):
+ * verifyParent → mkdir temp → classifyGhostDirEntry(temp) →
+ * 从 sourceDir 复制内容到 temp → matches(temp) 内容哈希 → 多层 pre-rename 复验
+ * (verifyParent + verifyDirectory + classifyGhostDirEntry + lstat absence) →
+ * 原子 rename → post-rename classifyGhostDirEntry → matches(target) → cleanup。
+ *
+ * pre-check(classifyGhostDirEntry / verifyParent / matches)只缩窄 TOCTOU
+ * 窗口;post-rename classifyGhostDirEntry + matches() 才是真正的安全判定
+ * (matches() 自身不校验 baseDir,见 ghostContentTree.ts:68)。
+ * pre-rename classifyGhostDirEntry 与 rename 之间的单 await 间隙是 Node.js
+ * 未暴露 renameat 的硬边界,不可消除——安全判定必须在操作之后做,不能靠
+ * 叠加更多 pre-check。
+ * cleanup 按 verifyParent + verifyDirectory 做身份守卫,不按 pathname 删。
+ */
import fs from 'node:fs';
import path from 'node:path';
diff --git a/apps/desktop/src/main/ownerNamespaceMigration.ts b/apps/desktop/src/main/ownerNamespaceMigration.ts
index 0a4bad9de2..2838c93312 100644
--- a/apps/desktop/src/main/ownerNamespaceMigration.ts
+++ b/apps/desktop/src/main/ownerNamespaceMigration.ts
@@ -873,7 +873,6 @@ export function getLegacyGhostRecoveryStatus(
ownerScopedLegacyGhostRootDir(root, ownerKey),
]);
const targetRoot = path.join(root, 'owners', ownerKey, 'cindy-brain');
- const targetDiscovery = scanLegacyGhostDirsInRoots([targetRoot]);
const sharedLegacyGhosts = sharedDiscovery.ghosts;
const scopedLegacyGhosts = scopedDiscovery.ghosts;
const legacyGhosts = [...sharedLegacyGhosts, ...scopedLegacyGhosts];
@@ -881,6 +880,16 @@ export function getLegacyGhostRecoveryStatus(
const recoveryMarker = recoveryMarkerRead.kind === 'ready'
? recoveryMarkerRead.marker
: null;
+ // Defer the target-root scan until we know there are legacy sources or
+ // pending recovery ids. Scanning beforehand wastes work in a healthy
+ // profile and, worse, can block the recovery-status IPC or report a
+ // spurious deferred status when an unrelated installed plugin has a
+ // corrupt ghost.json (e.g. leftover FIFO after a sync conflict).
+ const hasRecoveryCandidates =
+ legacyGhosts.length > 0 || (recoveryMarker?.pendingIds?.length ?? 0) > 0;
+ const targetDiscovery = hasRecoveryCandidates
+ ? scanLegacyGhostDirsInRoots([targetRoot])
+ : { ghosts: [], deferredIds: [], invalidIds: [], deferredRoots: [] };
const sourceDiscoveryProblemIds = new Set([
...sharedDiscovery.deferredIds,
...sharedDiscovery.invalidIds,
diff --git a/docs/dev-rules/plugin-security-and-authoring.md b/docs/dev-rules/plugin-security-and-authoring.md
index 4aceb01a1c..ec173d2279 100644
--- a/docs/dev-rules/plugin-security-and-authoring.md
+++ b/docs/dev-rules/plugin-security-and-authoring.md
@@ -435,6 +435,13 @@ topic 路由;产品层多端语义见
相对路径逐段解析 + 统一指纹格式),而不是就地 `readdir` + `isDirectory()` 或 `stat`
直读?策略差异(点开头条目算不算内容、非普通条目 throw 还是 flag)是否以显式参数
表达而不是复制一份实现?
+6.5a. **新增的路径式 worker(skill snapshot、Forge scaffold 等)是否采用
+ 「操作前检查(fast fail)→ 原子操作 → 操作后复验 → identity-guarded cleanup」
+ 模式,而不是仅靠操作前检查判定安全?** 操作后复验是否落在原子操作**之后**、
+ 无 TOCTOU 间隙的位置(如 `rename` 之后立刻 `lstat` + identity compare,而不是
+ 在 `rename` 之前加更多 `lstat`)?pre-check 与 rename 之间的间隙是 Node.js 未暴露
+ `renameat` 等 fd-relative 原语的硬边界,不可消除——因此安全判定必须在操作之后做。
+ cleanup 时是否按 `dev`/`ino` identity 守卫而非仅按 pathname 删除?
6.6. **存量插件升级后还能不能用(第 5 节红线)**:本次是否改了 receipt schema/必填
字段/落盘位置、指纹或摘要编码、manifest 校验、slot 形态、快照与链接命名、安装根或
状态根路径、`.cindy` 包格式、管子协议、内置 id?命中就逐条问:用户升级后**什么都不做**