diff --git a/plugins/minimax-ai/minimax-bot/.minimax-plugin/plugin.json b/plugins/minimax-ai/minimax-bot/.minimax-plugin/plugin.json index 555b7cd3..37445d15 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.37", + "version": "0.7.38", "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 e64a391a..e93a87e6 100644 --- a/plugins/minimax-ai/minimax-bot/README.md +++ b/plugins/minimax-ai/minimax-bot/README.md @@ -145,6 +145,17 @@ The hand-over between two Bots of a group carried the tail of the previous reply - The stored name now replaces a run of dots, and a long name is cut on a whole character so an emoji is never left half-written. - The extension is kept apart from the rest of the name and survives that cleaning, because it decides how the file is served: `photo..png` was stored as `photo_png`, so it went out as `application/octet-stream` and the Bot received a plain file instead of an image. It is stored as `photo.png` now and stays a PNG end to end. +## 0.7.38 The page tells the user when a chat is stuck and when a job was stopped + +- A turn whose outcome cannot be confirmed — the process stopped while it was running, or the Host took it and never said — leaves the request marked as unknown. Every later message of that chat waits for it, on purpose: a correction must not act on a state nobody can vouch for. +- But the way out was not reachable. The page draws its continuation button on a held message and on a request a restart left stranded; a message with an unknown outcome got no button, and the continuation route answered `busy` when the chat held nothing but that one message. So a chat with a single unknown outcome had no way out at all: the user could not confirm it, could not send it again under the same id, and every new request joined the queue behind it. Restarting did not help, because the state is on disk — and a restart in the middle of a turn is what produces it. +- A message with an unknown outcome now carries the same button, and the continuation route accepts a chat that holds nothing but such a message. Confirming it does not send that turn again: the request stays marked as unknown, and the chat goes on. The requests that were waiting behind it are sent. Once confirmed, the button is gone and the line says the later messages may go on. +- The stop button of a background job had the same trouble from the other side. It asked the Host to end the turn, but the job kept the `running` state until that turn actually came back. The Host ends a turn between its steps, so a job inside a long tool call kept reading as running for a while — and to a user pressing the button, nothing had happened at all. The only thing left to try was quitting the whole app. +- The job is marked stopped the moment the stop is asked for, so the page stops reading it as running. The turn still winds down in the background; what changes is that the page tells the truth about it. The abort still goes to the same turn, and a job that was already stopped or already finished is left alone. +- Asking to let a stuck chat go now shows on the page that it took: the message says the later messages may go on, and the button is gone. A message that arrives while that is being written goes out with it, instead of waiting for a second press of the button. +- A job that was stopped and then killed by the process still leaves its turn in the usage ledger. The row is written under the job's own id, so a job that did finish is not counted twice. +- Nothing else moves: a request with an unknown outcome is still never replayed on its own, and the order of the messages around it is unchanged. + ## 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/client/app.js b/plugins/minimax-ai/minimax-bot/miniapp/client/app.js index 75f2d0e2..ba7a5519 100644 --- a/plugins/minimax-ai/minimax-bot/miniapp/client/app.js +++ b/plugins/minimax-ai/minimax-bot/miniapp/client/app.js @@ -804,7 +804,13 @@ function userDelivery(message) { const stranded = status === 'pending' && !sentHere.has(message.id) && !state.busy.has(message.chatId) && !(state.messages.get(message.chatId) ?? []).some( (item) => item.sourceMessageId === message.id || item.sourceMessageIds?.includes(message.id)); - const button = status === 'held' || stranded ? h('button', { + // A turn whose outcome is unknown holds every later message of the chat. The page has to offer + // the way out on the message that carries that state: asking from anywhere else would not show + // the user where the chat is stuck. + // Once the user has confirmed it, the asking is done: the button goes away, so the page says + // that the confirmation took effect. + const confirmed = message.continuationAcknowledged === true; + const button = status === 'held' || (status === 'uncertain' && !confirmed) || stranded ? h('button', { type: 'button', class: 'btn sm', disabled: deliveryActions.has(key), onclick: async (event) => { if (deliveryActions.has(key)) return; @@ -825,7 +831,7 @@ function userDelivery(message) { }, }, t('delivery.resume')) : null; return h('div', { class: 'message-delivery', role: 'status' }, - h('span', null, tOr(`delivery.${status}`, undefined, '')), button); + h('span', null, confirmed && status === 'uncertain' ? t('delivery.uncertainConfirmed') : tOr(`delivery.${status}`, undefined, '')), button); } const deliveryActions = new Set(); diff --git a/plugins/minimax-ai/minimax-bot/miniapp/client/strings-app.js b/plugins/minimax-ai/minimax-bot/miniapp/client/strings-app.js index b5662ba0..032e50c6 100644 --- a/plugins/minimax-ai/minimax-bot/miniapp/client/strings-app.js +++ b/plugins/minimax-ai/minimax-bot/miniapp/client/strings-app.js @@ -22,6 +22,7 @@ export default { "delivery.adjusting": "正在调整…", "delivery.held": "尚未确认可以继续。请先检查完整会话,再继续处理补充。", "delivery.uncertain": "处理结果待确认,请查看完整会话。", + "delivery.uncertainConfirmed": "处理结果仍待确认,已确认继续后面的消息。", "delivery.cancelled": "已取消处理", "delivery.failed": "未送达,请查看回复中的错误提示。", "delivery.resume": "继续处理补充", @@ -302,6 +303,7 @@ export default { "delivery.adjusting": "Adjusting…", "delivery.held": "Continuation needs confirmation. Check the full session before continuing with follow-ups.", "delivery.uncertain": "Outcome unknown. Check the full session.", + "delivery.uncertainConfirmed": "Outcome still unknown; you confirmed to go on with the later messages.", "delivery.cancelled": "Cancelled", "delivery.failed": "Not delivered. Check the reply for details.", "delivery.resume": "Continue with follow-ups", diff --git a/plugins/minimax-ai/minimax-bot/miniapp/node/engine.mjs b/plugins/minimax-ai/minimax-bot/miniapp/node/engine.mjs index 0e2168fc..33ffe93b 100644 --- a/plugins/minimax-ai/minimax-bot/miniapp/node/engine.mjs +++ b/plugins/minimax-ai/minimax-bot/miniapp/node/engine.mjs @@ -2051,6 +2051,13 @@ export class Engine { } if (!HELPER_ACTIVE.has(helper.status)) return { stopped: false, status: helper.status }; run.stoppedBy = who; + // The Host ends a turn between its steps, so a run that is inside a long tool call keeps + // going for a while after the abort. The job is not working any more, and the page has to + // say so at once: a stop button that leaves the row reading "running" looks like it did + // nothing, and the only thing left to try is quitting the whole app. + helper.status = 'stopped'; + void this.store.save().catch((error) => this.logger.warn('minimax_bot.helper_stop_save_failed', { helperId: helper.id, code: error?.code ?? 'unknown' })); + this.broadcastHelpers(); run.controller.abort(); return { stopped: true, status: 'stopped' }; } diff --git a/plugins/minimax-ai/minimax-bot/miniapp/node/mcp.mjs b/plugins/minimax-ai/minimax-bot/miniapp/node/mcp.mjs index ed9da831..d4fd1987 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.37' }, + serverInfo: { name: 'minimax-bot', version: '0.7.38' }, instructions: t('mcp.instructions'), }; break; diff --git a/plugins/minimax-ai/minimax-bot/miniapp/node/store.mjs b/plugins/minimax-ai/minimax-bot/miniapp/node/store.mjs index 7eae5c30..be21039d 100644 --- a/plugins/minimax-ai/minimax-bot/miniapp/node/store.mjs +++ b/plugins/minimax-ai/minimax-bot/miniapp/node/store.mjs @@ -303,6 +303,24 @@ export class Store { // A queued job never reached the model; a running one had already called it. attempts: wasQueued ? 0 : 1, }); + } else if (helper.status === 'stopped') { + // The stop was asked for and the job was marked stopped at once, but the turn it was in + // writes its ledger row when it winds down. If the process was killed in between, that + // row is missing: a job the user stopped still spent a turn. The row is written under the + // job's own id, so a job that did finish is not counted twice. + await this.recordUsageOnce({ + ts: helper.startedAt ?? helper.createdAt ?? helper.endedAt ?? Date.now(), + turnId: helper.id, + botId: helper.botId, + botName: this.getBot(helper.botId)?.name ?? '', + chatId: helper.chatId, + kind: 'helper-run', + status: 'aborted', + durationMs: null, + chars: 0, + tools: helper.turnTools ?? 0, + attempts: helper.startedAt ? 1 : 0, + }); } } this.state.taskBatches = Array.isArray(this.state.taskBatches) ? this.state.taskBatches : []; diff --git a/plugins/minimax-ai/minimax-bot/miniapp/node/user-inbox.mjs b/plugins/minimax-ai/minimax-bot/miniapp/node/user-inbox.mjs index e1ec8f1b..e9eac1d0 100644 --- a/plugins/minimax-ai/minimax-bot/miniapp/node/user-inbox.mjs +++ b/plugins/minimax-ai/minimax-bot/miniapp/node/user-inbox.mjs @@ -152,25 +152,41 @@ export class UserInbox { async resumeHeld(chatId) { // Only resend supplements that never started; an uncertain original request is never replayed. - const history = await this.engine.store.getMessages(chatId); - const held = history.filter((message) => { + /** The messages of a list that can be sent again. */ + const recoverableOf = (list) => list.filter((message) => { if (message.role !== 'user' || this.has(message.id)) return false; if (message.dispatchState === 'held') return true; // A restart leaves an accepted request `pending` with the queue that owned it gone. It is // recoverable on the same proof the retry path uses: no reply and no durable assistant // placeholder means the request never reached the Host. return message.dispatchState === 'pending' - && !history.some((item) => item.sourceMessageId === message.id || item.sourceMessageIds?.includes(message.id)); + && !list.some((item) => item.sourceMessageId === message.id || item.sourceMessageIds?.includes(message.id)); }); - if (!held.length || this.busy(chatId) || [...this.engine.tickets].some((ticket) => ticket.chatId === chatId)) { + /** The same list, read fresh: it changes while the disk is written. */ + const recoverable = async () => recoverableOf(await this.engine.store.getMessages(chatId)); + const history = await this.engine.store.getMessages(chatId); + const held = await recoverable(); + // A turn whose outcome is unknown holds every later message of the chat, so a chat that has + // one is stuck until the user says what to do with it. Asking is the only way out, so the + // asking must work even when there is nothing to send again: the unknown turn is confirmed + // as seen, and the chat goes on. + const uncertainIds = history.filter((message) => message.role === 'user' && message.dispatchState === 'uncertain').map((message) => message.id); + if ((!held.length && !uncertainIds.length) || this.busy(chatId) || [...this.engine.tickets].some((ticket) => ticket.chatId === chatId)) { throw new this.ErrorType('busy', t('err.messageAlreadyProcessing')); } const bot = chatId.startsWith('bot:') ? this.engine.store.getBot(chatId.slice(4)) : undefined; const group = chatId.startsWith('group:') ? this.engine.store.getGroup(chatId.slice(6)) : undefined; if (!bot && !group) throw new this.ErrorType('not_found', t('err.chatMissing')); - const uncertainIds = history.filter((message) => message.role === 'user' && message.dispatchState === 'uncertain').map((message) => message.id); - await this.engine.store.updateMessages(chatId, uncertainIds, { continuationAcknowledged: true }); - const heldIds = new Set(held.map((message) => message.id)); + // Confirming the unknown turn is not replaying it: it is the user saying they have seen it. + // The turn itself is never sent again. + const acknowledged = await this.engine.store.updateMessages(chatId, uncertainIds, { continuationAcknowledged: true }); + // The page has to hear about it: without this the button stays on the message that was just + // confirmed, and the user cannot tell whether asking did anything. + for (const message of acknowledged) this.engine.broadcast({ type: 'message.upsert', chatId, message }); + // Read the list again: a message that arrived while the confirmation was being written was + // held too, and it belongs to this continuation rather than to the next button press. + const heldNow = await recoverable(); + const heldIds = new Set(heldNow.map((message) => message.id)); // Re-check inside the write: a retry carrying the same clientMessageId can enqueue the message // while this call waits for the disk, and a message that moved on must not be sent a second // time. Only the state this call actually reached is still recoverable. diff --git a/plugins/minimax-ai/minimax-bot/package.json b/plugins/minimax-ai/minimax-bot/package.json index c152016d..e0d76793 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.37", + "version": "0.7.38", "private": true, "type": "module", "mcode": { diff --git a/plugins/minimax-ai/minimax-bot/tests/client-reliability.test.mjs b/plugins/minimax-ai/minimax-bot/tests/client-reliability.test.mjs index 3f410e9e..7ec54e8f 100644 --- a/plugins/minimax-ai/minimax-bot/tests/client-reliability.test.mjs +++ b/plugins/minimax-ai/minimax-bot/tests/client-reliability.test.mjs @@ -844,4 +844,16 @@ test('the background task strip keeps the official one line rows and folds a lon emit({ type: 'helpers.changed', helpers }); await settle(); assert.match(strip.querySelector('.helper-more').textContent, /4 more tasks/, 'the queue starts folded again'); -}); \ No newline at end of file +}); +test('an unknown outcome offers the way out, so the chat is not stuck for good', async () => { + // A turn whose outcome is unknown holds every later message of the chat. The page has to + // offer the way out on the message that carries that state. + const unknown = message('msg_unknown', 'Possibly already executed', { role: 'user', dispatchState: 'uncertain' }); + await reset([unknown]); + assert.match(document.querySelector('.message-delivery').textContent, /Outcome unknown/); + const button = document.querySelector('.message-delivery button'); + assert.notEqual(button, null, 'the message that holds the chat offers the continuation'); + route = ({ path }) => path.endsWith('/resume-pending') ? response({ ok: true }) : undefined; + button.click(); await settle(); + assert.equal(calls.filter((call) => call.path.endsWith('/resume-pending')).length, 1); +}); diff --git a/plugins/minimax-ai/minimax-bot/tests/engine-reliability.test.mjs b/plugins/minimax-ai/minimax-bot/tests/engine-reliability.test.mjs index a00d8bc4..c7e82e3b 100644 --- a/plugins/minimax-ai/minimax-bot/tests/engine-reliability.test.mjs +++ b/plugins/minimax-ai/minimax-bot/tests/engine-reliability.test.mjs @@ -1493,3 +1493,150 @@ test('a pass by the last speaker does not overwrite what an earlier one said', a const messages = await store.getMessages(chatId); assert.equal(messages.filter((message) => message.status === 'pass').length, 1); }); + +test('a chat with one unknown outcome can be let go, so it is not stuck for good', async (t) => { + const { store, engine } = await fixture(t); + const bot = store.state.bots[0]; + const chatId = `bot:${bot.id}`; + // A turn that was cut off leaves its request uncertain: the outcome cannot be known, and the + // request is never replayed on its own. Every later message of the chat waits for it. + await store.appendMessage(chatId, { role: 'user', text: 'Possibly already executed', dispatchState: 'uncertain' }); + await store.writeChain; + + let runs = 0; + engine.agent = { async run(input, options) { + runs += 1; + return { ...started(input, options, 'after-unknown'), status: 'finished', text: 'Done' }; + } }; + + // The user says they have seen it: the chat goes on. The unknown turn itself is not sent again. + const resumed = await engine.inbox.resumeHeld(chatId); + assert.deepEqual(resumed, { ok: true }); + const history = await store.getMessages(chatId); + assert.equal(history.find((m) => m.text === 'Possibly already executed').continuationAcknowledged, true); + + // And a later request is no longer held back by it. + await engine.sendUserMessage(chatId, 'A new request', [], 'after-unknown-1'); + await until(() => idle(engine)); + assert.equal(runs, 1, 'the new request runs'); + const after = await store.getMessages(chatId); + assert.equal(after.find((m) => m.text === 'A new request').dispatchState, 'delivered'); +}); + +test('an unknown outcome does not stop a later request from running once it is acknowledged', async (t) => { + const { store, engine } = await fixture(t); + const bot = store.state.bots[0]; + const chatId = `bot:${bot.id}`; + await store.appendMessage(chatId, { role: 'user', text: 'Possibly already executed', dispatchState: 'uncertain' }); + await store.writeChain; + + let runs = 0; + engine.agent = { async run(input, options) { + runs += 1; + return { ...started(input, options, 'held-then-asked'), status: 'finished', text: 'Done' }; + } }; + + // A request that arrives while the outcome is unknown waits for it. + await engine.sendUserMessage(chatId, 'A correction', [], 'held-1'); + await until(() => idle(engine)); + assert.equal(runs, 0, 'the correction waits for the unknown outcome'); + assert.equal((await store.getMessages(chatId)).find((m) => m.text === 'A correction').dispatchState, 'held'); + + // Asking lets the chat go: the unknown outcome is confirmed as seen and the waiting request + // is sent. + await engine.inbox.resumeHeld(chatId); + await until(() => idle(engine)); + assert.equal(runs, 1, 'the waiting request is sent'); + const after = await store.getMessages(chatId); + assert.equal(after.find((m) => m.text === 'A correction').dispatchState, 'delivered'); +}); + +test('a running helper shows as stopped the moment the stop is asked for', async (t) => { + const { store, engine } = await fixture(t); + const bot = store.state.bots[0]; + const chatId = `bot:${bot.id}`; + const finish = deferred(); + let insideHelper = false; + engine.agent = { async run(input, options) { + const ids = started(input, options, 'helper-session'); + if (input.text.startsWith('')) { + insideHelper = true; + // The run is inside a tool call that does not come back on its own. + await finish.promise; + return { ...ids, status: 'finished', text: 'Done' }; + } + await engine.startHelper(bot, { task: 'Watch the automation' }); + return { ...ids, status: 'finished', text: 'Started' }; + } }; + const source = engine.runBotTurn({ bot, chatId, text: 'Watch the automation for me', kind: 'private' }); + await until(() => insideHelper); + const helper = store.state.helpers.find((item) => item.status === 'running'); + assert.ok(helper, 'the helper is running'); + + const stopped = engine.stopHelper(null, helper.id); + assert.equal(stopped.stopped, true); + + // The Host ends a turn between its steps, so the run is still winding down. The page must + // not keep reading "running": a stop button that changes nothing looks like it did nothing. + assert.equal(store.state.helpers.find((item) => item.id === helper.id).status, 'stopped'); + + finish.resolve(); + await source; + await new Promise((resolve) => setTimeout(resolve, 60)); + assert.equal(store.state.helpers.find((item) => item.id === helper.id).status, 'stopped'); +}); + +test('confirming an unknown outcome tells the page about it', async (t) => { + const { store, engine, events } = await fixture(t); + const bot = store.state.bots[0]; + const chatId = `bot:${bot.id}`; + await store.appendMessage(chatId, { role: 'user', text: 'Possibly already executed', dispatchState: 'uncertain' }); + await store.writeChain; + + await engine.inbox.resumeHeld(chatId); + + // The page can only say the confirmation took effect if it hears about it. + const sent = events.filter((event) => event.type === 'message.upsert' + && event.chatId === chatId + && event.message?.continuationAcknowledged === true); + assert.equal(sent.length, 1, 'the confirmed message is sent to the page'); + const stored = (await store.getMessages(chatId)).find((message) => message.text === 'Possibly already executed'); + assert.equal(stored.continuationAcknowledged, true); + assert.equal(stored.dispatchState, 'uncertain', 'the message itself is still not replayed'); +}); + +test('a message that arrives while the confirmation is written goes out with it', async (t) => { + const { store, engine } = await fixture(t); + const bot = store.state.bots[0]; + const chatId = `bot:${bot.id}`; + await store.appendMessage(chatId, { role: 'user', text: 'Possibly already executed', dispatchState: 'uncertain' }); + await store.writeChain; + + let runs = 0; + engine.agent = { async run(input, options) { + runs += 1; + return { ...started(input, options, 'after-confirm'), status: 'finished', text: 'Done' }; + } }; + + // Hold the write that confirms the unknown outcome, and let a new message arrive behind it. + const writing = deferred(); + const release = deferred(); + const update = store.updateMessages.bind(store); + let first = true; + store.updateMessages = async (...args) => { + if (first && args[2]?.continuationAcknowledged) { first = false; writing.resolve(); await release.promise; } + return update(...args); + }; + const confirming = engine.inbox.resumeHeld(chatId); + await writing.promise; + await store.appendMessage(chatId, { role: 'user', text: 'A correction', dispatchState: 'held' }); + await store.writeChain; + release.resolve(); + await confirming; + await until(() => idle(engine)); + + // The correction was held while the confirmation was being written; it must not need a + // second press of the button. + assert.equal(runs, 1); + assert.equal((await store.getMessages(chatId)).find((message) => message.text === 'A correction').dispatchState, 'delivered'); +}); diff --git a/plugins/minimax-ai/minimax-bot/tests/helper-usage.test.mjs b/plugins/minimax-ai/minimax-bot/tests/helper-usage.test.mjs index db3eec79..e317f323 100644 --- a/plugins/minimax-ai/minimax-bot/tests/helper-usage.test.mjs +++ b/plugins/minimax-ai/minimax-bot/tests/helper-usage.test.mjs @@ -226,3 +226,39 @@ test('the usage panel counts every model call, background jobs included', async assert.equal(usage.totals.tools, 1); assert.ok(usage.totals.durationMs > 0); }); + +test('a job stopped and then killed still leaves its turn in the ledger', async (t) => { + const { store, engine, dir } = await fixture(t); + const bot = { id: 'bot_a', name: 'Alice', role: 'Writer' }; + store.state.bots = [bot]; + const chatId = `bot:${bot.id}`; + const helperDone = deferred(); + engine.agent = { async run(input, options) { + const ids = { sessionId: 'session-1', turnId: 'turn-1' }; + options.onEvent({ type: 'turn.started', ...ids }); + if (input.text.startsWith(HELPER_PROMPT)) { + await helperDone.promise; + return { ...ids, status: 'finished', text: 'BACKGROUND_ANSWER' }; + } + await engine.startHelper(bot, { task: 'Look this up' }); + return { ...ids, status: 'finished', text: 'Started' }; + } }; + await engine.runBotTurn({ bot, chatId, text: 'START_BACKGROUND', kind: 'private' }); + await until(() => store.state.helpers[0]?.status === 'running'); + // The stop is asked for and the job says so at once; the turn it is in does not come back. + engine.stopHelper(null, store.state.helpers[0].id); + await until(() => store.state.helpers[0]?.status === 'stopped'); + // The turn is still in its tool call; the process goes away with it still running. + helperDone.resolve(); + engine.dispose(); + await until(() => store.state.helpers[0]?.status === 'stopped'); + + // A restart has to say the job spent a turn. + const restored = new Store(dir); + await restored.init(); + const rows = await restored.readUsage(0); + const background = rows.find((row) => row.kind === 'helper-run'); + assert.ok(background, 'the stopped job left a row in the ledger'); + assert.equal(background.status, 'aborted'); + assert.equal(background.turnId, store.state.helpers[0].id); +});