Skip to content

Commit dd59dd0

Browse files
committed
Merge origin/main into minimax-bot PR #153
2 parents 80c0b6c + 0e8c24a commit dd59dd0

3 files changed

Lines changed: 92 additions & 7 deletions

File tree

‎plugins/minimax-ai/minimax-bot/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ The hand-over between two Bots of a group carried the tail of the previous reply
116116
- Two writes the plugin fires and forgets had nothing to catch their failure: the session id recorded when a turn starts, and the scheduled-task scan that runs every 30 seconds. A failed write there became an unhandled rejection, and the Host fails the whole Mini App process on one of those — so a disk that would not take a write took every running turn with it, and the scheduled task kept retrying every 30 seconds.
117117
- Both now report the failure through the plugin log, the way the task save next to one of them already did. The turn carries on, and the scan keeps running.
118118

119+
## 0.7.33 Stopping a queued background task actually stops it
120+
121+
- The Stop button is offered for a background task that is still queued, but the request only found a run to abort once that task had started: pressed before then, it answered `stopped: false`, left the task queued, and the task started anyway. A queued task could not be stopped at all.
122+
- Stopping before the run exists now marks the task stopped and saves that, and a task already marked stopped no longer starts a background turn.
123+
- A task that had already started is unchanged: it is stopped by aborting its run, as before.
124+
119125
## 0.7.37 A round of passes does not hand [PASS] back as its outcome
120126

121127
- When a member of a group has nothing to add it replies `[PASS]` and is skipped. That turn was still kept as the round's result, so a discussion that ended on a pass reported `[PASS]` as what it produced. A group task opened from a private chat showed exactly that in its result line.

