Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): 补种档案改用原子的不存在才创建;恢复伙伴时补投独立于例行任务
Claude96c8227a539e
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.tsindex 0dd8c0dbd3..f3da78fc53 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts@@ -9,7 +9,7 @@import { promises as fs } from 'node:fs';import os from 'node:os';import path from 'node:path';-import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import {BOT_PROFILE_TEXT_MAX_BYTES,@@ -251,6 +251,42 @@ describe('搬家', () => {expect(content.config).toEqual(SEED.config);});+ it('检查之后用户刚好保存了文件:补种原子地让路,不覆盖刚保存的内容', async () => {+ await writeBotProfileFolder(root, 'bot-a', {+ identitySource: '编辑器刚保存的灵魂',+ userContextSource: '编辑器刚保存的档案',+ });+ // The existence check still saw nothing: the save landed right after it.+ const access = vi.spyOn(fs, 'access').mockRejectedValueOnce(Object.assign(new Error('ENOENT'), { code: 'ENOENT' }));+ try {+ const result = await migrateBotProfileFolder(root, 'bot-a', SEED);+ expect(result.seeded).toBe(false);+ } finally {+ access.mockRestore();+ }+ const content = await readBotProfileFolder(root, 'bot-a');+ expect(content.identitySource).toBe('编辑器刚保存的灵魂');+ expect(content.userContextSource).toBe('编辑器刚保存的档案');+ // No temp files are left in the user's folder.+ const home = botProfileDir(root, 'bot-a');+ expect((await fs.readdir(home)).filter((name) => name.includes('.tmp-'))).toEqual([]);+ expect((await fs.readdir(path.join(home, 'memories'))).filter((name) => name.includes('.tmp-'))).toEqual([]);+ });++ it('不支持硬链接的文件系统退回独占创建,同样只补缺失的文件', async () => {+ await writeBotProfileFolder(root, 'bot-a', { userContextSource: '用户手改的档案' });+ const link = vi.spyOn(fs, 'link').mockRejectedValue(Object.assign(new Error('EPERM'), { code: 'EPERM' }));+ try {+ expect((await migrateBotProfileFolder(root, 'bot-a', SEED)).seeded).toBe(true);+ } finally {+ link.mockRestore();+ }+ const content = await readBotProfileFolder(root, 'bot-a');+ expect(content.identitySource).toBe(SEED.identitySource);+ expect(content.userContextSource).toBe('用户手改的档案');+ expect(content.config).toEqual(SEED.config);+ });+it('技能从旧目录整体搬进新家,内容与 slug 都不变', async () => {const legacy = path.join(root, 'bot-skills', 'bot-a');await fs.mkdir(path.join(legacy, 'skills', 'weekly-report'), { recursive: true });diff --git a/apps/desktop/src/main/maker-ipc/botProfileFolder.ts b/apps/desktop/src/main/maker-ipc/botProfileFolder.tsindex 232be019c9..7448766d90 100644--- a/apps/desktop/src/main/maker-ipc/botProfileFolder.ts+++ b/apps/desktop/src/main/maker-ipc/botProfileFolder.ts@@ -214,6 +214,41 @@ async function writeTextAtomic(absPath: string, content: string): Promise<void>}}+/**+ * 原子的「不存在才创建」:补种不能覆盖用户此刻正在编辑器里保存的文件。+ *+ * 先检查再 rename 覆盖,中间有一个窗口:检查时还没有,rename 前用户刚好保存了,+ * 补种就会把刚保存的内容冲掉。这里把内容写进临时文件后用 link 挂到目标路径 ——+ * 目标已存在时 link 直接报 EEXIST 而不是替换。不支持硬链接的文件系统退回+ * `wx` 独占创建,同样绝不替换已有文件。返回是否真的创建了。+ */+async function writeTextIfAbsent(absPath: string, content: string): Promise<boolean> {+ if (Buffer.byteLength(content, 'utf8') > BOT_PROFILE_TEXT_MAX_BYTES) {+ throw new BotProfileFolderError(+ 'TEXT_TOO_LARGE',+ `content exceeds ${BOT_PROFILE_TEXT_MAX_BYTES} bytes`,+ );+ }+ await fs.mkdir(path.dirname(absPath), { recursive: true });+ const tmp = `${absPath}.tmp-${process.pid}-${(writeSeq += 1)}`;+ await fs.writeFile(tmp, content, 'utf8');+ try {+ await fs.link(tmp, absPath);+ return true;+ } catch (cause) {+ if ((cause as NodeJS.ErrnoException).code === 'EEXIST') return false;+ 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;+ }+ } finally {+ await fs.rm(tmp, { force: true }).catch(() => {});+ }+}+/** 一份摊开的伙伴档案。 */export interface BotProfileFolderContent {/** 身份(SOUL.md)。 */@@ -347,16 +382,13 @@ export async function migrateBotProfileFolder(try {await fs.access(soulPath);} catch {- // Seed only what is missing: a hand-edited USER.md or config.json left next to- // a missing SOUL.md is still the user's content and must not be reset.- const exists = (relative: string) =>- fs.access(resolveInside(userDataDir, botId, relative)).then(() => true, () => false);- await writeBotProfileFolder(userDataDir, botId, {- identitySource: seed.identitySource,- ...((await exists(SLOT.userContext)) ? {} : { userContextSource: seed.userContextSource }),- ...((await exists(SLOT.config)) ? {} : { config: seed.config }),- });- seeded = true;+ // Seed only what is missing, each slot with an atomic create-if-absent: a hand-edited+ // USER.md or config.json next to a missing SOUL.md — or a file the user saves in an+ // editor at this very moment — is the user's content and must not be reset.+ const at = (relative: string) => resolveInside(userDataDir, botId, relative);+ seeded = await writeTextIfAbsent(soulPath, seed.identitySource);+ await writeTextIfAbsent(at(SLOT.userContext), seed.userContextSource);+ await writeTextIfAbsent(at(SLOT.config), `${JSON.stringify(seed.config, null, 2)}\n`);}const skillsMoved = await migrateBotSkillsIntoProfileFolder(userDataDir, botId, legacyUserDataDir);diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.tsindex a6ea15e806..6469ec43f8 100644--- a/apps/desktop/src/main/maker-ipc/register.ts+++ b/apps/desktop/src/main/maker-ipc/register.ts@@ -9922,10 +9922,11 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)getDelegationService: () => botDelegationServiceHolder,onPaused: (botId) => updateBotRoutineLifecycle(botId, 'pause'),onResumed: async (botId) => {- await updateBotRoutineLifecycle(botId, 'resume');- // Task results that finished while the teammate was paused were held, not retried;- // the service retries its own transient failures.+ // Task results that finished while the teammate was paused were held, not retried.+ // Start their delivery first and independently: a routine-engine failure below must+ // not strand them until the next launch. The service retries its own failures.void botDelegationServiceHolder?.resumeCompletionDelivery(botId);+ await updateBotRoutineLifecycle(botId, 'resume');},onBeforeDelete: (botId) => updateBotRoutineLifecycle(botId, 'delete'),});