diff --git a/plugins/minimax-ai/minimax-bot/.minimax-plugin/plugin.json b/plugins/minimax-ai/minimax-bot/.minimax-plugin/plugin.json index e08811fe..b6cfa02e 100644 --- a/plugins/minimax-ai/minimax-bot/.minimax-plugin/plugin.json +++ b/plugins/minimax-ai/minimax-bot/.minimax-plugin/plugin.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "name": "minimax-bot", "displayName": "MiniMax Bot", - "version": "0.7.28", + "version": "0.7.30", "description": "Turn MiniMax Code into an always-on team of Bots. A Chief Bot helps you build the team; in group chats the admin answers and @mentions hand work to other members. Every Bot has its own octopus avatar and memory, with scheduled tasks and usage stats.", "author": "MiniMax Code", "icon": "icon.png", diff --git a/plugins/minimax-ai/minimax-bot/README.md b/plugins/minimax-ai/minimax-bot/README.md index fd2d8c50..98632125 100644 --- a/plugins/minimax-ai/minimax-bot/README.md +++ b/plugins/minimax-ai/minimax-bot/README.md @@ -100,6 +100,11 @@ The hand-over between two Bots of a group carried the tail of the previous reply - **Start new chat**, deleting a Bot and deleting a group all empty the transcript and then write the state that forgets the chat. When that state write failed, the empty transcript was already on disk: the request reported a failure, the chat was still listed, and its history was gone for good. - The state is now written first, and the in-memory session entry is dropped only after that write succeeded. A failure leaves the chat, its messages and its session cursor as they were, so the same action can simply be tried again. +## 0.7.30 A create that fails does not leave the thing behind + +- Creating a Bot, a group or a scheduled task writes into the live state and then saves it. When that save failed, the new object stayed in memory: the request reported a failure while the Bot, group or task was already there, and the next unrelated save — changing a setting, any other write — published it to disk. After a restart the thing the user was told had failed was in the team. +- A failed save now puts the state back: a new object is removed and a changed schedule returns to the values still on disk, so a refusal leaves nothing for a later save to publish. This is the rule `updateBot` already followed; the three create paths now do the same. + ## 0.7.10 Optional group notes - A group can keep a note of what it said. After every round the group records the closing lines of the replies, and every Bot of the group gets that record in its standing brief. diff --git a/plugins/minimax-ai/minimax-bot/miniapp/node/mcp.mjs b/plugins/minimax-ai/minimax-bot/miniapp/node/mcp.mjs index 74d9def9..768cfd99 100644 --- a/plugins/minimax-ai/minimax-bot/miniapp/node/mcp.mjs +++ b/plugins/minimax-ai/minimax-bot/miniapp/node/mcp.mjs @@ -662,7 +662,7 @@ export function createMcpHandler({ team, engine }) { result = { protocolVersion: PROTOCOL_VERSIONS.includes(requested) ? requested : PROTOCOL_VERSIONS[0], capabilities: { tools: { listChanged: false } }, - serverInfo: { name: 'minimax-bot', version: '0.7.28' }, + serverInfo: { name: 'minimax-bot', version: '0.7.30' }, instructions: t('mcp.instructions'), }; break; diff --git a/plugins/minimax-ai/minimax-bot/miniapp/node/team.mjs b/plugins/minimax-ai/minimax-bot/miniapp/node/team.mjs index 48e51eb6..d9b212ef 100644 --- a/plugins/minimax-ai/minimax-bot/miniapp/node/team.mjs +++ b/plugins/minimax-ai/minimax-bot/miniapp/node/team.mjs @@ -180,7 +180,15 @@ export class Team { if (avatar) bot.avatar = await this.saveAvatar(avatar); this.store.state.bots.push(bot); this.store.bumpRoster(); - await this.store.save(); + try { + await this.store.save(); + } catch (error) { + // Same rule as updateBot: a rejected request leaves no trace. Without this the Bot stays in + // memory after a failed save, and the next unrelated save publishes what the user was told + // had failed. + this.store.state.bots = this.store.state.bots.filter((item) => item.id !== bot.id); + throw error; + } this.changed(); return bot; } @@ -379,7 +387,14 @@ export class Team { }; this.store.state.groups.push(group); this.store.bumpRoster(); - await this.store.save(); + try { + await this.store.save(); + } catch (error) { + // A rejected request leaves no trace: a group kept in memory after a failed save would be + // published by the next unrelated save, under a name the user was told was refused. + this.store.state.groups = this.store.state.groups.filter((item) => item.id !== group.id); + throw error; + } const welcome = await this.store.appendMessage(`group:${group.id}`, { role: 'system', key: 'notice.groupCreated', @@ -595,8 +610,11 @@ export class Team { } if (kind === 'once' && !runAt) throw new EngineError('invalid', t('err.scheduleInMinutes')); const schedule = existing ?? { id: newId('sch'), createdAt: Date.now() }; - if (!existing) list.push(schedule); - Object.assign(schedule, { + // Build the next value beside the live object and publish it only once the save went through. + // Editing in place and restoring on failure would also undo an update another caller completed + // while this save was in flight: it wrote into the same object this call is holding. + const next = { + ...schedule, name: String(input.name ?? '').trim().slice(0, 40) || prompt.slice(0, 20), botId: bot.id, prompt: prompt.slice(0, 4000), @@ -605,11 +623,26 @@ export class Team { everyMinutes, runAt: kind === 'once' ? runAt : undefined, enabled: input.enabled !== false, - }); - schedule.nextRunAt = nextRun(schedule, Date.now()); - await this.store.save(); - this.changed(); - return schedule; + }; + next.nextRunAt = nextRun(next, Date.now()); + if (!existing) list.push(next); + else list[list.indexOf(schedule)] = next; + try { + await this.store.save(); + return next; + } catch (error) { + // Put the list back the way it was; a concurrent caller that got here first keeps its value. + if (!existing) { + const at = list.indexOf(next); + if (at !== -1) list.splice(at, 1); + } else { + const at = list.indexOf(next); + if (at !== -1) list[at] = schedule; + } + throw error; + } finally { + this.changed(); + } } /** diff --git a/plugins/minimax-ai/minimax-bot/package.json b/plugins/minimax-ai/minimax-bot/package.json index 8035a030..aa7d48cd 100644 --- a/plugins/minimax-ai/minimax-bot/package.json +++ b/plugins/minimax-ai/minimax-bot/package.json @@ -1,6 +1,6 @@ { "name": "minimax-bot", - "version": "0.7.28", + "version": "0.7.30", "private": true, "type": "module", "mcode": { diff --git a/plugins/minimax-ai/minimax-bot/tests/storage-reliability.test.mjs b/plugins/minimax-ai/minimax-bot/tests/storage-reliability.test.mjs index 6363b979..cd06ac84 100644 --- a/plugins/minimax-ai/minimax-bot/tests/storage-reliability.test.mjs +++ b/plugins/minimax-ai/minimax-bot/tests/storage-reliability.test.mjs @@ -125,6 +125,52 @@ test('a session created while another chat is being deleted survives in memory a assert.equal(restored.getSession(other, bot.id)?.sessionId, 'session-other'); }); +test('a create that cannot be saved leaves nothing behind for a later save to publish', async (t) => { + const { dir, store, chat } = await fixture(t); + const { Team } = await import('../miniapp/node/team.mjs'); + const team = new Team({ store, memory: undefined, engine: { cancelBot() {}, cancelChat() {} }, broadcast() {} }); + const bot = store.state.bots[0]; + + // A directory where state.json belongs makes every save in this test fail. + const statePath = join(dir, 'state.json'); + await rm(statePath, { force: true }); + await mkdir(statePath, { recursive: true }); + + // Every create refuses, and the refusal has to be complete: a Bot or group left in memory would + // be published by the next unrelated save, after the user was told it had failed. + await assert.rejects(team.createBot(null, { name: 'Ghost Bot', instructions: 'x' })); + assert.equal(store.state.bots.some((item) => item.name === 'Ghost Bot'), false); + await assert.rejects(team.createGroup(null, { name: 'Ghost Group', members: [bot.id] })); + assert.equal(store.state.groups.some((item) => item.name === 'Ghost Group'), false); + await assert.rejects(team.saveSchedule({ botId: bot.id, prompt: 'Ghost Schedule', kind: 'daily', time: '09:00' })); + assert.equal(store.state.schedules.some((item) => item.prompt === 'Ghost Schedule'), false); + + // Storage recovers and something unrelated writes the state: the refused objects must stay gone. + await rm(statePath, { recursive: true, force: true }); + await store.save(); + const onDisk = JSON.parse(await readFile(statePath, 'utf8')); + assert.equal(onDisk.bots.some((item) => item.name === 'Ghost Bot'), false); + assert.equal(onDisk.groups.some((item) => item.name === 'Ghost Group'), false); + assert.equal(onDisk.schedules.some((item) => item.prompt === 'Ghost Schedule'), false); +}); + +test('a schedule update that cannot be saved keeps the values the user was shown', async (t) => { + const { dir, store } = await fixture(t); + const { Team } = await import('../miniapp/node/team.mjs'); + const team = new Team({ store, memory: undefined, engine: { cancelBot() {}, cancelChat() {} }, broadcast() {} }); + const bot = store.state.bots[0]; + const schedule = await team.saveSchedule({ botId: bot.id, prompt: 'Original prompt', kind: 'daily', time: '09:00' }); + await store.writeChain; + + const statePath = join(dir, 'state.json'); + await rm(statePath, { force: true }); + await mkdir(statePath, { recursive: true }); + await assert.rejects(team.saveSchedule({ id: schedule.id, botId: bot.id, prompt: 'Refused prompt', kind: 'daily', time: '09:00' })); + + // The in-memory object must go back to what is still on disk, not keep the refused values. + const current = store.state.schedules.find((item) => item.id === schedule.id); + assert.equal(current.prompt, 'Original prompt'); +}); test('legacy sequence migration preserves text/tool order and stable cursors across restarts', async (t) => { const { dir, store, chat } = await fixture(t); const parts = [{ type: 'text', text: 'The current conclusion: ' }, { type: 'tool', name: 'build', state: 'ok' }, { type: 'text', text: 'The current conclusion: publish after the build passes.' }]; @@ -249,3 +295,40 @@ test('delivery transitions commit a complete batch or leave every message unchan assert.deepEqual(saved.map((message) => message.dispatchState), ['delivered', 'delivered']); assert.deepEqual(saved.map((message) => message.dispatchRevision), [1, 1]); }); + +test('a refused schedule update does not undo an update that succeeded while it was saving', async (t) => { + const { dir, store } = await fixture(t); + const { Team } = await import('../miniapp/node/team.mjs'); + const team = new Team({ store, memory: undefined, engine: { cancelBot() {}, cancelChat() {} }, broadcast() {} }); + const bot = store.state.bots[0]; + const schedule = await team.saveSchedule({ botId: bot.id, prompt: 'A', kind: 'daily', time: '09:00' }); + await store.writeChain; + + // B starts saving and is held; C then saves successfully; B finally fails. + let releaseB; + const heldB = new Promise((resolve) => { releaseB = resolve; }); + const realSave = store.save.bind(store); + let holding = true; + store.save = async () => { + if (holding && store.state.schedules.some((item) => item.prompt === 'B')) { holding = false; await heldB; } + return realSave(); + }; + const b = team.saveSchedule({ id: schedule.id, botId: bot.id, prompt: 'B', kind: 'daily', time: '09:00' }); + await new Promise((resolve) => setTimeout(resolve, 20)); + const c = await team.saveSchedule({ id: schedule.id, botId: bot.id, prompt: 'C', kind: 'daily', time: '09:00' }); + assert.equal(store.state.schedules.find((item) => item.id === schedule.id).prompt, 'C'); + + // Make B's write fail, then let it go. + const statePath = join(dir, 'state.json'); + await rm(statePath, { force: true }); + await mkdir(statePath, { recursive: true }); + releaseB(); + await assert.rejects(b); + store.save = realSave; + + // B's refusal must not roll the shared object back past C's success. + const current = store.state.schedules.find((item) => item.id === schedule.id); + assert.equal(current.prompt, 'C'); + assert.equal(store.state.schedules.length, 1); + assert.equal(c.prompt, 'C'); +});