‎plugins/minimax-ai/minimax-bot/miniapp/node/engine.mjs‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,18 +1335,19 @@ export class Engine {
13351335
const result = await this.runBotTurn({ bot, chatId, text: '', kind: 'group', sourceMessageId, sourceMessageIds, signal: options.signal, taskId: options.taskId, handover });
13361336
accepted ||= result.accepted;
13371337
if (result.status === 'aborted' && (options.signal?.aborted || loop.cancelled)) queue.unshift(botId);
1338-
// A member that passed said nothing this round: it does not become the round's result.
1339-
// Every other turn does, whichever way the round ends next.
1338+
// The caller needs the stopped turn to resume this member before advancing the group.
1339+
if (result.safeToContinue === false || (result.status === 'done' && result.waitingForTasks)) {
1340+
lastResult = result;
1341+
break;
1342+
}
1343+
// An ordinary pass contributes nothing to the round's result.
13401344
if (result.status === 'done' && !result.pendingQuestion && PASS_RE.test(result.text)) {
13411345
await this.store.updateMessage(chatId, result.messageId, { status: 'pass' });
13421346
const updated = (await this.store.getMessages(chatId)).find((m) => m.id === result.messageId);
13431347
this.broadcast({ type: 'message.upsert', chatId, message: updated });
13441348
continue;
13451349
}
13461350
lastResult = result;
1347-
if (result.safeToContinue === false) break;
1348-
// Resume this member's verification under the same task before advancing the group.
1349-
if (result.status === 'done' && result.waitingForTasks) break;
13501351
if (result.status === 'error' && !loop.cancelled) {
13511352
// One member could not reply (its error and a retry button are in the chat): the round
13521353
// goes on. If nobody else is queued, the admin picks it up so the user is not left hanging.
@@ -1803,6 +1804,9 @@ export class Engine {
18031804
}
18041805

18051806
async runHelper(helper) {
1807+
// The stop button can be pressed while the helper is still `queued` and no run exists yet; that
1808+
// path marks the helper stopped instead of aborting a controller. Nothing to do here then.
1809+
if (helper.status === 'stopped') return;
18061810
const controller = new AbortController();
18071811
const run = { controller };
18081812
const approvals = new Set();
@@ -2006,9 +2010,21 @@ export class Engine {
20062010
const helper = (this.store.state.helpers ?? []).find((h) => h.id === String(id ?? ''));
20072011
if (!helper) throw new EngineError('not_found', t('err.helperMissing', { id: String(id ?? '') }));
20082012
if (caller && !caller.isMain && helper.botId !== caller.id) throw new EngineError('forbidden', t('err.helperNotYours'));
2013+
const who = caller ? caller.name : 'user';
20092014
const run = this.helperRuns.get(helper.id);
2010-
if (!run || !HELPER_ACTIVE.has(helper.status)) return { stopped: false, status: helper.status };
2011-
run.stoppedBy = caller ? caller.name : 'user';
2015+
if (!run) {
2016+
// A helper is in the list before `runHelper` registers a run for it, and the page offers the
2017+
// stop button for `queued` too. Without this the request is answered `stopped: false` and the
2018+
// job starts anyway: mark it so `runHelper` sees it and leaves it alone.
2019+
if (helper.status !== 'queued') return { stopped: false, status: helper.status };
2020+
helper.status = 'stopped';
2021+
helper.endedAt = Date.now();
2022+
void this.store.save().catch((error) => this.logger.warn('minimax_bot.helper_stop_save_failed', { helperId: helper.id, code: error?.code ?? 'unknown' }));
2023+
this.broadcastHelpers();
2024+
return { stopped: true, status: 'stopped' };
2025+
}
2026+
if (!HELPER_ACTIVE.has(helper.status)) return { stopped: false, status: helper.status };
2027+
run.stoppedBy = who;
20122028
run.controller.abort();
20132029
return { stopped: true, status: 'stopped' };
20142030
}

‎plugins/minimax-ai/minimax-bot/tests/engine-reliability.test.mjs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,48 @@ test('a failed save in the schedule tick is reported, not thrown as an unhandled
13861386
await store.writeChain.catch(() => {});
13871387
});
13881388

1389+
test('stopping a helper while its admission is still being saved stops it for good', async (t) => {
1390+
const { store, engine } = await fixture(t);
1391+
const bot = store.state.bots[0];
1392+
const chatId = `bot:${bot.id}`;
1393+
const finish = deferred();
1394+
const saveEntered = deferred();
1395+
const finishSave = deferred();
1396+
t.after(() => { finish.resolve(); finishSave.resolve(); });
1397+
let runs = 0;
1398+
engine.agent = { async run(input, options) {
1399+
const ids = started(input, options, 'source');
1400+
runs += 1;
1401+
await finish.promise;
1402+
return { ...ids, status: 'finished', text: 'Done' };
1403+
} };
1404+
const source = engine.runBotTurn({ bot, chatId, text: 'Start background work', kind: 'private' });
1405+
await until(() => runs === 1);
1406+
1407+
// Hold the save that admits the helper, so the stop lands while it is still `queued` and no run
1408+
// exists yet. That is the window the page's stop button can be pressed in.
1409+
const save = store.save.bind(store);
1410+
store.save = async () => {
1411+
if (store.state.helpers.some((helper) => helper.status === 'queued')) { saveEntered.resolve(); await finishSave.promise; }
1412+
return save();
1413+
};
1414+
const starting = engine.startHelper(bot, { task: 'A new task' });
1415+
await saveEntered.promise;
1416+
const queued = store.state.helpers.find((helper) => helper.status === 'queued');
1417+
assert.equal(engine.helperRuns.size, 0);
1418+
const stopped = engine.stopHelper(null, queued.id);
1419+
assert.equal(stopped.stopped, true);
1420+
finishSave.resolve();
1421+
await starting;
1422+
1423+
// The helper must stay stopped and must never start a background turn.
1424+
finish.resolve();
1425+
await source;
1426+
await new Promise((resolve) => setTimeout(resolve, 60));
1427+
assert.equal(store.state.helpers.find((helper) => helper.id === queued.id).status, 'stopped');
1428+
assert.equal(runs, 1);
1429+
});
1430+
13891431
test('a group round whose members pass does not report a pass as its outcome', async (t) => {
13901432
const { store, engine } = await fixture(t);
13911433
const { a, chatId } = groupFixture(store);
@@ -1420,3 +1462,24 @@ test('a pass by the last speaker does not overwrite what an earlier one said', a
14201462
const messages = await store.getMessages(chatId);
14211463
assert.equal(messages.filter((message) => message.status === 'pass').length, 1);
14221464
});
1465+
1466+
for (const [flag, value] of [['waitingForTasks', true], ['safeToContinue', false]]) {
1467+
test(`a pass preserves the group stop when ${flag} is ${value}`, async (t) => {
1468+
const { store, engine } = await fixture(t);
1469+
const { a, b, group, chatId } = groupFixture(store);
1470+
const message = await store.appendMessage(chatId, { role: 'bot', botId: a.id, text: '[PASS]', status: 'done' });
1471+
const speakers = [];
1472+
engine.runBotTurn = async ({ bot }) => {
1473+
speakers.push(bot.id);
1474+
return { status: 'done', accepted: true, text: '[PASS]', messageId: message.id, [flag]: value };
1475+
};
1476+
1477+
const result = await engine.driveGroup(group, [a.id, b.id], undefined);
1478+
1479+
assert.deepEqual(speakers, [a.id]);
1480+
assert.deepEqual(result.remaining, [b.id]);
1481+
assert.equal(result.messageId, message.id);
1482+
assert.deepEqual((await store.getMessages(chatId)).map((item) => item.status), ['done']);
1483+
assert.equal(result[flag], value);
1484+
});
1485+
}

0 commit comments

Comments
 (0)