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): track marker ownership and compare scaffold target identity

Yuewei-LL7e5b586863be
diff --git a/apps/desktop/src/main/cindy-brain/forgeScaffoldWorkerProcess.ts b/apps/desktop/src/main/cindy-brain/forgeScaffoldWorkerProcess.ts
index 0ed7fd0d61..97da1dcfce 100644
--- a/apps/desktop/src/main/cindy-brain/forgeScaffoldWorkerProcess.ts
+++ b/apps/desktop/src/main/cindy-brain/forgeScaffoldWorkerProcess.ts
@@ -110,25 +110,33 @@ async function run(request: Request): Promise<void> {
}
throw error;
}
+ let targetDev: bigint = 0n;
+ let targetIno: bigint = 0n;
try {
await verifyParent(request.expectedParent);
// Validate that the just-created target is still a real directory
// owned by this worker before moving entries into it. A concurrent
// process could delete the empty directory and replace it with a
// symlink/junction between mkdir and the rename loop.
- const targetStat = await fs.promises.lstat(request.targetName);
+ const targetStat = await fs.promises.lstat(request.targetName, { bigint: true });
if (!targetStat.isDirectory() || targetStat.isSymbolicLink()) {
throw new Error('Forge scaffold target replaced after mkdir');
}
const targetRealPath = await fs.promises.realpath(request.targetName);
+ targetDev = targetStat.dev;
+ targetIno = targetStat.ino;
for (const entry of await fs.promises.readdir(staging, { withFileTypes: true })) {
// Per-entry revalidation: a concurrent swap between the single-shot
// lstat check above and each rename would let path.join follow the
- // swapped link. Re-check the target identity before every file move.
- const currentStat = await fs.promises.lstat(request.targetName);
+ // swapped link. Re-check the target identity (dev/ino + realpath)
+ // before every file move.
+ const currentStat = await fs.promises.lstat(request.targetName, { bigint: true });
if (!currentStat.isDirectory() || currentStat.isSymbolicLink()) {
throw new Error('Forge scaffold target identity changed during publish');
}
+ if (currentStat.dev !== targetDev || currentStat.ino !== targetIno) {
+ throw new Error('Forge scaffold target replaced by a different directory during publish');
+ }
const currentReal = await fs.promises.realpath(request.targetName);
if (!samePath(currentReal, targetRealPath)) {
throw new Error('Forge scaffold target path changed during publish');
@@ -143,11 +151,17 @@ async function run(request: Request): Promise<void> {
} catch (error) {
// Publish failed after mkdir: the partially-populated target must be
// cleaned up so it doesn't block the next scaffold attempt.
- // Only remove a real directory — if the target was swapped for a
- // symlink/junction, rm with recursive would follow it.
+ // Only remove the directory that we created — if the target was
+ // replaced by a concurrent process (different dev/ino), leave it
+ // alone rather than destroying another process's directory.
try {
- const cleanupStat = await fs.promises.lstat(request.targetName);
- if (cleanupStat.isDirectory() && !cleanupStat.isSymbolicLink()) {
+ const cleanupStat = await fs.promises.lstat(request.targetName, { bigint: true });
+ if (
+ cleanupStat.isDirectory() &&
+ !cleanupStat.isSymbolicLink() &&
+ cleanupStat.dev === targetDev &&
+ cleanupStat.ino === targetIno
+ ) {
await fs.promises.rm(request.targetName, { recursive: true, force: true });
}
} catch {
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index c2b4b6d60f..969896fcc0 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -260,10 +260,11 @@ export class GhostInstallReceiptStore {
//
// 标记写入在 receipt 之前,receipt 写失败必须回滚标记,避免留一个
// 无 receipt 的哨兵把合法 legacy 插件永久挡在迁移之外。
+ // ensureMigrationMarker 返回 true 仅当本次调用实际创建了 marker,
+ // 因此不存在 TOCTOU:检查和创建在同一个调用内完成。
let markerWritten = false;
if (!this.hasMigrationLedger()) {
- await this.ensureMigrationMarker(receipt.id);
- markerWritten = true;
+ markerWritten = await this.ensureMigrationMarker(receipt.id);
}
const target = this.receiptPath(receipt.id);
const temp = path.join(
@@ -539,12 +540,15 @@ export class GhostInstallReceiptStore {
* 确保 per-id 迁移标记存在。receipt 初次落账时调用;标记是零字节普通文件,
* 在 migration ledger 按 completed 关闭前钉住"此 id 已进入新模型"。
*/
- async ensureMigrationMarker(id: string): Promise<void> {
+ async ensureMigrationMarker(id: string): Promise<boolean> {
// Idempotent: if the marker survived a previous failed receipt commit
// (rollback also failed), this retry must not fail with EEXIST. The
// marker's semantics ("this id entered through the new model") is
// correct regardless of whether the prior receipt write succeeded.
- if (this.hasMigrationMarker(id)) return;
+ //
+ // Returns true only when this call actually created the marker, so
+ // the caller knows whether to roll it back on receipt write failure.
+ if (this.hasMigrationMarker(id)) return false;
const root = this.rootDir();
await fs.promises.mkdir(root, { recursive: true });
const target = this.migrationMarkerPath(id);
@@ -552,6 +556,11 @@ export class GhostInstallReceiptStore {
try {
await fs.promises.writeFile(temp, '', { encoding: 'utf8', flag: 'wx', mode: 0o600 });
await fs.promises.rename(temp, target);
+ return true;
+ } catch {
+ // rename failed (e.g. target already exists from a concurrent
+ // writer) — the marker exists, but we didn't create it.
+ return false;
} finally {
await fs.promises.rm(temp, { force: true }).catch(() => undefined);
}