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.

feat(chat): remember the Mercury Code hand-off choice per session

Salman Qureshi67efb6ed844d
diff --git a/src/channels/choice-prompt-timeout.test.ts b/src/channels/choice-prompt-timeout.test.ts
index 143a41bc..f1234a3c 100644
--- a/src/channels/choice-prompt-timeout.test.ts
+++ b/src/channels/choice-prompt-timeout.test.ts
@@ -41,4 +41,16 @@ describe('choice prompt timeout / dismissal', () => {
expect(agentSrc).toContain('promptMercuryCodeHandoff');
expect(agentSrc).toMatch(/promptMercuryCodeHandoff\(channel: CLIChannel[\s\S]*?this\.queueMessage\(msg, workKey\);\s*\n\s*this\.processQueue\(\);/s);
});
+
+ it('agent: the choice is remembered PER SESSION and every auto-switch explains itself', () => {
+ // Preference keyed by the canonical session id → a new session asks again.
+ expect(agentSrc).toMatch(/getOrCreateBound\(msg\.channelType, 'current'[\s\S]*?\.id;\s*\n\s*const remembered = /s);
+ expect(agentSrc).toContain('mercuryCodeHandoffPreferences');
+ // Remembered "No" → never asks this session.
+ expect(agentSrc).toMatch(/remembered === 'chat'[\s\S]*?this\.queueMessage\(msg, workKey\);\s*\n\s*this\.processQueue\(\);/s);
+ // Remembered "Yes" → auto-switch WITH the reason.
+ expect(agentSrc).toMatch(/remembered === 'code'[\s\S]*?you chose it for coding tasks earlier this session/s);
+ // Timeout is NOT a choice — nothing remembered when nobody answers.
+ expect(agentSrc).toContain('not remembered — nobody answered');
+ });
});
\ No newline at end of file
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 2ff7db6a..a6f4b7d7 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -624,39 +624,73 @@ export class Agent {
}
/**
- * Mercury Code hand-off: in the normal chat TUI, a coding-shaped task may be
- * better served by the full-screen coding TUI. Ask ONCE with a timeout — an
- * unanswered prompt resolves to "keep chatting" after
- * MERCURY_CODE_HANDOFF_TIMEOUT_MS so the request is never left blocked.
- * Only the CLI TUI is prompted (Mercury Code is a terminal surface; other
- * channels just keep chatting).
+ * Mercury Code hand-off, with SESSION memory (like the session permission
+ * modes): an explicit Yes/No is remembered for the current session —
+ *
+ * 'code' → later coding tasks auto-switch to Mercury Code, and every
+ * automatic switch says WHY ("you chose it earlier this session")
+ * 'chat' → later coding tasks never ask; they continue in normal chat
+ *
+ * A TIMEOUT is not a choice — nothing is remembered, and the next coding
+ * task asks again. The prompt itself still has a timeout so the request is
+ * never left blocked.
*/
private async promptMercuryCodeHandoff(channel: CLIChannel, msg: ChannelMessage, workKey?: string): Promise<void> {
+ const preferenceKey = this.sessions.getOrCreateBound(msg.channelType, 'current', msg.sessionId ?? undefined).id;
+ const remembered = this.mercuryCodeHandoffPreferences.get(preferenceKey);
+
+ if (remembered === 'chat') {
+ // Remembered "No": keep chatting, never ask again this session.
+ this.queueMessage(msg, workKey);
+ this.processQueue();
+ return;
+ }
+ if (remembered === 'code') {
+ // Remembered "Yes": switch back automatically — and say why.
+ const cwd = this.capabilities.getCwd();
+ const entered = channel.enterMercuryCode(cwd, channel.getTuiState().version || 'dev');
+ if (entered.ok) {
+ this.syncProgrammingModeForCode(cwd, channel);
+ await channel.send('Switching to Mercury Code — you chose it for coding tasks earlier this session.', msg.channelId).catch(() => {});
+ }
+ this.queueMessage(msg, workKey);
+ this.processQueue();
+ return;
+ }
+
const choice = await this.presentChoiceWithTimeout(
'This looks like a coding task. Open it in Mercury Code (full-screen coding TUI — plan + build in one flow)?',
- ['Yes — switch to Mercury Code and continue there', 'No — continue in normal chat'],
+ ['Yes — switch to Mercury Code (remembered for this session)', 'No — continue in normal chat (not asked again this session)'],
msg.channelId,
msg.channelType,
MERCURY_CODE_HANDOFF_TIMEOUT_MS,
- 1, // time-weighted default: No
+ 1, // time-weighted default: No (not remembered — nobody answered)
);
if (choice.startsWith('Yes')) {
+ this.mercuryCodeHandoffPreferences.set(preferenceKey, 'code');
const cwd = this.capabilities.getCwd();
const entered = channel.enterMercuryCode(cwd, channel.getTuiState().version || 'dev');
if (entered.ok) {
- this.programmingMode.setAuto();
- this.programmingMode.setProjectContext(cwd);
- channel.setProgrammingStatus(this.programmingMode.getState(), this.programmingMode.getProjectContext());
- await channel.send('Mercury Code active (AUTO) — continuing your request here.', msg.channelId).catch(() => {});
+ this.syncProgrammingModeForCode(cwd, channel);
+ await channel.send('Mercury Code active (AUTO) — continuing your request here. I\'ll open it automatically for coding tasks for the rest of this session.', msg.channelId).catch(() => {});
} else {
await channel.send(`Could not open Mercury Code (${entered.message}) — continuing in normal chat.`, msg.channelId).catch(() => {});
}
+ } else {
+ await channel.send('Continuing in normal chat — I won\'t ask again this session.', msg.channelId).catch(() => {});
}
this.queueMessage(msg, workKey);
this.processQueue();
}
+ /** AUTO flow + project context, mirrored from the /code handler. */
+ private syncProgrammingModeForCode(cwd: string, channel: CLIChannel): void {
+ this.programmingMode.setAuto();
+ this.programmingMode.setProjectContext(cwd);
+ channel.setProgrammingStatus(this.programmingMode.getState(), this.programmingMode.getProjectContext());
+ }
+
/**
* presentChoice with a time-weighted default: if the user does not answer
* within `timeoutMs`, the fallback choice wins and the work continues; a
@@ -1458,6 +1492,11 @@ export class Agent {
private channelProviderOverrides = new Map<string, { providerName: string; modelName: string; provider: BaseProvider }>();
/** Dedupes the "served by a fallback provider" notice — one per (default → served) pair per runtime. */
private lastFallbackNoticeKey: string | null = null;
+ /** Mercury Code hand-off preference, remembered PER SESSION (like the
+ * session permission modes): 'code' = auto-switch on coding tasks, 'chat' =
+ * never ask in this session. Only EXPLICIT answers are remembered — a
+ * timeout (nobody answered) asks again on the next coding task. */
+ private mercuryCodeHandoffPreferences = new Map<string, 'chat' | 'code'>();
async listChatModelOptions(): Promise<Array<{ provider: string; label: string; model: string; models: string[]; selected: boolean }>> {
const active = getActiveProviders(this.config);