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(bots): 无硬链接时改为确认不存在后 rename 补种,不再放弃补种

Claude6a053560e9fc
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
index 59afc32918..78b96d821f 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
@@ -918,7 +918,7 @@ describe('Bot canonical Session lifecycle', () => {
expect(readFileSync(join(home, 'SOUL.md'), 'utf8')).toBe('Stored identity');
});
- it('keeps the stored identity when the Home cannot be seeded without hard links', async () => {
+ it('seeds a missing SOUL.md from the database on a filesystem without hard links, without deriving', async () => {
const created = await invoke('local-db:bots:create', {
id: 'no-hardlinks', name: 'No Hardlinks', identitySource: 'Stored identity',
userContextSource: 'Stored user context',
@@ -934,11 +934,11 @@ describe('Bot canonical Session lifecycle', () => {
} finally {
link.mockRestore();
}
- // The database stays authoritative: no version is derived from a Home that could not be seeded.
expect(await invoke('local-db:bots:get', created.id)).toMatchObject({
currentVersion: 1, identitySource: 'Stored identity', userContextSource: 'Stored user context',
});
- expect(existsSync(join(home, 'SOUL.md'))).toBe(false);
+ expect(readFileSync(join(home, 'SOUL.md'), 'utf8')).toBe('Stored identity');
+ expect(readFileSync(join(home, 'memories', 'USER.md'), 'utf8')).toBe('Stored user context');
});
it('seeds a never-created Bot Home on an unrelated save without touching an existing one', async () => {
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts
index 8edae31a2e..08234b9808 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts
@@ -273,16 +273,13 @@ describe('搬家', () => {
expect((await fs.readdir(path.join(home, 'memories'))).filter((name) => name.includes('.tmp-'))).toEqual([]);
});
- it('不支持硬链接的文件系统不补种:不写目标文件,已有文件不动,不留临时文件,技能照常搬', async () => {
+ it('不支持硬链接的文件系统确认不存在后 rename 补种:只补缺失的文件,不直接写目标,不留临时文件', async () => {
await writeBotProfileFolder(root, 'bot-a', { userContextSource: '用户手改的档案' });
- const legacy = path.join(root, 'bot-skills', 'bot-a');
- await fs.mkdir(path.join(legacy, 'skills', 's1'), { recursive: true });
- await fs.writeFile(path.join(legacy, 'skills', 's1', 'SKILL.md'), 'v1', 'utf8');
const link = vi.spyOn(fs, 'link').mockRejectedValue(Object.assign(new Error('EPERM'), { code: 'EPERM' }));
const writeFile = vi.spyOn(fs, 'writeFile');
try {
- await expect(migrateBotProfileFolder(root, 'bot-a', SEED)).rejects.toMatchObject({ code: 'EPERM' });
- // Nothing is ever written straight onto a target path.
+ expect((await migrateBotProfileFolder(root, 'bot-a', SEED)).seeded).toBe(true);
+ // Content only ever reaches a target path complete, through a rename.
for (const [target] of writeFile.mock.calls) {
expect(String(target)).toMatch(/\.tmp-/);
}
@@ -290,14 +287,37 @@ describe('搬家', () => {
link.mockRestore();
writeFile.mockRestore();
}
+ const content = await readBotProfileFolder(root, 'bot-a');
+ expect(content.identitySource).toBe(SEED.identitySource);
+ expect(content.userContextSource).toBe('用户手改的档案');
+ expect(content.config).toEqual(SEED.config);
const home = botProfileDir(root, 'bot-a');
- await expect(fs.access(path.join(home, 'SOUL.md'))).rejects.toBeTruthy();
- expect((await readBotProfileFolder(root, 'bot-a')).userContextSource).toBe('用户手改的档案');
const leftovers = [...await fs.readdir(home), ...await fs.readdir(path.join(home, 'memories'))]
.filter((name) => name.includes('.tmp-'));
expect(leftovers).toEqual([]);
- // Skills still move into the Home even though seeding could not run.
- expect(await fs.readFile(path.join(home, 'skills', 's1', 'SKILL.md'), 'utf8')).toBe('v1');
+ });
+
+ it('不支持硬链接且 rename 失败时不留半截目标,下一次补种照常补齐', async () => {
+ const link = vi.spyOn(fs, 'link').mockRejectedValue(Object.assign(new Error('EPERM'), { code: 'EPERM' }));
+ const realRename = fs.rename.bind(fs);
+ const rename = vi.spyOn(fs, 'rename').mockImplementation(async (from, to) => {
+ if (String(to).endsWith('SOUL.md')) throw Object.assign(new Error('EIO'), { code: 'EIO' });
+ return realRename(from, to);
+ });
+ const home = botProfileDir(root, 'bot-a');
+ try {
+ await expect(migrateBotProfileFolder(root, 'bot-a', SEED)).rejects.toMatchObject({ code: 'EIO' });
+ } finally {
+ rename.mockRestore();
+ }
+ try {
+ await expect(fs.access(path.join(home, 'SOUL.md'))).rejects.toBeTruthy();
+ expect((await fs.readdir(home)).filter((name) => name.includes('.tmp-'))).toEqual([]);
+ expect((await migrateBotProfileFolder(root, 'bot-a', SEED)).seeded).toBe(true);
+ } finally {
+ link.mockRestore();
+ }
+ expect((await readBotProfileFolder(root, 'bot-a')).identitySource).toBe(SEED.identitySource);
});
it('SOUL.md 之外的槽写失败时不落 SOUL.md,下一次仍会补齐,而不是把缺失的 USER.md 当成清空', async () => {
diff --git a/apps/desktop/src/main/maker-ipc/botProfileFolder.ts b/apps/desktop/src/main/maker-ipc/botProfileFolder.ts
index 11f6e77191..322054202e 100644
--- a/apps/desktop/src/main/maker-ipc/botProfileFolder.ts
+++ b/apps/desktop/src/main/maker-ipc/botProfileFolder.ts
@@ -215,16 +215,17 @@ async function writeTextAtomic(absPath: string, content: string): Promise<void>
}
/**
- * 原子的「不存在才创建」:补种不能覆盖用户此刻正在编辑器里保存的文件,也不能留下
- * 写到一半的文件(半截 SOUL.md 会被对账当成正式身份收进数据库)。
+ * 「不存在才创建」:补种不能覆盖用户已有的文件,也不能留下写到一半的文件(半截
+ * SOUL.md 会被对账当成正式身份收进数据库)。内容总是先完整写进临时文件,目标路径上
+ * 只会出现完整内容。返回是否真的创建了。
*
- * 内容写进临时文件后用 link 挂到目标路径 —— 目标已存在时 link 报 EEXIST 而不是
- * 替换,且内容完整后才出现在目标路径,两条都满足。返回是否真的创建了。
+ * 常见文件系统:用 link 把临时文件挂到目标 —— 目标已存在时报 EEXIST 而不是替换,
+ * 原子且不替换。
*
- * 没有硬链接的文件系统上不存在「原子且不替换」的写法(Node 不提供 NOREPLACE 的
- * rename),任何模拟都要在「可能替换用户文件」和「只能猜文件是不是写到一半」之间
- * 二选一。所以这里不模拟:link 的其他错误原样抛出,这次不补种。数据库仍是正本,
- * 设置页的显式修改照常写文件,下一次对账再试。
+ * 不支持硬链接的文件系统(exFAT、部分网络盘):Node 不提供 NOREPLACE 的 rename,不存在
+ * 既原子又不替换的写法。这里确认目标不存在后 rename:崩溃时目标要么不存在要么完整,
+ * 不需要任何「写到一半」的猜测与恢复;代价是确认与 rename 两次系统调用之间若有人恰好
+ * 新建了同名文件会被替换 —— 只在这类文件系统、只在该文件本来缺失时存在。
*/
async function writeTextIfAbsent(absPath: string, content: string): Promise<boolean> {
if (Buffer.byteLength(content, 'utf8') > BOT_PROFILE_TEXT_MAX_BYTES) {
@@ -237,11 +238,20 @@ async function writeTextIfAbsent(absPath: string, content: string): Promise<bool
const tmp = `${absPath}.tmp-${process.pid}-${(writeSeq += 1)}`;
try {
await fs.writeFile(tmp, content, 'utf8');
- await fs.link(tmp, absPath);
+ try {
+ await fs.link(tmp, absPath);
+ return true;
+ } catch (cause) {
+ if ((cause as NodeJS.ErrnoException).code === 'EEXIST') return false;
+ }
+ try {
+ await fs.access(absPath);
+ return false;
+ } catch (cause) {
+ if ((cause as NodeJS.ErrnoException).code !== 'ENOENT') throw cause;
+ }
+ await fs.rename(tmp, absPath);
return true;
- } catch (cause) {
- if ((cause as NodeJS.ErrnoException).code === 'EEXIST') return false;
- throw cause;
} finally {
await fs.rm(tmp, { force: true }).catch(() => {});
}
@@ -398,8 +408,8 @@ export async function migrateBotProfileFolder(
}
}
- // Skills move regardless: a Home that cannot be seeded (e.g. no hard links) must not
- // also strand the Bot's legacy skills.
+ // Skills move regardless: a seeding failure (e.g. disk full) must not also strand the
+ // Bot's legacy skills.
const skillsMoved = await migrateBotSkillsIntoProfileFolder(userDataDir, botId, legacyUserDataDir);
if (seedFailure !== null) throw seedFailure;
return { seeded, skillsMoved };