Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): 补种写入失败时保留恢复标记,对账读取前先收尾
Claude3359a9327c02
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.tsindex 6e20bbac15..088b5972e9 100644--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts@@ -918,6 +918,23 @@ describe('Bot canonical Session lifecycle', () => {expect(readFileSync(join(home, 'SOUL.md'), 'utf8')).toBe('Stored identity');});+ it('completes an interrupted seed before reconcile reads it, instead of absorbing a truncated SOUL.md', async () => {+ const created = await invoke('local-db:bots:create', {+ id: 'interrupted-seed', name: 'Interrupted Seed', identitySource: 'Stored identity',+ });+ const home = join(h.userDataDir, createHash('sha256').update(h.ownerScopeKey).digest('hex'), 'bots', created.id);+ // A seed that failed mid-write on a filesystem without hard links.+ writeFileSync(join(home, 'SOUL.md'), 'Stored');+ writeFileSync(join(home, 'SOUL.md.seeding'), 'Stored identity');+ const canonical = await invoke('local-db:bots:create-canonical-session', {+ botId: created.id, expectedCanonicalSessionId: null, expectedProfileVersion: 1,+ });+ expect(canonical.canonicalSessionId).toBeTruthy();+ expect(await invoke('local-db:bots:get', created.id)).toMatchObject({ currentVersion: 1, identitySource: 'Stored identity' });+ expect(readFileSync(join(home, 'SOUL.md'), 'utf8')).toBe('Stored identity');+ expect(existsSync(join(home, 'SOUL.md.seeding'))).toBe(false);+ });+it('seeds a never-created Bot Home on an unrelated save without touching an existing one', async () => {const created = await invoke('local-db:bots:create', {id: 'legacy-home', name: 'Legacy Home', identitySource: 'Stored identity',diff --git a/apps/desktop/src/main/localDb/ipc/bots.ts b/apps/desktop/src/main/localDb/ipc/bots.tsindex 90a6cfa98c..8c88c0e16d 100644--- a/apps/desktop/src/main/localDb/ipc/bots.ts+++ b/apps/desktop/src/main/localDb/ipc/bots.ts@@ -52,6 +52,7 @@ import {botProfileDir,ensureBotWorkspaceDir,migrateBotProfileFolder,+ recoverInterruptedBotProfileSeed,readBotProfileFolder,writeBotProfileFolder,} from '../../maker-ipc/botProfileFolder.js';@@ -341,7 +342,10 @@ export async function reconcileBotProfileFolder(currentVersion: profile.currentVersion,};},- readFolder: (id) => readBotProfileFolder(userDataDir, id),+ readFolder: async (id) => {+ await recoverInterruptedBotProfileSeed(userDataDir, id);+ return readBotProfileFolder(userDataDir, id);+ },// 播种顺带把技能从旧目录搬进来 —— 存量伙伴第一次走到这里时一并完成。seedFolder: async (id, seed) => {await migrateBotProfileFolder(userDataDir, id, seed, legacyUserDataDir);diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.tsindex daadda62a0..c57f6d506a 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/botProfileFolder.test.ts@@ -315,6 +315,34 @@ describe('搬家', () => {expect((await readBotProfileFolder(root, 'bot-a')).identitySource).toBe('编辑器刚保存的灵魂');});+ it('无硬链接补种写到一半失败时保留恢复标记,下一次补种把截断的文件补全', async () => {+ const link = vi.spyOn(fs, 'link').mockRejectedValue(Object.assign(new Error('EPERM'), { code: 'EPERM' }));+ const realOpen = fs.open.bind(fs);+ const open = vi.spyOn(fs, 'open').mockImplementationOnce(async (target, flags) => {+ const handle = await realOpen(target, flags);+ return Object.assign(Object.create(handle), {+ writeFile: async (data: string) => {+ await handle.writeFile(data.slice(0, 2));+ throw Object.assign(new Error('ENOSPC'), { code: 'ENOSPC' });+ },+ sync: () => handle.sync(),+ close: () => handle.close(),+ });+ });+ const home = botProfileDir(root, 'bot-a');+ try {+ await expect(migrateBotProfileFolder(root, 'bot-a', SEED)).rejects.toMatchObject({ code: 'ENOSPC' });+ // The truncated SOUL.md still has its marker to recover from.+ expect(await fs.readFile(path.join(home, 'SOUL.md.seeding'), 'utf8')).toBe(SEED.identitySource);+ } finally {+ open.mockRestore();+ link.mockRestore();+ }+ await migrateBotProfileFolder(root, 'bot-a', SEED);+ expect((await readBotProfileFolder(root, 'bot-a')).identitySource).toBe(SEED.identitySource);+ await expect(fs.access(path.join(home, 'SOUL.md.seeding'))).rejects.toBeTruthy();+ });+it('被中断的无硬链接补种:截断的文件补全,中断后用户改过的原样保留', async () => {const home = botProfileDir(root, 'bot-a');await fs.mkdir(path.join(home, 'memories'), { recursive: true });diff --git a/apps/desktop/src/main/maker-ipc/botProfileFolder.ts b/apps/desktop/src/main/maker-ipc/botProfileFolder.tsindex a43cfa3a39..74497e8669 100644--- a/apps/desktop/src/main/maker-ipc/botProfileFolder.ts+++ b/apps/desktop/src/main/maker-ipc/botProfileFolder.ts@@ -259,6 +259,8 @@ async function createExclusiveRecoverable(absPath: string, content: string): Proconst run = previous.catch(() => undefined).then(async () => {const marker = seedingMarker(absPath);await fs.writeFile(marker, content, 'utf8');+ let created = false;+ let completed = false;try {let handle: Awaited<ReturnType<typeof fs.open>>;try {@@ -267,15 +269,19 @@ async function createExclusiveRecoverable(absPath: string, content: string): Proif ((cause as NodeJS.ErrnoException).code === 'EEXIST') return false;throw cause;}+ created = true;try {await handle.writeFile(content, 'utf8');await handle.sync();} finally {await handle.close();}+ completed = true;return true;} finally {- await fs.rm(marker, { force: true }).catch(() => {});+ // A target we created but could not finish (ENOSPC, I/O error) keeps its marker,+ // so the next read or seed completes it instead of trusting a truncated file.+ if (!created || completed) await fs.rm(marker, { force: true }).catch(() => {});}});exclusiveSeedTails.set(absPath, run);@@ -286,6 +292,16 @@ async function createExclusiveRecoverable(absPath: string, content: string): Pro}}+/**+ * 读 Home 之前先收尾被中断的补种:对账在决定要不要补种之前就会读文件,+ * 一个截断但非空的 SOUL.md 不能被当成正式身份收进数据库。+ */+export async function recoverInterruptedBotProfileSeed(userDataDir: string, botId: string): Promise<void> {+ for (const slot of [SLOT.soul, SLOT.userContext, SLOT.config]) {+ await recoverInterruptedSeed(resolveInside(userDataDir, botId, slot));+ }+}+/*** 收尾一次被中断的无硬链接补种:标记里是当时要写的完整内容。目标恰好是它的前缀* (空或截断)说明写到一半,补全;否则是中断之后用户自己写的,原样保留。标记总是删掉。@@ -442,7 +458,7 @@ export async function migrateBotProfileFolder(const soulPath = resolveInside(userDataDir, botId, SLOT.soul);const at = (relative: string) => resolveInside(userDataDir, botId, relative);// Finish any seeding a crash interrupted before deciding what is missing.- for (const slot of [SLOT.soul, SLOT.userContext, SLOT.config]) await recoverInterruptedSeed(at(slot));+ await recoverInterruptedBotProfileSeed(userDataDir, botId);let seeded = false;try {await fs.access(soulPath);