Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): 别的伙伴接走机器人时不再重开原来的,回滚重启失败可重试
Chris2de5b160cfc6
diff --git a/apps/desktop/src/main/bot-import/__tests__/botTakeover.test.ts b/apps/desktop/src/main/bot-import/__tests__/botTakeover.test.tsindex 7a8758791f..10b442733a 100644--- a/apps/desktop/src/main/bot-import/__tests__/botTakeover.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/botTakeover.test.ts@@ -146,6 +146,25 @@ it('finishes an interrupted rollback and re-stops a source that came back on, neexpect(t.target.connect).toHaveBeenCalledTimes(1);});+it('keeps the source off when another companion claimed the bot, and retries a rollback whose restart failed', async () => {+ const t = await setup('hermes', { name: 'Ada' }, { live: true, env: telegramEnv });+ // A concurrent import connected the bot to another companion first.+ t.target.connect.mockResolvedValueOnce('IMPORT_IM_BOT_IN_USE');+ expect(await t.run()).toBe('IMPORT_IM_BOT_IN_USE');+ expect((await t.readConfig()).platforms.telegram.enabled).toBe(false);+ expect(t.saves.at(-1)).toBeUndefined();+ // An ordinary connect failure rolls back; a failed restart keeps the rollback pending.+ const stopped = { phase: 'stopped' as const, original: { present: false }, wasLive: true, changed: true, restartRequested: true, restarted: true };+ t.target.connect.mockResolvedValueOnce('IMPORT_IM_BOT_CONNECT_FAILED');+ await fs.writeFile(path.join(t.state, 'fail-restart'), '');+ expect(await t.run(stopped)).toBe('IMPORT_IM_BOT_CONNECT_FAILED');+ expect(t.saves.at(-1)).toMatchObject({ phase: 'restoring' });+ await fs.rm(path.join(t.state, 'fail-restart'));+ expect(await t.run(t.saves.at(-1))).toBe('IMPORT_IM_BOT_CONNECT_FAILED');+ expect(t.saves.at(-1)).toBeUndefined();+ expect(t.target.connect).toHaveBeenCalledTimes(2);+});+it('leaves a bot swapped in after the preview running and does not connect the old one', async () => {const t = await setup('hermes', { name: 'Ada' }, { live: true, env: telegramEnv });await fs.writeFile(path.join(t.state, '.env'), 'TELEGRAM_BOT_TOKEN=67890:fake-new-token\nTELEGRAM_ALLOWED_USERS=424242');diff --git a/apps/desktop/src/main/bot-import/botTakeover.ts b/apps/desktop/src/main/bot-import/botTakeover.tsindex eb77faa5d2..9fee670c16 100644--- a/apps/desktop/src/main/bot-import/botTakeover.ts+++ b/apps/desktop/src/main/bot-import/botTakeover.ts@@ -125,7 +125,8 @@ export async function takeOverImportedBot(input: BotTakeoverInput): Promise<stri// The switch was on when this import turned it off.if (!(await readImportSourceBot(source, bot)).enabled) return;const { parked } = await liveness();- if ((record.restartRequested || record.restarted) && !parked) await restartGateway();+ // A failed restart keeps the `restoring` record, so the original bot is not left stopped.+ if ((record.restartRequested || record.restarted) && !parked && !(await restartGateway())) return;} catch (error) {if (error instanceof CompanionImportError && error.code === 'OWNER_CHANGED') throw error;return;@@ -138,7 +139,8 @@ export async function takeOverImportedBot(input: BotTakeoverInput): Promise<striif (record?.phase === 'restoring') { await restore(); return 'IMPORT_IM_BOT_CONNECT_FAILED'; }const existing = target.existing();if (existing === 'self') { await save({ ...(record ?? {}), phase: 'connected' }); return undefined; }- if (existing === 'other') { await restore(); return 'IMPORT_IM_BOT_IN_USE'; }+ // Another companion took the bot meanwhile: it serves it now, so the source stays off.+ if (existing === 'other') { await save(undefined); return 'IMPORT_IM_BOT_IN_USE'; }/** The switch at this path must still belong to the previewed bot before this import touches it. */const sameBot = async () => {const current = await readImportSourceBotIdentity(source, bot, readers); assertOwner();@@ -195,7 +197,8 @@ export async function takeOverImportedBot(input: BotTakeoverInput): Promise<striif ((await readImportSourceBot(source, bot)).enabled) return 'IMPORT_IM_BOT_SOURCE_STOP_UNCONFIRMED';const issue = await target.connect(); assertOwner();if (!issue) { await save({ ...record!, phase: 'connected' }); return undefined; }- await restore();+ // A concurrent import connected it to another companion; re-enabling the source would make both serve it.+ if (issue === 'IMPORT_IM_BOT_IN_USE') await save(undefined); else await restore();return issue;} catch (error) {if (error instanceof CompanionImportError && error.code === 'SOURCE_COMMAND_UNAVAILABLE') {diff --git a/apps/desktop/src/main/bot-import/sources.ts b/apps/desktop/src/main/bot-import/sources.tsindex 55ca7de78b..087c4402bd 100644--- a/apps/desktop/src/main/bot-import/sources.ts+++ b/apps/desktop/src/main/bot-import/sources.ts@@ -494,12 +494,12 @@ export function importBotIdentity(credential: unknown): string {return BOT_IDENTITY_FIELDS.map(field => string(value[field])).find(Boolean) ?? '';}-/** The bot's current identity in its source, re-read with the source's own environment; '' when it is gone. */+/** The bot's current identity in its source, re-read with the source's own environment; '' when it is gone or routed elsewhere. */export async function readImportSourceBotIdentity(source: ImportSource, bot: NonNullable<ImportItem['bot']>, deps: SourceReaderDeps): Promise<string> {const budget = createImportBudget();const values = await config(path.dirname(source.configFile), source.configFile, budget);const env = await sourceEnvironment(source, values, budget);- const found = (await sourceBots(source, values, env, deps, budget, optionalText, true))+ const found = (await sourceBots(source, values, env, deps, budget)).find(candidate => candidate.platform === bot.platform && candidate.account === bot.account);if (!found) return '';return importBotIdentity(Object.fromEntries(Object.entries(found.credential).map(([field, value]) =>diff --git a/apps/desktop/src/main/bot-import/transfer.ts b/apps/desktop/src/main/bot-import/transfer.tsindex abf3b4c361..7a65eb5842 100644--- a/apps/desktop/src/main/bot-import/transfer.ts+++ b/apps/desktop/src/main/bot-import/transfer.ts@@ -73,6 +73,8 @@ export const IMPORT_IM_BOT_ISSUES: ReadonlySet<string> = new Set(['IMPORT_IM_BOT_CONNECT_FAILED','IMPORT_IM_BOT_SOURCE_STOP_UNCONFIRMED','IMPORT_IM_BOT_SOURCE_CHANGED',+ // The source CLI is missing: the bot credential is saved, only its source stop did not run.+ 'SOURCE_COMMAND_UNAVAILABLE',]);const IM_BOT_FORMATS = new Set(['telegram', 'discord']);