Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): 无硬链接时的补种回退也保证不留半截文件
Claude68472aaa6fca
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.tsindex f3da78fc53..c649e6e6da 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts@@ -273,13 +273,21 @@ 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 link = vi.spyOn(fs, 'link').mockRejectedValue(Object.assign(new Error('EPERM'), { code: 'EPERM' }));+ const writeFile = vi.spyOn(fs, 'writeFile');try {expect((await migrateBotProfileFolder(root, 'bot-a', SEED)).seeded).toBe(true);+ // Crash-atomic: content is only ever written to temp files and renamed into place.+ const home = botProfileDir(root, 'bot-a');+ for (const [target] of writeFile.mock.calls) {+ expect(String(target)).toMatch(/\.tmp-/);+ }+ expect((await fs.readdir(home)).filter((name) => name.includes('.tmp-'))).toEqual([]);} finally {link.mockRestore();+ writeFile.mockRestore();}const content = await readBotProfileFolder(root, 'bot-a');expect(content.identitySource).toBe(SEED.identitySource);diff --git a/apps/desktop/src/main/maker-ipc/botProfileFolder.ts b/apps/desktop/src/main/maker-ipc/botProfileFolder.tsindex 7448766d90..ae9fd7191e 100644--- a/apps/desktop/src/main/maker-ipc/botProfileFolder.ts+++ b/apps/desktop/src/main/maker-ipc/botProfileFolder.ts@@ -219,8 +219,9 @@ async function writeTextAtomic(absPath: string, content: string): Promise<void>** 先检查再 rename 覆盖,中间有一个窗口:检查时还没有,rename 前用户刚好保存了,* 补种就会把刚保存的内容冲掉。这里把内容写进临时文件后用 link 挂到目标路径 ——- * 目标已存在时 link 直接报 EEXIST 而不是替换。不支持硬链接的文件系统退回- * `wx` 独占创建,同样绝不替换已有文件。返回是否真的创建了。+ * 目标已存在时 link 直接报 EEXIST 而不是替换,且内容已完整写好才出现在目标路径。+ * 不支持硬链接的文件系统退回「紧挨着再确认不存在 + rename」,保证不留半截文件。+ * 返回是否真的创建了。*/async function writeTextIfAbsent(absPath: string, content: string): Promise<boolean> {if (Buffer.byteLength(content, 'utf8') > BOT_PROFILE_TEXT_MAX_BYTES) {@@ -237,13 +238,20 @@ async function writeTextIfAbsent(absPath: string, content: string): Promise<boolreturn true;} catch (cause) {if ((cause as NodeJS.ErrnoException).code === 'EEXIST') return false;+ /*+ 没有硬链接的文件系统上不存在「原子且不替换」的 rename。这里优先保证崩溃原子性+ (绝不留下写到一半的文件 —— 半截 SOUL.md 会被对账当成正式身份收进数据库),+ 在 rename 前紧挨着再确认一次目标不存在;剩下的只是两次系统调用之间的窗口,+ 且只出现在这类少见的文件系统上。+ */try {- await fs.writeFile(absPath, content, { encoding: 'utf8', flag: 'wx' });- return true;- } catch (fallback) {- if ((fallback as NodeJS.ErrnoException).code === 'EEXIST') return false;- throw fallback;+ await fs.access(absPath);+ return false;+ } catch (missing) {+ if ((missing as NodeJS.ErrnoException).code !== 'ENOENT') throw missing;}+ await fs.rename(tmp, absPath);+ return true;} finally {await fs.rm(tmp, { force: true }).catch(() => {});}