Skip to content

Commit 25449d0

Browse files
authored
fix(minimax-bot-team): write the state that forgets a chat before its transcript is cleared (#139)
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.
1 parent 0c20f90 commit 25449d0

8 files changed

Lines changed: 88 additions & 8 deletions

File tree

‎plugins/antianqi/minimax-bot-team/.minimax-plugin/plugin.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"schemaVersion": 1,
33
"name": "minimax-bot-team",
44
"displayName": "MiniMax Bot Team / MiniMax Bot 团队",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"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.",
77
"author": "antianqi",
88
"icon": "icon.png",

‎plugins/antianqi/minimax-bot-team/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Bot Team gives you a team instead: members with different skills and different
77
temperaments, each guarding one field, and they can sit down together and argue about the
88
same question from different angles.
99

10-
Author: [antianqi](https://github.com/antianqi) - Version: `1.0.0`
10+
Author: [antianqi](https://github.com/antianqi) - Version: `1.0.1`
1111

1212
## What it does
1313

‎plugins/antianqi/minimax-bot-team/README.zh-CN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
现在的 bot 像一个人轮流带着几个面具跟你聊天。MiniMax Bot Team 给你的是一支真正各有
66
能力、各有性格的团队,各自守着一个领域,也能围坐一起,从不同角度讨论同一个问题。
77

8-
作者:[antianqi](https://github.com/antianqi) - 版本:`1.0.0`
8+
作者:[antianqi](https://github.com/antianqi) - 版本:`1.0.1`
99

1010
## 它做什么
1111

‎plugins/antianqi/minimax-bot-team/miniapp/node/plugin-id.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
export const PLUGIN_ID = 'minimax-bot-team';
1313

1414
/** The version the Host and the MCP tools report. */
15-
export const PLUGIN_VERSION = '1.0.0';
15+
export const PLUGIN_VERSION = '1.0.1';

‎plugins/antianqi/minimax-bot-team/miniapp/node/store.mjs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,17 @@ export class Store {
601601

602602
async deleteChat(chatId) {
603603
await this.getMessages(chatId);
604+
// The transcript is the one thing here that cannot be rebuilt, so the state that
605+
// forgets this chat goes to disk first. A failed state write then leaves the chat and
606+
// its messages both as they were, and the caller can simply try again.
604607
await this.enqueue(async () => {
608+
const sessions = { ...this.state.sessions };
609+
delete sessions[chatId];
610+
await writeJsonAtomic(this.statePath, { ...this.state, sessions });
611+
delete this.state.sessions[chatId];
605612
await this.writeMessages(chatId, []);
606613
this.messageCache.set(chatId, []);
607-
delete this.state.sessions[chatId];
608614
});
609-
await this.save();
610615
}
611616

612617
/** Sidebar summary of one chat, read from the warm cache. */

‎plugins/antianqi/minimax-bot-team/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "minimax-bot-team",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"private": true,
55
"type": "module",
66
"mcode": {

‎plugins/antianqi/minimax-bot-team/skills/bot-team-setup/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Use this skill when the user wants to build or change a team of Min
44
license: Apache-2.0
55
metadata:
66
author: antianqi
7-
version: 1.0.0
7+
version: 1.0.1
88
---
99

1010
# MiniMax Bot 团队搭建
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// @ts-check
2+
// Deleting a chat must not lose its messages when the state write fails.
3+
//
4+
// The transcript is the one thing that cannot be rebuilt, so the state that forgets the
5+
// chat goes to disk first. A failed write then leaves the chat and its messages both as
6+
// they were, and the caller can simply try again. This file fails if that order flips
7+
// back: the failure injection below replaces state.json with a directory of the same name,
8+
// so every write to it fails.
9+
10+
import assert from 'node:assert/strict';
11+
import { test } from 'node:test';
12+
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
13+
import { tmpdir } from 'node:os';
14+
import { join } from 'node:path';
15+
16+
import { Store } from '../miniapp/node/store.mjs';
17+
18+
async function fixture(t) {
19+
const dir = await mkdtemp(join(tmpdir(), 'bot-delete-chat-'));
20+
t.after(() => rm(dir, { recursive: true, force: true }));
21+
const store = new Store(dir);
22+
await store.init();
23+
return { dir, store, chat: store.chatIds()[0] };
24+
}
25+
26+
test('a failed state write leaves the chat and its messages untouched', async (t) => {
27+
const { dir, store, chat } = await fixture(t);
28+
await store.appendMessage(chat, { role: 'user', text: 'the greeting' });
29+
await store.appendMessage(chat, { role: 'bot', text: 'the answer' });
30+
store.state.sessions[chat] = { sessionId: 's_1', stamp: 1, lastSeen: 1 };
31+
await store.save();
32+
// The store seeds a greeting of its own, so the count is read, never assumed.
33+
const before = (await store.getMessages(chat)).length;
34+
assert.ok(before >= 2, 'the chat has a transcript to lose');
35+
36+
// The state file becomes a directory: every write to it fails.
37+
const statePath = join(dir, 'state.json');
38+
const saved = await readFile(statePath, 'utf8');
39+
await rm(statePath, { force: true });
40+
await mkdir(statePath, { recursive: true });
41+
42+
await assert.rejects(() => store.deleteChat(chat));
43+
44+
// The transcript is still on disk, and the live state still knows the chat.
45+
const onDisk = JSON.parse(await readFile(store.messagePath(chat), 'utf8'));
46+
assert.equal(onDisk.length, before, 'the messages are still on disk');
47+
assert.ok(store.state.sessions[chat], 'the live state still knows the chat');
48+
49+
// Put the state file back and restart: the chat is still there with its messages.
50+
await rm(statePath, { recursive: true, force: true });
51+
await writeFile(statePath, saved, 'utf8');
52+
const restarted = new Store(dir);
53+
await restarted.init();
54+
const messages = await restarted.getMessages(chat);
55+
assert.equal(messages.length, before, 'the messages survive the restart');
56+
assert.ok(messages.some((message) => message.text === 'the greeting'), 'the greeting is still there');
57+
assert.ok(restarted.state.sessions[chat], 'the session survives the restart');
58+
});
59+
60+
test('a delete that succeeds clears the transcript and forgets the chat', async (t) => {
61+
const { dir, store, chat } = await fixture(t);
62+
await store.appendMessage(chat, { role: 'user', text: 'the greeting' });
63+
await store.appendMessage(chat, { role: 'bot', text: 'the answer' });
64+
store.state.sessions[chat] = { sessionId: 's_1', stamp: 1, lastSeen: 1 };
65+
await store.save();
66+
67+
await store.deleteChat(chat);
68+
69+
assert.equal((await store.getMessages(chat)).length, 0, 'the transcript is empty');
70+
assert.equal(store.state.sessions[chat], undefined, 'the chat is forgotten');
71+
const restarted = new Store(dir);
72+
await restarted.init();
73+
assert.equal((await restarted.getMessages(chat)).length, 0, 'the transcript stays empty');
74+
assert.equal(restarted.state.sessions[chat], undefined, 'the chat stays forgotten');
75+
});

0 commit comments

Comments
 (0)