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(pi): wx 创建锁后写 pid 失败时清掉空锁并放弃落盘(review P2)

Zhang Yunjin98784c52c80f
diff --git a/packages/maker-core/src/agents/pi/__tests__/mcpDisclosureFingerprint.test.ts b/packages/maker-core/src/agents/pi/__tests__/mcpDisclosureFingerprint.test.ts
index b6a12e198d..509e5d5f18 100644
--- a/packages/maker-core/src/agents/pi/__tests__/mcpDisclosureFingerprint.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/mcpDisclosureFingerprint.test.ts
@@ -49,6 +49,8 @@ function loadGateway(options: {
killErrorCode?: string;
/** 模拟回收残留锁时被另一个实例抢先(rename 失败)。 */
reclaimLostRace?: boolean;
+ /** 模拟 wx 创建锁后写入 pid 失败(磁盘满等)。 */
+ lockPidWriteFails?: boolean;
/** 模拟挪走的残留锁内容其实是另一个实例重建的活锁(pid)。 */
reclaimReplacesWith?: number;
} = {}) {
@@ -107,6 +109,9 @@ function loadGateway(options: {
writeFileSync: (file: string, body: string) => {
if (options.writeFails) throw Object.assign(new Error('denied'), { code: 'EACCES' });
if (file.endsWith('.lock')) {
+ if (options.lockPidWriteFails && lockFile.value === '') {
+ throw Object.assign(new Error('no space'), { code: 'ENOSPC' });
+ }
lockFile.value = body; // 记录持锁 pid
return;
}
@@ -351,6 +356,22 @@ describe('MCP gateway schema disclosure', () => {
expect(lockOwnerAfter()).toBe('777');
});
+ it('cleans up an empty lock when writing the pid fails, and recovers on the next call', async () => {
+ // wx 创建锁后写 pid 失败(磁盘满):空锁 owner 读不出来会被永远拒绝回收,
+ // 必须当场清掉并放弃本次落盘;下次调用(磁盘恢复)重新获取并正常落盘。
+ const { call, requests, disk, lockOwnerAfter } = loadGateway({ lockPidWriteFails: true });
+ await call();
+ expect(requests).toHaveLength(1);
+ expect(disk.value).toBeNull();
+ expect(lockOwnerAfter()).toBeNull();
+
+ // 磁盘恢复(桩不再抛):下一次调用正常获取锁并落盘。
+ const second = loadGateway();
+ await second.call();
+ expect(second.requests).toHaveLength(1);
+ expect(second.disk.value?.entries['cindy_orcasend_to_worker']).toBe(fingerprint(TOOL));
+ });
+
it('keeps another instance newer fingerprint when persisting a different tool', async () => {
// A 启动时载入 X 的旧指纹;另一实例随后更新 X 并落盘新指纹(外部写入磁盘);
// A 之后只为 Y 落盘。磁盘优先 + 只覆盖本次键:X 必须保持新指纹,不能被 A 内存
diff --git a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
index 5e1aacdc14..2d10b42063 100644
--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
@@ -2883,8 +2883,15 @@ function acquireDisclosureLock(lockPath: string): boolean {
try {
// fd 只参与 wx 独占创建这一步;锁语义是文件存在性 + pid 内容,写完即可释放。
const fd = openSync(lockPath, 'wx', 0o600);
- try { writeFileSync(lockPath, String(process.pid)); } catch { /* pid 写不进去时只当普通锁 */ }
+ let pidWritten = false;
+ try { writeFileSync(lockPath, String(process.pid)); pidWritten = true; } catch { /* 磁盘满等 */ }
try { closeSync(fd); } catch { /* 关闭失败不影响已持有的锁 */ }
+ if (!pidWritten) {
+ // 空锁谁也无法认领(owner 读不出来会被永远拒绝回收),留在路径上就是死锁。
+ // 删掉刚建的空锁、放弃本次落盘;下次调用(磁盘恢复或重启后)重新走获取。
+ try { rmSync(lockPath, { force: true }); } catch { /* 下次写入再清 */ }
+ return false;
+ }
return true;
} catch { /* 锁已存在 → 走接管判定 */ }
const owner = readLockOwner(lockPath);