From b92e622e76824579347dbbb45581aae30583f3b1 Mon Sep 17 00:00:00 2001 From: antianqi <75944423+antianqi@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:05:22 +0800 Subject: [PATCH] fix(minimax-bot-team): write the state that forgets a chat before its transcript is cleared This answers the first point of issue #136. Deleting a chat cleared the transcript first and saved the state afterwards. When the state write failed, the transcript was already gone: the chat stayed in the sidebar and its messages were lost for good, with no way to get them back. The state that no longer lists the chat now goes to disk first, and the transcript is cleared only after that write succeeded. A failed write leaves the chat and its messages exactly as they were, and the caller can simply try again - the same order the official minimax-bot package uses since #135. The test that covers it travels with the package, so it can be run against the package itself: node --test plugins/antianqi/minimax-bot-team/tests/chat-delete-atomicity.test.mjs It replaces state.json with a directory of the same name, so every write to it fails, and then checks that the messages are still on disk, that the live state still knows the chat, and that after the state file is put back and the store is restarted the chat and its messages are still there. The second test checks that a delete that succeeds does clear the transcript and forget the chat. Checks: the test passes against the package (2/2), the upstream validator reports 0 errors and 1 warning for this package (ENTRY_STDOUT - the stdout in question is the memory helper's own output channel), and npm run check passes 92/92. Version 1.0.1. --- .../.minimax-plugin/plugin.json | 2 +- plugins/antianqi/minimax-bot-team/README.md | 2 +- .../antianqi/minimax-bot-team/README.zh-CN.md | 2 +- .../miniapp/node/plugin-id.mjs | 2 +- .../minimax-bot-team/miniapp/node/store.mjs | 9 ++- .../antianqi/minimax-bot-team/package.json | 2 +- .../skills/bot-team-setup/SKILL.md | 2 +- .../tests/chat-delete-atomicity.test.mjs | 75 +++++++++++++++++++ 8 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 plugins/antianqi/minimax-bot-team/tests/chat-delete-atomicity.test.mjs diff --git a/plugins/antianqi/minimax-bot-team/.minimax-plugin/plugin.json b/plugins/antianqi/minimax-bot-team/.minimax-plugin/plugin.json index 02e575ac..9be6758d 100644 --- a/plugins/antianqi/minimax-bot-team/.minimax-plugin/plugin.json +++ b/plugins/antianqi/minimax-bot-team/.minimax-plugin/plugin.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "name": "minimax-bot-team", "displayName": "MiniMax Bot Team / MiniMax Bot 团队", - "version": "1.0.0", + "version": "1.0.1", "description": "现在的 bot 像一个人轮流带着几个面具跟你聊天。MiniMax Bot Team 给你的是一支真正各有能力、各有性格的团队,各自守着一个领域,也能围坐一起,从不同角度讨论同一个问题。 / Today's bot is one person wearing several masks, taking turns with you. MiniMax Bot Team gives you a team instead: members with different skills and different temperaments, each guarding one field, and they can sit down together and argue about the same question from different angles.", "author": "antianqi", "icon": "icon.png", diff --git a/plugins/antianqi/minimax-bot-team/README.md b/plugins/antianqi/minimax-bot-team/README.md index 521012a4..a8e2a5c4 100644 --- a/plugins/antianqi/minimax-bot-team/README.md +++ b/plugins/antianqi/minimax-bot-team/README.md @@ -7,7 +7,7 @@ Bot Team gives you a team instead: members with different skills and different temperaments, each guarding one field, and they can sit down together and argue about the same question from different angles. -Author: [antianqi](https://github.com/antianqi) - Version: `1.0.0` +Author: [antianqi](https://github.com/antianqi) - Version: `1.0.1` ## What it does diff --git a/plugins/antianqi/minimax-bot-team/README.zh-CN.md b/plugins/antianqi/minimax-bot-team/README.zh-CN.md index 32bf7e26..27974f06 100644 --- a/plugins/antianqi/minimax-bot-team/README.zh-CN.md +++ b/plugins/antianqi/minimax-bot-team/README.zh-CN.md @@ -5,7 +5,7 @@ 现在的 bot 像一个人轮流带着几个面具跟你聊天。MiniMax Bot Team 给你的是一支真正各有 能力、各有性格的团队,各自守着一个领域,也能围坐一起,从不同角度讨论同一个问题。 -作者:[antianqi](https://github.com/antianqi) - 版本:`1.0.0` +作者:[antianqi](https://github.com/antianqi) - 版本:`1.0.1` ## 它做什么 diff --git a/plugins/antianqi/minimax-bot-team/miniapp/node/plugin-id.mjs b/plugins/antianqi/minimax-bot-team/miniapp/node/plugin-id.mjs index fc7dadea..512ca8b4 100644 --- a/plugins/antianqi/minimax-bot-team/miniapp/node/plugin-id.mjs +++ b/plugins/antianqi/minimax-bot-team/miniapp/node/plugin-id.mjs @@ -12,4 +12,4 @@ export const PLUGIN_ID = 'minimax-bot-team'; /** The version the Host and the MCP tools report. */ -export const PLUGIN_VERSION = '1.0.0'; +export const PLUGIN_VERSION = '1.0.1'; diff --git a/plugins/antianqi/minimax-bot-team/miniapp/node/store.mjs b/plugins/antianqi/minimax-bot-team/miniapp/node/store.mjs index 51c36262..38968c4d 100644 --- a/plugins/antianqi/minimax-bot-team/miniapp/node/store.mjs +++ b/plugins/antianqi/minimax-bot-team/miniapp/node/store.mjs @@ -601,12 +601,17 @@ export class Store { async deleteChat(chatId) { await this.getMessages(chatId); + // The transcript is the one thing here that cannot be rebuilt, so the state that + // forgets this chat goes to disk first. A failed state write then leaves the chat and + // its messages both as they were, and the caller can simply try again. await this.enqueue(async () => { + const sessions = { ...this.state.sessions }; + delete sessions[chatId]; + await writeJsonAtomic(this.statePath, { ...this.state, sessions }); + delete this.state.sessions[chatId]; await this.writeMessages(chatId, []); this.messageCache.set(chatId, []); - delete this.state.sessions[chatId]; }); - await this.save(); } /** Sidebar summary of one chat, read from the warm cache. */ diff --git a/plugins/antianqi/minimax-bot-team/package.json b/plugins/antianqi/minimax-bot-team/package.json index 3fd0512f..ea7d6763 100644 --- a/plugins/antianqi/minimax-bot-team/package.json +++ b/plugins/antianqi/minimax-bot-team/package.json @@ -1,6 +1,6 @@ { "name": "minimax-bot-team", - "version": "1.0.0", + "version": "1.0.1", "private": true, "type": "module", "mcode": { diff --git a/plugins/antianqi/minimax-bot-team/skills/bot-team-setup/SKILL.md b/plugins/antianqi/minimax-bot-team/skills/bot-team-setup/SKILL.md index aec15463..2a94c28e 100644 --- a/plugins/antianqi/minimax-bot-team/skills/bot-team-setup/SKILL.md +++ b/plugins/antianqi/minimax-bot-team/skills/bot-team-setup/SKILL.md @@ -4,7 +4,7 @@ description: Use this skill when the user wants to build or change a team of Min license: Apache-2.0 metadata: author: antianqi - version: 1.0.0 + version: 1.0.1 --- # MiniMax Bot 团队搭建 diff --git a/plugins/antianqi/minimax-bot-team/tests/chat-delete-atomicity.test.mjs b/plugins/antianqi/minimax-bot-team/tests/chat-delete-atomicity.test.mjs new file mode 100644 index 00000000..2275a608 --- /dev/null +++ b/plugins/antianqi/minimax-bot-team/tests/chat-delete-atomicity.test.mjs @@ -0,0 +1,75 @@ +// @ts-check +// Deleting a chat must not lose its messages when the state write fails. +// +// The transcript is the one thing that cannot be rebuilt, so the state that forgets the +// chat goes to disk first. A failed write then leaves the chat and its messages both as +// they were, and the caller can simply try again. This file fails if that order flips +// back: the failure injection below replaces state.json with a directory of the same name, +// so every write to it fails. + +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +import { Store } from '../miniapp/node/store.mjs'; + +async function fixture(t) { + const dir = await mkdtemp(join(tmpdir(), 'bot-delete-chat-')); + t.after(() => rm(dir, { recursive: true, force: true })); + const store = new Store(dir); + await store.init(); + return { dir, store, chat: store.chatIds()[0] }; +} + +test('a failed state write leaves the chat and its messages untouched', async (t) => { + const { dir, store, chat } = await fixture(t); + await store.appendMessage(chat, { role: 'user', text: 'the greeting' }); + await store.appendMessage(chat, { role: 'bot', text: 'the answer' }); + store.state.sessions[chat] = { sessionId: 's_1', stamp: 1, lastSeen: 1 }; + await store.save(); + // The store seeds a greeting of its own, so the count is read, never assumed. + const before = (await store.getMessages(chat)).length; + assert.ok(before >= 2, 'the chat has a transcript to lose'); + + // The state file becomes a directory: every write to it fails. + const statePath = join(dir, 'state.json'); + const saved = await readFile(statePath, 'utf8'); + await rm(statePath, { force: true }); + await mkdir(statePath, { recursive: true }); + + await assert.rejects(() => store.deleteChat(chat)); + + // The transcript is still on disk, and the live state still knows the chat. + const onDisk = JSON.parse(await readFile(store.messagePath(chat), 'utf8')); + assert.equal(onDisk.length, before, 'the messages are still on disk'); + assert.ok(store.state.sessions[chat], 'the live state still knows the chat'); + + // Put the state file back and restart: the chat is still there with its messages. + await rm(statePath, { recursive: true, force: true }); + await writeFile(statePath, saved, 'utf8'); + const restarted = new Store(dir); + await restarted.init(); + const messages = await restarted.getMessages(chat); + assert.equal(messages.length, before, 'the messages survive the restart'); + assert.ok(messages.some((message) => message.text === 'the greeting'), 'the greeting is still there'); + assert.ok(restarted.state.sessions[chat], 'the session survives the restart'); +}); + +test('a delete that succeeds clears the transcript and forgets the chat', async (t) => { + const { dir, store, chat } = await fixture(t); + await store.appendMessage(chat, { role: 'user', text: 'the greeting' }); + await store.appendMessage(chat, { role: 'bot', text: 'the answer' }); + store.state.sessions[chat] = { sessionId: 's_1', stamp: 1, lastSeen: 1 }; + await store.save(); + + await store.deleteChat(chat); + + assert.equal((await store.getMessages(chat)).length, 0, 'the transcript is empty'); + assert.equal(store.state.sessions[chat], undefined, 'the chat is forgotten'); + const restarted = new Store(dir); + await restarted.init(); + assert.equal((await restarted.getMessages(chat)).length, 0, 'the transcript stays empty'); + assert.equal(restarted.state.sessions[chat], undefined, 'the chat stays forgotten'); +});