Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(bots): /bot <fleet-verb> re-routes to /bots instead of failing as an unknown target

Salman Qureshid50bd8c863b4
diff --git a/src/channels/choice-prompt-timeout.test.ts b/src/channels/choice-prompt-timeout.test.ts
index f1234a3c..497c6695 100644
--- a/src/channels/choice-prompt-timeout.test.ts
+++ b/src/channels/choice-prompt-timeout.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { readFileSync } from 'node:fs';
+import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { CLIChannel } from './cli.js';
@@ -53,4 +54,31 @@ describe('choice prompt timeout / dismissal', () => {
// Timeout is NOT a choice — nothing remembered when nobody answers.
expect(agentSrc).toContain('not remembered — nobody answered');
});
+
+ it('agent: the RESEARCH prompt also has a time-weighted default (never blocks forever)', () => {
+ // The research prompt historically used a bare presentChoice with NO
+ // timeout — an unanswered question held the chat message indefinitely.
+ // It must use the same time-weighted contract, defaulting to Quick answer.
+ expect(agentSrc).toMatch(/promptResearchMode[\s\S]*?this\.presentChoiceWithTimeout\(/s);
+ expect(agentSrc).toMatch(/MERCURY_CODE_HANDOFF_TIMEOUT_MS,\s*\n\s*1, \/\/ unanswered = Quick answer/);
+ });
+
+ it('enterMercuryCode tears down a stranded bot chat (no parked sends afterwards)', async () => {
+ const channel = new CLIChannel();
+ channel.enterBotChat('researcher', 'Researcher');
+ expect(channel.getActiveBotChat()).not.toBeNull();
+ // While a bot chat is open, main-agent sends park into the HIDDEN main
+ // transcript. After entering Mercury Code they must land visibly again.
+ const tmpDir = mkdtempSync(join(tmpdir(), 'mercury-code-entry-'));
+ try {
+ const entered = channel.enterMercuryCode(tmpDir, 'test');
+ expect(entered.ok).toBe(true);
+ expect(channel.getActiveBotChat()).toBeNull();
+ await channel.send('visible reply');
+ const messages = channel.getTuiState().chatMessages;
+ expect(messages.some(m => m.role === 'agent' && m.content === 'visible reply')).toBe(true);
+ } finally {
+ rmSync(tmpDir, { recursive: true, force: true });
+ }
+ });
});
\ No newline at end of file
diff --git a/src/channels/cli.ts b/src/channels/cli.ts
index 025c0ea4..baa0a2d3 100644
--- a/src/channels/cli.ts
+++ b/src/channels/cli.ts
@@ -1821,6 +1821,13 @@ export class CLIChannel extends BaseChannel {
if (!fs.existsSync(target)) return { ok: false, message: `Directory does not exist: ${target}` };
if (!fs.statSync(target).isDirectory()) return { ok: false, message: `Not a directory: ${target}` };
+ // A bot chat must not survive a mode switch: a stranded activeBotId would
+ // park every later send into the hidden main transcript (invisible
+ // responses) and wrap nothing visibly. The typed /code path already
+ // exits the bot chat first — this covers every OTHER entry (the
+ // automatic coding hand-off, Esc flows).
+ if (this.activeBotId) this.exitBotChat();
+
const dirName = path.basename(target) || target;
this.exitEscArmed = false;
this.update({
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 7eb7f83b..f7c2dfdf 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -416,6 +416,17 @@ function looksResearchy(text: string): boolean {
/** No answer to the Mercury Code hand-off prompt is treated as "keep chatting". */
export const MERCURY_CODE_HANDOFF_TIMEOUT_MS = 45_000;
+/**
+ * Fleet-control verbs: `/bot <verb> …` where the first token resolves to NO
+ * configured bot is a control command typed on the message slash — it is
+ * routed to the /bots surface instead of failing as an unknown target.
+ * (A bot actually named e.g. "start" always wins — bot resolution first.)
+ */
+export const BOT_CONTROL_VERBS = new Set([
+ 'start', 'stop', 'pause', 'enable', 'disable', 'run', 'open', 'send',
+ 'journal', 'inbox', 'budget', 'edit', 'delete', 'replay', 'list', 'dlq', 'storage',
+]);
+
/**
* Whitelabel a provider error into one short, respectful line for the chat:
* the user gets the real reason (token expired, rate limit, server error) —
@@ -672,6 +683,19 @@ export class Agent {
const spaceIndex = rest.indexOf(' ');
const rawTarget = (spaceIndex === -1 ? rest : rest.slice(0, spaceIndex)).toLowerCase();
const message = spaceIndex === -1 ? '' : rest.slice(spaceIndex + 1).trim();
+ // `/bot start <id>` etc. — a fleet-control verb typed on the message
+ // slash: the first token resolves to NO bot, so it was never a target.
+ // Route it to /bots (the control surface) instead of failing with a
+ // confusing "Could not message start: target_unknown".
+ if (BOT_CONTROL_VERBS.has(rawTarget) && !this.botManager.resolveBotId(rawTarget)) {
+ const channel = this.channels.getChannelForMessage(msg);
+ if (channel) {
+ this.handleBotsCommand(trimmed.replace(/^\/bot\b/, '/bots'), msg, channel).catch((err) => {
+ logger.error({ err: (err as any)?.message ?? err, content: trimmed.slice(0, 50) }, '/bots command failed');
+ });
+ return;
+ }
+ }
const botId = this.botManager.resolveBotId(rawTarget) ?? rawTarget;
if (botId && message) {
void this.dispatchToBot(botId, message, msg);
@@ -884,11 +908,17 @@ export class Agent {
const explanation =
'Research mode will search the web, cross-check multiple sources, gather images, and produce a full research article as rich markdown. It runs longer than a quick answer and will not be killed early.';
- const choice = await this.presentChoice(
+ // Time-weighted default (same contract as the Mercury Code hand-off): an
+ // unanswered prompt must never hold a chat message hostage. No answer
+ // within the window → "Quick answer", nothing remembered, and the next
+ // research-shaped question asks again.
+ const choice = await this.presentChoiceWithTimeout(
`This looks like a research question. ${explanation}\n\nHow do you want to proceed?`,
['Full research mode (deep, multi-source article)', 'Quick answer (normal conversation)'],
msg.channelId,
msg.channelType,
+ MERCURY_CODE_HANDOFF_TIMEOUT_MS,
+ 1, // unanswered = Quick answer (not remembered — nobody chose)
);
if (choice.toLowerCase().startsWith('full')) {
diff --git a/src/core/bot-control-verbs.test.ts b/src/core/bot-control-verbs.test.ts
new file mode 100644
index 00000000..b6b84868
--- /dev/null
+++ b/src/core/bot-control-verbs.test.ts
@@ -0,0 +1,27 @@
+import { describe, expect, it } from 'vitest';
+import { readFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import { dirname, join } from 'node:path';
+
+const agentSrc = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'agent.ts'), 'utf8');
+
+/**
+ * `/bot start <id>` used to fail with "Could not message start:
+ * [reason: target_unknown]" — the fleet-control verb was treated as a bot id.
+ * The routing contract: a first token that is a fleet-control verb AND
+ * resolves to no configured bot is re-routed to the /bots control surface;
+ * a bot actually named like a verb always wins (bot resolution first).
+ */
+describe('/bot <fleet-verb> re-routes to /bots (control on the message slash)', () => {
+ it('covers the /bots surface control verbs', () => {
+ for (const verb of ['start', 'stop', 'pause', 'enable', 'disable', 'run', 'open', 'send', 'journal', 'inbox', 'budget', 'edit', 'delete', 'replay']) {
+ expect(agentSrc).toContain(new RegExp(`'${verb}'`).source);
+ }
+ expect(agentSrc).toContain('const BOT_CONTROL_VERBS = new Set([');
+ });
+
+ it('re-routes only when the verb is NOT a configured bot (bot names win)', () => {
+ expect(agentSrc).toMatch(/BOT_CONTROL_VERBS\.has\(rawTarget\) && !this\.botManager\.resolveBotId\(rawTarget\)/s);
+ expect(agentSrc).toContain("trimmed.replace(/^\\/bot\\b/, '/bots')");
+ });
+});
\ No newline at end of file